10b57cec5SDimitry Andric //===- DeclBase.h - Base Classes for representing declarations --*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric //  This file defines the Decl and DeclContext interfaces.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef LLVM_CLANG_AST_DECLBASE_H
140b57cec5SDimitry Andric #define LLVM_CLANG_AST_DECLBASE_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "clang/AST/ASTDumperUtils.h"
170b57cec5SDimitry Andric #include "clang/AST/AttrIterator.h"
180b57cec5SDimitry Andric #include "clang/AST/DeclarationName.h"
195f757f3fSDimitry Andric #include "clang/AST/SelectorLocationsKind.h"
200b57cec5SDimitry Andric #include "clang/Basic/IdentifierTable.h"
210b57cec5SDimitry Andric #include "clang/Basic/LLVM.h"
22297eecfbSDimitry Andric #include "clang/Basic/LangOptions.h"
230b57cec5SDimitry Andric #include "clang/Basic/SourceLocation.h"
240b57cec5SDimitry Andric #include "clang/Basic/Specifiers.h"
250b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
260b57cec5SDimitry Andric #include "llvm/ADT/PointerIntPair.h"
270b57cec5SDimitry Andric #include "llvm/ADT/PointerUnion.h"
280b57cec5SDimitry Andric #include "llvm/ADT/iterator.h"
290b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
300b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
310b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
320b57cec5SDimitry Andric #include "llvm/Support/PrettyStackTrace.h"
330b57cec5SDimitry Andric #include "llvm/Support/VersionTuple.h"
340b57cec5SDimitry Andric #include <algorithm>
350b57cec5SDimitry Andric #include <cassert>
360b57cec5SDimitry Andric #include <cstddef>
370b57cec5SDimitry Andric #include <iterator>
380b57cec5SDimitry Andric #include <string>
390b57cec5SDimitry Andric #include <type_traits>
400b57cec5SDimitry Andric #include <utility>
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric namespace clang {
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric class ASTContext;
450b57cec5SDimitry Andric class ASTMutationListener;
460b57cec5SDimitry Andric class Attr;
470b57cec5SDimitry Andric class BlockDecl;
480b57cec5SDimitry Andric class DeclContext;
490b57cec5SDimitry Andric class ExternalSourceSymbolAttr;
500b57cec5SDimitry Andric class FunctionDecl;
510b57cec5SDimitry Andric class FunctionType;
520b57cec5SDimitry Andric class IdentifierInfo;
535f757f3fSDimitry Andric enum class Linkage : unsigned char;
540b57cec5SDimitry Andric class LinkageSpecDecl;
550b57cec5SDimitry Andric class Module;
560b57cec5SDimitry Andric class NamedDecl;
570b57cec5SDimitry Andric class ObjCContainerDecl;
580b57cec5SDimitry Andric class ObjCMethodDecl;
590b57cec5SDimitry Andric struct PrintingPolicy;
600b57cec5SDimitry Andric class RecordDecl;
610b57cec5SDimitry Andric class SourceManager;
620b57cec5SDimitry Andric class Stmt;
630b57cec5SDimitry Andric class StoredDeclsMap;
640b57cec5SDimitry Andric class TemplateDecl;
655ffd83dbSDimitry Andric class TemplateParameterList;
660b57cec5SDimitry Andric class TranslationUnitDecl;
670b57cec5SDimitry Andric class UsingDirectiveDecl;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric /// Captures the result of checking the availability of a
700b57cec5SDimitry Andric /// declaration.
710b57cec5SDimitry Andric enum AvailabilityResult {
720b57cec5SDimitry Andric   AR_Available = 0,
730b57cec5SDimitry Andric   AR_NotYetIntroduced,
740b57cec5SDimitry Andric   AR_Deprecated,
750b57cec5SDimitry Andric   AR_Unavailable
760b57cec5SDimitry Andric };
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric /// Decl - This represents one declaration (or definition), e.g. a variable,
790b57cec5SDimitry Andric /// typedef, function, struct, etc.
800b57cec5SDimitry Andric ///
810b57cec5SDimitry Andric /// Note: There are objects tacked on before the *beginning* of Decl
820b57cec5SDimitry Andric /// (and its subclasses) in its Decl::operator new(). Proper alignment
830b57cec5SDimitry Andric /// of all subclasses (not requiring more than the alignment of Decl) is
840b57cec5SDimitry Andric /// asserted in DeclBase.cpp.
850b57cec5SDimitry Andric class alignas(8) Decl {
860b57cec5SDimitry Andric public:
870b57cec5SDimitry Andric   /// Lists the kind of concrete classes of Decl.
880b57cec5SDimitry Andric   enum Kind {
890b57cec5SDimitry Andric #define DECL(DERIVED, BASE) DERIVED,
900b57cec5SDimitry Andric #define ABSTRACT_DECL(DECL)
910b57cec5SDimitry Andric #define DECL_RANGE(BASE, START, END) \
920b57cec5SDimitry Andric         first##BASE = START, last##BASE = END,
930b57cec5SDimitry Andric #define LAST_DECL_RANGE(BASE, START, END) \
940b57cec5SDimitry Andric         first##BASE = START, last##BASE = END
950b57cec5SDimitry Andric #include "clang/AST/DeclNodes.inc"
960b57cec5SDimitry Andric   };
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   /// A placeholder type used to construct an empty shell of a
990b57cec5SDimitry Andric   /// decl-derived type that will be filled in later (e.g., by some
1000b57cec5SDimitry Andric   /// deserialization method).
1010b57cec5SDimitry Andric   struct EmptyShell {};
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   /// IdentifierNamespace - The different namespaces in which
1040b57cec5SDimitry Andric   /// declarations may appear.  According to C99 6.2.3, there are
1050b57cec5SDimitry Andric   /// four namespaces, labels, tags, members and ordinary
1060b57cec5SDimitry Andric   /// identifiers.  C++ describes lookup completely differently:
1070b57cec5SDimitry Andric   /// certain lookups merely "ignore" certain kinds of declarations,
1080b57cec5SDimitry Andric   /// usually based on whether the declaration is of a type, etc.
1090b57cec5SDimitry Andric   ///
1100b57cec5SDimitry Andric   /// These are meant as bitmasks, so that searches in
1110b57cec5SDimitry Andric   /// C++ can look into the "tag" namespace during ordinary lookup.
1120b57cec5SDimitry Andric   ///
1130b57cec5SDimitry Andric   /// Decl currently provides 15 bits of IDNS bits.
1140b57cec5SDimitry Andric   enum IdentifierNamespace {
1150b57cec5SDimitry Andric     /// Labels, declared with 'x:' and referenced with 'goto x'.
1160b57cec5SDimitry Andric     IDNS_Label               = 0x0001,
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric     /// Tags, declared with 'struct foo;' and referenced with
1190b57cec5SDimitry Andric     /// 'struct foo'.  All tags are also types.  This is what
1200b57cec5SDimitry Andric     /// elaborated-type-specifiers look for in C.
1210b57cec5SDimitry Andric     /// This also contains names that conflict with tags in the
1220b57cec5SDimitry Andric     /// same scope but that are otherwise ordinary names (non-type
1230b57cec5SDimitry Andric     /// template parameters and indirect field declarations).
1240b57cec5SDimitry Andric     IDNS_Tag                 = 0x0002,
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric     /// Types, declared with 'struct foo', typedefs, etc.
1270b57cec5SDimitry Andric     /// This is what elaborated-type-specifiers look for in C++,
1280b57cec5SDimitry Andric     /// but note that it's ill-formed to find a non-tag.
1290b57cec5SDimitry Andric     IDNS_Type                = 0x0004,
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric     /// Members, declared with object declarations within tag
1320b57cec5SDimitry Andric     /// definitions.  In C, these can only be found by "qualified"
1330b57cec5SDimitry Andric     /// lookup in member expressions.  In C++, they're found by
1340b57cec5SDimitry Andric     /// normal lookup.
1350b57cec5SDimitry Andric     IDNS_Member              = 0x0008,
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric     /// Namespaces, declared with 'namespace foo {}'.
1380b57cec5SDimitry Andric     /// Lookup for nested-name-specifiers find these.
1390b57cec5SDimitry Andric     IDNS_Namespace           = 0x0010,
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric     /// Ordinary names.  In C, everything that's not a label, tag,
1420b57cec5SDimitry Andric     /// member, or function-local extern ends up here.
1430b57cec5SDimitry Andric     IDNS_Ordinary            = 0x0020,
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric     /// Objective C \@protocol.
1460b57cec5SDimitry Andric     IDNS_ObjCProtocol        = 0x0040,
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric     /// This declaration is a friend function.  A friend function
1490b57cec5SDimitry Andric     /// declaration is always in this namespace but may also be in
1500b57cec5SDimitry Andric     /// IDNS_Ordinary if it was previously declared.
1510b57cec5SDimitry Andric     IDNS_OrdinaryFriend      = 0x0080,
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric     /// This declaration is a friend class.  A friend class
1540b57cec5SDimitry Andric     /// declaration is always in this namespace but may also be in
1550b57cec5SDimitry Andric     /// IDNS_Tag|IDNS_Type if it was previously declared.
1560b57cec5SDimitry Andric     IDNS_TagFriend           = 0x0100,
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric     /// This declaration is a using declaration.  A using declaration
1590b57cec5SDimitry Andric     /// *introduces* a number of other declarations into the current
1600b57cec5SDimitry Andric     /// scope, and those declarations use the IDNS of their targets,
1610b57cec5SDimitry Andric     /// but the actual using declarations go in this namespace.
1620b57cec5SDimitry Andric     IDNS_Using               = 0x0200,
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric     /// This declaration is a C++ operator declared in a non-class
1650b57cec5SDimitry Andric     /// context.  All such operators are also in IDNS_Ordinary.
1660b57cec5SDimitry Andric     /// C++ lexical operator lookup looks for these.
1670b57cec5SDimitry Andric     IDNS_NonMemberOperator   = 0x0400,
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric     /// This declaration is a function-local extern declaration of a
1700b57cec5SDimitry Andric     /// variable or function. This may also be IDNS_Ordinary if it
1710b57cec5SDimitry Andric     /// has been declared outside any function. These act mostly like
1720b57cec5SDimitry Andric     /// invisible friend declarations, but are also visible to unqualified
1730b57cec5SDimitry Andric     /// lookup within the scope of the declaring function.
1740b57cec5SDimitry Andric     IDNS_LocalExtern         = 0x0800,
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric     /// This declaration is an OpenMP user defined reduction construction.
1770b57cec5SDimitry Andric     IDNS_OMPReduction        = 0x1000,
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric     /// This declaration is an OpenMP user defined mapper.
1800b57cec5SDimitry Andric     IDNS_OMPMapper           = 0x2000,
1810b57cec5SDimitry Andric   };
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
1840b57cec5SDimitry Andric   /// parameter types in method declarations.  Other than remembering
1850b57cec5SDimitry Andric   /// them and mangling them into the method's signature string, these
1860b57cec5SDimitry Andric   /// are ignored by the compiler; they are consumed by certain
1870b57cec5SDimitry Andric   /// remote-messaging frameworks.
1880b57cec5SDimitry Andric   ///
1890b57cec5SDimitry Andric   /// in, inout, and out are mutually exclusive and apply only to
1900b57cec5SDimitry Andric   /// method parameters.  bycopy and byref are mutually exclusive and
1910b57cec5SDimitry Andric   /// apply only to method parameters (?).  oneway applies only to
1920b57cec5SDimitry Andric   /// results.  All of these expect their corresponding parameter to
1930b57cec5SDimitry Andric   /// have a particular type.  None of this is currently enforced by
1940b57cec5SDimitry Andric   /// clang.
1950b57cec5SDimitry Andric   ///
1960b57cec5SDimitry Andric   /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
1970b57cec5SDimitry Andric   enum ObjCDeclQualifier {
1980b57cec5SDimitry Andric     OBJC_TQ_None = 0x0,
1990b57cec5SDimitry Andric     OBJC_TQ_In = 0x1,
2000b57cec5SDimitry Andric     OBJC_TQ_Inout = 0x2,
2010b57cec5SDimitry Andric     OBJC_TQ_Out = 0x4,
2020b57cec5SDimitry Andric     OBJC_TQ_Bycopy = 0x8,
2030b57cec5SDimitry Andric     OBJC_TQ_Byref = 0x10,
2040b57cec5SDimitry Andric     OBJC_TQ_Oneway = 0x20,
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric     /// The nullability qualifier is set when the nullability of the
2070b57cec5SDimitry Andric     /// result or parameter was expressed via a context-sensitive
2080b57cec5SDimitry Andric     /// keyword.
2090b57cec5SDimitry Andric     OBJC_TQ_CSNullability = 0x40
2100b57cec5SDimitry Andric   };
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric   /// The kind of ownership a declaration has, for visibility purposes.
2130b57cec5SDimitry Andric   /// This enumeration is designed such that higher values represent higher
2140b57cec5SDimitry Andric   /// levels of name hiding.
2155f757f3fSDimitry Andric   enum class ModuleOwnershipKind : unsigned char {
2160b57cec5SDimitry Andric     /// This declaration is not owned by a module.
2170b57cec5SDimitry Andric     Unowned,
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric     /// This declaration has an owning module, but is globally visible
2200b57cec5SDimitry Andric     /// (typically because its owning module is visible and we know that
2210b57cec5SDimitry Andric     /// modules cannot later become hidden in this compilation).
2220b57cec5SDimitry Andric     /// After serialization and deserialization, this will be converted
2230b57cec5SDimitry Andric     /// to VisibleWhenImported.
2240b57cec5SDimitry Andric     Visible,
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric     /// This declaration has an owning module, and is visible when that
2270b57cec5SDimitry Andric     /// module is imported.
2280b57cec5SDimitry Andric     VisibleWhenImported,
2290b57cec5SDimitry Andric 
23081ad6265SDimitry Andric     /// This declaration has an owning module, and is visible to lookups
23181ad6265SDimitry Andric     /// that occurs within that module. And it is reachable in other module
23281ad6265SDimitry Andric     /// when the owning module is transitively imported.
23381ad6265SDimitry Andric     ReachableWhenImported,
23481ad6265SDimitry Andric 
2350b57cec5SDimitry Andric     /// This declaration has an owning module, but is only visible to
2360b57cec5SDimitry Andric     /// lookups that occur within that module.
23781ad6265SDimitry Andric     /// The discarded declarations in global module fragment belongs
23881ad6265SDimitry Andric     /// to this group too.
2390b57cec5SDimitry Andric     ModulePrivate
2400b57cec5SDimitry Andric   };
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric protected:
2430b57cec5SDimitry Andric   /// The next declaration within the same lexical
2440b57cec5SDimitry Andric   /// DeclContext. These pointers form the linked list that is
2450b57cec5SDimitry Andric   /// traversed via DeclContext's decls_begin()/decls_end().
2460b57cec5SDimitry Andric   ///
24781ad6265SDimitry Andric   /// The extra three bits are used for the ModuleOwnershipKind.
24881ad6265SDimitry Andric   llvm::PointerIntPair<Decl *, 3, ModuleOwnershipKind> NextInContextAndBits;
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric private:
2510b57cec5SDimitry Andric   friend class DeclContext;
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric   struct MultipleDC {
2540b57cec5SDimitry Andric     DeclContext *SemanticDC;
2550b57cec5SDimitry Andric     DeclContext *LexicalDC;
2560b57cec5SDimitry Andric   };
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric   /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
2590b57cec5SDimitry Andric   /// For declarations that don't contain C++ scope specifiers, it contains
2600b57cec5SDimitry Andric   /// the DeclContext where the Decl was declared.
2610b57cec5SDimitry Andric   /// For declarations with C++ scope specifiers, it contains a MultipleDC*
2620b57cec5SDimitry Andric   /// with the context where it semantically belongs (SemanticDC) and the
2630b57cec5SDimitry Andric   /// context where it was lexically declared (LexicalDC).
2640b57cec5SDimitry Andric   /// e.g.:
2650b57cec5SDimitry Andric   ///
2660b57cec5SDimitry Andric   ///   namespace A {
2670b57cec5SDimitry Andric   ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
2680b57cec5SDimitry Andric   ///   }
2690b57cec5SDimitry Andric   ///   void A::f(); // SemanticDC == namespace 'A'
2700b57cec5SDimitry Andric   ///                // LexicalDC == global namespace
2710b57cec5SDimitry Andric   llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
2720b57cec5SDimitry Andric 
isInSemaDC()2730b57cec5SDimitry Andric   bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
isOutOfSemaDC()2740b57cec5SDimitry Andric   bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
2750b57cec5SDimitry Andric 
getMultipleDC()2760b57cec5SDimitry Andric   MultipleDC *getMultipleDC() const {
2770b57cec5SDimitry Andric     return DeclCtx.get<MultipleDC*>();
2780b57cec5SDimitry Andric   }
2790b57cec5SDimitry Andric 
getSemanticDC()2800b57cec5SDimitry Andric   DeclContext *getSemanticDC() const {
2810b57cec5SDimitry Andric     return DeclCtx.get<DeclContext*>();
2820b57cec5SDimitry Andric   }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   /// Loc - The location of this decl.
2850b57cec5SDimitry Andric   SourceLocation Loc;
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   /// DeclKind - This indicates which class this is.
2885f757f3fSDimitry Andric   LLVM_PREFERRED_TYPE(Kind)
2890b57cec5SDimitry Andric   unsigned DeclKind : 7;
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric   /// InvalidDecl - This indicates a semantic error occurred.
2925f757f3fSDimitry Andric   LLVM_PREFERRED_TYPE(bool)
2930b57cec5SDimitry Andric   unsigned InvalidDecl :  1;
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric   /// HasAttrs - This indicates whether the decl has attributes or not.
2965f757f3fSDimitry Andric   LLVM_PREFERRED_TYPE(bool)
2970b57cec5SDimitry Andric   unsigned HasAttrs : 1;
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   /// Implicit - Whether this declaration was implicitly generated by
3000b57cec5SDimitry Andric   /// the implementation rather than explicitly written by the user.
3015f757f3fSDimitry Andric   LLVM_PREFERRED_TYPE(bool)
3020b57cec5SDimitry Andric   unsigned Implicit : 1;
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric   /// Whether this declaration was "used", meaning that a definition is
3050b57cec5SDimitry Andric   /// required.
3065f757f3fSDimitry Andric   LLVM_PREFERRED_TYPE(bool)
3070b57cec5SDimitry Andric   unsigned Used : 1;
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric   /// Whether this declaration was "referenced".
3100b57cec5SDimitry Andric   /// The difference with 'Used' is whether the reference appears in a
3110b57cec5SDimitry Andric   /// evaluated context or not, e.g. functions used in uninstantiated templates
3120b57cec5SDimitry Andric   /// are regarded as "referenced" but not "used".
3135f757f3fSDimitry Andric   LLVM_PREFERRED_TYPE(bool)
3140b57cec5SDimitry Andric   unsigned Referenced : 1;
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   /// Whether this declaration is a top-level declaration (function,
3170b57cec5SDimitry Andric   /// global variable, etc.) that is lexically inside an objc container
3180b57cec5SDimitry Andric   /// definition.
3195f757f3fSDimitry Andric   LLVM_PREFERRED_TYPE(bool)
3200b57cec5SDimitry Andric   unsigned TopLevelDeclInObjCContainer : 1;
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   /// Whether statistic collection is enabled.
3230b57cec5SDimitry Andric   static bool StatisticsEnabled;
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric protected:
3260b57cec5SDimitry Andric   friend class ASTDeclReader;
3270b57cec5SDimitry Andric   friend class ASTDeclWriter;
3280b57cec5SDimitry Andric   friend class ASTNodeImporter;
3290b57cec5SDimitry Andric   friend class ASTReader;
3300b57cec5SDimitry Andric   friend class CXXClassMemberWrapper;
3310b57cec5SDimitry Andric   friend class LinkageComputer;
33281ad6265SDimitry Andric   friend class RecordDecl;
3330b57cec5SDimitry Andric   template<typename decl_type> friend class Redeclarable;
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric   /// Access - Used by C++ decls for the access specifier.
3360b57cec5SDimitry Andric   // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
3375f757f3fSDimitry Andric   LLVM_PREFERRED_TYPE(AccessSpecifier)
3380b57cec5SDimitry Andric   unsigned Access : 2;
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   /// Whether this declaration was loaded from an AST file.
3415f757f3fSDimitry Andric   LLVM_PREFERRED_TYPE(bool)
3420b57cec5SDimitry Andric   unsigned FromASTFile : 1;
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric   /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
3455f757f3fSDimitry Andric   LLVM_PREFERRED_TYPE(IdentifierNamespace)
3460b57cec5SDimitry Andric   unsigned IdentifierNamespace : 14;
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   /// If 0, we have not computed the linkage of this declaration.
3495f757f3fSDimitry Andric   LLVM_PREFERRED_TYPE(Linkage)
3500b57cec5SDimitry Andric   mutable unsigned CacheValidAndLinkage : 3;
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric   /// Allocate memory for a deserialized declaration.
3530b57cec5SDimitry Andric   ///
3540b57cec5SDimitry Andric   /// This routine must be used to allocate memory for any declaration that is
3550b57cec5SDimitry Andric   /// deserialized from a module file.
3560b57cec5SDimitry Andric   ///
3570b57cec5SDimitry Andric   /// \param Size The size of the allocated object.
3580b57cec5SDimitry Andric   /// \param Ctx The context in which we will allocate memory.
3590b57cec5SDimitry Andric   /// \param ID The global ID of the deserialized declaration.
3600b57cec5SDimitry Andric   /// \param Extra The amount of extra space to allocate after the object.
3610b57cec5SDimitry Andric   void *operator new(std::size_t Size, const ASTContext &Ctx, unsigned ID,
3620b57cec5SDimitry Andric                      std::size_t Extra = 0);
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   /// Allocate memory for a non-deserialized declaration.
3650b57cec5SDimitry Andric   void *operator new(std::size_t Size, const ASTContext &Ctx,
3660b57cec5SDimitry Andric                      DeclContext *Parent, std::size_t Extra = 0);
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric private:
3694824e7fdSDimitry Andric   bool AccessDeclContextCheck() const;
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric   /// Get the module ownership kind to use for a local lexical child of \p DC,
3720b57cec5SDimitry Andric   /// which may be either a local or (rarely) an imported declaration.
getModuleOwnershipKindForChildOf(DeclContext * DC)3730b57cec5SDimitry Andric   static ModuleOwnershipKind getModuleOwnershipKindForChildOf(DeclContext *DC) {
3740b57cec5SDimitry Andric     if (DC) {
3750b57cec5SDimitry Andric       auto *D = cast<Decl>(DC);
3760b57cec5SDimitry Andric       auto MOK = D->getModuleOwnershipKind();
3770b57cec5SDimitry Andric       if (MOK != ModuleOwnershipKind::Unowned &&
3780b57cec5SDimitry Andric           (!D->isFromASTFile() || D->hasLocalOwningModuleStorage()))
3790b57cec5SDimitry Andric         return MOK;
3800b57cec5SDimitry Andric       // If D is not local and we have no local module storage, then we don't
3810b57cec5SDimitry Andric       // need to track module ownership at all.
3820b57cec5SDimitry Andric     }
3830b57cec5SDimitry Andric     return ModuleOwnershipKind::Unowned;
3840b57cec5SDimitry Andric   }
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric public:
3870b57cec5SDimitry Andric   Decl() = delete;
3880b57cec5SDimitry Andric   Decl(const Decl&) = delete;
3890b57cec5SDimitry Andric   Decl(Decl &&) = delete;
3900b57cec5SDimitry Andric   Decl &operator=(const Decl&) = delete;
3910b57cec5SDimitry Andric   Decl &operator=(Decl&&) = delete;
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric protected:
Decl(Kind DK,DeclContext * DC,SourceLocation L)3940b57cec5SDimitry Andric   Decl(Kind DK, DeclContext *DC, SourceLocation L)
3950b57cec5SDimitry Andric       : NextInContextAndBits(nullptr, getModuleOwnershipKindForChildOf(DC)),
3960b57cec5SDimitry Andric         DeclCtx(DC), Loc(L), DeclKind(DK), InvalidDecl(false), HasAttrs(false),
3970b57cec5SDimitry Andric         Implicit(false), Used(false), Referenced(false),
3980b57cec5SDimitry Andric         TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0),
3990b57cec5SDimitry Andric         IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
4005f757f3fSDimitry Andric         CacheValidAndLinkage(llvm::to_underlying(Linkage::Invalid)) {
4010b57cec5SDimitry Andric     if (StatisticsEnabled) add(DK);
4020b57cec5SDimitry Andric   }
4030b57cec5SDimitry Andric 
Decl(Kind DK,EmptyShell Empty)4040b57cec5SDimitry Andric   Decl(Kind DK, EmptyShell Empty)
4050b57cec5SDimitry Andric       : DeclKind(DK), InvalidDecl(false), HasAttrs(false), Implicit(false),
4060b57cec5SDimitry Andric         Used(false), Referenced(false), TopLevelDeclInObjCContainer(false),
4070b57cec5SDimitry Andric         Access(AS_none), FromASTFile(0),
4080b57cec5SDimitry Andric         IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
4095f757f3fSDimitry Andric         CacheValidAndLinkage(llvm::to_underlying(Linkage::Invalid)) {
4100b57cec5SDimitry Andric     if (StatisticsEnabled) add(DK);
4110b57cec5SDimitry Andric   }
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric   virtual ~Decl();
4140b57cec5SDimitry Andric 
4150b57cec5SDimitry Andric   /// Update a potentially out-of-date declaration.
4160b57cec5SDimitry Andric   void updateOutOfDate(IdentifierInfo &II) const;
4170b57cec5SDimitry Andric 
getCachedLinkage()4180b57cec5SDimitry Andric   Linkage getCachedLinkage() const {
4195f757f3fSDimitry Andric     return static_cast<Linkage>(CacheValidAndLinkage);
4200b57cec5SDimitry Andric   }
4210b57cec5SDimitry Andric 
setCachedLinkage(Linkage L)4220b57cec5SDimitry Andric   void setCachedLinkage(Linkage L) const {
4235f757f3fSDimitry Andric     CacheValidAndLinkage = llvm::to_underlying(L);
4240b57cec5SDimitry Andric   }
4250b57cec5SDimitry Andric 
hasCachedLinkage()4260b57cec5SDimitry Andric   bool hasCachedLinkage() const {
4270b57cec5SDimitry Andric     return CacheValidAndLinkage;
4280b57cec5SDimitry Andric   }
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric public:
4310b57cec5SDimitry Andric   /// Source range that this declaration covers.
getSourceRange()4320b57cec5SDimitry Andric   virtual SourceRange getSourceRange() const LLVM_READONLY {
4330b57cec5SDimitry Andric     return SourceRange(getLocation(), getLocation());
4340b57cec5SDimitry Andric   }
4350b57cec5SDimitry Andric 
getBeginLoc()4360b57cec5SDimitry Andric   SourceLocation getBeginLoc() const LLVM_READONLY {
4370b57cec5SDimitry Andric     return getSourceRange().getBegin();
4380b57cec5SDimitry Andric   }
4390b57cec5SDimitry Andric 
getEndLoc()4400b57cec5SDimitry Andric   SourceLocation getEndLoc() const LLVM_READONLY {
4410b57cec5SDimitry Andric     return getSourceRange().getEnd();
4420b57cec5SDimitry Andric   }
4430b57cec5SDimitry Andric 
getLocation()4440b57cec5SDimitry Andric   SourceLocation getLocation() const { return Loc; }
setLocation(SourceLocation L)4450b57cec5SDimitry Andric   void setLocation(SourceLocation L) { Loc = L; }
4460b57cec5SDimitry Andric 
getKind()4470b57cec5SDimitry Andric   Kind getKind() const { return static_cast<Kind>(DeclKind); }
4480b57cec5SDimitry Andric   const char *getDeclKindName() const;
4490b57cec5SDimitry Andric 
getNextDeclInContext()4500b57cec5SDimitry Andric   Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); }
getNextDeclInContext()4510b57cec5SDimitry Andric   const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();}
4520b57cec5SDimitry Andric 
getDeclContext()4530b57cec5SDimitry Andric   DeclContext *getDeclContext() {
4540b57cec5SDimitry Andric     if (isInSemaDC())
4550b57cec5SDimitry Andric       return getSemanticDC();
4560b57cec5SDimitry Andric     return getMultipleDC()->SemanticDC;
4570b57cec5SDimitry Andric   }
getDeclContext()4580b57cec5SDimitry Andric   const DeclContext *getDeclContext() const {
4590b57cec5SDimitry Andric     return const_cast<Decl*>(this)->getDeclContext();
4600b57cec5SDimitry Andric   }
4610b57cec5SDimitry Andric 
462bdd1243dSDimitry Andric   /// Return the non transparent context.
463bdd1243dSDimitry Andric   /// See the comment of `DeclContext::isTransparentContext()` for the
464bdd1243dSDimitry Andric   /// definition of transparent context.
465bdd1243dSDimitry Andric   DeclContext *getNonTransparentDeclContext();
getNonTransparentDeclContext()466bdd1243dSDimitry Andric   const DeclContext *getNonTransparentDeclContext() const {
467bdd1243dSDimitry Andric     return const_cast<Decl *>(this)->getNonTransparentDeclContext();
468bdd1243dSDimitry Andric   }
469bdd1243dSDimitry Andric 
4700b57cec5SDimitry Andric   /// Find the innermost non-closure ancestor of this declaration,
4710b57cec5SDimitry Andric   /// walking up through blocks, lambdas, etc.  If that ancestor is
4720b57cec5SDimitry Andric   /// not a code context (!isFunctionOrMethod()), returns null.
4730b57cec5SDimitry Andric   ///
4740b57cec5SDimitry Andric   /// A declaration may be its own non-closure context.
4750b57cec5SDimitry Andric   Decl *getNonClosureContext();
getNonClosureContext()4760b57cec5SDimitry Andric   const Decl *getNonClosureContext() const {
4770b57cec5SDimitry Andric     return const_cast<Decl*>(this)->getNonClosureContext();
4780b57cec5SDimitry Andric   }
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   TranslationUnitDecl *getTranslationUnitDecl();
getTranslationUnitDecl()4810b57cec5SDimitry Andric   const TranslationUnitDecl *getTranslationUnitDecl() const {
4820b57cec5SDimitry Andric     return const_cast<Decl*>(this)->getTranslationUnitDecl();
4830b57cec5SDimitry Andric   }
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric   bool isInAnonymousNamespace() const;
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric   bool isInStdNamespace() const;
4880b57cec5SDimitry Andric 
489bdd1243dSDimitry Andric   // Return true if this is a FileContext Decl.
490bdd1243dSDimitry Andric   bool isFileContextDecl() const;
491bdd1243dSDimitry Andric 
492297eecfbSDimitry Andric   /// Whether it resembles a flexible array member. This is a static member
493297eecfbSDimitry Andric   /// because we want to be able to call it with a nullptr. That allows us to
494297eecfbSDimitry Andric   /// perform non-Decl specific checks based on the object's type and strict
495297eecfbSDimitry Andric   /// flex array level.
496297eecfbSDimitry Andric   static bool isFlexibleArrayMemberLike(
497297eecfbSDimitry Andric       ASTContext &Context, const Decl *D, QualType Ty,
498297eecfbSDimitry Andric       LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel,
499297eecfbSDimitry Andric       bool IgnoreTemplateOrMacroSubstitution);
500297eecfbSDimitry Andric 
5010b57cec5SDimitry Andric   ASTContext &getASTContext() const LLVM_READONLY;
5020b57cec5SDimitry Andric 
5035ffd83dbSDimitry Andric   /// Helper to get the language options from the ASTContext.
5045ffd83dbSDimitry Andric   /// Defined out of line to avoid depending on ASTContext.h.
5055ffd83dbSDimitry Andric   const LangOptions &getLangOpts() const LLVM_READONLY;
5065ffd83dbSDimitry Andric 
setAccess(AccessSpecifier AS)5070b57cec5SDimitry Andric   void setAccess(AccessSpecifier AS) {
5080b57cec5SDimitry Andric     Access = AS;
5094824e7fdSDimitry Andric     assert(AccessDeclContextCheck());
5100b57cec5SDimitry Andric   }
5110b57cec5SDimitry Andric 
getAccess()5120b57cec5SDimitry Andric   AccessSpecifier getAccess() const {
5134824e7fdSDimitry Andric     assert(AccessDeclContextCheck());
5140b57cec5SDimitry Andric     return AccessSpecifier(Access);
5150b57cec5SDimitry Andric   }
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric   /// Retrieve the access specifier for this declaration, even though
5180b57cec5SDimitry Andric   /// it may not yet have been properly set.
getAccessUnsafe()5190b57cec5SDimitry Andric   AccessSpecifier getAccessUnsafe() const {
5200b57cec5SDimitry Andric     return AccessSpecifier(Access);
5210b57cec5SDimitry Andric   }
5220b57cec5SDimitry Andric 
hasAttrs()5230b57cec5SDimitry Andric   bool hasAttrs() const { return HasAttrs; }
5240b57cec5SDimitry Andric 
setAttrs(const AttrVec & Attrs)5250b57cec5SDimitry Andric   void setAttrs(const AttrVec& Attrs) {
5260b57cec5SDimitry Andric     return setAttrsImpl(Attrs, getASTContext());
5270b57cec5SDimitry Andric   }
5280b57cec5SDimitry Andric 
getAttrs()5290b57cec5SDimitry Andric   AttrVec &getAttrs() {
5300b57cec5SDimitry Andric     return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
5310b57cec5SDimitry Andric   }
5320b57cec5SDimitry Andric 
5330b57cec5SDimitry Andric   const AttrVec &getAttrs() const;
5340b57cec5SDimitry Andric   void dropAttrs();
5350b57cec5SDimitry Andric   void addAttr(Attr *A);
5360b57cec5SDimitry Andric 
5370b57cec5SDimitry Andric   using attr_iterator = AttrVec::const_iterator;
5380b57cec5SDimitry Andric   using attr_range = llvm::iterator_range<attr_iterator>;
5390b57cec5SDimitry Andric 
attrs()5400b57cec5SDimitry Andric   attr_range attrs() const {
5410b57cec5SDimitry Andric     return attr_range(attr_begin(), attr_end());
5420b57cec5SDimitry Andric   }
5430b57cec5SDimitry Andric 
attr_begin()5440b57cec5SDimitry Andric   attr_iterator attr_begin() const {
5450b57cec5SDimitry Andric     return hasAttrs() ? getAttrs().begin() : nullptr;
5460b57cec5SDimitry Andric   }
attr_end()5470b57cec5SDimitry Andric   attr_iterator attr_end() const {
5480b57cec5SDimitry Andric     return hasAttrs() ? getAttrs().end() : nullptr;
5490b57cec5SDimitry Andric   }
5500b57cec5SDimitry Andric 
dropAttrs()5517a6dacacSDimitry Andric   template <typename... Ts> void dropAttrs() {
5520b57cec5SDimitry Andric     if (!HasAttrs) return;
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric     AttrVec &Vec = getAttrs();
5557a6dacacSDimitry Andric     llvm::erase_if(Vec, [](Attr *A) { return isa<Ts...>(A); });
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric     if (Vec.empty())
5580b57cec5SDimitry Andric       HasAttrs = false;
5590b57cec5SDimitry Andric   }
5600b57cec5SDimitry Andric 
dropAttr()5617a6dacacSDimitry Andric   template <typename T> void dropAttr() { dropAttrs<T>(); }
5627a6dacacSDimitry Andric 
5630b57cec5SDimitry Andric   template <typename T>
specific_attrs()5640b57cec5SDimitry Andric   llvm::iterator_range<specific_attr_iterator<T>> specific_attrs() const {
5650b57cec5SDimitry Andric     return llvm::make_range(specific_attr_begin<T>(), specific_attr_end<T>());
5660b57cec5SDimitry Andric   }
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric   template <typename T>
specific_attr_begin()5690b57cec5SDimitry Andric   specific_attr_iterator<T> specific_attr_begin() const {
5700b57cec5SDimitry Andric     return specific_attr_iterator<T>(attr_begin());
5710b57cec5SDimitry Andric   }
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   template <typename T>
specific_attr_end()5740b57cec5SDimitry Andric   specific_attr_iterator<T> specific_attr_end() const {
5750b57cec5SDimitry Andric     return specific_attr_iterator<T>(attr_end());
5760b57cec5SDimitry Andric   }
5770b57cec5SDimitry Andric 
getAttr()5780b57cec5SDimitry Andric   template<typename T> T *getAttr() const {
5790b57cec5SDimitry Andric     return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : nullptr;
5800b57cec5SDimitry Andric   }
5810b57cec5SDimitry Andric 
hasAttr()5820b57cec5SDimitry Andric   template<typename T> bool hasAttr() const {
5830b57cec5SDimitry Andric     return hasAttrs() && hasSpecificAttr<T>(getAttrs());
5840b57cec5SDimitry Andric   }
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric   /// getMaxAlignment - return the maximum alignment specified by attributes
5870b57cec5SDimitry Andric   /// on this decl, 0 if there are none.
5880b57cec5SDimitry Andric   unsigned getMaxAlignment() const;
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric   /// setInvalidDecl - Indicates the Decl had a semantic error. This
5910b57cec5SDimitry Andric   /// allows for graceful error recovery.
5920b57cec5SDimitry Andric   void setInvalidDecl(bool Invalid = true);
isInvalidDecl()5930b57cec5SDimitry Andric   bool isInvalidDecl() const { return (bool) InvalidDecl; }
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric   /// isImplicit - Indicates whether the declaration was implicitly
5960b57cec5SDimitry Andric   /// generated by the implementation. If false, this declaration
5970b57cec5SDimitry Andric   /// was written explicitly in the source code.
isImplicit()5980b57cec5SDimitry Andric   bool isImplicit() const { return Implicit; }
5990b57cec5SDimitry Andric   void setImplicit(bool I = true) { Implicit = I; }
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric   /// Whether *any* (re-)declaration of the entity was used, meaning that
6020b57cec5SDimitry Andric   /// a definition is required.
6030b57cec5SDimitry Andric   ///
6040b57cec5SDimitry Andric   /// \param CheckUsedAttr When true, also consider the "used" attribute
6050b57cec5SDimitry Andric   /// (in addition to the "used" bit set by \c setUsed()) when determining
6060b57cec5SDimitry Andric   /// whether the function is used.
6070b57cec5SDimitry Andric   bool isUsed(bool CheckUsedAttr = true) const;
6080b57cec5SDimitry Andric 
6090b57cec5SDimitry Andric   /// Set whether the declaration is used, in the sense of odr-use.
6100b57cec5SDimitry Andric   ///
6110b57cec5SDimitry Andric   /// This should only be used immediately after creating a declaration.
6120b57cec5SDimitry Andric   /// It intentionally doesn't notify any listeners.
setIsUsed()6130b57cec5SDimitry Andric   void setIsUsed() { getCanonicalDecl()->Used = true; }
6140b57cec5SDimitry Andric 
6150b57cec5SDimitry Andric   /// Mark the declaration used, in the sense of odr-use.
6160b57cec5SDimitry Andric   ///
6170b57cec5SDimitry Andric   /// This notifies any mutation listeners in addition to setting a bit
6180b57cec5SDimitry Andric   /// indicating the declaration is used.
6190b57cec5SDimitry Andric   void markUsed(ASTContext &C);
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric   /// Whether any declaration of this entity was referenced.
6220b57cec5SDimitry Andric   bool isReferenced() const;
6230b57cec5SDimitry Andric 
6240b57cec5SDimitry Andric   /// Whether this declaration was referenced. This should not be relied
6250b57cec5SDimitry Andric   /// upon for anything other than debugging.
isThisDeclarationReferenced()6260b57cec5SDimitry Andric   bool isThisDeclarationReferenced() const { return Referenced; }
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric   void setReferenced(bool R = true) { Referenced = R; }
6290b57cec5SDimitry Andric 
6300b57cec5SDimitry Andric   /// Whether this declaration is a top-level declaration (function,
6310b57cec5SDimitry Andric   /// global variable, etc.) that is lexically inside an objc container
6320b57cec5SDimitry Andric   /// definition.
isTopLevelDeclInObjCContainer()6330b57cec5SDimitry Andric   bool isTopLevelDeclInObjCContainer() const {
6340b57cec5SDimitry Andric     return TopLevelDeclInObjCContainer;
6350b57cec5SDimitry Andric   }
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric   void setTopLevelDeclInObjCContainer(bool V = true) {
6380b57cec5SDimitry Andric     TopLevelDeclInObjCContainer = V;
6390b57cec5SDimitry Andric   }
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric   /// Looks on this and related declarations for an applicable
6420b57cec5SDimitry Andric   /// external source symbol attribute.
6430b57cec5SDimitry Andric   ExternalSourceSymbolAttr *getExternalSourceSymbolAttr() const;
6440b57cec5SDimitry Andric 
6450b57cec5SDimitry Andric   /// Whether this declaration was marked as being private to the
6460b57cec5SDimitry Andric   /// module in which it was defined.
isModulePrivate()6470b57cec5SDimitry Andric   bool isModulePrivate() const {
6480b57cec5SDimitry Andric     return getModuleOwnershipKind() == ModuleOwnershipKind::ModulePrivate;
6490b57cec5SDimitry Andric   }
6500b57cec5SDimitry Andric 
65104eeddc0SDimitry Andric   /// Whether this declaration was exported in a lexical context.
65204eeddc0SDimitry Andric   /// e.g.:
65304eeddc0SDimitry Andric   ///
65404eeddc0SDimitry Andric   ///   export namespace A {
65504eeddc0SDimitry Andric   ///      void f1();        // isInExportDeclContext() == true
65604eeddc0SDimitry Andric   ///   }
65704eeddc0SDimitry Andric   ///   void A::f1();        // isInExportDeclContext() == false
65804eeddc0SDimitry Andric   ///
65904eeddc0SDimitry Andric   ///   namespace B {
66004eeddc0SDimitry Andric   ///      void f2();        // isInExportDeclContext() == false
66104eeddc0SDimitry Andric   ///   }
66204eeddc0SDimitry Andric   ///   export void B::f2(); // isInExportDeclContext() == true
66304eeddc0SDimitry Andric   bool isInExportDeclContext() const;
66404eeddc0SDimitry Andric 
isInvisibleOutsideTheOwningModule()66581ad6265SDimitry Andric   bool isInvisibleOutsideTheOwningModule() const {
66681ad6265SDimitry Andric     return getModuleOwnershipKind() > ModuleOwnershipKind::VisibleWhenImported;
66781ad6265SDimitry Andric   }
66881ad6265SDimitry Andric 
66906c3fb27SDimitry Andric   /// Whether this declaration comes from another module unit.
67006c3fb27SDimitry Andric   bool isInAnotherModuleUnit() const;
67106c3fb27SDimitry Andric 
67281ad6265SDimitry Andric   /// FIXME: Implement discarding declarations actually in global module
67381ad6265SDimitry Andric   /// fragment. See [module.global.frag]p3,4 for details.
isDiscardedInGlobalModuleFragment()67481ad6265SDimitry Andric   bool isDiscardedInGlobalModuleFragment() const { return false; }
67581ad6265SDimitry Andric 
676439352acSDimitry Andric   /// Check if we should skip checking ODRHash for declaration \param D.
677439352acSDimitry Andric   ///
678439352acSDimitry Andric   /// The existing ODRHash mechanism seems to be not stable enough and
679439352acSDimitry Andric   /// the false positive ODR violation reports are annoying and we rarely see
680439352acSDimitry Andric   /// true ODR violation reports. Also we learned that MSVC disabled ODR checks
681439352acSDimitry Andric   /// for declarations in GMF. So we try to disable ODR checks in the GMF to
682439352acSDimitry Andric   /// get better user experiences before we make the ODR violation checks stable
683439352acSDimitry Andric   /// enough.
684439352acSDimitry Andric   bool shouldSkipCheckingODR() const;
685439352acSDimitry Andric 
6860b57cec5SDimitry Andric   /// Return true if this declaration has an attribute which acts as
6870b57cec5SDimitry Andric   /// definition of the entity, such as 'alias' or 'ifunc'.
6880b57cec5SDimitry Andric   bool hasDefiningAttr() const;
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric   /// Return this declaration's defining attribute if it has one.
6910b57cec5SDimitry Andric   const Attr *getDefiningAttr() const;
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric protected:
6940b57cec5SDimitry Andric   /// Specify that this declaration was marked as being private
6950b57cec5SDimitry Andric   /// to the module in which it was defined.
setModulePrivate()6960b57cec5SDimitry Andric   void setModulePrivate() {
6970b57cec5SDimitry Andric     // The module-private specifier has no effect on unowned declarations.
6980b57cec5SDimitry Andric     // FIXME: We should track this in some way for source fidelity.
6990b57cec5SDimitry Andric     if (getModuleOwnershipKind() == ModuleOwnershipKind::Unowned)
7000b57cec5SDimitry Andric       return;
7010b57cec5SDimitry Andric     setModuleOwnershipKind(ModuleOwnershipKind::ModulePrivate);
7020b57cec5SDimitry Andric   }
7030b57cec5SDimitry Andric 
7045ffd83dbSDimitry Andric public:
7055ffd83dbSDimitry Andric   /// Set the FromASTFile flag. This indicates that this declaration
7065ffd83dbSDimitry Andric   /// was deserialized and not parsed from source code and enables
7075ffd83dbSDimitry Andric   /// features such as module ownership information.
setFromASTFile()7085ffd83dbSDimitry Andric   void setFromASTFile() {
7095ffd83dbSDimitry Andric     FromASTFile = true;
7105ffd83dbSDimitry Andric   }
7115ffd83dbSDimitry Andric 
7125ffd83dbSDimitry Andric   /// Set the owning module ID.  This may only be called for
7135ffd83dbSDimitry Andric   /// deserialized Decls.
setOwningModuleID(unsigned ID)7140b57cec5SDimitry Andric   void setOwningModuleID(unsigned ID) {
7150b57cec5SDimitry Andric     assert(isFromASTFile() && "Only works on a deserialized declaration");
7160b57cec5SDimitry Andric     *((unsigned*)this - 2) = ID;
7170b57cec5SDimitry Andric   }
7180b57cec5SDimitry Andric 
7190b57cec5SDimitry Andric public:
7200b57cec5SDimitry Andric   /// Determine the availability of the given declaration.
7210b57cec5SDimitry Andric   ///
7220b57cec5SDimitry Andric   /// This routine will determine the most restrictive availability of
7230b57cec5SDimitry Andric   /// the given declaration (e.g., preferring 'unavailable' to
7240b57cec5SDimitry Andric   /// 'deprecated').
7250b57cec5SDimitry Andric   ///
7260b57cec5SDimitry Andric   /// \param Message If non-NULL and the result is not \c
7270b57cec5SDimitry Andric   /// AR_Available, will be set to a (possibly empty) message
7280b57cec5SDimitry Andric   /// describing why the declaration has not been introduced, is
7290b57cec5SDimitry Andric   /// deprecated, or is unavailable.
7300b57cec5SDimitry Andric   ///
7310b57cec5SDimitry Andric   /// \param EnclosingVersion The version to compare with. If empty, assume the
7320b57cec5SDimitry Andric   /// deployment target version.
7330b57cec5SDimitry Andric   ///
7340b57cec5SDimitry Andric   /// \param RealizedPlatform If non-NULL and the availability result is found
7350b57cec5SDimitry Andric   /// in an available attribute it will set to the platform which is written in
7360b57cec5SDimitry Andric   /// the available attribute.
7370b57cec5SDimitry Andric   AvailabilityResult
7380b57cec5SDimitry Andric   getAvailability(std::string *Message = nullptr,
7390b57cec5SDimitry Andric                   VersionTuple EnclosingVersion = VersionTuple(),
7400b57cec5SDimitry Andric                   StringRef *RealizedPlatform = nullptr) const;
7410b57cec5SDimitry Andric 
7420b57cec5SDimitry Andric   /// Retrieve the version of the target platform in which this
7430b57cec5SDimitry Andric   /// declaration was introduced.
7440b57cec5SDimitry Andric   ///
7450b57cec5SDimitry Andric   /// \returns An empty version tuple if this declaration has no 'introduced'
7460b57cec5SDimitry Andric   /// availability attributes, or the version tuple that's specified in the
7470b57cec5SDimitry Andric   /// attribute otherwise.
7480b57cec5SDimitry Andric   VersionTuple getVersionIntroduced() const;
7490b57cec5SDimitry Andric 
7500b57cec5SDimitry Andric   /// Determine whether this declaration is marked 'deprecated'.
7510b57cec5SDimitry Andric   ///
7520b57cec5SDimitry Andric   /// \param Message If non-NULL and the declaration is deprecated,
7530b57cec5SDimitry Andric   /// this will be set to the message describing why the declaration
7540b57cec5SDimitry Andric   /// was deprecated (which may be empty).
7550b57cec5SDimitry Andric   bool isDeprecated(std::string *Message = nullptr) const {
7560b57cec5SDimitry Andric     return getAvailability(Message) == AR_Deprecated;
7570b57cec5SDimitry Andric   }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   /// Determine whether this declaration is marked 'unavailable'.
7600b57cec5SDimitry Andric   ///
7610b57cec5SDimitry Andric   /// \param Message If non-NULL and the declaration is unavailable,
7620b57cec5SDimitry Andric   /// this will be set to the message describing why the declaration
7630b57cec5SDimitry Andric   /// was made unavailable (which may be empty).
7640b57cec5SDimitry Andric   bool isUnavailable(std::string *Message = nullptr) const {
7650b57cec5SDimitry Andric     return getAvailability(Message) == AR_Unavailable;
7660b57cec5SDimitry Andric   }
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric   /// Determine whether this is a weak-imported symbol.
7690b57cec5SDimitry Andric   ///
7700b57cec5SDimitry Andric   /// Weak-imported symbols are typically marked with the
7710b57cec5SDimitry Andric   /// 'weak_import' attribute, but may also be marked with an
7720b57cec5SDimitry Andric   /// 'availability' attribute where we're targing a platform prior to
7730b57cec5SDimitry Andric   /// the introduction of this feature.
7740b57cec5SDimitry Andric   bool isWeakImported() const;
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric   /// Determines whether this symbol can be weak-imported,
7770b57cec5SDimitry Andric   /// e.g., whether it would be well-formed to add the weak_import
7780b57cec5SDimitry Andric   /// attribute.
7790b57cec5SDimitry Andric   ///
7800b57cec5SDimitry Andric   /// \param IsDefinition Set to \c true to indicate that this
7810b57cec5SDimitry Andric   /// declaration cannot be weak-imported because it has a definition.
7820b57cec5SDimitry Andric   bool canBeWeakImported(bool &IsDefinition) const;
7830b57cec5SDimitry Andric 
7840b57cec5SDimitry Andric   /// Determine whether this declaration came from an AST file (such as
7850b57cec5SDimitry Andric   /// a precompiled header or module) rather than having been parsed.
isFromASTFile()7860b57cec5SDimitry Andric   bool isFromASTFile() const { return FromASTFile; }
7870b57cec5SDimitry Andric 
7880b57cec5SDimitry Andric   /// Retrieve the global declaration ID associated with this
7890b57cec5SDimitry Andric   /// declaration, which specifies where this Decl was loaded from.
getGlobalID()7900b57cec5SDimitry Andric   unsigned getGlobalID() const {
7910b57cec5SDimitry Andric     if (isFromASTFile())
7920b57cec5SDimitry Andric       return *((const unsigned*)this - 1);
7930b57cec5SDimitry Andric     return 0;
7940b57cec5SDimitry Andric   }
7950b57cec5SDimitry Andric 
7960b57cec5SDimitry Andric   /// Retrieve the global ID of the module that owns this particular
7970b57cec5SDimitry Andric   /// declaration.
getOwningModuleID()7980b57cec5SDimitry Andric   unsigned getOwningModuleID() const {
7990b57cec5SDimitry Andric     if (isFromASTFile())
8000b57cec5SDimitry Andric       return *((const unsigned*)this - 2);
8010b57cec5SDimitry Andric     return 0;
8020b57cec5SDimitry Andric   }
8030b57cec5SDimitry Andric 
8040b57cec5SDimitry Andric private:
8050b57cec5SDimitry Andric   Module *getOwningModuleSlow() const;
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric protected:
8080b57cec5SDimitry Andric   bool hasLocalOwningModuleStorage() const;
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric public:
8110b57cec5SDimitry Andric   /// Get the imported owning module, if this decl is from an imported
8120b57cec5SDimitry Andric   /// (non-local) module.
getImportedOwningModule()8130b57cec5SDimitry Andric   Module *getImportedOwningModule() const {
8140b57cec5SDimitry Andric     if (!isFromASTFile() || !hasOwningModule())
8150b57cec5SDimitry Andric       return nullptr;
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric     return getOwningModuleSlow();
8180b57cec5SDimitry Andric   }
8190b57cec5SDimitry Andric 
8200b57cec5SDimitry Andric   /// Get the local owning module, if known. Returns nullptr if owner is
8210b57cec5SDimitry Andric   /// not yet known or declaration is not from a module.
getLocalOwningModule()8220b57cec5SDimitry Andric   Module *getLocalOwningModule() const {
8230b57cec5SDimitry Andric     if (isFromASTFile() || !hasOwningModule())
8240b57cec5SDimitry Andric       return nullptr;
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric     assert(hasLocalOwningModuleStorage() &&
8270b57cec5SDimitry Andric            "owned local decl but no local module storage");
8280b57cec5SDimitry Andric     return reinterpret_cast<Module *const *>(this)[-1];
8290b57cec5SDimitry Andric   }
setLocalOwningModule(Module * M)8300b57cec5SDimitry Andric   void setLocalOwningModule(Module *M) {
8310b57cec5SDimitry Andric     assert(!isFromASTFile() && hasOwningModule() &&
8320b57cec5SDimitry Andric            hasLocalOwningModuleStorage() &&
8330b57cec5SDimitry Andric            "should not have a cached owning module");
8340b57cec5SDimitry Andric     reinterpret_cast<Module **>(this)[-1] = M;
8350b57cec5SDimitry Andric   }
8360b57cec5SDimitry Andric 
8370b57cec5SDimitry Andric   /// Is this declaration owned by some module?
hasOwningModule()8380b57cec5SDimitry Andric   bool hasOwningModule() const {
8390b57cec5SDimitry Andric     return getModuleOwnershipKind() != ModuleOwnershipKind::Unowned;
8400b57cec5SDimitry Andric   }
8410b57cec5SDimitry Andric 
8420b57cec5SDimitry Andric   /// Get the module that owns this declaration (for visibility purposes).
getOwningModule()8430b57cec5SDimitry Andric   Module *getOwningModule() const {
8440b57cec5SDimitry Andric     return isFromASTFile() ? getImportedOwningModule() : getLocalOwningModule();
8450b57cec5SDimitry Andric   }
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric   /// Get the module that owns this declaration for linkage purposes.
84806c3fb27SDimitry Andric   /// There only ever is such a standard C++ module.
8490b57cec5SDimitry Andric   ///
8500b57cec5SDimitry Andric   /// \param IgnoreLinkage Ignore the linkage of the entity; assume that
8510b57cec5SDimitry Andric   /// all declarations in a global module fragment are unowned.
8520b57cec5SDimitry Andric   Module *getOwningModuleForLinkage(bool IgnoreLinkage = false) const;
8530b57cec5SDimitry Andric 
8545ffd83dbSDimitry Andric   /// Determine whether this declaration is definitely visible to name lookup,
8555ffd83dbSDimitry Andric   /// independent of whether the owning module is visible.
8565ffd83dbSDimitry Andric   /// Note: The declaration may be visible even if this returns \c false if the
8575ffd83dbSDimitry Andric   /// owning module is visible within the query context. This is a low-level
8585ffd83dbSDimitry Andric   /// helper function; most code should be calling Sema::isVisible() instead.
isUnconditionallyVisible()8595ffd83dbSDimitry Andric   bool isUnconditionallyVisible() const {
8605ffd83dbSDimitry Andric     return (int)getModuleOwnershipKind() <= (int)ModuleOwnershipKind::Visible;
8610b57cec5SDimitry Andric   }
8620b57cec5SDimitry Andric 
isReachable()86381ad6265SDimitry Andric   bool isReachable() const {
86481ad6265SDimitry Andric     return (int)getModuleOwnershipKind() <=
86581ad6265SDimitry Andric            (int)ModuleOwnershipKind::ReachableWhenImported;
86681ad6265SDimitry Andric   }
86781ad6265SDimitry Andric 
8680b57cec5SDimitry Andric   /// Set that this declaration is globally visible, even if it came from a
8690b57cec5SDimitry Andric   /// module that is not visible.
setVisibleDespiteOwningModule()8700b57cec5SDimitry Andric   void setVisibleDespiteOwningModule() {
8715ffd83dbSDimitry Andric     if (!isUnconditionallyVisible())
8720b57cec5SDimitry Andric       setModuleOwnershipKind(ModuleOwnershipKind::Visible);
8730b57cec5SDimitry Andric   }
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric   /// Get the kind of module ownership for this declaration.
getModuleOwnershipKind()8760b57cec5SDimitry Andric   ModuleOwnershipKind getModuleOwnershipKind() const {
8770b57cec5SDimitry Andric     return NextInContextAndBits.getInt();
8780b57cec5SDimitry Andric   }
8790b57cec5SDimitry Andric 
8800b57cec5SDimitry Andric   /// Set whether this declaration is hidden from name lookup.
setModuleOwnershipKind(ModuleOwnershipKind MOK)8810b57cec5SDimitry Andric   void setModuleOwnershipKind(ModuleOwnershipKind MOK) {
8820b57cec5SDimitry Andric     assert(!(getModuleOwnershipKind() == ModuleOwnershipKind::Unowned &&
8830b57cec5SDimitry Andric              MOK != ModuleOwnershipKind::Unowned && !isFromASTFile() &&
8840b57cec5SDimitry Andric              !hasLocalOwningModuleStorage()) &&
8850b57cec5SDimitry Andric            "no storage available for owning module for this declaration");
8860b57cec5SDimitry Andric     NextInContextAndBits.setInt(MOK);
8870b57cec5SDimitry Andric   }
8880b57cec5SDimitry Andric 
getIdentifierNamespace()8890b57cec5SDimitry Andric   unsigned getIdentifierNamespace() const {
8900b57cec5SDimitry Andric     return IdentifierNamespace;
8910b57cec5SDimitry Andric   }
8920b57cec5SDimitry Andric 
isInIdentifierNamespace(unsigned NS)8930b57cec5SDimitry Andric   bool isInIdentifierNamespace(unsigned NS) const {
8940b57cec5SDimitry Andric     return getIdentifierNamespace() & NS;
8950b57cec5SDimitry Andric   }
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric   static unsigned getIdentifierNamespaceForKind(Kind DK);
8980b57cec5SDimitry Andric 
hasTagIdentifierNamespace()8990b57cec5SDimitry Andric   bool hasTagIdentifierNamespace() const {
9000b57cec5SDimitry Andric     return isTagIdentifierNamespace(getIdentifierNamespace());
9010b57cec5SDimitry Andric   }
9020b57cec5SDimitry Andric 
isTagIdentifierNamespace(unsigned NS)9030b57cec5SDimitry Andric   static bool isTagIdentifierNamespace(unsigned NS) {
9040b57cec5SDimitry Andric     // TagDecls have Tag and Type set and may also have TagFriend.
9050b57cec5SDimitry Andric     return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
9060b57cec5SDimitry Andric   }
9070b57cec5SDimitry Andric 
9080b57cec5SDimitry Andric   /// getLexicalDeclContext - The declaration context where this Decl was
9090b57cec5SDimitry Andric   /// lexically declared (LexicalDC). May be different from
9100b57cec5SDimitry Andric   /// getDeclContext() (SemanticDC).
9110b57cec5SDimitry Andric   /// e.g.:
9120b57cec5SDimitry Andric   ///
9130b57cec5SDimitry Andric   ///   namespace A {
9140b57cec5SDimitry Andric   ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
9150b57cec5SDimitry Andric   ///   }
9160b57cec5SDimitry Andric   ///   void A::f(); // SemanticDC == namespace 'A'
9170b57cec5SDimitry Andric   ///                // LexicalDC == global namespace
getLexicalDeclContext()9180b57cec5SDimitry Andric   DeclContext *getLexicalDeclContext() {
9190b57cec5SDimitry Andric     if (isInSemaDC())
9200b57cec5SDimitry Andric       return getSemanticDC();
9210b57cec5SDimitry Andric     return getMultipleDC()->LexicalDC;
9220b57cec5SDimitry Andric   }
getLexicalDeclContext()9230b57cec5SDimitry Andric   const DeclContext *getLexicalDeclContext() const {
9240b57cec5SDimitry Andric     return const_cast<Decl*>(this)->getLexicalDeclContext();
9250b57cec5SDimitry Andric   }
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric   /// Determine whether this declaration is declared out of line (outside its
9280b57cec5SDimitry Andric   /// semantic context).
9290b57cec5SDimitry Andric   virtual bool isOutOfLine() const;
9300b57cec5SDimitry Andric 
9310b57cec5SDimitry Andric   /// setDeclContext - Set both the semantic and lexical DeclContext
9320b57cec5SDimitry Andric   /// to DC.
9330b57cec5SDimitry Andric   void setDeclContext(DeclContext *DC);
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric   void setLexicalDeclContext(DeclContext *DC);
9360b57cec5SDimitry Andric 
9370b57cec5SDimitry Andric   /// Determine whether this declaration is a templated entity (whether it is
9380b57cec5SDimitry Andric   // within the scope of a template parameter).
9390b57cec5SDimitry Andric   bool isTemplated() const;
9400b57cec5SDimitry Andric 
9415ffd83dbSDimitry Andric   /// Determine the number of levels of template parameter surrounding this
9425ffd83dbSDimitry Andric   /// declaration.
9435ffd83dbSDimitry Andric   unsigned getTemplateDepth() const;
9445ffd83dbSDimitry Andric 
9450b57cec5SDimitry Andric   /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
9460b57cec5SDimitry Andric   /// scoped decl is defined outside the current function or method.  This is
9470b57cec5SDimitry Andric   /// roughly global variables and functions, but also handles enums (which
9480b57cec5SDimitry Andric   /// could be defined inside or outside a function etc).
isDefinedOutsideFunctionOrMethod()9490b57cec5SDimitry Andric   bool isDefinedOutsideFunctionOrMethod() const {
9500b57cec5SDimitry Andric     return getParentFunctionOrMethod() == nullptr;
9510b57cec5SDimitry Andric   }
9520b57cec5SDimitry Andric 
9535ffd83dbSDimitry Andric   /// Determine whether a substitution into this declaration would occur as
9545ffd83dbSDimitry Andric   /// part of a substitution into a dependent local scope. Such a substitution
9555ffd83dbSDimitry Andric   /// transitively substitutes into all constructs nested within this
9565ffd83dbSDimitry Andric   /// declaration.
9575ffd83dbSDimitry Andric   ///
9585ffd83dbSDimitry Andric   /// This recognizes non-defining declarations as well as members of local
9595ffd83dbSDimitry Andric   /// classes and lambdas:
9600b57cec5SDimitry Andric   /// \code
9615ffd83dbSDimitry Andric   ///     template<typename T> void foo() { void bar(); }
9625ffd83dbSDimitry Andric   ///     template<typename T> void foo2() { class ABC { void bar(); }; }
9635ffd83dbSDimitry Andric   ///     template<typename T> inline int x = [](){ return 0; }();
9640b57cec5SDimitry Andric   /// \endcode
9655ffd83dbSDimitry Andric   bool isInLocalScopeForInstantiation() const;
9660b57cec5SDimitry Andric 
9670b57cec5SDimitry Andric   /// If this decl is defined inside a function/method/block it returns
9680b57cec5SDimitry Andric   /// the corresponding DeclContext, otherwise it returns null.
969fcaf7f86SDimitry Andric   const DeclContext *
970fcaf7f86SDimitry Andric   getParentFunctionOrMethod(bool LexicalParent = false) const;
971fcaf7f86SDimitry Andric   DeclContext *getParentFunctionOrMethod(bool LexicalParent = false) {
9720b57cec5SDimitry Andric     return const_cast<DeclContext *>(
973fcaf7f86SDimitry Andric         const_cast<const Decl *>(this)->getParentFunctionOrMethod(
974fcaf7f86SDimitry Andric             LexicalParent));
9750b57cec5SDimitry Andric   }
9760b57cec5SDimitry Andric 
9770b57cec5SDimitry Andric   /// Retrieves the "canonical" declaration of the given declaration.
getCanonicalDecl()9780b57cec5SDimitry Andric   virtual Decl *getCanonicalDecl() { return this; }
getCanonicalDecl()9790b57cec5SDimitry Andric   const Decl *getCanonicalDecl() const {
9800b57cec5SDimitry Andric     return const_cast<Decl*>(this)->getCanonicalDecl();
9810b57cec5SDimitry Andric   }
9820b57cec5SDimitry Andric 
9830b57cec5SDimitry Andric   /// Whether this particular Decl is a canonical one.
isCanonicalDecl()9840b57cec5SDimitry Andric   bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric protected:
9870b57cec5SDimitry Andric   /// Returns the next redeclaration or itself if this is the only decl.
9880b57cec5SDimitry Andric   ///
9890b57cec5SDimitry Andric   /// Decl subclasses that can be redeclared should override this method so that
9900b57cec5SDimitry Andric   /// Decl::redecl_iterator can iterate over them.
getNextRedeclarationImpl()9910b57cec5SDimitry Andric   virtual Decl *getNextRedeclarationImpl() { return this; }
9920b57cec5SDimitry Andric 
9930b57cec5SDimitry Andric   /// Implementation of getPreviousDecl(), to be overridden by any
9940b57cec5SDimitry Andric   /// subclass that has a redeclaration chain.
getPreviousDeclImpl()9950b57cec5SDimitry Andric   virtual Decl *getPreviousDeclImpl() { return nullptr; }
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric   /// Implementation of getMostRecentDecl(), to be overridden by any
9980b57cec5SDimitry Andric   /// subclass that has a redeclaration chain.
getMostRecentDeclImpl()9990b57cec5SDimitry Andric   virtual Decl *getMostRecentDeclImpl() { return this; }
10000b57cec5SDimitry Andric 
10010b57cec5SDimitry Andric public:
10020b57cec5SDimitry Andric   /// Iterates through all the redeclarations of the same decl.
10030b57cec5SDimitry Andric   class redecl_iterator {
10040b57cec5SDimitry Andric     /// Current - The current declaration.
10050b57cec5SDimitry Andric     Decl *Current = nullptr;
10060b57cec5SDimitry Andric     Decl *Starter;
10070b57cec5SDimitry Andric 
10080b57cec5SDimitry Andric   public:
10090b57cec5SDimitry Andric     using value_type = Decl *;
10100b57cec5SDimitry Andric     using reference = const value_type &;
10110b57cec5SDimitry Andric     using pointer = const value_type *;
10120b57cec5SDimitry Andric     using iterator_category = std::forward_iterator_tag;
10130b57cec5SDimitry Andric     using difference_type = std::ptrdiff_t;
10140b57cec5SDimitry Andric 
10150b57cec5SDimitry Andric     redecl_iterator() = default;
redecl_iterator(Decl * C)10160b57cec5SDimitry Andric     explicit redecl_iterator(Decl *C) : Current(C), Starter(C) {}
10170b57cec5SDimitry Andric 
10180b57cec5SDimitry Andric     reference operator*() const { return Current; }
10190b57cec5SDimitry Andric     value_type operator->() const { return Current; }
10200b57cec5SDimitry Andric 
10210b57cec5SDimitry Andric     redecl_iterator& operator++() {
10220b57cec5SDimitry Andric       assert(Current && "Advancing while iterator has reached end");
10230b57cec5SDimitry Andric       // Get either previous decl or latest decl.
10240b57cec5SDimitry Andric       Decl *Next = Current->getNextRedeclarationImpl();
10250b57cec5SDimitry Andric       assert(Next && "Should return next redeclaration or itself, never null!");
10260b57cec5SDimitry Andric       Current = (Next != Starter) ? Next : nullptr;
10270b57cec5SDimitry Andric       return *this;
10280b57cec5SDimitry Andric     }
10290b57cec5SDimitry Andric 
10300b57cec5SDimitry Andric     redecl_iterator operator++(int) {
10310b57cec5SDimitry Andric       redecl_iterator tmp(*this);
10320b57cec5SDimitry Andric       ++(*this);
10330b57cec5SDimitry Andric       return tmp;
10340b57cec5SDimitry Andric     }
10350b57cec5SDimitry Andric 
10360b57cec5SDimitry Andric     friend bool operator==(redecl_iterator x, redecl_iterator y) {
10370b57cec5SDimitry Andric       return x.Current == y.Current;
10380b57cec5SDimitry Andric     }
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric     friend bool operator!=(redecl_iterator x, redecl_iterator y) {
10410b57cec5SDimitry Andric       return x.Current != y.Current;
10420b57cec5SDimitry Andric     }
10430b57cec5SDimitry Andric   };
10440b57cec5SDimitry Andric 
10450b57cec5SDimitry Andric   using redecl_range = llvm::iterator_range<redecl_iterator>;
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric   /// Returns an iterator range for all the redeclarations of the same
10480b57cec5SDimitry Andric   /// decl. It will iterate at least once (when this decl is the only one).
redecls()10490b57cec5SDimitry Andric   redecl_range redecls() const {
10500b57cec5SDimitry Andric     return redecl_range(redecls_begin(), redecls_end());
10510b57cec5SDimitry Andric   }
10520b57cec5SDimitry Andric 
redecls_begin()10530b57cec5SDimitry Andric   redecl_iterator redecls_begin() const {
10540b57cec5SDimitry Andric     return redecl_iterator(const_cast<Decl *>(this));
10550b57cec5SDimitry Andric   }
10560b57cec5SDimitry Andric 
redecls_end()10570b57cec5SDimitry Andric   redecl_iterator redecls_end() const { return redecl_iterator(); }
10580b57cec5SDimitry Andric 
10590b57cec5SDimitry Andric   /// Retrieve the previous declaration that declares the same entity
10600b57cec5SDimitry Andric   /// as this declaration, or NULL if there is no previous declaration.
getPreviousDecl()10610b57cec5SDimitry Andric   Decl *getPreviousDecl() { return getPreviousDeclImpl(); }
10620b57cec5SDimitry Andric 
1063a7dea167SDimitry Andric   /// Retrieve the previous declaration that declares the same entity
10640b57cec5SDimitry Andric   /// as this declaration, or NULL if there is no previous declaration.
getPreviousDecl()10650b57cec5SDimitry Andric   const Decl *getPreviousDecl() const {
10660b57cec5SDimitry Andric     return const_cast<Decl *>(this)->getPreviousDeclImpl();
10670b57cec5SDimitry Andric   }
10680b57cec5SDimitry Andric 
10690b57cec5SDimitry Andric   /// True if this is the first declaration in its redeclaration chain.
isFirstDecl()10700b57cec5SDimitry Andric   bool isFirstDecl() const {
10710b57cec5SDimitry Andric     return getPreviousDecl() == nullptr;
10720b57cec5SDimitry Andric   }
10730b57cec5SDimitry Andric 
10740b57cec5SDimitry Andric   /// Retrieve the most recent declaration that declares the same entity
10750b57cec5SDimitry Andric   /// as this declaration (which may be this declaration).
getMostRecentDecl()10760b57cec5SDimitry Andric   Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); }
10770b57cec5SDimitry Andric 
10780b57cec5SDimitry Andric   /// Retrieve the most recent declaration that declares the same entity
10790b57cec5SDimitry Andric   /// as this declaration (which may be this declaration).
getMostRecentDecl()10800b57cec5SDimitry Andric   const Decl *getMostRecentDecl() const {
10810b57cec5SDimitry Andric     return const_cast<Decl *>(this)->getMostRecentDeclImpl();
10820b57cec5SDimitry Andric   }
10830b57cec5SDimitry Andric 
10840b57cec5SDimitry Andric   /// getBody - If this Decl represents a declaration for a body of code,
10850b57cec5SDimitry Andric   ///  such as a function or method definition, this method returns the
10860b57cec5SDimitry Andric   ///  top-level Stmt* of that body.  Otherwise this method returns null.
getBody()10870b57cec5SDimitry Andric   virtual Stmt* getBody() const { return nullptr; }
10880b57cec5SDimitry Andric 
10890b57cec5SDimitry Andric   /// Returns true if this \c Decl represents a declaration for a body of
10900b57cec5SDimitry Andric   /// code, such as a function or method definition.
10910b57cec5SDimitry Andric   /// Note that \c hasBody can also return true if any redeclaration of this
10920b57cec5SDimitry Andric   /// \c Decl represents a declaration for a body of code.
hasBody()10930b57cec5SDimitry Andric   virtual bool hasBody() const { return getBody() != nullptr; }
10940b57cec5SDimitry Andric 
10950b57cec5SDimitry Andric   /// getBodyRBrace - Gets the right brace of the body, if a body exists.
10960b57cec5SDimitry Andric   /// This works whether the body is a CompoundStmt or a CXXTryStmt.
10970b57cec5SDimitry Andric   SourceLocation getBodyRBrace() const;
10980b57cec5SDimitry Andric 
10990b57cec5SDimitry Andric   // global temp stats (until we have a per-module visitor)
11000b57cec5SDimitry Andric   static void add(Kind k);
11010b57cec5SDimitry Andric   static void EnableStatistics();
11020b57cec5SDimitry Andric   static void PrintStats();
11030b57cec5SDimitry Andric 
11040b57cec5SDimitry Andric   /// isTemplateParameter - Determines whether this declaration is a
11050b57cec5SDimitry Andric   /// template parameter.
11060b57cec5SDimitry Andric   bool isTemplateParameter() const;
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric   /// isTemplateParameter - Determines whether this declaration is a
11090b57cec5SDimitry Andric   /// template parameter pack.
11100b57cec5SDimitry Andric   bool isTemplateParameterPack() const;
11110b57cec5SDimitry Andric 
11120b57cec5SDimitry Andric   /// Whether this declaration is a parameter pack.
11130b57cec5SDimitry Andric   bool isParameterPack() const;
11140b57cec5SDimitry Andric 
11150b57cec5SDimitry Andric   /// returns true if this declaration is a template
11160b57cec5SDimitry Andric   bool isTemplateDecl() const;
11170b57cec5SDimitry Andric 
11180b57cec5SDimitry Andric   /// Whether this declaration is a function or function template.
isFunctionOrFunctionTemplate()11190b57cec5SDimitry Andric   bool isFunctionOrFunctionTemplate() const {
11200b57cec5SDimitry Andric     return (DeclKind >= Decl::firstFunction &&
11210b57cec5SDimitry Andric             DeclKind <= Decl::lastFunction) ||
11220b57cec5SDimitry Andric            DeclKind == FunctionTemplate;
11230b57cec5SDimitry Andric   }
11240b57cec5SDimitry Andric 
11250b57cec5SDimitry Andric   /// If this is a declaration that describes some template, this
11260b57cec5SDimitry Andric   /// method returns that template declaration.
11275ffd83dbSDimitry Andric   ///
11285ffd83dbSDimitry Andric   /// Note that this returns nullptr for partial specializations, because they
11295ffd83dbSDimitry Andric   /// are not modeled as TemplateDecls. Use getDescribedTemplateParams to handle
11305ffd83dbSDimitry Andric   /// those cases.
11310b57cec5SDimitry Andric   TemplateDecl *getDescribedTemplate() const;
11320b57cec5SDimitry Andric 
11335ffd83dbSDimitry Andric   /// If this is a declaration that describes some template or partial
11345ffd83dbSDimitry Andric   /// specialization, this returns the corresponding template parameter list.
11355ffd83dbSDimitry Andric   const TemplateParameterList *getDescribedTemplateParams() const;
11365ffd83dbSDimitry Andric 
11370b57cec5SDimitry Andric   /// Returns the function itself, or the templated function if this is a
11380b57cec5SDimitry Andric   /// function template.
11390b57cec5SDimitry Andric   FunctionDecl *getAsFunction() LLVM_READONLY;
11400b57cec5SDimitry Andric 
getAsFunction()11410b57cec5SDimitry Andric   const FunctionDecl *getAsFunction() const {
11420b57cec5SDimitry Andric     return const_cast<Decl *>(this)->getAsFunction();
11430b57cec5SDimitry Andric   }
11440b57cec5SDimitry Andric 
11450b57cec5SDimitry Andric   /// Changes the namespace of this declaration to reflect that it's
11460b57cec5SDimitry Andric   /// a function-local extern declaration.
11470b57cec5SDimitry Andric   ///
11480b57cec5SDimitry Andric   /// These declarations appear in the lexical context of the extern
11490b57cec5SDimitry Andric   /// declaration, but in the semantic context of the enclosing namespace
11500b57cec5SDimitry Andric   /// scope.
setLocalExternDecl()11510b57cec5SDimitry Andric   void setLocalExternDecl() {
11520b57cec5SDimitry Andric     Decl *Prev = getPreviousDecl();
11530b57cec5SDimitry Andric     IdentifierNamespace &= ~IDNS_Ordinary;
11540b57cec5SDimitry Andric 
11550b57cec5SDimitry Andric     // It's OK for the declaration to still have the "invisible friend" flag or
11560b57cec5SDimitry Andric     // the "conflicts with tag declarations in this scope" flag for the outer
11570b57cec5SDimitry Andric     // scope.
11580b57cec5SDimitry Andric     assert((IdentifierNamespace & ~(IDNS_OrdinaryFriend | IDNS_Tag)) == 0 &&
11590b57cec5SDimitry Andric            "namespace is not ordinary");
11600b57cec5SDimitry Andric 
11610b57cec5SDimitry Andric     IdentifierNamespace |= IDNS_LocalExtern;
11620b57cec5SDimitry Andric     if (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary)
11630b57cec5SDimitry Andric       IdentifierNamespace |= IDNS_Ordinary;
11640b57cec5SDimitry Andric   }
11650b57cec5SDimitry Andric 
11660b57cec5SDimitry Andric   /// Determine whether this is a block-scope declaration with linkage.
11670b57cec5SDimitry Andric   /// This will either be a local variable declaration declared 'extern', or a
11680b57cec5SDimitry Andric   /// local function declaration.
isLocalExternDecl()1169bdd1243dSDimitry Andric   bool isLocalExternDecl() const {
11700b57cec5SDimitry Andric     return IdentifierNamespace & IDNS_LocalExtern;
11710b57cec5SDimitry Andric   }
11720b57cec5SDimitry Andric 
11730b57cec5SDimitry Andric   /// Changes the namespace of this declaration to reflect that it's
11740b57cec5SDimitry Andric   /// the object of a friend declaration.
11750b57cec5SDimitry Andric   ///
11760b57cec5SDimitry Andric   /// These declarations appear in the lexical context of the friending
11770b57cec5SDimitry Andric   /// class, but in the semantic context of the actual entity.  This property
11780b57cec5SDimitry Andric   /// applies only to a specific decl object;  other redeclarations of the
11790b57cec5SDimitry Andric   /// same entity may not (and probably don't) share this property.
11800b57cec5SDimitry Andric   void setObjectOfFriendDecl(bool PerformFriendInjection = false) {
11810b57cec5SDimitry Andric     unsigned OldNS = IdentifierNamespace;
11820b57cec5SDimitry Andric     assert((OldNS & (IDNS_Tag | IDNS_Ordinary |
11830b57cec5SDimitry Andric                      IDNS_TagFriend | IDNS_OrdinaryFriend |
11840b57cec5SDimitry Andric                      IDNS_LocalExtern | IDNS_NonMemberOperator)) &&
11850b57cec5SDimitry Andric            "namespace includes neither ordinary nor tag");
11860b57cec5SDimitry Andric     assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |
11870b57cec5SDimitry Andric                        IDNS_TagFriend | IDNS_OrdinaryFriend |
11880b57cec5SDimitry Andric                        IDNS_LocalExtern | IDNS_NonMemberOperator)) &&
11890b57cec5SDimitry Andric            "namespace includes other than ordinary or tag");
11900b57cec5SDimitry Andric 
11910b57cec5SDimitry Andric     Decl *Prev = getPreviousDecl();
11920b57cec5SDimitry Andric     IdentifierNamespace &= ~(IDNS_Ordinary | IDNS_Tag | IDNS_Type);
11930b57cec5SDimitry Andric 
11940b57cec5SDimitry Andric     if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
11950b57cec5SDimitry Andric       IdentifierNamespace |= IDNS_TagFriend;
11960b57cec5SDimitry Andric       if (PerformFriendInjection ||
11970b57cec5SDimitry Andric           (Prev && Prev->getIdentifierNamespace() & IDNS_Tag))
11980b57cec5SDimitry Andric         IdentifierNamespace |= IDNS_Tag | IDNS_Type;
11990b57cec5SDimitry Andric     }
12000b57cec5SDimitry Andric 
12010b57cec5SDimitry Andric     if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend |
12020b57cec5SDimitry Andric                  IDNS_LocalExtern | IDNS_NonMemberOperator)) {
12030b57cec5SDimitry Andric       IdentifierNamespace |= IDNS_OrdinaryFriend;
12040b57cec5SDimitry Andric       if (PerformFriendInjection ||
12050b57cec5SDimitry Andric           (Prev && Prev->getIdentifierNamespace() & IDNS_Ordinary))
12060b57cec5SDimitry Andric         IdentifierNamespace |= IDNS_Ordinary;
12070b57cec5SDimitry Andric     }
12080b57cec5SDimitry Andric   }
12090b57cec5SDimitry Andric 
121006c3fb27SDimitry Andric   /// Clears the namespace of this declaration.
121106c3fb27SDimitry Andric   ///
121206c3fb27SDimitry Andric   /// This is useful if we want this declaration to be available for
121306c3fb27SDimitry Andric   /// redeclaration lookup but otherwise hidden for ordinary name lookups.
clearIdentifierNamespace()121406c3fb27SDimitry Andric   void clearIdentifierNamespace() { IdentifierNamespace = 0; }
121506c3fb27SDimitry Andric 
12160b57cec5SDimitry Andric   enum FriendObjectKind {
12170b57cec5SDimitry Andric     FOK_None,      ///< Not a friend object.
12180b57cec5SDimitry Andric     FOK_Declared,  ///< A friend of a previously-declared entity.
12190b57cec5SDimitry Andric     FOK_Undeclared ///< A friend of a previously-undeclared entity.
12200b57cec5SDimitry Andric   };
12210b57cec5SDimitry Andric 
12220b57cec5SDimitry Andric   /// Determines whether this declaration is the object of a
12230b57cec5SDimitry Andric   /// friend declaration and, if so, what kind.
12240b57cec5SDimitry Andric   ///
12250b57cec5SDimitry Andric   /// There is currently no direct way to find the associated FriendDecl.
getFriendObjectKind()12260b57cec5SDimitry Andric   FriendObjectKind getFriendObjectKind() const {
12270b57cec5SDimitry Andric     unsigned mask =
12280b57cec5SDimitry Andric         (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
12290b57cec5SDimitry Andric     if (!mask) return FOK_None;
12300b57cec5SDimitry Andric     return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ? FOK_Declared
12310b57cec5SDimitry Andric                                                              : FOK_Undeclared);
12320b57cec5SDimitry Andric   }
12330b57cec5SDimitry Andric 
12340b57cec5SDimitry Andric   /// Specifies that this declaration is a C++ overloaded non-member.
setNonMemberOperator()12350b57cec5SDimitry Andric   void setNonMemberOperator() {
12360b57cec5SDimitry Andric     assert(getKind() == Function || getKind() == FunctionTemplate);
12370b57cec5SDimitry Andric     assert((IdentifierNamespace & IDNS_Ordinary) &&
12380b57cec5SDimitry Andric            "visible non-member operators should be in ordinary namespace");
12390b57cec5SDimitry Andric     IdentifierNamespace |= IDNS_NonMemberOperator;
12400b57cec5SDimitry Andric   }
12410b57cec5SDimitry Andric 
classofKind(Kind K)12420b57cec5SDimitry Andric   static bool classofKind(Kind K) { return true; }
12430b57cec5SDimitry Andric   static DeclContext *castToDeclContext(const Decl *);
12440b57cec5SDimitry Andric   static Decl *castFromDeclContext(const DeclContext *);
12450b57cec5SDimitry Andric 
12460b57cec5SDimitry Andric   void print(raw_ostream &Out, unsigned Indentation = 0,
12470b57cec5SDimitry Andric              bool PrintInstantiation = false) const;
12480b57cec5SDimitry Andric   void print(raw_ostream &Out, const PrintingPolicy &Policy,
12490b57cec5SDimitry Andric              unsigned Indentation = 0, bool PrintInstantiation = false) const;
12500b57cec5SDimitry Andric   static void printGroup(Decl** Begin, unsigned NumDecls,
12510b57cec5SDimitry Andric                          raw_ostream &Out, const PrintingPolicy &Policy,
12520b57cec5SDimitry Andric                          unsigned Indentation = 0);
12530b57cec5SDimitry Andric 
12540b57cec5SDimitry Andric   // Debuggers don't usually respect default arguments.
12550b57cec5SDimitry Andric   void dump() const;
12560b57cec5SDimitry Andric 
12570b57cec5SDimitry Andric   // Same as dump(), but forces color printing.
12580b57cec5SDimitry Andric   void dumpColor() const;
12590b57cec5SDimitry Andric 
12600b57cec5SDimitry Andric   void dump(raw_ostream &Out, bool Deserialize = false,
12610b57cec5SDimitry Andric             ASTDumpOutputFormat OutputFormat = ADOF_Default) const;
12620b57cec5SDimitry Andric 
12630b57cec5SDimitry Andric   /// \return Unique reproducible object identifier
12640b57cec5SDimitry Andric   int64_t getID() const;
12650b57cec5SDimitry Andric 
12660b57cec5SDimitry Andric   /// Looks through the Decl's underlying type to extract a FunctionType
12670b57cec5SDimitry Andric   /// when possible. Will return null if the type underlying the Decl does not
12680b57cec5SDimitry Andric   /// have a FunctionType.
12690b57cec5SDimitry Andric   const FunctionType *getFunctionType(bool BlocksToo = true) const;
12700b57cec5SDimitry Andric 
127106c3fb27SDimitry Andric   // Looks through the Decl's underlying type to determine if it's a
127206c3fb27SDimitry Andric   // function pointer type.
127306c3fb27SDimitry Andric   bool isFunctionPointerType() const;
127406c3fb27SDimitry Andric 
12750b57cec5SDimitry Andric private:
12760b57cec5SDimitry Andric   void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
12770b57cec5SDimitry Andric   void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
12780b57cec5SDimitry Andric                            ASTContext &Ctx);
12790b57cec5SDimitry Andric 
12800b57cec5SDimitry Andric protected:
12810b57cec5SDimitry Andric   ASTMutationListener *getASTMutationListener() const;
12820b57cec5SDimitry Andric };
12830b57cec5SDimitry Andric 
12840b57cec5SDimitry Andric /// Determine whether two declarations declare the same entity.
declaresSameEntity(const Decl * D1,const Decl * D2)12850b57cec5SDimitry Andric inline bool declaresSameEntity(const Decl *D1, const Decl *D2) {
12860b57cec5SDimitry Andric   if (!D1 || !D2)
12870b57cec5SDimitry Andric     return false;
12880b57cec5SDimitry Andric 
12890b57cec5SDimitry Andric   if (D1 == D2)
12900b57cec5SDimitry Andric     return true;
12910b57cec5SDimitry Andric 
12920b57cec5SDimitry Andric   return D1->getCanonicalDecl() == D2->getCanonicalDecl();
12930b57cec5SDimitry Andric }
12940b57cec5SDimitry Andric 
12950b57cec5SDimitry Andric /// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
12960b57cec5SDimitry Andric /// doing something to a specific decl.
12970b57cec5SDimitry Andric class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
12980b57cec5SDimitry Andric   const Decl *TheDecl;
12990b57cec5SDimitry Andric   SourceLocation Loc;
13000b57cec5SDimitry Andric   SourceManager &SM;
13010b57cec5SDimitry Andric   const char *Message;
13020b57cec5SDimitry Andric 
13030b57cec5SDimitry Andric public:
PrettyStackTraceDecl(const Decl * theDecl,SourceLocation L,SourceManager & sm,const char * Msg)13040b57cec5SDimitry Andric   PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
13050b57cec5SDimitry Andric                        SourceManager &sm, const char *Msg)
13060b57cec5SDimitry Andric       : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
13070b57cec5SDimitry Andric 
13080b57cec5SDimitry Andric   void print(raw_ostream &OS) const override;
13090b57cec5SDimitry Andric };
1310fe6060f1SDimitry Andric } // namespace clang
13110b57cec5SDimitry Andric 
1312fe6060f1SDimitry Andric // Required to determine the layout of the PointerUnion<NamedDecl*> before
1313fe6060f1SDimitry Andric // seeing the NamedDecl definition being first used in DeclListNode::operator*.
1314fe6060f1SDimitry Andric namespace llvm {
1315fe6060f1SDimitry Andric   template <> struct PointerLikeTypeTraits<::clang::NamedDecl *> {
1316fe6060f1SDimitry Andric     static inline void *getAsVoidPointer(::clang::NamedDecl *P) { return P; }
1317fe6060f1SDimitry Andric     static inline ::clang::NamedDecl *getFromVoidPointer(void *P) {
1318fe6060f1SDimitry Andric       return static_cast<::clang::NamedDecl *>(P);
1319fe6060f1SDimitry Andric     }
1320fe6060f1SDimitry Andric     static constexpr int NumLowBitsAvailable = 3;
1321fe6060f1SDimitry Andric   };
1322fe6060f1SDimitry Andric }
1323fe6060f1SDimitry Andric 
1324fe6060f1SDimitry Andric namespace clang {
1325fe6060f1SDimitry Andric /// A list storing NamedDecls in the lookup tables.
1326fe6060f1SDimitry Andric class DeclListNode {
1327fe6060f1SDimitry Andric   friend class ASTContext; // allocate, deallocate nodes.
1328fe6060f1SDimitry Andric   friend class StoredDeclsList;
1329fe6060f1SDimitry Andric public:
1330fe6060f1SDimitry Andric   using Decls = llvm::PointerUnion<NamedDecl*, DeclListNode*>;
1331fe6060f1SDimitry Andric   class iterator {
1332fe6060f1SDimitry Andric     friend class DeclContextLookupResult;
1333fe6060f1SDimitry Andric     friend class StoredDeclsList;
1334fe6060f1SDimitry Andric 
1335fe6060f1SDimitry Andric     Decls Ptr;
1336fe6060f1SDimitry Andric     iterator(Decls Node) : Ptr(Node) { }
1337fe6060f1SDimitry Andric   public:
1338fe6060f1SDimitry Andric     using difference_type = ptrdiff_t;
1339fe6060f1SDimitry Andric     using value_type = NamedDecl*;
1340fe6060f1SDimitry Andric     using pointer = void;
1341fe6060f1SDimitry Andric     using reference = value_type;
1342fe6060f1SDimitry Andric     using iterator_category = std::forward_iterator_tag;
1343fe6060f1SDimitry Andric 
1344fe6060f1SDimitry Andric     iterator() = default;
1345fe6060f1SDimitry Andric 
1346fe6060f1SDimitry Andric     reference operator*() const {
1347fe6060f1SDimitry Andric       assert(Ptr && "dereferencing end() iterator");
1348fe6060f1SDimitry Andric       if (DeclListNode *CurNode = Ptr.dyn_cast<DeclListNode*>())
1349fe6060f1SDimitry Andric         return CurNode->D;
1350fe6060f1SDimitry Andric       return Ptr.get<NamedDecl*>();
1351fe6060f1SDimitry Andric     }
1352fe6060f1SDimitry Andric     void operator->() const { } // Unsupported.
1353fe6060f1SDimitry Andric     bool operator==(const iterator &X) const { return Ptr == X.Ptr; }
1354fe6060f1SDimitry Andric     bool operator!=(const iterator &X) const { return Ptr != X.Ptr; }
1355fe6060f1SDimitry Andric     inline iterator &operator++() { // ++It
1356fe6060f1SDimitry Andric       assert(!Ptr.isNull() && "Advancing empty iterator");
1357fe6060f1SDimitry Andric 
1358fe6060f1SDimitry Andric       if (DeclListNode *CurNode = Ptr.dyn_cast<DeclListNode*>())
1359fe6060f1SDimitry Andric         Ptr = CurNode->Rest;
1360fe6060f1SDimitry Andric       else
1361fe6060f1SDimitry Andric         Ptr = nullptr;
1362fe6060f1SDimitry Andric       return *this;
1363fe6060f1SDimitry Andric     }
1364fe6060f1SDimitry Andric     iterator operator++(int) { // It++
1365fe6060f1SDimitry Andric       iterator temp = *this;
1366fe6060f1SDimitry Andric       ++(*this);
1367fe6060f1SDimitry Andric       return temp;
1368fe6060f1SDimitry Andric     }
1369fe6060f1SDimitry Andric     // Enables the pattern for (iterator I =..., E = I.end(); I != E; ++I)
1370fe6060f1SDimitry Andric     iterator end() { return iterator(); }
1371fe6060f1SDimitry Andric   };
1372fe6060f1SDimitry Andric private:
1373fe6060f1SDimitry Andric   NamedDecl *D = nullptr;
1374fe6060f1SDimitry Andric   Decls Rest = nullptr;
1375fe6060f1SDimitry Andric   DeclListNode(NamedDecl *ND) : D(ND) {}
1376fe6060f1SDimitry Andric };
1377fe6060f1SDimitry Andric 
1378fe6060f1SDimitry Andric /// The results of name lookup within a DeclContext.
13790b57cec5SDimitry Andric class DeclContextLookupResult {
1380fe6060f1SDimitry Andric   using Decls = DeclListNode::Decls;
13810b57cec5SDimitry Andric 
1382fe6060f1SDimitry Andric   /// When in collection form, this is what the Data pointer points to.
1383fe6060f1SDimitry Andric   Decls Result;
13840b57cec5SDimitry Andric 
13850b57cec5SDimitry Andric public:
13860b57cec5SDimitry Andric   DeclContextLookupResult() = default;
1387fe6060f1SDimitry Andric   DeclContextLookupResult(Decls Result) : Result(Result) {}
13880b57cec5SDimitry Andric 
1389fe6060f1SDimitry Andric   using iterator = DeclListNode::iterator;
13900b57cec5SDimitry Andric   using const_iterator = iterator;
13910b57cec5SDimitry Andric   using reference = iterator::reference;
13920b57cec5SDimitry Andric 
1393fe6060f1SDimitry Andric   iterator begin() { return iterator(Result); }
1394fe6060f1SDimitry Andric   iterator end() { return iterator(); }
1395fe6060f1SDimitry Andric   const_iterator begin() const {
1396fe6060f1SDimitry Andric     return const_cast<DeclContextLookupResult*>(this)->begin();
1397fe6060f1SDimitry Andric   }
1398fe6060f1SDimitry Andric   const_iterator end() const { return iterator(); }
13990b57cec5SDimitry Andric 
1400fe6060f1SDimitry Andric   bool empty() const { return Result.isNull();  }
1401fe6060f1SDimitry Andric   bool isSingleResult() const { return Result.dyn_cast<NamedDecl*>(); }
1402fe6060f1SDimitry Andric   reference front() const { return *begin(); }
14030b57cec5SDimitry Andric 
1404fe6060f1SDimitry Andric   // Find the first declaration of the given type in the list. Note that this
1405fe6060f1SDimitry Andric   // is not in general the earliest-declared declaration, and should only be
1406fe6060f1SDimitry Andric   // used when it's not possible for there to be more than one match or where
1407fe6060f1SDimitry Andric   // it doesn't matter which one is found.
1408fe6060f1SDimitry Andric   template<class T> T *find_first() const {
1409fe6060f1SDimitry Andric     for (auto *D : *this)
1410fe6060f1SDimitry Andric       if (T *Decl = dyn_cast<T>(D))
1411fe6060f1SDimitry Andric         return Decl;
1412fe6060f1SDimitry Andric 
1413fe6060f1SDimitry Andric     return nullptr;
14140b57cec5SDimitry Andric   }
14150b57cec5SDimitry Andric };
14160b57cec5SDimitry Andric 
141706c3fb27SDimitry Andric /// Only used by CXXDeductionGuideDecl.
141806c3fb27SDimitry Andric enum class DeductionCandidate : unsigned char {
141906c3fb27SDimitry Andric   Normal,
142006c3fb27SDimitry Andric   Copy,
142106c3fb27SDimitry Andric   Aggregate,
142206c3fb27SDimitry Andric };
142306c3fb27SDimitry Andric 
14245f757f3fSDimitry Andric enum class RecordArgPassingKind;
14255f757f3fSDimitry Andric enum class OMPDeclareReductionInitKind;
14265f757f3fSDimitry Andric enum class ObjCImplementationControl;
14275f757f3fSDimitry Andric enum class LinkageSpecLanguageIDs;
14285f757f3fSDimitry Andric 
14290b57cec5SDimitry Andric /// DeclContext - This is used only as base class of specific decl types that
14300b57cec5SDimitry Andric /// can act as declaration contexts. These decls are (only the top classes
14310b57cec5SDimitry Andric /// that directly derive from DeclContext are mentioned, not their subclasses):
14320b57cec5SDimitry Andric ///
14330b57cec5SDimitry Andric ///   TranslationUnitDecl
14340b57cec5SDimitry Andric ///   ExternCContext
14350b57cec5SDimitry Andric ///   NamespaceDecl
14360b57cec5SDimitry Andric ///   TagDecl
14370b57cec5SDimitry Andric ///   OMPDeclareReductionDecl
14380b57cec5SDimitry Andric ///   OMPDeclareMapperDecl
14390b57cec5SDimitry Andric ///   FunctionDecl
14400b57cec5SDimitry Andric ///   ObjCMethodDecl
14410b57cec5SDimitry Andric ///   ObjCContainerDecl
14420b57cec5SDimitry Andric ///   LinkageSpecDecl
14430b57cec5SDimitry Andric ///   ExportDecl
14440b57cec5SDimitry Andric ///   BlockDecl
14450b57cec5SDimitry Andric ///   CapturedDecl
14460b57cec5SDimitry Andric class DeclContext {
14470b57cec5SDimitry Andric   /// For makeDeclVisibleInContextImpl
14480b57cec5SDimitry Andric   friend class ASTDeclReader;
144906c3fb27SDimitry Andric   /// For checking the new bits in the Serialization part.
145006c3fb27SDimitry Andric   friend class ASTDeclWriter;
14510b57cec5SDimitry Andric   /// For reconcileExternalVisibleStorage, CreateStoredDeclsMap,
14520b57cec5SDimitry Andric   /// hasNeedToReconcileExternalVisibleStorage
14530b57cec5SDimitry Andric   friend class ExternalASTSource;
14540b57cec5SDimitry Andric   /// For CreateStoredDeclsMap
14550b57cec5SDimitry Andric   friend class DependentDiagnostic;
14560b57cec5SDimitry Andric   /// For hasNeedToReconcileExternalVisibleStorage,
14570b57cec5SDimitry Andric   /// hasLazyLocalLexicalLookups, hasLazyExternalLexicalLookups
14580b57cec5SDimitry Andric   friend class ASTWriter;
14590b57cec5SDimitry Andric 
14600b57cec5SDimitry Andric   // We use uint64_t in the bit-fields below since some bit-fields
14610b57cec5SDimitry Andric   // cross the unsigned boundary and this breaks the packing.
14620b57cec5SDimitry Andric 
14630b57cec5SDimitry Andric   /// Stores the bits used by DeclContext.
14640b57cec5SDimitry Andric   /// If modified NumDeclContextBit, the ctor of DeclContext and the accessor
14650b57cec5SDimitry Andric   /// methods in DeclContext should be updated appropriately.
14660b57cec5SDimitry Andric   class DeclContextBitfields {
14670b57cec5SDimitry Andric     friend class DeclContext;
14680b57cec5SDimitry Andric     /// DeclKind - This indicates which class this is.
14695f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(Decl::Kind)
14700b57cec5SDimitry Andric     uint64_t DeclKind : 7;
14710b57cec5SDimitry Andric 
14720b57cec5SDimitry Andric     /// Whether this declaration context also has some external
14730b57cec5SDimitry Andric     /// storage that contains additional declarations that are lexically
14740b57cec5SDimitry Andric     /// part of this context.
14755f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
14760b57cec5SDimitry Andric     mutable uint64_t ExternalLexicalStorage : 1;
14770b57cec5SDimitry Andric 
14780b57cec5SDimitry Andric     /// Whether this declaration context also has some external
14790b57cec5SDimitry Andric     /// storage that contains additional declarations that are visible
14800b57cec5SDimitry Andric     /// in this context.
14815f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
14820b57cec5SDimitry Andric     mutable uint64_t ExternalVisibleStorage : 1;
14830b57cec5SDimitry Andric 
14840b57cec5SDimitry Andric     /// Whether this declaration context has had externally visible
14850b57cec5SDimitry Andric     /// storage added since the last lookup. In this case, \c LookupPtr's
14860b57cec5SDimitry Andric     /// invariant may not hold and needs to be fixed before we perform
14870b57cec5SDimitry Andric     /// another lookup.
14885f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
14890b57cec5SDimitry Andric     mutable uint64_t NeedToReconcileExternalVisibleStorage : 1;
14900b57cec5SDimitry Andric 
14910b57cec5SDimitry Andric     /// If \c true, this context may have local lexical declarations
14920b57cec5SDimitry Andric     /// that are missing from the lookup table.
14935f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
14940b57cec5SDimitry Andric     mutable uint64_t HasLazyLocalLexicalLookups : 1;
14950b57cec5SDimitry Andric 
14960b57cec5SDimitry Andric     /// If \c true, the external source may have lexical declarations
14970b57cec5SDimitry Andric     /// that are missing from the lookup table.
14985f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
14990b57cec5SDimitry Andric     mutable uint64_t HasLazyExternalLexicalLookups : 1;
15000b57cec5SDimitry Andric 
15010b57cec5SDimitry Andric     /// If \c true, lookups should only return identifier from
15020b57cec5SDimitry Andric     /// DeclContext scope (for example TranslationUnit). Used in
15030b57cec5SDimitry Andric     /// LookupQualifiedName()
15045f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
15050b57cec5SDimitry Andric     mutable uint64_t UseQualifiedLookup : 1;
15060b57cec5SDimitry Andric   };
15070b57cec5SDimitry Andric 
15080b57cec5SDimitry Andric   /// Number of bits in DeclContextBitfields.
15090b57cec5SDimitry Andric   enum { NumDeclContextBits = 13 };
15100b57cec5SDimitry Andric 
15110b57cec5SDimitry Andric   /// Stores the bits used by TagDecl.
15120b57cec5SDimitry Andric   /// If modified NumTagDeclBits and the accessor
15130b57cec5SDimitry Andric   /// methods in TagDecl should be updated appropriately.
15140b57cec5SDimitry Andric   class TagDeclBitfields {
15150b57cec5SDimitry Andric     friend class TagDecl;
15160b57cec5SDimitry Andric     /// For the bits in DeclContextBitfields
15175f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(DeclContextBitfields)
15180b57cec5SDimitry Andric     uint64_t : NumDeclContextBits;
15190b57cec5SDimitry Andric 
15200b57cec5SDimitry Andric     /// The TagKind enum.
15215f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(TagTypeKind)
15220b57cec5SDimitry Andric     uint64_t TagDeclKind : 3;
15230b57cec5SDimitry Andric 
15240b57cec5SDimitry Andric     /// True if this is a definition ("struct foo {};"), false if it is a
15250b57cec5SDimitry Andric     /// declaration ("struct foo;").  It is not considered a definition
15260b57cec5SDimitry Andric     /// until the definition has been fully processed.
15275f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
15280b57cec5SDimitry Andric     uint64_t IsCompleteDefinition : 1;
15290b57cec5SDimitry Andric 
15300b57cec5SDimitry Andric     /// True if this is currently being defined.
15315f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
15320b57cec5SDimitry Andric     uint64_t IsBeingDefined : 1;
15330b57cec5SDimitry Andric 
15340b57cec5SDimitry Andric     /// True if this tag declaration is "embedded" (i.e., defined or declared
15350b57cec5SDimitry Andric     /// for the very first time) in the syntax of a declarator.
15365f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
15370b57cec5SDimitry Andric     uint64_t IsEmbeddedInDeclarator : 1;
15380b57cec5SDimitry Andric 
15390b57cec5SDimitry Andric     /// True if this tag is free standing, e.g. "struct foo;".
15405f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
15410b57cec5SDimitry Andric     uint64_t IsFreeStanding : 1;
15420b57cec5SDimitry Andric 
15430b57cec5SDimitry Andric     /// Indicates whether it is possible for declarations of this kind
15440b57cec5SDimitry Andric     /// to have an out-of-date definition.
15450b57cec5SDimitry Andric     ///
15460b57cec5SDimitry Andric     /// This option is only enabled when modules are enabled.
15475f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
15480b57cec5SDimitry Andric     uint64_t MayHaveOutOfDateDef : 1;
15490b57cec5SDimitry Andric 
15500b57cec5SDimitry Andric     /// Has the full definition of this type been required by a use somewhere in
15510b57cec5SDimitry Andric     /// the TU.
15525f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
15530b57cec5SDimitry Andric     uint64_t IsCompleteDefinitionRequired : 1;
155481ad6265SDimitry Andric 
155581ad6265SDimitry Andric     /// Whether this tag is a definition which was demoted due to
155681ad6265SDimitry Andric     /// a module merge.
15575f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
155881ad6265SDimitry Andric     uint64_t IsThisDeclarationADemotedDefinition : 1;
15590b57cec5SDimitry Andric   };
15600b57cec5SDimitry Andric 
15615f757f3fSDimitry Andric   /// Number of inherited and non-inherited bits in TagDeclBitfields.
15625f757f3fSDimitry Andric   enum { NumTagDeclBits = NumDeclContextBits + 10 };
15630b57cec5SDimitry Andric 
15640b57cec5SDimitry Andric   /// Stores the bits used by EnumDecl.
15650b57cec5SDimitry Andric   /// If modified NumEnumDeclBit and the accessor
15660b57cec5SDimitry Andric   /// methods in EnumDecl should be updated appropriately.
15670b57cec5SDimitry Andric   class EnumDeclBitfields {
15680b57cec5SDimitry Andric     friend class EnumDecl;
15690b57cec5SDimitry Andric     /// For the bits in TagDeclBitfields.
15705f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(TagDeclBitfields)
15710b57cec5SDimitry Andric     uint64_t : NumTagDeclBits;
15720b57cec5SDimitry Andric 
15730b57cec5SDimitry Andric     /// Width in bits required to store all the non-negative
15740b57cec5SDimitry Andric     /// enumerators of this enum.
15750b57cec5SDimitry Andric     uint64_t NumPositiveBits : 8;
15760b57cec5SDimitry Andric 
15770b57cec5SDimitry Andric     /// Width in bits required to store all the negative
15780b57cec5SDimitry Andric     /// enumerators of this enum.
15790b57cec5SDimitry Andric     uint64_t NumNegativeBits : 8;
15800b57cec5SDimitry Andric 
15810b57cec5SDimitry Andric     /// True if this tag declaration is a scoped enumeration. Only
15820b57cec5SDimitry Andric     /// possible in C++11 mode.
15835f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
15840b57cec5SDimitry Andric     uint64_t IsScoped : 1;
15850b57cec5SDimitry Andric 
15860b57cec5SDimitry Andric     /// If this tag declaration is a scoped enum,
15870b57cec5SDimitry Andric     /// then this is true if the scoped enum was declared using the class
15880b57cec5SDimitry Andric     /// tag, false if it was declared with the struct tag. No meaning is
15890b57cec5SDimitry Andric     /// associated if this tag declaration is not a scoped enum.
15905f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
15910b57cec5SDimitry Andric     uint64_t IsScopedUsingClassTag : 1;
15920b57cec5SDimitry Andric 
15930b57cec5SDimitry Andric     /// True if this is an enumeration with fixed underlying type. Only
15940b57cec5SDimitry Andric     /// possible in C++11, Microsoft extensions, or Objective C mode.
15955f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
15960b57cec5SDimitry Andric     uint64_t IsFixed : 1;
15970b57cec5SDimitry Andric 
15980b57cec5SDimitry Andric     /// True if a valid hash is stored in ODRHash.
15995f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16000b57cec5SDimitry Andric     uint64_t HasODRHash : 1;
16010b57cec5SDimitry Andric   };
16020b57cec5SDimitry Andric 
16035f757f3fSDimitry Andric   /// Number of inherited and non-inherited bits in EnumDeclBitfields.
16045f757f3fSDimitry Andric   enum { NumEnumDeclBits = NumTagDeclBits + 20 };
16050b57cec5SDimitry Andric 
16060b57cec5SDimitry Andric   /// Stores the bits used by RecordDecl.
16070b57cec5SDimitry Andric   /// If modified NumRecordDeclBits and the accessor
16080b57cec5SDimitry Andric   /// methods in RecordDecl should be updated appropriately.
16090b57cec5SDimitry Andric   class RecordDeclBitfields {
16100b57cec5SDimitry Andric     friend class RecordDecl;
16110b57cec5SDimitry Andric     /// For the bits in TagDeclBitfields.
16125f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(TagDeclBitfields)
16130b57cec5SDimitry Andric     uint64_t : NumTagDeclBits;
16140b57cec5SDimitry Andric 
16150b57cec5SDimitry Andric     /// This is true if this struct ends with a flexible
16160b57cec5SDimitry Andric     /// array member (e.g. int X[]) or if this union contains a struct that does.
16170b57cec5SDimitry Andric     /// If so, this cannot be contained in arrays or other structs as a member.
16185f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16190b57cec5SDimitry Andric     uint64_t HasFlexibleArrayMember : 1;
16200b57cec5SDimitry Andric 
16210b57cec5SDimitry Andric     /// Whether this is the type of an anonymous struct or union.
16225f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16230b57cec5SDimitry Andric     uint64_t AnonymousStructOrUnion : 1;
16240b57cec5SDimitry Andric 
16250b57cec5SDimitry Andric     /// This is true if this struct has at least one member
16260b57cec5SDimitry Andric     /// containing an Objective-C object pointer type.
16275f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16280b57cec5SDimitry Andric     uint64_t HasObjectMember : 1;
16290b57cec5SDimitry Andric 
16300b57cec5SDimitry Andric     /// This is true if struct has at least one member of
16310b57cec5SDimitry Andric     /// 'volatile' type.
16325f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16330b57cec5SDimitry Andric     uint64_t HasVolatileMember : 1;
16340b57cec5SDimitry Andric 
16350b57cec5SDimitry Andric     /// Whether the field declarations of this record have been loaded
16360b57cec5SDimitry Andric     /// from external storage. To avoid unnecessary deserialization of
16370b57cec5SDimitry Andric     /// methods/nested types we allow deserialization of just the fields
16380b57cec5SDimitry Andric     /// when needed.
16395f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16400b57cec5SDimitry Andric     mutable uint64_t LoadedFieldsFromExternalStorage : 1;
16410b57cec5SDimitry Andric 
16420b57cec5SDimitry Andric     /// Basic properties of non-trivial C structs.
16435f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16440b57cec5SDimitry Andric     uint64_t NonTrivialToPrimitiveDefaultInitialize : 1;
16455f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16460b57cec5SDimitry Andric     uint64_t NonTrivialToPrimitiveCopy : 1;
16475f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16480b57cec5SDimitry Andric     uint64_t NonTrivialToPrimitiveDestroy : 1;
16490b57cec5SDimitry Andric 
16500b57cec5SDimitry Andric     /// The following bits indicate whether this is or contains a C union that
16510b57cec5SDimitry Andric     /// is non-trivial to default-initialize, destruct, or copy. These bits
16520b57cec5SDimitry Andric     /// imply the associated basic non-triviality predicates declared above.
16535f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16540b57cec5SDimitry Andric     uint64_t HasNonTrivialToPrimitiveDefaultInitializeCUnion : 1;
16555f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16560b57cec5SDimitry Andric     uint64_t HasNonTrivialToPrimitiveDestructCUnion : 1;
16575f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16580b57cec5SDimitry Andric     uint64_t HasNonTrivialToPrimitiveCopyCUnion : 1;
16590b57cec5SDimitry Andric 
16600b57cec5SDimitry Andric     /// Indicates whether this struct is destroyed in the callee.
16615f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
16620b57cec5SDimitry Andric     uint64_t ParamDestroyedInCallee : 1;
16630b57cec5SDimitry Andric 
16640b57cec5SDimitry Andric     /// Represents the way this type is passed to a function.
16655f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(RecordArgPassingKind)
16660b57cec5SDimitry Andric     uint64_t ArgPassingRestrictions : 2;
166781ad6265SDimitry Andric 
166881ad6265SDimitry Andric     /// Indicates whether this struct has had its field layout randomized.
16695f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
167081ad6265SDimitry Andric     uint64_t IsRandomized : 1;
1671bdd1243dSDimitry Andric 
1672bdd1243dSDimitry Andric     /// True if a valid hash is stored in ODRHash. This should shave off some
1673bdd1243dSDimitry Andric     /// extra storage and prevent CXXRecordDecl to store unused bits.
1674bdd1243dSDimitry Andric     uint64_t ODRHash : 26;
16750b57cec5SDimitry Andric   };
16760b57cec5SDimitry Andric 
16775f757f3fSDimitry Andric   /// Number of inherited and non-inherited bits in RecordDeclBitfields.
16785f757f3fSDimitry Andric   enum { NumRecordDeclBits = NumTagDeclBits + 41 };
16790b57cec5SDimitry Andric 
16800b57cec5SDimitry Andric   /// Stores the bits used by OMPDeclareReductionDecl.
16810b57cec5SDimitry Andric   /// If modified NumOMPDeclareReductionDeclBits and the accessor
16820b57cec5SDimitry Andric   /// methods in OMPDeclareReductionDecl should be updated appropriately.
16830b57cec5SDimitry Andric   class OMPDeclareReductionDeclBitfields {
16840b57cec5SDimitry Andric     friend class OMPDeclareReductionDecl;
16850b57cec5SDimitry Andric     /// For the bits in DeclContextBitfields
16865f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(DeclContextBitfields)
16870b57cec5SDimitry Andric     uint64_t : NumDeclContextBits;
16880b57cec5SDimitry Andric 
16890b57cec5SDimitry Andric     /// Kind of initializer,
169006c3fb27SDimitry Andric     /// function call or omp_priv<init_expr> initialization.
16915f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(OMPDeclareReductionInitKind)
16920b57cec5SDimitry Andric     uint64_t InitializerKind : 2;
16930b57cec5SDimitry Andric   };
16940b57cec5SDimitry Andric 
16955f757f3fSDimitry Andric   /// Number of inherited and non-inherited bits in
16965f757f3fSDimitry Andric   /// OMPDeclareReductionDeclBitfields.
16975f757f3fSDimitry Andric   enum { NumOMPDeclareReductionDeclBits = NumDeclContextBits + 2 };
16980b57cec5SDimitry Andric 
16990b57cec5SDimitry Andric   /// Stores the bits used by FunctionDecl.
17000b57cec5SDimitry Andric   /// If modified NumFunctionDeclBits and the accessor
17010b57cec5SDimitry Andric   /// methods in FunctionDecl and CXXDeductionGuideDecl
170206c3fb27SDimitry Andric   /// (for DeductionCandidateKind) should be updated appropriately.
17030b57cec5SDimitry Andric   class FunctionDeclBitfields {
17040b57cec5SDimitry Andric     friend class FunctionDecl;
170506c3fb27SDimitry Andric     /// For DeductionCandidateKind
17060b57cec5SDimitry Andric     friend class CXXDeductionGuideDecl;
17070b57cec5SDimitry Andric     /// For the bits in DeclContextBitfields.
17085f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(DeclContextBitfields)
17090b57cec5SDimitry Andric     uint64_t : NumDeclContextBits;
17100b57cec5SDimitry Andric 
17115f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(StorageClass)
17120b57cec5SDimitry Andric     uint64_t SClass : 3;
17135f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17140b57cec5SDimitry Andric     uint64_t IsInline : 1;
17155f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17160b57cec5SDimitry Andric     uint64_t IsInlineSpecified : 1;
17170b57cec5SDimitry Andric 
17185f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17190b57cec5SDimitry Andric     uint64_t IsVirtualAsWritten : 1;
17205f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17217a6dacacSDimitry Andric     uint64_t IsPureVirtual : 1;
17225f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17230b57cec5SDimitry Andric     uint64_t HasInheritedPrototype : 1;
17245f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17250b57cec5SDimitry Andric     uint64_t HasWrittenPrototype : 1;
17265f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17270b57cec5SDimitry Andric     uint64_t IsDeleted : 1;
17280b57cec5SDimitry Andric     /// Used by CXXMethodDecl
17295f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17300b57cec5SDimitry Andric     uint64_t IsTrivial : 1;
17310b57cec5SDimitry Andric 
17320b57cec5SDimitry Andric     /// This flag indicates whether this function is trivial for the purpose of
17330b57cec5SDimitry Andric     /// calls. This is meaningful only when this function is a copy/move
17340b57cec5SDimitry Andric     /// constructor or a destructor.
17355f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17360b57cec5SDimitry Andric     uint64_t IsTrivialForCall : 1;
17370b57cec5SDimitry Andric 
17385f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17390b57cec5SDimitry Andric     uint64_t IsDefaulted : 1;
17405f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17410b57cec5SDimitry Andric     uint64_t IsExplicitlyDefaulted : 1;
17425f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
1743480093f4SDimitry Andric     uint64_t HasDefaultedFunctionInfo : 1;
174481ad6265SDimitry Andric 
174581ad6265SDimitry Andric     /// For member functions of complete types, whether this is an ineligible
174681ad6265SDimitry Andric     /// special member function or an unselected destructor. See
174781ad6265SDimitry Andric     /// [class.mem.special].
17485f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
174981ad6265SDimitry Andric     uint64_t IsIneligibleOrNotSelected : 1;
175081ad6265SDimitry Andric 
17515f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17520b57cec5SDimitry Andric     uint64_t HasImplicitReturnZero : 1;
17535f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17540b57cec5SDimitry Andric     uint64_t IsLateTemplateParsed : 1;
17550b57cec5SDimitry Andric 
17560b57cec5SDimitry Andric     /// Kind of contexpr specifier as defined by ConstexprSpecKind.
17575f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(ConstexprSpecKind)
17580b57cec5SDimitry Andric     uint64_t ConstexprKind : 2;
17595f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
176006c3fb27SDimitry Andric     uint64_t BodyContainsImmediateEscalatingExpression : 1;
176106c3fb27SDimitry Andric 
17625f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17630b57cec5SDimitry Andric     uint64_t InstantiationIsPending : 1;
17640b57cec5SDimitry Andric 
17650b57cec5SDimitry Andric     /// Indicates if the function uses __try.
17665f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17670b57cec5SDimitry Andric     uint64_t UsesSEHTry : 1;
17680b57cec5SDimitry Andric 
17690b57cec5SDimitry Andric     /// Indicates if the function was a definition
17700b57cec5SDimitry Andric     /// but its body was skipped.
17715f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17720b57cec5SDimitry Andric     uint64_t HasSkippedBody : 1;
17730b57cec5SDimitry Andric 
17740b57cec5SDimitry Andric     /// Indicates if the function declaration will
17750b57cec5SDimitry Andric     /// have a body, once we're done parsing it.
17765f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17770b57cec5SDimitry Andric     uint64_t WillHaveBody : 1;
17780b57cec5SDimitry Andric 
17790b57cec5SDimitry Andric     /// Indicates that this function is a multiversioned
17800b57cec5SDimitry Andric     /// function using attribute 'target'.
17815f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17820b57cec5SDimitry Andric     uint64_t IsMultiVersion : 1;
17830b57cec5SDimitry Andric 
178406c3fb27SDimitry Andric     /// Only used by CXXDeductionGuideDecl. Indicates the kind
178506c3fb27SDimitry Andric     /// of the Deduction Guide that is implicitly generated
178606c3fb27SDimitry Andric     /// (used during overload resolution).
17875f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(DeductionCandidate)
178806c3fb27SDimitry Andric     uint64_t DeductionCandidateKind : 2;
17890b57cec5SDimitry Andric 
17900b57cec5SDimitry Andric     /// Store the ODRHash after first calculation.
17915f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
17920b57cec5SDimitry Andric     uint64_t HasODRHash : 1;
1793480093f4SDimitry Andric 
1794480093f4SDimitry Andric     /// Indicates if the function uses Floating Point Constrained Intrinsics
17955f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
1796480093f4SDimitry Andric     uint64_t UsesFPIntrin : 1;
1797bdd1243dSDimitry Andric 
1798bdd1243dSDimitry Andric     // Indicates this function is a constrained friend, where the constraint
1799bdd1243dSDimitry Andric     // refers to an enclosing template for hte purposes of [temp.friend]p9.
18005f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
1801bdd1243dSDimitry Andric     uint64_t FriendConstraintRefersToEnclosingTemplate : 1;
18020b57cec5SDimitry Andric   };
18030b57cec5SDimitry Andric 
18045f757f3fSDimitry Andric   /// Number of inherited and non-inherited bits in FunctionDeclBitfields.
18055f757f3fSDimitry Andric   enum { NumFunctionDeclBits = NumDeclContextBits + 31 };
18060b57cec5SDimitry Andric 
18070b57cec5SDimitry Andric   /// Stores the bits used by CXXConstructorDecl. If modified
18080b57cec5SDimitry Andric   /// NumCXXConstructorDeclBits and the accessor
18090b57cec5SDimitry Andric   /// methods in CXXConstructorDecl should be updated appropriately.
18100b57cec5SDimitry Andric   class CXXConstructorDeclBitfields {
18110b57cec5SDimitry Andric     friend class CXXConstructorDecl;
18120b57cec5SDimitry Andric     /// For the bits in FunctionDeclBitfields.
18135f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(FunctionDeclBitfields)
18140b57cec5SDimitry Andric     uint64_t : NumFunctionDeclBits;
18150b57cec5SDimitry Andric 
18168a4dda33SDimitry Andric     /// 20 bits to fit in the remaining available space.
18170b57cec5SDimitry Andric     /// Note that this makes CXXConstructorDeclBitfields take
18180b57cec5SDimitry Andric     /// exactly 64 bits and thus the width of NumCtorInitializers
18190b57cec5SDimitry Andric     /// will need to be shrunk if some bit is added to NumDeclContextBitfields,
18200b57cec5SDimitry Andric     /// NumFunctionDeclBitfields or CXXConstructorDeclBitfields.
18218a4dda33SDimitry Andric     uint64_t NumCtorInitializers : 17;
18225f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
18230b57cec5SDimitry Andric     uint64_t IsInheritingConstructor : 1;
18240b57cec5SDimitry Andric 
18250b57cec5SDimitry Andric     /// Whether this constructor has a trail-allocated explicit specifier.
18265f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
18270b57cec5SDimitry Andric     uint64_t HasTrailingExplicitSpecifier : 1;
18280b57cec5SDimitry Andric     /// If this constructor does't have a trail-allocated explicit specifier.
18290b57cec5SDimitry Andric     /// Whether this constructor is explicit specified.
18305f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
18310b57cec5SDimitry Andric     uint64_t IsSimpleExplicit : 1;
18320b57cec5SDimitry Andric   };
18330b57cec5SDimitry Andric 
18345f757f3fSDimitry Andric   /// Number of inherited and non-inherited bits in CXXConstructorDeclBitfields.
18355f757f3fSDimitry Andric   enum { NumCXXConstructorDeclBits = NumFunctionDeclBits + 20 };
18360b57cec5SDimitry Andric 
18370b57cec5SDimitry Andric   /// Stores the bits used by ObjCMethodDecl.
18380b57cec5SDimitry Andric   /// If modified NumObjCMethodDeclBits and the accessor
18390b57cec5SDimitry Andric   /// methods in ObjCMethodDecl should be updated appropriately.
18400b57cec5SDimitry Andric   class ObjCMethodDeclBitfields {
18410b57cec5SDimitry Andric     friend class ObjCMethodDecl;
18420b57cec5SDimitry Andric 
18430b57cec5SDimitry Andric     /// For the bits in DeclContextBitfields.
18445f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(DeclContextBitfields)
18450b57cec5SDimitry Andric     uint64_t : NumDeclContextBits;
18460b57cec5SDimitry Andric 
18470b57cec5SDimitry Andric     /// The conventional meaning of this method; an ObjCMethodFamily.
18480b57cec5SDimitry Andric     /// This is not serialized; instead, it is computed on demand and
18490b57cec5SDimitry Andric     /// cached.
18505f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(ObjCMethodFamily)
18510b57cec5SDimitry Andric     mutable uint64_t Family : ObjCMethodFamilyBitWidth;
18520b57cec5SDimitry Andric 
18530b57cec5SDimitry Andric     /// instance (true) or class (false) method.
18545f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
18550b57cec5SDimitry Andric     uint64_t IsInstance : 1;
18565f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
18570b57cec5SDimitry Andric     uint64_t IsVariadic : 1;
18580b57cec5SDimitry Andric 
18590b57cec5SDimitry Andric     /// True if this method is the getter or setter for an explicit property.
18605f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
18610b57cec5SDimitry Andric     uint64_t IsPropertyAccessor : 1;
18620b57cec5SDimitry Andric 
1863480093f4SDimitry Andric     /// True if this method is a synthesized property accessor stub.
18645f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
1865480093f4SDimitry Andric     uint64_t IsSynthesizedAccessorStub : 1;
1866480093f4SDimitry Andric 
18670b57cec5SDimitry Andric     /// Method has a definition.
18685f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
18690b57cec5SDimitry Andric     uint64_t IsDefined : 1;
18700b57cec5SDimitry Andric 
18710b57cec5SDimitry Andric     /// Method redeclaration in the same interface.
18725f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
18730b57cec5SDimitry Andric     uint64_t IsRedeclaration : 1;
18740b57cec5SDimitry Andric 
18750b57cec5SDimitry Andric     /// Is redeclared in the same interface.
18765f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
18770b57cec5SDimitry Andric     mutable uint64_t HasRedeclaration : 1;
18780b57cec5SDimitry Andric 
18790b57cec5SDimitry Andric     /// \@required/\@optional
18805f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(ObjCImplementationControl)
18810b57cec5SDimitry Andric     uint64_t DeclImplementation : 2;
18820b57cec5SDimitry Andric 
18830b57cec5SDimitry Andric     /// in, inout, etc.
18845f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(Decl::ObjCDeclQualifier)
18850b57cec5SDimitry Andric     uint64_t objcDeclQualifier : 7;
18860b57cec5SDimitry Andric 
18870b57cec5SDimitry Andric     /// Indicates whether this method has a related result type.
18885f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
18890b57cec5SDimitry Andric     uint64_t RelatedResultType : 1;
18900b57cec5SDimitry Andric 
18910b57cec5SDimitry Andric     /// Whether the locations of the selector identifiers are in a
18920b57cec5SDimitry Andric     /// "standard" position, a enum SelectorLocationsKind.
18935f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(SelectorLocationsKind)
18940b57cec5SDimitry Andric     uint64_t SelLocsKind : 2;
18950b57cec5SDimitry Andric 
18960b57cec5SDimitry Andric     /// Whether this method overrides any other in the class hierarchy.
18970b57cec5SDimitry Andric     ///
18980b57cec5SDimitry Andric     /// A method is said to override any method in the class's
18990b57cec5SDimitry Andric     /// base classes, its protocols, or its categories' protocols, that has
19000b57cec5SDimitry Andric     /// the same selector and is of the same kind (class or instance).
19010b57cec5SDimitry Andric     /// A method in an implementation is not considered as overriding the same
19020b57cec5SDimitry Andric     /// method in the interface or its categories.
19035f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
19040b57cec5SDimitry Andric     uint64_t IsOverriding : 1;
19050b57cec5SDimitry Andric 
19060b57cec5SDimitry Andric     /// Indicates if the method was a definition but its body was skipped.
19075f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
19080b57cec5SDimitry Andric     uint64_t HasSkippedBody : 1;
19090b57cec5SDimitry Andric   };
19100b57cec5SDimitry Andric 
19115f757f3fSDimitry Andric   /// Number of inherited and non-inherited bits in ObjCMethodDeclBitfields.
19125f757f3fSDimitry Andric   enum { NumObjCMethodDeclBits = NumDeclContextBits + 24 };
19130b57cec5SDimitry Andric 
19140b57cec5SDimitry Andric   /// Stores the bits used by ObjCContainerDecl.
19150b57cec5SDimitry Andric   /// If modified NumObjCContainerDeclBits and the accessor
19160b57cec5SDimitry Andric   /// methods in ObjCContainerDecl should be updated appropriately.
19170b57cec5SDimitry Andric   class ObjCContainerDeclBitfields {
19180b57cec5SDimitry Andric     friend class ObjCContainerDecl;
19190b57cec5SDimitry Andric     /// For the bits in DeclContextBitfields
19205f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(DeclContextBitfields)
19210b57cec5SDimitry Andric     uint32_t : NumDeclContextBits;
19220b57cec5SDimitry Andric 
19230b57cec5SDimitry Andric     // Not a bitfield but this saves space.
19240b57cec5SDimitry Andric     // Note that ObjCContainerDeclBitfields is full.
19250b57cec5SDimitry Andric     SourceLocation AtStart;
19260b57cec5SDimitry Andric   };
19270b57cec5SDimitry Andric 
19285f757f3fSDimitry Andric   /// Number of inherited and non-inherited bits in ObjCContainerDeclBitfields.
19290b57cec5SDimitry Andric   /// Note that here we rely on the fact that SourceLocation is 32 bits
19300b57cec5SDimitry Andric   /// wide. We check this with the static_assert in the ctor of DeclContext.
19315f757f3fSDimitry Andric   enum { NumObjCContainerDeclBits = 64 };
19320b57cec5SDimitry Andric 
19330b57cec5SDimitry Andric   /// Stores the bits used by LinkageSpecDecl.
19340b57cec5SDimitry Andric   /// If modified NumLinkageSpecDeclBits and the accessor
19350b57cec5SDimitry Andric   /// methods in LinkageSpecDecl should be updated appropriately.
19360b57cec5SDimitry Andric   class LinkageSpecDeclBitfields {
19370b57cec5SDimitry Andric     friend class LinkageSpecDecl;
19380b57cec5SDimitry Andric     /// For the bits in DeclContextBitfields.
19395f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(DeclContextBitfields)
19400b57cec5SDimitry Andric     uint64_t : NumDeclContextBits;
19410b57cec5SDimitry Andric 
19425f757f3fSDimitry Andric     /// The language for this linkage specification.
19435f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(LinkageSpecLanguageIDs)
19440b57cec5SDimitry Andric     uint64_t Language : 3;
19450b57cec5SDimitry Andric 
19460b57cec5SDimitry Andric     /// True if this linkage spec has braces.
19470b57cec5SDimitry Andric     /// This is needed so that hasBraces() returns the correct result while the
19480b57cec5SDimitry Andric     /// linkage spec body is being parsed.  Once RBraceLoc has been set this is
19490b57cec5SDimitry Andric     /// not used, so it doesn't need to be serialized.
19505f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
19510b57cec5SDimitry Andric     uint64_t HasBraces : 1;
19520b57cec5SDimitry Andric   };
19530b57cec5SDimitry Andric 
19545f757f3fSDimitry Andric   /// Number of inherited and non-inherited bits in LinkageSpecDeclBitfields.
19555f757f3fSDimitry Andric   enum { NumLinkageSpecDeclBits = NumDeclContextBits + 4 };
19560b57cec5SDimitry Andric 
19570b57cec5SDimitry Andric   /// Stores the bits used by BlockDecl.
19580b57cec5SDimitry Andric   /// If modified NumBlockDeclBits and the accessor
19590b57cec5SDimitry Andric   /// methods in BlockDecl should be updated appropriately.
19600b57cec5SDimitry Andric   class BlockDeclBitfields {
19610b57cec5SDimitry Andric     friend class BlockDecl;
19620b57cec5SDimitry Andric     /// For the bits in DeclContextBitfields.
19635f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(DeclContextBitfields)
19640b57cec5SDimitry Andric     uint64_t : NumDeclContextBits;
19650b57cec5SDimitry Andric 
19665f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
19670b57cec5SDimitry Andric     uint64_t IsVariadic : 1;
19685f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
19690b57cec5SDimitry Andric     uint64_t CapturesCXXThis : 1;
19705f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
19710b57cec5SDimitry Andric     uint64_t BlockMissingReturnType : 1;
19725f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
19730b57cec5SDimitry Andric     uint64_t IsConversionFromLambda : 1;
19740b57cec5SDimitry Andric 
19750b57cec5SDimitry Andric     /// A bit that indicates this block is passed directly to a function as a
19760b57cec5SDimitry Andric     /// non-escaping parameter.
19775f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
19780b57cec5SDimitry Andric     uint64_t DoesNotEscape : 1;
19790b57cec5SDimitry Andric 
19800b57cec5SDimitry Andric     /// A bit that indicates whether it's possible to avoid coying this block to
19810b57cec5SDimitry Andric     /// the heap when it initializes or is assigned to a local variable with
19820b57cec5SDimitry Andric     /// automatic storage.
19835f757f3fSDimitry Andric     LLVM_PREFERRED_TYPE(bool)
19840b57cec5SDimitry Andric     uint64_t CanAvoidCopyToHeap : 1;
19850b57cec5SDimitry Andric   };
19860b57cec5SDimitry Andric 
19875f757f3fSDimitry Andric   /// Number of inherited and non-inherited bits in BlockDeclBitfields.
19885f757f3fSDimitry Andric   enum { NumBlockDeclBits = NumDeclContextBits + 5 };
19890b57cec5SDimitry Andric 
19900b57cec5SDimitry Andric   /// Pointer to the data structure used to lookup declarations
19910b57cec5SDimitry Andric   /// within this context (or a DependentStoredDeclsMap if this is a
19920b57cec5SDimitry Andric   /// dependent context). We maintain the invariant that, if the map
19930b57cec5SDimitry Andric   /// contains an entry for a DeclarationName (and we haven't lazily
19940b57cec5SDimitry Andric   /// omitted anything), then it contains all relevant entries for that
19950b57cec5SDimitry Andric   /// name (modulo the hasExternalDecls() flag).
19960b57cec5SDimitry Andric   mutable StoredDeclsMap *LookupPtr = nullptr;
19970b57cec5SDimitry Andric 
19980b57cec5SDimitry Andric protected:
19990b57cec5SDimitry Andric   /// This anonymous union stores the bits belonging to DeclContext and classes
20000b57cec5SDimitry Andric   /// deriving from it. The goal is to use otherwise wasted
20010b57cec5SDimitry Andric   /// space in DeclContext to store data belonging to derived classes.
20020b57cec5SDimitry Andric   /// The space saved is especially significient when pointers are aligned
20030b57cec5SDimitry Andric   /// to 8 bytes. In this case due to alignment requirements we have a
20040b57cec5SDimitry Andric   /// little less than 8 bytes free in DeclContext which we can use.
20050b57cec5SDimitry Andric   /// We check that none of the classes in this union is larger than
20060b57cec5SDimitry Andric   /// 8 bytes with static_asserts in the ctor of DeclContext.
20070b57cec5SDimitry Andric   union {
20080b57cec5SDimitry Andric     DeclContextBitfields DeclContextBits;
20090b57cec5SDimitry Andric     TagDeclBitfields TagDeclBits;
20100b57cec5SDimitry Andric     EnumDeclBitfields EnumDeclBits;
20110b57cec5SDimitry Andric     RecordDeclBitfields RecordDeclBits;
20120b57cec5SDimitry Andric     OMPDeclareReductionDeclBitfields OMPDeclareReductionDeclBits;
20130b57cec5SDimitry Andric     FunctionDeclBitfields FunctionDeclBits;
20140b57cec5SDimitry Andric     CXXConstructorDeclBitfields CXXConstructorDeclBits;
20150b57cec5SDimitry Andric     ObjCMethodDeclBitfields ObjCMethodDeclBits;
20160b57cec5SDimitry Andric     ObjCContainerDeclBitfields ObjCContainerDeclBits;
20170b57cec5SDimitry Andric     LinkageSpecDeclBitfields LinkageSpecDeclBits;
20180b57cec5SDimitry Andric     BlockDeclBitfields BlockDeclBits;
20190b57cec5SDimitry Andric 
20200b57cec5SDimitry Andric     static_assert(sizeof(DeclContextBitfields) <= 8,
20210b57cec5SDimitry Andric                   "DeclContextBitfields is larger than 8 bytes!");
20220b57cec5SDimitry Andric     static_assert(sizeof(TagDeclBitfields) <= 8,
20230b57cec5SDimitry Andric                   "TagDeclBitfields is larger than 8 bytes!");
20240b57cec5SDimitry Andric     static_assert(sizeof(EnumDeclBitfields) <= 8,
20250b57cec5SDimitry Andric                   "EnumDeclBitfields is larger than 8 bytes!");
20260b57cec5SDimitry Andric     static_assert(sizeof(RecordDeclBitfields) <= 8,
20270b57cec5SDimitry Andric                   "RecordDeclBitfields is larger than 8 bytes!");
20280b57cec5SDimitry Andric     static_assert(sizeof(OMPDeclareReductionDeclBitfields) <= 8,
20290b57cec5SDimitry Andric                   "OMPDeclareReductionDeclBitfields is larger than 8 bytes!");
20300b57cec5SDimitry Andric     static_assert(sizeof(FunctionDeclBitfields) <= 8,
20310b57cec5SDimitry Andric                   "FunctionDeclBitfields is larger than 8 bytes!");
20320b57cec5SDimitry Andric     static_assert(sizeof(CXXConstructorDeclBitfields) <= 8,
20330b57cec5SDimitry Andric                   "CXXConstructorDeclBitfields is larger than 8 bytes!");
20340b57cec5SDimitry Andric     static_assert(sizeof(ObjCMethodDeclBitfields) <= 8,
20350b57cec5SDimitry Andric                   "ObjCMethodDeclBitfields is larger than 8 bytes!");
20360b57cec5SDimitry Andric     static_assert(sizeof(ObjCContainerDeclBitfields) <= 8,
20370b57cec5SDimitry Andric                   "ObjCContainerDeclBitfields is larger than 8 bytes!");
20380b57cec5SDimitry Andric     static_assert(sizeof(LinkageSpecDeclBitfields) <= 8,
20390b57cec5SDimitry Andric                   "LinkageSpecDeclBitfields is larger than 8 bytes!");
20400b57cec5SDimitry Andric     static_assert(sizeof(BlockDeclBitfields) <= 8,
20410b57cec5SDimitry Andric                   "BlockDeclBitfields is larger than 8 bytes!");
204228a41182SDimitry Andric   };
20430b57cec5SDimitry Andric 
20440b57cec5SDimitry Andric   /// FirstDecl - The first declaration stored within this declaration
20450b57cec5SDimitry Andric   /// context.
20460b57cec5SDimitry Andric   mutable Decl *FirstDecl = nullptr;
20470b57cec5SDimitry Andric 
20480b57cec5SDimitry Andric   /// LastDecl - The last declaration stored within this declaration
20490b57cec5SDimitry Andric   /// context. FIXME: We could probably cache this value somewhere
20500b57cec5SDimitry Andric   /// outside of the DeclContext, to reduce the size of DeclContext by
20510b57cec5SDimitry Andric   /// another pointer.
20520b57cec5SDimitry Andric   mutable Decl *LastDecl = nullptr;
20530b57cec5SDimitry Andric 
20540b57cec5SDimitry Andric   /// Build up a chain of declarations.
20550b57cec5SDimitry Andric   ///
20560b57cec5SDimitry Andric   /// \returns the first/last pair of declarations.
20570b57cec5SDimitry Andric   static std::pair<Decl *, Decl *>
20580b57cec5SDimitry Andric   BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded);
20590b57cec5SDimitry Andric 
20600b57cec5SDimitry Andric   DeclContext(Decl::Kind K);
20610b57cec5SDimitry Andric 
20620b57cec5SDimitry Andric public:
20630b57cec5SDimitry Andric   ~DeclContext();
20640b57cec5SDimitry Andric 
2065bdd1243dSDimitry Andric   // For use when debugging; hasValidDeclKind() will always return true for
2066bdd1243dSDimitry Andric   // a correctly constructed object within its lifetime.
2067bdd1243dSDimitry Andric   bool hasValidDeclKind() const;
2068bdd1243dSDimitry Andric 
20690b57cec5SDimitry Andric   Decl::Kind getDeclKind() const {
20700b57cec5SDimitry Andric     return static_cast<Decl::Kind>(DeclContextBits.DeclKind);
20710b57cec5SDimitry Andric   }
20720b57cec5SDimitry Andric 
20730b57cec5SDimitry Andric   const char *getDeclKindName() const;
20740b57cec5SDimitry Andric 
20750b57cec5SDimitry Andric   /// getParent - Returns the containing DeclContext.
20760b57cec5SDimitry Andric   DeclContext *getParent() {
20770b57cec5SDimitry Andric     return cast<Decl>(this)->getDeclContext();
20780b57cec5SDimitry Andric   }
20790b57cec5SDimitry Andric   const DeclContext *getParent() const {
20800b57cec5SDimitry Andric     return const_cast<DeclContext*>(this)->getParent();
20810b57cec5SDimitry Andric   }
20820b57cec5SDimitry Andric 
20830b57cec5SDimitry Andric   /// getLexicalParent - Returns the containing lexical DeclContext. May be
20840b57cec5SDimitry Andric   /// different from getParent, e.g.:
20850b57cec5SDimitry Andric   ///
20860b57cec5SDimitry Andric   ///   namespace A {
20870b57cec5SDimitry Andric   ///      struct S;
20880b57cec5SDimitry Andric   ///   }
20890b57cec5SDimitry Andric   ///   struct A::S {}; // getParent() == namespace 'A'
20900b57cec5SDimitry Andric   ///                   // getLexicalParent() == translation unit
20910b57cec5SDimitry Andric   ///
20920b57cec5SDimitry Andric   DeclContext *getLexicalParent() {
20930b57cec5SDimitry Andric     return cast<Decl>(this)->getLexicalDeclContext();
20940b57cec5SDimitry Andric   }
20950b57cec5SDimitry Andric   const DeclContext *getLexicalParent() const {
20960b57cec5SDimitry Andric     return const_cast<DeclContext*>(this)->getLexicalParent();
20970b57cec5SDimitry Andric   }
20980b57cec5SDimitry Andric 
20990b57cec5SDimitry Andric   DeclContext *getLookupParent();
21000b57cec5SDimitry Andric 
21010b57cec5SDimitry Andric   const DeclContext *getLookupParent() const {
21020b57cec5SDimitry Andric     return const_cast<DeclContext*>(this)->getLookupParent();
21030b57cec5SDimitry Andric   }
21040b57cec5SDimitry Andric 
21050b57cec5SDimitry Andric   ASTContext &getParentASTContext() const {
21060b57cec5SDimitry Andric     return cast<Decl>(this)->getASTContext();
21070b57cec5SDimitry Andric   }
21080b57cec5SDimitry Andric 
21090b57cec5SDimitry Andric   bool isClosure() const { return getDeclKind() == Decl::Block; }
21100b57cec5SDimitry Andric 
21110b57cec5SDimitry Andric   /// Return this DeclContext if it is a BlockDecl. Otherwise, return the
21120b57cec5SDimitry Andric   /// innermost enclosing BlockDecl or null if there are no enclosing blocks.
21130b57cec5SDimitry Andric   const BlockDecl *getInnermostBlockDecl() const;
21140b57cec5SDimitry Andric 
21150b57cec5SDimitry Andric   bool isObjCContainer() const {
21160b57cec5SDimitry Andric     switch (getDeclKind()) {
21170b57cec5SDimitry Andric     case Decl::ObjCCategory:
21180b57cec5SDimitry Andric     case Decl::ObjCCategoryImpl:
21190b57cec5SDimitry Andric     case Decl::ObjCImplementation:
21200b57cec5SDimitry Andric     case Decl::ObjCInterface:
21210b57cec5SDimitry Andric     case Decl::ObjCProtocol:
21220b57cec5SDimitry Andric       return true;
21230b57cec5SDimitry Andric     default:
21240b57cec5SDimitry Andric       return false;
21250b57cec5SDimitry Andric     }
21260b57cec5SDimitry Andric   }
21270b57cec5SDimitry Andric 
21280b57cec5SDimitry Andric   bool isFunctionOrMethod() const {
21290b57cec5SDimitry Andric     switch (getDeclKind()) {
21300b57cec5SDimitry Andric     case Decl::Block:
21310b57cec5SDimitry Andric     case Decl::Captured:
21320b57cec5SDimitry Andric     case Decl::ObjCMethod:
21330b57cec5SDimitry Andric       return true;
21340b57cec5SDimitry Andric     default:
21350b57cec5SDimitry Andric       return getDeclKind() >= Decl::firstFunction &&
21360b57cec5SDimitry Andric              getDeclKind() <= Decl::lastFunction;
21370b57cec5SDimitry Andric     }
21380b57cec5SDimitry Andric   }
21390b57cec5SDimitry Andric 
21400b57cec5SDimitry Andric   /// Test whether the context supports looking up names.
21410b57cec5SDimitry Andric   bool isLookupContext() const {
21420b57cec5SDimitry Andric     return !isFunctionOrMethod() && getDeclKind() != Decl::LinkageSpec &&
21430b57cec5SDimitry Andric            getDeclKind() != Decl::Export;
21440b57cec5SDimitry Andric   }
21450b57cec5SDimitry Andric 
21460b57cec5SDimitry Andric   bool isFileContext() const {
21470b57cec5SDimitry Andric     return getDeclKind() == Decl::TranslationUnit ||
21480b57cec5SDimitry Andric            getDeclKind() == Decl::Namespace;
21490b57cec5SDimitry Andric   }
21500b57cec5SDimitry Andric 
21510b57cec5SDimitry Andric   bool isTranslationUnit() const {
21520b57cec5SDimitry Andric     return getDeclKind() == Decl::TranslationUnit;
21530b57cec5SDimitry Andric   }
21540b57cec5SDimitry Andric 
21550b57cec5SDimitry Andric   bool isRecord() const {
21560b57cec5SDimitry Andric     return getDeclKind() >= Decl::firstRecord &&
21570b57cec5SDimitry Andric            getDeclKind() <= Decl::lastRecord;
21580b57cec5SDimitry Andric   }
21590b57cec5SDimitry Andric 
21600b57cec5SDimitry Andric   bool isNamespace() const { return getDeclKind() == Decl::Namespace; }
21610b57cec5SDimitry Andric 
21620b57cec5SDimitry Andric   bool isStdNamespace() const;
21630b57cec5SDimitry Andric 
21640b57cec5SDimitry Andric   bool isInlineNamespace() const;
21650b57cec5SDimitry Andric 
21660b57cec5SDimitry Andric   /// Determines whether this context is dependent on a
21670b57cec5SDimitry Andric   /// template parameter.
21680b57cec5SDimitry Andric   bool isDependentContext() const;
21690b57cec5SDimitry Andric 
21700b57cec5SDimitry Andric   /// isTransparentContext - Determines whether this context is a
21710b57cec5SDimitry Andric   /// "transparent" context, meaning that the members declared in this
21720b57cec5SDimitry Andric   /// context are semantically declared in the nearest enclosing
21730b57cec5SDimitry Andric   /// non-transparent (opaque) context but are lexically declared in
21740b57cec5SDimitry Andric   /// this context. For example, consider the enumerators of an
21750b57cec5SDimitry Andric   /// enumeration type:
21760b57cec5SDimitry Andric   /// @code
21770b57cec5SDimitry Andric   /// enum E {
21780b57cec5SDimitry Andric   ///   Val1
21790b57cec5SDimitry Andric   /// };
21800b57cec5SDimitry Andric   /// @endcode
21810b57cec5SDimitry Andric   /// Here, E is a transparent context, so its enumerator (Val1) will
21820b57cec5SDimitry Andric   /// appear (semantically) that it is in the same context of E.
21830b57cec5SDimitry Andric   /// Examples of transparent contexts include: enumerations (except for
2184bdd1243dSDimitry Andric   /// C++0x scoped enums), C++ linkage specifications and export declaration.
21850b57cec5SDimitry Andric   bool isTransparentContext() const;
21860b57cec5SDimitry Andric 
21870b57cec5SDimitry Andric   /// Determines whether this context or some of its ancestors is a
21880b57cec5SDimitry Andric   /// linkage specification context that specifies C linkage.
21890b57cec5SDimitry Andric   bool isExternCContext() const;
21900b57cec5SDimitry Andric 
21910b57cec5SDimitry Andric   /// Retrieve the nearest enclosing C linkage specification context.
21920b57cec5SDimitry Andric   const LinkageSpecDecl *getExternCContext() const;
21930b57cec5SDimitry Andric 
21940b57cec5SDimitry Andric   /// Determines whether this context or some of its ancestors is a
21950b57cec5SDimitry Andric   /// linkage specification context that specifies C++ linkage.
21960b57cec5SDimitry Andric   bool isExternCXXContext() const;
21970b57cec5SDimitry Andric 
21980b57cec5SDimitry Andric   /// Determine whether this declaration context is equivalent
21990b57cec5SDimitry Andric   /// to the declaration context DC.
22000b57cec5SDimitry Andric   bool Equals(const DeclContext *DC) const {
22010b57cec5SDimitry Andric     return DC && this->getPrimaryContext() == DC->getPrimaryContext();
22020b57cec5SDimitry Andric   }
22030b57cec5SDimitry Andric 
22040b57cec5SDimitry Andric   /// Determine whether this declaration context encloses the
22050b57cec5SDimitry Andric   /// declaration context DC.
22060b57cec5SDimitry Andric   bool Encloses(const DeclContext *DC) const;
22070b57cec5SDimitry Andric 
22080b57cec5SDimitry Andric   /// Find the nearest non-closure ancestor of this context,
22090b57cec5SDimitry Andric   /// i.e. the innermost semantic parent of this context which is not
22100b57cec5SDimitry Andric   /// a closure.  A context may be its own non-closure ancestor.
22110b57cec5SDimitry Andric   Decl *getNonClosureAncestor();
22120b57cec5SDimitry Andric   const Decl *getNonClosureAncestor() const {
22130b57cec5SDimitry Andric     return const_cast<DeclContext*>(this)->getNonClosureAncestor();
22140b57cec5SDimitry Andric   }
22150b57cec5SDimitry Andric 
2216349cc55cSDimitry Andric   // Retrieve the nearest context that is not a transparent context.
2217349cc55cSDimitry Andric   DeclContext *getNonTransparentContext();
2218349cc55cSDimitry Andric   const DeclContext *getNonTransparentContext() const {
2219349cc55cSDimitry Andric     return const_cast<DeclContext *>(this)->getNonTransparentContext();
2220349cc55cSDimitry Andric   }
2221349cc55cSDimitry Andric 
22220b57cec5SDimitry Andric   /// getPrimaryContext - There may be many different
22230b57cec5SDimitry Andric   /// declarations of the same entity (including forward declarations
22240b57cec5SDimitry Andric   /// of classes, multiple definitions of namespaces, etc.), each with
22250b57cec5SDimitry Andric   /// a different set of declarations. This routine returns the
22260b57cec5SDimitry Andric   /// "primary" DeclContext structure, which will contain the
22270b57cec5SDimitry Andric   /// information needed to perform name lookup into this context.
22280b57cec5SDimitry Andric   DeclContext *getPrimaryContext();
22290b57cec5SDimitry Andric   const DeclContext *getPrimaryContext() const {
22300b57cec5SDimitry Andric     return const_cast<DeclContext*>(this)->getPrimaryContext();
22310b57cec5SDimitry Andric   }
22320b57cec5SDimitry Andric 
22330b57cec5SDimitry Andric   /// getRedeclContext - Retrieve the context in which an entity conflicts with
22340b57cec5SDimitry Andric   /// other entities of the same name, or where it is a redeclaration if the
22350b57cec5SDimitry Andric   /// two entities are compatible. This skips through transparent contexts.
22360b57cec5SDimitry Andric   DeclContext *getRedeclContext();
22370b57cec5SDimitry Andric   const DeclContext *getRedeclContext() const {
22380b57cec5SDimitry Andric     return const_cast<DeclContext *>(this)->getRedeclContext();
22390b57cec5SDimitry Andric   }
22400b57cec5SDimitry Andric 
22410b57cec5SDimitry Andric   /// Retrieve the nearest enclosing namespace context.
22420b57cec5SDimitry Andric   DeclContext *getEnclosingNamespaceContext();
22430b57cec5SDimitry Andric   const DeclContext *getEnclosingNamespaceContext() const {
22440b57cec5SDimitry Andric     return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
22450b57cec5SDimitry Andric   }
22460b57cec5SDimitry Andric 
22470b57cec5SDimitry Andric   /// Retrieve the outermost lexically enclosing record context.
22480b57cec5SDimitry Andric   RecordDecl *getOuterLexicalRecordContext();
22490b57cec5SDimitry Andric   const RecordDecl *getOuterLexicalRecordContext() const {
22500b57cec5SDimitry Andric     return const_cast<DeclContext *>(this)->getOuterLexicalRecordContext();
22510b57cec5SDimitry Andric   }
22520b57cec5SDimitry Andric 
22530b57cec5SDimitry Andric   /// Test if this context is part of the enclosing namespace set of
22540b57cec5SDimitry Andric   /// the context NS, as defined in C++0x [namespace.def]p9. If either context
22550b57cec5SDimitry Andric   /// isn't a namespace, this is equivalent to Equals().
22560b57cec5SDimitry Andric   ///
22570b57cec5SDimitry Andric   /// The enclosing namespace set of a namespace is the namespace and, if it is
22580b57cec5SDimitry Andric   /// inline, its enclosing namespace, recursively.
22590b57cec5SDimitry Andric   bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
22600b57cec5SDimitry Andric 
22610b57cec5SDimitry Andric   /// Collects all of the declaration contexts that are semantically
22620b57cec5SDimitry Andric   /// connected to this declaration context.
22630b57cec5SDimitry Andric   ///
22640b57cec5SDimitry Andric   /// For declaration contexts that have multiple semantically connected but
22650b57cec5SDimitry Andric   /// syntactically distinct contexts, such as C++ namespaces, this routine
22660b57cec5SDimitry Andric   /// retrieves the complete set of such declaration contexts in source order.
22670b57cec5SDimitry Andric   /// For example, given:
22680b57cec5SDimitry Andric   ///
22690b57cec5SDimitry Andric   /// \code
22700b57cec5SDimitry Andric   /// namespace N {
22710b57cec5SDimitry Andric   ///   int x;
22720b57cec5SDimitry Andric   /// }
22730b57cec5SDimitry Andric   /// namespace N {
22740b57cec5SDimitry Andric   ///   int y;
22750b57cec5SDimitry Andric   /// }
22760b57cec5SDimitry Andric   /// \endcode
22770b57cec5SDimitry Andric   ///
22780b57cec5SDimitry Andric   /// The \c Contexts parameter will contain both definitions of N.
22790b57cec5SDimitry Andric   ///
22800b57cec5SDimitry Andric   /// \param Contexts Will be cleared and set to the set of declaration
22810b57cec5SDimitry Andric   /// contexts that are semanticaly connected to this declaration context,
22820b57cec5SDimitry Andric   /// in source order, including this context (which may be the only result,
22830b57cec5SDimitry Andric   /// for non-namespace contexts).
22840b57cec5SDimitry Andric   void collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts);
22850b57cec5SDimitry Andric 
22860b57cec5SDimitry Andric   /// decl_iterator - Iterates through the declarations stored
22870b57cec5SDimitry Andric   /// within this context.
22880b57cec5SDimitry Andric   class decl_iterator {
22890b57cec5SDimitry Andric     /// Current - The current declaration.
22900b57cec5SDimitry Andric     Decl *Current = nullptr;
22910b57cec5SDimitry Andric 
22920b57cec5SDimitry Andric   public:
22930b57cec5SDimitry Andric     using value_type = Decl *;
22940b57cec5SDimitry Andric     using reference = const value_type &;
22950b57cec5SDimitry Andric     using pointer = const value_type *;
22960b57cec5SDimitry Andric     using iterator_category = std::forward_iterator_tag;
22970b57cec5SDimitry Andric     using difference_type = std::ptrdiff_t;
22980b57cec5SDimitry Andric 
22990b57cec5SDimitry Andric     decl_iterator() = default;
23000b57cec5SDimitry Andric     explicit decl_iterator(Decl *C) : Current(C) {}
23010b57cec5SDimitry Andric 
23020b57cec5SDimitry Andric     reference operator*() const { return Current; }
23030b57cec5SDimitry Andric 
23040b57cec5SDimitry Andric     // This doesn't meet the iterator requirements, but it's convenient
23050b57cec5SDimitry Andric     value_type operator->() const { return Current; }
23060b57cec5SDimitry Andric 
23070b57cec5SDimitry Andric     decl_iterator& operator++() {
23080b57cec5SDimitry Andric       Current = Current->getNextDeclInContext();
23090b57cec5SDimitry Andric       return *this;
23100b57cec5SDimitry Andric     }
23110b57cec5SDimitry Andric 
23120b57cec5SDimitry Andric     decl_iterator operator++(int) {
23130b57cec5SDimitry Andric       decl_iterator tmp(*this);
23140b57cec5SDimitry Andric       ++(*this);
23150b57cec5SDimitry Andric       return tmp;
23160b57cec5SDimitry Andric     }
23170b57cec5SDimitry Andric 
23180b57cec5SDimitry Andric     friend bool operator==(decl_iterator x, decl_iterator y) {
23190b57cec5SDimitry Andric       return x.Current == y.Current;
23200b57cec5SDimitry Andric     }
23210b57cec5SDimitry Andric 
23220b57cec5SDimitry Andric     friend bool operator!=(decl_iterator x, decl_iterator y) {
23230b57cec5SDimitry Andric       return x.Current != y.Current;
23240b57cec5SDimitry Andric     }
23250b57cec5SDimitry Andric   };
23260b57cec5SDimitry Andric 
23270b57cec5SDimitry Andric   using decl_range = llvm::iterator_range<decl_iterator>;
23280b57cec5SDimitry Andric 
23290b57cec5SDimitry Andric   /// decls_begin/decls_end - Iterate over the declarations stored in
23300b57cec5SDimitry Andric   /// this context.
23310b57cec5SDimitry Andric   decl_range decls() const { return decl_range(decls_begin(), decls_end()); }
23320b57cec5SDimitry Andric   decl_iterator decls_begin() const;
23330b57cec5SDimitry Andric   decl_iterator decls_end() const { return decl_iterator(); }
23340b57cec5SDimitry Andric   bool decls_empty() const;
23350b57cec5SDimitry Andric 
23360b57cec5SDimitry Andric   /// noload_decls_begin/end - Iterate over the declarations stored in this
23370b57cec5SDimitry Andric   /// context that are currently loaded; don't attempt to retrieve anything
23380b57cec5SDimitry Andric   /// from an external source.
23390b57cec5SDimitry Andric   decl_range noload_decls() const {
23400b57cec5SDimitry Andric     return decl_range(noload_decls_begin(), noload_decls_end());
23410b57cec5SDimitry Andric   }
23420b57cec5SDimitry Andric   decl_iterator noload_decls_begin() const { return decl_iterator(FirstDecl); }
23430b57cec5SDimitry Andric   decl_iterator noload_decls_end() const { return decl_iterator(); }
23440b57cec5SDimitry Andric 
23450b57cec5SDimitry Andric   /// specific_decl_iterator - Iterates over a subrange of
23460b57cec5SDimitry Andric   /// declarations stored in a DeclContext, providing only those that
23470b57cec5SDimitry Andric   /// are of type SpecificDecl (or a class derived from it). This
23480b57cec5SDimitry Andric   /// iterator is used, for example, to provide iteration over just
23490b57cec5SDimitry Andric   /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
23500b57cec5SDimitry Andric   template<typename SpecificDecl>
23510b57cec5SDimitry Andric   class specific_decl_iterator {
23520b57cec5SDimitry Andric     /// Current - The current, underlying declaration iterator, which
23530b57cec5SDimitry Andric     /// will either be NULL or will point to a declaration of
23540b57cec5SDimitry Andric     /// type SpecificDecl.
23550b57cec5SDimitry Andric     DeclContext::decl_iterator Current;
23560b57cec5SDimitry Andric 
23570b57cec5SDimitry Andric     /// SkipToNextDecl - Advances the current position up to the next
23580b57cec5SDimitry Andric     /// declaration of type SpecificDecl that also meets the criteria
23590b57cec5SDimitry Andric     /// required by Acceptable.
23600b57cec5SDimitry Andric     void SkipToNextDecl() {
23610b57cec5SDimitry Andric       while (*Current && !isa<SpecificDecl>(*Current))
23620b57cec5SDimitry Andric         ++Current;
23630b57cec5SDimitry Andric     }
23640b57cec5SDimitry Andric 
23650b57cec5SDimitry Andric   public:
23660b57cec5SDimitry Andric     using value_type = SpecificDecl *;
23670b57cec5SDimitry Andric     // TODO: Add reference and pointer types (with some appropriate proxy type)
23680b57cec5SDimitry Andric     // if we ever have a need for them.
23690b57cec5SDimitry Andric     using reference = void;
23700b57cec5SDimitry Andric     using pointer = void;
23710b57cec5SDimitry Andric     using difference_type =
23720b57cec5SDimitry Andric         std::iterator_traits<DeclContext::decl_iterator>::difference_type;
23730b57cec5SDimitry Andric     using iterator_category = std::forward_iterator_tag;
23740b57cec5SDimitry Andric 
23750b57cec5SDimitry Andric     specific_decl_iterator() = default;
23760b57cec5SDimitry Andric 
23770b57cec5SDimitry Andric     /// specific_decl_iterator - Construct a new iterator over a
23780b57cec5SDimitry Andric     /// subset of the declarations the range [C,
23790b57cec5SDimitry Andric     /// end-of-declarations). If A is non-NULL, it is a pointer to a
23800b57cec5SDimitry Andric     /// member function of SpecificDecl that should return true for
23810b57cec5SDimitry Andric     /// all of the SpecificDecl instances that will be in the subset
23820b57cec5SDimitry Andric     /// of iterators. For example, if you want Objective-C instance
23830b57cec5SDimitry Andric     /// methods, SpecificDecl will be ObjCMethodDecl and A will be
23840b57cec5SDimitry Andric     /// &ObjCMethodDecl::isInstanceMethod.
23850b57cec5SDimitry Andric     explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
23860b57cec5SDimitry Andric       SkipToNextDecl();
23870b57cec5SDimitry Andric     }
23880b57cec5SDimitry Andric 
23890b57cec5SDimitry Andric     value_type operator*() const { return cast<SpecificDecl>(*Current); }
23900b57cec5SDimitry Andric 
23910b57cec5SDimitry Andric     // This doesn't meet the iterator requirements, but it's convenient
23920b57cec5SDimitry Andric     value_type operator->() const { return **this; }
23930b57cec5SDimitry Andric 
23940b57cec5SDimitry Andric     specific_decl_iterator& operator++() {
23950b57cec5SDimitry Andric       ++Current;
23960b57cec5SDimitry Andric       SkipToNextDecl();
23970b57cec5SDimitry Andric       return *this;
23980b57cec5SDimitry Andric     }
23990b57cec5SDimitry Andric 
24000b57cec5SDimitry Andric     specific_decl_iterator operator++(int) {
24010b57cec5SDimitry Andric       specific_decl_iterator tmp(*this);
24020b57cec5SDimitry Andric       ++(*this);
24030b57cec5SDimitry Andric       return tmp;
24040b57cec5SDimitry Andric     }
24050b57cec5SDimitry Andric 
24060b57cec5SDimitry Andric     friend bool operator==(const specific_decl_iterator& x,
24070b57cec5SDimitry Andric                            const specific_decl_iterator& y) {
24080b57cec5SDimitry Andric       return x.Current == y.Current;
24090b57cec5SDimitry Andric     }
24100b57cec5SDimitry Andric 
24110b57cec5SDimitry Andric     friend bool operator!=(const specific_decl_iterator& x,
24120b57cec5SDimitry Andric                            const specific_decl_iterator& y) {
24130b57cec5SDimitry Andric       return x.Current != y.Current;
24140b57cec5SDimitry Andric     }
24150b57cec5SDimitry Andric   };
24160b57cec5SDimitry Andric 
24170b57cec5SDimitry Andric   /// Iterates over a filtered subrange of declarations stored
24180b57cec5SDimitry Andric   /// in a DeclContext.
24190b57cec5SDimitry Andric   ///
24200b57cec5SDimitry Andric   /// This iterator visits only those declarations that are of type
24210b57cec5SDimitry Andric   /// SpecificDecl (or a class derived from it) and that meet some
24220b57cec5SDimitry Andric   /// additional run-time criteria. This iterator is used, for
24230b57cec5SDimitry Andric   /// example, to provide access to the instance methods within an
24240b57cec5SDimitry Andric   /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
24250b57cec5SDimitry Andric   /// Acceptable = ObjCMethodDecl::isInstanceMethod).
24260b57cec5SDimitry Andric   template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
24270b57cec5SDimitry Andric   class filtered_decl_iterator {
24280b57cec5SDimitry Andric     /// Current - The current, underlying declaration iterator, which
24290b57cec5SDimitry Andric     /// will either be NULL or will point to a declaration of
24300b57cec5SDimitry Andric     /// type SpecificDecl.
24310b57cec5SDimitry Andric     DeclContext::decl_iterator Current;
24320b57cec5SDimitry Andric 
24330b57cec5SDimitry Andric     /// SkipToNextDecl - Advances the current position up to the next
24340b57cec5SDimitry Andric     /// declaration of type SpecificDecl that also meets the criteria
24350b57cec5SDimitry Andric     /// required by Acceptable.
24360b57cec5SDimitry Andric     void SkipToNextDecl() {
24370b57cec5SDimitry Andric       while (*Current &&
24380b57cec5SDimitry Andric              (!isa<SpecificDecl>(*Current) ||
24390b57cec5SDimitry Andric               (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
24400b57cec5SDimitry Andric         ++Current;
24410b57cec5SDimitry Andric     }
24420b57cec5SDimitry Andric 
24430b57cec5SDimitry Andric   public:
24440b57cec5SDimitry Andric     using value_type = SpecificDecl *;
24450b57cec5SDimitry Andric     // TODO: Add reference and pointer types (with some appropriate proxy type)
24460b57cec5SDimitry Andric     // if we ever have a need for them.
24470b57cec5SDimitry Andric     using reference = void;
24480b57cec5SDimitry Andric     using pointer = void;
24490b57cec5SDimitry Andric     using difference_type =
24500b57cec5SDimitry Andric         std::iterator_traits<DeclContext::decl_iterator>::difference_type;
24510b57cec5SDimitry Andric     using iterator_category = std::forward_iterator_tag;
24520b57cec5SDimitry Andric 
24530b57cec5SDimitry Andric     filtered_decl_iterator() = default;
24540b57cec5SDimitry Andric 
24550b57cec5SDimitry Andric     /// filtered_decl_iterator - Construct a new iterator over a
24560b57cec5SDimitry Andric     /// subset of the declarations the range [C,
24570b57cec5SDimitry Andric     /// end-of-declarations). If A is non-NULL, it is a pointer to a
24580b57cec5SDimitry Andric     /// member function of SpecificDecl that should return true for
24590b57cec5SDimitry Andric     /// all of the SpecificDecl instances that will be in the subset
24600b57cec5SDimitry Andric     /// of iterators. For example, if you want Objective-C instance
24610b57cec5SDimitry Andric     /// methods, SpecificDecl will be ObjCMethodDecl and A will be
24620b57cec5SDimitry Andric     /// &ObjCMethodDecl::isInstanceMethod.
24630b57cec5SDimitry Andric     explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
24640b57cec5SDimitry Andric       SkipToNextDecl();
24650b57cec5SDimitry Andric     }
24660b57cec5SDimitry Andric 
24670b57cec5SDimitry Andric     value_type operator*() const { return cast<SpecificDecl>(*Current); }
24680b57cec5SDimitry Andric     value_type operator->() const { return cast<SpecificDecl>(*Current); }
24690b57cec5SDimitry Andric 
24700b57cec5SDimitry Andric     filtered_decl_iterator& operator++() {
24710b57cec5SDimitry Andric       ++Current;
24720b57cec5SDimitry Andric       SkipToNextDecl();
24730b57cec5SDimitry Andric       return *this;
24740b57cec5SDimitry Andric     }
24750b57cec5SDimitry Andric 
24760b57cec5SDimitry Andric     filtered_decl_iterator operator++(int) {
24770b57cec5SDimitry Andric       filtered_decl_iterator tmp(*this);
24780b57cec5SDimitry Andric       ++(*this);
24790b57cec5SDimitry Andric       return tmp;
24800b57cec5SDimitry Andric     }
24810b57cec5SDimitry Andric 
24820b57cec5SDimitry Andric     friend bool operator==(const filtered_decl_iterator& x,
24830b57cec5SDimitry Andric                            const filtered_decl_iterator& y) {
24840b57cec5SDimitry Andric       return x.Current == y.Current;
24850b57cec5SDimitry Andric     }
24860b57cec5SDimitry Andric 
24870b57cec5SDimitry Andric     friend bool operator!=(const filtered_decl_iterator& x,
24880b57cec5SDimitry Andric                            const filtered_decl_iterator& y) {
24890b57cec5SDimitry Andric       return x.Current != y.Current;
24900b57cec5SDimitry Andric     }
24910b57cec5SDimitry Andric   };
24920b57cec5SDimitry Andric 
24930b57cec5SDimitry Andric   /// Add the declaration D into this context.
24940b57cec5SDimitry Andric   ///
24950b57cec5SDimitry Andric   /// This routine should be invoked when the declaration D has first
24960b57cec5SDimitry Andric   /// been declared, to place D into the context where it was
24970b57cec5SDimitry Andric   /// (lexically) defined. Every declaration must be added to one
24980b57cec5SDimitry Andric   /// (and only one!) context, where it can be visited via
24990b57cec5SDimitry Andric   /// [decls_begin(), decls_end()). Once a declaration has been added
25000b57cec5SDimitry Andric   /// to its lexical context, the corresponding DeclContext owns the
25010b57cec5SDimitry Andric   /// declaration.
25020b57cec5SDimitry Andric   ///
25030b57cec5SDimitry Andric   /// If D is also a NamedDecl, it will be made visible within its
25040b57cec5SDimitry Andric   /// semantic context via makeDeclVisibleInContext.
25050b57cec5SDimitry Andric   void addDecl(Decl *D);
25060b57cec5SDimitry Andric 
25070b57cec5SDimitry Andric   /// Add the declaration D into this context, but suppress
25080b57cec5SDimitry Andric   /// searches for external declarations with the same name.
25090b57cec5SDimitry Andric   ///
25100b57cec5SDimitry Andric   /// Although analogous in function to addDecl, this removes an
25110b57cec5SDimitry Andric   /// important check.  This is only useful if the Decl is being
25120b57cec5SDimitry Andric   /// added in response to an external search; in all other cases,
25130b57cec5SDimitry Andric   /// addDecl() is the right function to use.
25140b57cec5SDimitry Andric   /// See the ASTImporter for use cases.
25150b57cec5SDimitry Andric   void addDeclInternal(Decl *D);
25160b57cec5SDimitry Andric 
25170b57cec5SDimitry Andric   /// Add the declaration D to this context without modifying
25180b57cec5SDimitry Andric   /// any lookup tables.
25190b57cec5SDimitry Andric   ///
25200b57cec5SDimitry Andric   /// This is useful for some operations in dependent contexts where
25210b57cec5SDimitry Andric   /// the semantic context might not be dependent;  this basically
25220b57cec5SDimitry Andric   /// only happens with friends.
25230b57cec5SDimitry Andric   void addHiddenDecl(Decl *D);
25240b57cec5SDimitry Andric 
25250b57cec5SDimitry Andric   /// Removes a declaration from this context.
25260b57cec5SDimitry Andric   void removeDecl(Decl *D);
25270b57cec5SDimitry Andric 
25280b57cec5SDimitry Andric   /// Checks whether a declaration is in this context.
25290b57cec5SDimitry Andric   bool containsDecl(Decl *D) const;
25300b57cec5SDimitry Andric 
25310b57cec5SDimitry Andric   /// Checks whether a declaration is in this context.
25320b57cec5SDimitry Andric   /// This also loads the Decls from the external source before the check.
25330b57cec5SDimitry Andric   bool containsDeclAndLoad(Decl *D) const;
25340b57cec5SDimitry Andric 
25350b57cec5SDimitry Andric   using lookup_result = DeclContextLookupResult;
25360b57cec5SDimitry Andric   using lookup_iterator = lookup_result::iterator;
25370b57cec5SDimitry Andric 
25380b57cec5SDimitry Andric   /// lookup - Find the declarations (if any) with the given Name in
25390b57cec5SDimitry Andric   /// this context. Returns a range of iterators that contains all of
25400b57cec5SDimitry Andric   /// the declarations with this name, with object, function, member,
25410b57cec5SDimitry Andric   /// and enumerator names preceding any tag name. Note that this
25420b57cec5SDimitry Andric   /// routine will not look into parent contexts.
25430b57cec5SDimitry Andric   lookup_result lookup(DeclarationName Name) const;
25440b57cec5SDimitry Andric 
25450b57cec5SDimitry Andric   /// Find the declarations with the given name that are visible
25460b57cec5SDimitry Andric   /// within this context; don't attempt to retrieve anything from an
25470b57cec5SDimitry Andric   /// external source.
25480b57cec5SDimitry Andric   lookup_result noload_lookup(DeclarationName Name);
25490b57cec5SDimitry Andric 
25500b57cec5SDimitry Andric   /// A simplistic name lookup mechanism that performs name lookup
25510b57cec5SDimitry Andric   /// into this declaration context without consulting the external source.
25520b57cec5SDimitry Andric   ///
25530b57cec5SDimitry Andric   /// This function should almost never be used, because it subverts the
25540b57cec5SDimitry Andric   /// usual relationship between a DeclContext and the external source.
25550b57cec5SDimitry Andric   /// See the ASTImporter for the (few, but important) use cases.
25560b57cec5SDimitry Andric   ///
25570b57cec5SDimitry Andric   /// FIXME: This is very inefficient; replace uses of it with uses of
25580b57cec5SDimitry Andric   /// noload_lookup.
25590b57cec5SDimitry Andric   void localUncachedLookup(DeclarationName Name,
25600b57cec5SDimitry Andric                            SmallVectorImpl<NamedDecl *> &Results);
25610b57cec5SDimitry Andric 
25620b57cec5SDimitry Andric   /// Makes a declaration visible within this context.
25630b57cec5SDimitry Andric   ///
25640b57cec5SDimitry Andric   /// This routine makes the declaration D visible to name lookup
25650b57cec5SDimitry Andric   /// within this context and, if this is a transparent context,
25660b57cec5SDimitry Andric   /// within its parent contexts up to the first enclosing
25670b57cec5SDimitry Andric   /// non-transparent context. Making a declaration visible within a
25680b57cec5SDimitry Andric   /// context does not transfer ownership of a declaration, and a
25690b57cec5SDimitry Andric   /// declaration can be visible in many contexts that aren't its
25700b57cec5SDimitry Andric   /// lexical context.
25710b57cec5SDimitry Andric   ///
25720b57cec5SDimitry Andric   /// If D is a redeclaration of an existing declaration that is
25730b57cec5SDimitry Andric   /// visible from this context, as determined by
25740b57cec5SDimitry Andric   /// NamedDecl::declarationReplaces, the previous declaration will be
25750b57cec5SDimitry Andric   /// replaced with D.
25760b57cec5SDimitry Andric   void makeDeclVisibleInContext(NamedDecl *D);
25770b57cec5SDimitry Andric 
25780b57cec5SDimitry Andric   /// all_lookups_iterator - An iterator that provides a view over the results
25790b57cec5SDimitry Andric   /// of looking up every possible name.
25800b57cec5SDimitry Andric   class all_lookups_iterator;
25810b57cec5SDimitry Andric 
25820b57cec5SDimitry Andric   using lookups_range = llvm::iterator_range<all_lookups_iterator>;
25830b57cec5SDimitry Andric 
25840b57cec5SDimitry Andric   lookups_range lookups() const;
25850b57cec5SDimitry Andric   // Like lookups(), but avoids loading external declarations.
25860b57cec5SDimitry Andric   // If PreserveInternalState, avoids building lookup data structures too.
25870b57cec5SDimitry Andric   lookups_range noload_lookups(bool PreserveInternalState) const;
25880b57cec5SDimitry Andric 
25890b57cec5SDimitry Andric   /// Iterators over all possible lookups within this context.
25900b57cec5SDimitry Andric   all_lookups_iterator lookups_begin() const;
25910b57cec5SDimitry Andric   all_lookups_iterator lookups_end() const;
25920b57cec5SDimitry Andric 
25930b57cec5SDimitry Andric   /// Iterators over all possible lookups within this context that are
25940b57cec5SDimitry Andric   /// currently loaded; don't attempt to retrieve anything from an external
25950b57cec5SDimitry Andric   /// source.
25960b57cec5SDimitry Andric   all_lookups_iterator noload_lookups_begin() const;
25970b57cec5SDimitry Andric   all_lookups_iterator noload_lookups_end() const;
25980b57cec5SDimitry Andric 
25990b57cec5SDimitry Andric   struct udir_iterator;
26000b57cec5SDimitry Andric 
26010b57cec5SDimitry Andric   using udir_iterator_base =
26020b57cec5SDimitry Andric       llvm::iterator_adaptor_base<udir_iterator, lookup_iterator,
2603fe6060f1SDimitry Andric                                   typename lookup_iterator::iterator_category,
26040b57cec5SDimitry Andric                                   UsingDirectiveDecl *>;
26050b57cec5SDimitry Andric 
26060b57cec5SDimitry Andric   struct udir_iterator : udir_iterator_base {
26070b57cec5SDimitry Andric     udir_iterator(lookup_iterator I) : udir_iterator_base(I) {}
26080b57cec5SDimitry Andric 
26090b57cec5SDimitry Andric     UsingDirectiveDecl *operator*() const;
26100b57cec5SDimitry Andric   };
26110b57cec5SDimitry Andric 
26120b57cec5SDimitry Andric   using udir_range = llvm::iterator_range<udir_iterator>;
26130b57cec5SDimitry Andric 
26140b57cec5SDimitry Andric   udir_range using_directives() const;
26150b57cec5SDimitry Andric 
26160b57cec5SDimitry Andric   // These are all defined in DependentDiagnostic.h.
26170b57cec5SDimitry Andric   class ddiag_iterator;
26180b57cec5SDimitry Andric 
26190b57cec5SDimitry Andric   using ddiag_range = llvm::iterator_range<DeclContext::ddiag_iterator>;
26200b57cec5SDimitry Andric 
26210b57cec5SDimitry Andric   inline ddiag_range ddiags() const;
26220b57cec5SDimitry Andric 
26230b57cec5SDimitry Andric   // Low-level accessors
26240b57cec5SDimitry Andric 
26250b57cec5SDimitry Andric   /// Mark that there are external lexical declarations that we need
26260b57cec5SDimitry Andric   /// to include in our lookup table (and that are not available as external
26270b57cec5SDimitry Andric   /// visible lookups). These extra lookup results will be found by walking
26280b57cec5SDimitry Andric   /// the lexical declarations of this context. This should be used only if
26290b57cec5SDimitry Andric   /// setHasExternalLexicalStorage() has been called on any decl context for
26300b57cec5SDimitry Andric   /// which this is the primary context.
26310b57cec5SDimitry Andric   void setMustBuildLookupTable() {
26320b57cec5SDimitry Andric     assert(this == getPrimaryContext() &&
26330b57cec5SDimitry Andric            "should only be called on primary context");
26340b57cec5SDimitry Andric     DeclContextBits.HasLazyExternalLexicalLookups = true;
26350b57cec5SDimitry Andric   }
26360b57cec5SDimitry Andric 
26370b57cec5SDimitry Andric   /// Retrieve the internal representation of the lookup structure.
26380b57cec5SDimitry Andric   /// This may omit some names if we are lazily building the structure.
26390b57cec5SDimitry Andric   StoredDeclsMap *getLookupPtr() const { return LookupPtr; }
26400b57cec5SDimitry Andric 
26410b57cec5SDimitry Andric   /// Ensure the lookup structure is fully-built and return it.
26420b57cec5SDimitry Andric   StoredDeclsMap *buildLookup();
26430b57cec5SDimitry Andric 
26440b57cec5SDimitry Andric   /// Whether this DeclContext has external storage containing
26450b57cec5SDimitry Andric   /// additional declarations that are lexically in this context.
26460b57cec5SDimitry Andric   bool hasExternalLexicalStorage() const {
26470b57cec5SDimitry Andric     return DeclContextBits.ExternalLexicalStorage;
26480b57cec5SDimitry Andric   }
26490b57cec5SDimitry Andric 
26500b57cec5SDimitry Andric   /// State whether this DeclContext has external storage for
26510b57cec5SDimitry Andric   /// declarations lexically in this context.
26520b57cec5SDimitry Andric   void setHasExternalLexicalStorage(bool ES = true) const {
26530b57cec5SDimitry Andric     DeclContextBits.ExternalLexicalStorage = ES;
26540b57cec5SDimitry Andric   }
26550b57cec5SDimitry Andric 
26560b57cec5SDimitry Andric   /// Whether this DeclContext has external storage containing
26570b57cec5SDimitry Andric   /// additional declarations that are visible in this context.
26580b57cec5SDimitry Andric   bool hasExternalVisibleStorage() const {
26590b57cec5SDimitry Andric     return DeclContextBits.ExternalVisibleStorage;
26600b57cec5SDimitry Andric   }
26610b57cec5SDimitry Andric 
26620b57cec5SDimitry Andric   /// State whether this DeclContext has external storage for
26630b57cec5SDimitry Andric   /// declarations visible in this context.
26640b57cec5SDimitry Andric   void setHasExternalVisibleStorage(bool ES = true) const {
26650b57cec5SDimitry Andric     DeclContextBits.ExternalVisibleStorage = ES;
26660b57cec5SDimitry Andric     if (ES && LookupPtr)
26670b57cec5SDimitry Andric       DeclContextBits.NeedToReconcileExternalVisibleStorage = true;
26680b57cec5SDimitry Andric   }
26690b57cec5SDimitry Andric 
26700b57cec5SDimitry Andric   /// Determine whether the given declaration is stored in the list of
26710b57cec5SDimitry Andric   /// declarations lexically within this context.
26720b57cec5SDimitry Andric   bool isDeclInLexicalTraversal(const Decl *D) const {
26730b57cec5SDimitry Andric     return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl ||
26740b57cec5SDimitry Andric                  D == LastDecl);
26750b57cec5SDimitry Andric   }
26760b57cec5SDimitry Andric 
267706c3fb27SDimitry Andric   void setUseQualifiedLookup(bool use = true) const {
26780b57cec5SDimitry Andric     DeclContextBits.UseQualifiedLookup = use;
26790b57cec5SDimitry Andric   }
26800b57cec5SDimitry Andric 
26810b57cec5SDimitry Andric   bool shouldUseQualifiedLookup() const {
26820b57cec5SDimitry Andric     return DeclContextBits.UseQualifiedLookup;
26830b57cec5SDimitry Andric   }
26840b57cec5SDimitry Andric 
26850b57cec5SDimitry Andric   static bool classof(const Decl *D);
26860b57cec5SDimitry Andric   static bool classof(const DeclContext *D) { return true; }
26870b57cec5SDimitry Andric 
2688bdd1243dSDimitry Andric   void dumpAsDecl() const;
2689bdd1243dSDimitry Andric   void dumpAsDecl(const ASTContext *Ctx) const;
26900b57cec5SDimitry Andric   void dumpDeclContext() const;
26910b57cec5SDimitry Andric   void dumpLookups() const;
26920b57cec5SDimitry Andric   void dumpLookups(llvm::raw_ostream &OS, bool DumpDecls = false,
26930b57cec5SDimitry Andric                    bool Deserialize = false) const;
26940b57cec5SDimitry Andric 
26950b57cec5SDimitry Andric private:
26960b57cec5SDimitry Andric   /// Whether this declaration context has had externally visible
26970b57cec5SDimitry Andric   /// storage added since the last lookup. In this case, \c LookupPtr's
26980b57cec5SDimitry Andric   /// invariant may not hold and needs to be fixed before we perform
26990b57cec5SDimitry Andric   /// another lookup.
27000b57cec5SDimitry Andric   bool hasNeedToReconcileExternalVisibleStorage() const {
27010b57cec5SDimitry Andric     return DeclContextBits.NeedToReconcileExternalVisibleStorage;
27020b57cec5SDimitry Andric   }
27030b57cec5SDimitry Andric 
27040b57cec5SDimitry Andric   /// State that this declaration context has had externally visible
27050b57cec5SDimitry Andric   /// storage added since the last lookup. In this case, \c LookupPtr's
27060b57cec5SDimitry Andric   /// invariant may not hold and needs to be fixed before we perform
27070b57cec5SDimitry Andric   /// another lookup.
27080b57cec5SDimitry Andric   void setNeedToReconcileExternalVisibleStorage(bool Need = true) const {
27090b57cec5SDimitry Andric     DeclContextBits.NeedToReconcileExternalVisibleStorage = Need;
27100b57cec5SDimitry Andric   }
27110b57cec5SDimitry Andric 
27120b57cec5SDimitry Andric   /// If \c true, this context may have local lexical declarations
27130b57cec5SDimitry Andric   /// that are missing from the lookup table.
27140b57cec5SDimitry Andric   bool hasLazyLocalLexicalLookups() const {
27150b57cec5SDimitry Andric     return DeclContextBits.HasLazyLocalLexicalLookups;
27160b57cec5SDimitry Andric   }
27170b57cec5SDimitry Andric 
27180b57cec5SDimitry Andric   /// If \c true, this context may have local lexical declarations
27190b57cec5SDimitry Andric   /// that are missing from the lookup table.
27200b57cec5SDimitry Andric   void setHasLazyLocalLexicalLookups(bool HasLLLL = true) const {
27210b57cec5SDimitry Andric     DeclContextBits.HasLazyLocalLexicalLookups = HasLLLL;
27220b57cec5SDimitry Andric   }
27230b57cec5SDimitry Andric 
27240b57cec5SDimitry Andric   /// If \c true, the external source may have lexical declarations
27250b57cec5SDimitry Andric   /// that are missing from the lookup table.
27260b57cec5SDimitry Andric   bool hasLazyExternalLexicalLookups() const {
27270b57cec5SDimitry Andric     return DeclContextBits.HasLazyExternalLexicalLookups;
27280b57cec5SDimitry Andric   }
27290b57cec5SDimitry Andric 
27300b57cec5SDimitry Andric   /// If \c true, the external source may have lexical declarations
27310b57cec5SDimitry Andric   /// that are missing from the lookup table.
27320b57cec5SDimitry Andric   void setHasLazyExternalLexicalLookups(bool HasLELL = true) const {
27330b57cec5SDimitry Andric     DeclContextBits.HasLazyExternalLexicalLookups = HasLELL;
27340b57cec5SDimitry Andric   }
27350b57cec5SDimitry Andric 
27360b57cec5SDimitry Andric   void reconcileExternalVisibleStorage() const;
27370b57cec5SDimitry Andric   bool LoadLexicalDeclsFromExternalStorage() const;
27380b57cec5SDimitry Andric 
27390b57cec5SDimitry Andric   StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
27400b57cec5SDimitry Andric 
27410b57cec5SDimitry Andric   void loadLazyLocalLexicalLookups();
27420b57cec5SDimitry Andric   void buildLookupImpl(DeclContext *DCtx, bool Internal);
27430b57cec5SDimitry Andric   void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
27440b57cec5SDimitry Andric                                          bool Rediscoverable);
27450b57cec5SDimitry Andric   void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal);
27460b57cec5SDimitry Andric };
27470b57cec5SDimitry Andric 
27480b57cec5SDimitry Andric inline bool Decl::isTemplateParameter() const {
27490b57cec5SDimitry Andric   return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
27500b57cec5SDimitry Andric          getKind() == TemplateTemplateParm;
27510b57cec5SDimitry Andric }
27520b57cec5SDimitry Andric 
27530b57cec5SDimitry Andric // Specialization selected when ToTy is not a known subclass of DeclContext.
27540b57cec5SDimitry Andric template <class ToTy,
27550b57cec5SDimitry Andric           bool IsKnownSubtype = ::std::is_base_of<DeclContext, ToTy>::value>
27560b57cec5SDimitry Andric struct cast_convert_decl_context {
27570b57cec5SDimitry Andric   static const ToTy *doit(const DeclContext *Val) {
27580b57cec5SDimitry Andric     return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
27590b57cec5SDimitry Andric   }
27600b57cec5SDimitry Andric 
27610b57cec5SDimitry Andric   static ToTy *doit(DeclContext *Val) {
27620b57cec5SDimitry Andric     return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
27630b57cec5SDimitry Andric   }
27640b57cec5SDimitry Andric };
27650b57cec5SDimitry Andric 
27660b57cec5SDimitry Andric // Specialization selected when ToTy is a known subclass of DeclContext.
27670b57cec5SDimitry Andric template <class ToTy>
27680b57cec5SDimitry Andric struct cast_convert_decl_context<ToTy, true> {
27690b57cec5SDimitry Andric   static const ToTy *doit(const DeclContext *Val) {
27700b57cec5SDimitry Andric     return static_cast<const ToTy*>(Val);
27710b57cec5SDimitry Andric   }
27720b57cec5SDimitry Andric 
27730b57cec5SDimitry Andric   static ToTy *doit(DeclContext *Val) {
27740b57cec5SDimitry Andric     return static_cast<ToTy*>(Val);
27750b57cec5SDimitry Andric   }
27760b57cec5SDimitry Andric };
27770b57cec5SDimitry Andric 
27780b57cec5SDimitry Andric } // namespace clang
27790b57cec5SDimitry Andric 
27800b57cec5SDimitry Andric namespace llvm {
27810b57cec5SDimitry Andric 
27820b57cec5SDimitry Andric /// isa<T>(DeclContext*)
27830b57cec5SDimitry Andric template <typename To>
27840b57cec5SDimitry Andric struct isa_impl<To, ::clang::DeclContext> {
27850b57cec5SDimitry Andric   static bool doit(const ::clang::DeclContext &Val) {
27860b57cec5SDimitry Andric     return To::classofKind(Val.getDeclKind());
27870b57cec5SDimitry Andric   }
27880b57cec5SDimitry Andric };
27890b57cec5SDimitry Andric 
27900b57cec5SDimitry Andric /// cast<T>(DeclContext*)
27910b57cec5SDimitry Andric template<class ToTy>
27920b57cec5SDimitry Andric struct cast_convert_val<ToTy,
27930b57cec5SDimitry Andric                         const ::clang::DeclContext,const ::clang::DeclContext> {
27940b57cec5SDimitry Andric   static const ToTy &doit(const ::clang::DeclContext &Val) {
27950b57cec5SDimitry Andric     return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
27960b57cec5SDimitry Andric   }
27970b57cec5SDimitry Andric };
27980b57cec5SDimitry Andric 
27990b57cec5SDimitry Andric template<class ToTy>
28000b57cec5SDimitry Andric struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
28010b57cec5SDimitry Andric   static ToTy &doit(::clang::DeclContext &Val) {
28020b57cec5SDimitry Andric     return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
28030b57cec5SDimitry Andric   }
28040b57cec5SDimitry Andric };
28050b57cec5SDimitry Andric 
28060b57cec5SDimitry Andric template<class ToTy>
28070b57cec5SDimitry Andric struct cast_convert_val<ToTy,
28080b57cec5SDimitry Andric                      const ::clang::DeclContext*, const ::clang::DeclContext*> {
28090b57cec5SDimitry Andric   static const ToTy *doit(const ::clang::DeclContext *Val) {
28100b57cec5SDimitry Andric     return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
28110b57cec5SDimitry Andric   }
28120b57cec5SDimitry Andric };
28130b57cec5SDimitry Andric 
28140b57cec5SDimitry Andric template<class ToTy>
28150b57cec5SDimitry Andric struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
28160b57cec5SDimitry Andric   static ToTy *doit(::clang::DeclContext *Val) {
28170b57cec5SDimitry Andric     return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
28180b57cec5SDimitry Andric   }
28190b57cec5SDimitry Andric };
28200b57cec5SDimitry Andric 
28210b57cec5SDimitry Andric /// Implement cast_convert_val for Decl -> DeclContext conversions.
28220b57cec5SDimitry Andric template<class FromTy>
28230b57cec5SDimitry Andric struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
28240b57cec5SDimitry Andric   static ::clang::DeclContext &doit(const FromTy &Val) {
28250b57cec5SDimitry Andric     return *FromTy::castToDeclContext(&Val);
28260b57cec5SDimitry Andric   }
28270b57cec5SDimitry Andric };
28280b57cec5SDimitry Andric 
28290b57cec5SDimitry Andric template<class FromTy>
28300b57cec5SDimitry Andric struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
28310b57cec5SDimitry Andric   static ::clang::DeclContext *doit(const FromTy *Val) {
28320b57cec5SDimitry Andric     return FromTy::castToDeclContext(Val);
28330b57cec5SDimitry Andric   }
28340b57cec5SDimitry Andric };
28350b57cec5SDimitry Andric 
28360b57cec5SDimitry Andric template<class FromTy>
28370b57cec5SDimitry Andric struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
28380b57cec5SDimitry Andric   static const ::clang::DeclContext &doit(const FromTy &Val) {
28390b57cec5SDimitry Andric     return *FromTy::castToDeclContext(&Val);
28400b57cec5SDimitry Andric   }
28410b57cec5SDimitry Andric };
28420b57cec5SDimitry Andric 
28430b57cec5SDimitry Andric template<class FromTy>
28440b57cec5SDimitry Andric struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
28450b57cec5SDimitry Andric   static const ::clang::DeclContext *doit(const FromTy *Val) {
28460b57cec5SDimitry Andric     return FromTy::castToDeclContext(Val);
28470b57cec5SDimitry Andric   }
28480b57cec5SDimitry Andric };
28490b57cec5SDimitry Andric 
28500b57cec5SDimitry Andric } // namespace llvm
28510b57cec5SDimitry Andric 
28520b57cec5SDimitry Andric #endif // LLVM_CLANG_AST_DECLBASE_H
2853