1193326Sed//===-- DeclBase.h - Base Classes for representing declarations -*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed//  This file defines the Decl and DeclContext interfaces.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#ifndef LLVM_CLANG_AST_DECLBASE_H
15193326Sed#define LLVM_CLANG_AST_DECLBASE_H
16193326Sed
17249423Sdim#include "clang/AST/AttrIterator.h"
18239462Sdim#include "clang/AST/DeclarationName.h"
19202879Srdivacky#include "clang/Basic/Specifiers.h"
20234353Sdim#include "llvm/ADT/PointerUnion.h"
21234353Sdim#include "llvm/Support/Compiler.h"
22193326Sed#include "llvm/Support/PrettyStackTrace.h"
23193326Sed
24193326Sednamespace clang {
25249423Sdimclass ASTMutationListener;
26249423Sdimclass BlockDecl;
27249423Sdimclass CXXRecordDecl;
28249423Sdimclass CompoundStmt;
29193326Sedclass DeclContext;
30249423Sdimclass DeclarationName;
31249423Sdimclass DependentDiagnostic;
32249423Sdimclass EnumDecl;
33249423Sdimclass FunctionDecl;
34249423Sdimclass LinkageSpecDecl;
35249423Sdimclass Module;
36249423Sdimclass NamedDecl;
37193326Sedclass NamespaceDecl;
38249423Sdimclass ObjCCategoryDecl;
39249423Sdimclass ObjCCategoryImplDecl;
40193326Sedclass ObjCContainerDecl;
41249423Sdimclass ObjCImplDecl;
42249423Sdimclass ObjCImplementationDecl;
43193326Sedclass ObjCInterfaceDecl;
44249423Sdimclass ObjCMethodDecl;
45193326Sedclass ObjCProtocolDecl;
46249423Sdimstruct PrintingPolicy;
47249423Sdimclass Stmt;
48206084Srdivackyclass StoredDeclsMap;
49249423Sdimclass TranslationUnitDecl;
50249423Sdimclass UsingDirectiveDecl;
51193326Sed}
52193326Sed
53193326Sednamespace llvm {
54193326Sed// DeclContext* is only 4-byte aligned on 32-bit systems.
55193326Sedtemplate<>
56193326Sed  class PointerLikeTypeTraits<clang::DeclContext*> {
57193326Sed  typedef clang::DeclContext* PT;
58193326Sedpublic:
59193326Sed  static inline void *getAsVoidPointer(PT P) { return P; }
60193326Sed  static inline PT getFromVoidPointer(void *P) {
61193326Sed    return static_cast<PT>(P);
62193326Sed  }
63193326Sed  enum { NumLowBitsAvailable = 2 };
64193326Sed};
65193326Sed}
66193326Sed
67193326Sednamespace clang {
68193326Sed
69221345Sdim  /// \brief Captures the result of checking the availability of a
70221345Sdim  /// declaration.
71221345Sdim  enum AvailabilityResult {
72221345Sdim    AR_Available = 0,
73221345Sdim    AR_NotYetIntroduced,
74221345Sdim    AR_Deprecated,
75221345Sdim    AR_Unavailable
76221345Sdim  };
77221345Sdim
78198092Srdivacky/// Decl - This represents one declaration (or definition), e.g. a variable,
79198092Srdivacky/// typedef, function, struct, etc.
80193326Sed///
81193326Sedclass Decl {
82193326Sedpublic:
83193326Sed  /// \brief Lists the kind of concrete classes of Decl.
84193326Sed  enum Kind {
85210299Sed#define DECL(DERIVED, BASE) DERIVED,
86210299Sed#define ABSTRACT_DECL(DECL)
87210299Sed#define DECL_RANGE(BASE, START, END) \
88210299Sed        first##BASE = START, last##BASE = END,
89210299Sed#define LAST_DECL_RANGE(BASE, START, END) \
90210299Sed        first##BASE = START, last##BASE = END
91210299Sed#include "clang/AST/DeclNodes.inc"
92193326Sed  };
93193326Sed
94208600Srdivacky  /// \brief A placeholder type used to construct an empty shell of a
95208600Srdivacky  /// decl-derived type that will be filled in later (e.g., by some
96208600Srdivacky  /// deserialization method).
97208600Srdivacky  struct EmptyShell { };
98208600Srdivacky
99207619Srdivacky  /// IdentifierNamespace - The different namespaces in which
100207619Srdivacky  /// declarations may appear.  According to C99 6.2.3, there are
101207619Srdivacky  /// four namespaces, labels, tags, members and ordinary
102207619Srdivacky  /// identifiers.  C++ describes lookup completely differently:
103207619Srdivacky  /// certain lookups merely "ignore" certain kinds of declarations,
104207619Srdivacky  /// usually based on whether the declaration is of a type, etc.
105234353Sdim  ///
106207619Srdivacky  /// These are meant as bitmasks, so that searches in
107207619Srdivacky  /// C++ can look into the "tag" namespace during ordinary lookup.
108207619Srdivacky  ///
109210299Sed  /// Decl currently provides 15 bits of IDNS bits.
110193326Sed  enum IdentifierNamespace {
111207619Srdivacky    /// Labels, declared with 'x:' and referenced with 'goto x'.
112207619Srdivacky    IDNS_Label               = 0x0001,
113207619Srdivacky
114207619Srdivacky    /// Tags, declared with 'struct foo;' and referenced with
115207619Srdivacky    /// 'struct foo'.  All tags are also types.  This is what
116207619Srdivacky    /// elaborated-type-specifiers look for in C.
117207619Srdivacky    IDNS_Tag                 = 0x0002,
118207619Srdivacky
119207619Srdivacky    /// Types, declared with 'struct foo', typedefs, etc.
120207619Srdivacky    /// This is what elaborated-type-specifiers look for in C++,
121207619Srdivacky    /// but note that it's ill-formed to find a non-tag.
122207619Srdivacky    IDNS_Type                = 0x0004,
123207619Srdivacky
124207619Srdivacky    /// Members, declared with object declarations within tag
125207619Srdivacky    /// definitions.  In C, these can only be found by "qualified"
126207619Srdivacky    /// lookup in member expressions.  In C++, they're found by
127207619Srdivacky    /// normal lookup.
128207619Srdivacky    IDNS_Member              = 0x0008,
129207619Srdivacky
130207619Srdivacky    /// Namespaces, declared with 'namespace foo {}'.
131207619Srdivacky    /// Lookup for nested-name-specifiers find these.
132207619Srdivacky    IDNS_Namespace           = 0x0010,
133207619Srdivacky
134207619Srdivacky    /// Ordinary names.  In C, everything that's not a label, tag,
135207619Srdivacky    /// or member ends up here.
136207619Srdivacky    IDNS_Ordinary            = 0x0020,
137207619Srdivacky
138249423Sdim    /// Objective C \@protocol.
139207619Srdivacky    IDNS_ObjCProtocol        = 0x0040,
140207619Srdivacky
141207619Srdivacky    /// This declaration is a friend function.  A friend function
142207619Srdivacky    /// declaration is always in this namespace but may also be in
143207619Srdivacky    /// IDNS_Ordinary if it was previously declared.
144207619Srdivacky    IDNS_OrdinaryFriend      = 0x0080,
145207619Srdivacky
146207619Srdivacky    /// This declaration is a friend class.  A friend class
147207619Srdivacky    /// declaration is always in this namespace but may also be in
148207619Srdivacky    /// IDNS_Tag|IDNS_Type if it was previously declared.
149207619Srdivacky    IDNS_TagFriend           = 0x0100,
150207619Srdivacky
151207619Srdivacky    /// This declaration is a using declaration.  A using declaration
152207619Srdivacky    /// *introduces* a number of other declarations into the current
153207619Srdivacky    /// scope, and those declarations use the IDNS of their targets,
154207619Srdivacky    /// but the actual using declarations go in this namespace.
155207619Srdivacky    IDNS_Using               = 0x0200,
156207619Srdivacky
157207619Srdivacky    /// This declaration is a C++ operator declared in a non-class
158207619Srdivacky    /// context.  All such operators are also in IDNS_Ordinary.
159207619Srdivacky    /// C++ lexical operator lookup looks for these.
160207619Srdivacky    IDNS_NonMemberOperator   = 0x0400
161193326Sed  };
162198092Srdivacky
163221345Sdim  /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
164221345Sdim  /// parameter types in method declarations.  Other than remembering
165221345Sdim  /// them and mangling them into the method's signature string, these
166221345Sdim  /// are ignored by the compiler; they are consumed by certain
167221345Sdim  /// remote-messaging frameworks.
168221345Sdim  ///
169221345Sdim  /// in, inout, and out are mutually exclusive and apply only to
170221345Sdim  /// method parameters.  bycopy and byref are mutually exclusive and
171221345Sdim  /// apply only to method parameters (?).  oneway applies only to
172221345Sdim  /// results.  All of these expect their corresponding parameter to
173221345Sdim  /// have a particular type.  None of this is currently enforced by
174221345Sdim  /// clang.
175221345Sdim  ///
176221345Sdim  /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
177193326Sed  enum ObjCDeclQualifier {
178193326Sed    OBJC_TQ_None = 0x0,
179193326Sed    OBJC_TQ_In = 0x1,
180193326Sed    OBJC_TQ_Inout = 0x2,
181193326Sed    OBJC_TQ_Out = 0x4,
182193326Sed    OBJC_TQ_Bycopy = 0x8,
183193326Sed    OBJC_TQ_Byref = 0x10,
184193326Sed    OBJC_TQ_Oneway = 0x20
185193326Sed  };
186198092Srdivacky
187234353Sdimprotected:
188234353Sdim  // Enumeration values used in the bits stored in NextInContextAndBits.
189234353Sdim  enum {
190234353Sdim    /// \brief Whether this declaration is a top-level declaration (function,
191234353Sdim    /// global variable, etc.) that is lexically inside an objc container
192234353Sdim    /// definition.
193234353Sdim    TopLevelDeclInObjCContainerFlag = 0x01,
194234353Sdim
195234353Sdim    /// \brief Whether this declaration is private to the module in which it was
196234353Sdim    /// defined.
197234353Sdim    ModulePrivateFlag = 0x02
198234353Sdim  };
199234353Sdim
200234353Sdim  /// \brief The next declaration within the same lexical
201193326Sed  /// DeclContext. These pointers form the linked list that is
202193326Sed  /// traversed via DeclContext's decls_begin()/decls_end().
203234353Sdim  ///
204234353Sdim  /// The extra two bits are used for the TopLevelDeclInObjCContainer and
205234353Sdim  /// ModulePrivate bits.
206234353Sdim  llvm::PointerIntPair<Decl *, 2, unsigned> NextInContextAndBits;
207193326Sed
208234353Sdimprivate:
209193326Sed  friend class DeclContext;
210193326Sed
211193326Sed  struct MultipleDC {
212193326Sed    DeclContext *SemanticDC;
213193326Sed    DeclContext *LexicalDC;
214193326Sed  };
215198092Srdivacky
216198092Srdivacky
217193326Sed  /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
218193326Sed  /// For declarations that don't contain C++ scope specifiers, it contains
219193326Sed  /// the DeclContext where the Decl was declared.
220193326Sed  /// For declarations with C++ scope specifiers, it contains a MultipleDC*
221193326Sed  /// with the context where it semantically belongs (SemanticDC) and the
222193326Sed  /// context where it was lexically declared (LexicalDC).
223193326Sed  /// e.g.:
224193326Sed  ///
225193326Sed  ///   namespace A {
226193326Sed  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
227193326Sed  ///   }
228193326Sed  ///   void A::f(); // SemanticDC == namespace 'A'
229193326Sed  ///                // LexicalDC == global namespace
230193326Sed  llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
231193326Sed
232193326Sed  inline bool isInSemaDC() const    { return DeclCtx.is<DeclContext*>(); }
233193326Sed  inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
234193326Sed  inline MultipleDC *getMultipleDC() const {
235193326Sed    return DeclCtx.get<MultipleDC*>();
236193326Sed  }
237193326Sed  inline DeclContext *getSemanticDC() const {
238193326Sed    return DeclCtx.get<DeclContext*>();
239193326Sed  }
240198092Srdivacky
241218893Sdim  /// Loc - The location of this decl.
242193326Sed  SourceLocation Loc;
243198092Srdivacky
244193326Sed  /// DeclKind - This indicates which class this is.
245218893Sdim  unsigned DeclKind : 8;
246198092Srdivacky
247193326Sed  /// InvalidDecl - This indicates a semantic error occurred.
248218893Sdim  unsigned InvalidDecl :  1;
249198092Srdivacky
250193326Sed  /// HasAttrs - This indicates whether the decl has attributes or not.
251218893Sdim  unsigned HasAttrs : 1;
252193326Sed
253193326Sed  /// Implicit - Whether this declaration was implicitly generated by
254193326Sed  /// the implementation rather than explicitly written by the user.
255218893Sdim  unsigned Implicit : 1;
256193326Sed
257194613Sed  /// \brief Whether this declaration was "used", meaning that a definition is
258194613Sed  /// required.
259218893Sdim  unsigned Used : 1;
260194613Sed
261221345Sdim  /// \brief Whether this declaration was "referenced".
262221345Sdim  /// The difference with 'Used' is whether the reference appears in a
263221345Sdim  /// evaluated context or not, e.g. functions used in uninstantiated templates
264221345Sdim  /// are regarded as "referenced" but not "used".
265221345Sdim  unsigned Referenced : 1;
266234353Sdim
267234353Sdim  /// \brief Whether statistic collection is enabled.
268234353Sdim  static bool StatisticsEnabled;
269234353Sdim
270194613Sedprotected:
271198398Srdivacky  /// Access - Used by C++ decls for the access specifier.
272198398Srdivacky  // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
273198398Srdivacky  unsigned Access : 2;
274198398Srdivacky  friend class CXXClassMemberWrapper;
275212904Sdim
276226633Sdim  /// \brief Whether this declaration was loaded from an AST file.
277226633Sdim  unsigned FromASTFile : 1;
278234353Sdim
279234353Sdim  /// \brief Whether this declaration is hidden from normal name lookup, e.g.,
280234353Sdim  /// because it is was loaded from an AST file is either module-private or
281234353Sdim  /// because its submodule has not been made visible.
282234353Sdim  unsigned Hidden : 1;
283226633Sdim
284193326Sed  /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
285218893Sdim  unsigned IdentifierNamespace : 12;
286198092Srdivacky
287218893Sdim  /// \brief Whether the \c CachedLinkage field is active.
288218893Sdim  ///
289218893Sdim  /// This field is only valid for NamedDecls subclasses.
290218893Sdim  mutable unsigned HasCachedLinkage : 1;
291234353Sdim
292218893Sdim  /// \brief If \c HasCachedLinkage, the linkage of this declaration.
293218893Sdim  ///
294218893Sdim  /// This field is only valid for NamedDecls subclasses.
295218893Sdim  mutable unsigned CachedLinkage : 2;
296234353Sdim
297226633Sdim  friend class ASTDeclWriter;
298226633Sdim  friend class ASTDeclReader;
299234353Sdim  friend class ASTReader;
300226633Sdim
301194613Sedprivate:
302193326Sed  void CheckAccessDeclContext() const;
303198092Srdivacky
304193326Sedprotected:
305193326Sed
306198092Srdivacky  Decl(Kind DK, DeclContext *DC, SourceLocation L)
307234353Sdim    : NextInContextAndBits(), DeclCtx(DC),
308193326Sed      Loc(L), DeclKind(DK), InvalidDecl(0),
309221345Sdim      HasAttrs(false), Implicit(false), Used(false), Referenced(false),
310234353Sdim      Access(AS_none), FromASTFile(0), Hidden(0),
311218893Sdim      IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
312234353Sdim      HasCachedLinkage(0)
313218893Sdim  {
314234353Sdim    if (StatisticsEnabled) add(DK);
315193326Sed  }
316193326Sed
317210299Sed  Decl(Kind DK, EmptyShell Empty)
318234353Sdim    : NextInContextAndBits(), DeclKind(DK), InvalidDecl(0),
319221345Sdim      HasAttrs(false), Implicit(false), Used(false), Referenced(false),
320234353Sdim      Access(AS_none), FromASTFile(0), Hidden(0),
321218893Sdim      IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
322218893Sdim      HasCachedLinkage(0)
323218893Sdim  {
324234353Sdim    if (StatisticsEnabled) add(DK);
325210299Sed  }
326210299Sed
327193326Sed  virtual ~Decl();
328193326Sed
329234353Sdim  /// \brief Allocate memory for a deserialized declaration.
330234353Sdim  ///
331234353Sdim  /// This routine must be used to allocate memory for any declaration that is
332234353Sdim  /// deserialized from a module file.
333234353Sdim  ///
334234353Sdim  /// \param Context The context in which we will allocate memory.
335234353Sdim  /// \param ID The global ID of the deserialized declaration.
336234353Sdim  /// \param Size The size of the allocated object.
337234353Sdim  static void *AllocateDeserializedDecl(const ASTContext &Context,
338234353Sdim                                        unsigned ID,
339234353Sdim                                        unsigned Size);
340249423Sdim
341249423Sdim  /// \brief Update a potentially out-of-date declaration.
342249423Sdim  void updateOutOfDate(IdentifierInfo &II) const;
343249423Sdim
344193326Sedpublic:
345194613Sed
346194613Sed  /// \brief Source range that this declaration covers.
347234353Sdim  virtual SourceRange getSourceRange() const LLVM_READONLY {
348194613Sed    return SourceRange(getLocation(), getLocation());
349194613Sed  }
350234353Sdim  SourceLocation getLocStart() const LLVM_READONLY {
351234353Sdim    return getSourceRange().getBegin();
352234353Sdim  }
353234353Sdim  SourceLocation getLocEnd() const LLVM_READONLY {
354234353Sdim    return getSourceRange().getEnd();
355234353Sdim  }
356194613Sed
357193326Sed  SourceLocation getLocation() const { return Loc; }
358193326Sed  void setLocation(SourceLocation L) { Loc = L; }
359193326Sed
360218893Sdim  Kind getKind() const { return static_cast<Kind>(DeclKind); }
361193326Sed  const char *getDeclKindName() const;
362198092Srdivacky
363234353Sdim  Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); }
364234353Sdim  const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();}
365193326Sed
366193326Sed  DeclContext *getDeclContext() {
367193326Sed    if (isInSemaDC())
368193326Sed      return getSemanticDC();
369193326Sed    return getMultipleDC()->SemanticDC;
370193326Sed  }
371193326Sed  const DeclContext *getDeclContext() const {
372193326Sed    return const_cast<Decl*>(this)->getDeclContext();
373193326Sed  }
374195341Sed
375251662Sdim  /// Find the innermost non-closure ancestor of this declaration,
376251662Sdim  /// walking up through blocks, lambdas, etc.  If that ancestor is
377251662Sdim  /// not a code context (!isFunctionOrMethod()), returns null.
378251662Sdim  ///
379251662Sdim  /// A declaration may be its own non-closure context.
380251662Sdim  Decl *getNonClosureContext();
381251662Sdim  const Decl *getNonClosureContext() const {
382219077Sdim    return const_cast<Decl*>(this)->getNonClosureContext();
383219077Sdim  }
384219077Sdim
385195341Sed  TranslationUnitDecl *getTranslationUnitDecl();
386195341Sed  const TranslationUnitDecl *getTranslationUnitDecl() const {
387195341Sed    return const_cast<Decl*>(this)->getTranslationUnitDecl();
388195341Sed  }
389195341Sed
390198092Srdivacky  bool isInAnonymousNamespace() const;
391198092Srdivacky
392234353Sdim  ASTContext &getASTContext() const LLVM_READONLY;
393198092Srdivacky
394193326Sed  void setAccess(AccessSpecifier AS) {
395198092Srdivacky    Access = AS;
396218893Sdim#ifndef NDEBUG
397193326Sed    CheckAccessDeclContext();
398218893Sdim#endif
399193326Sed  }
400198092Srdivacky
401198092Srdivacky  AccessSpecifier getAccess() const {
402218893Sdim#ifndef NDEBUG
403193326Sed    CheckAccessDeclContext();
404218893Sdim#endif
405198092Srdivacky    return AccessSpecifier(Access);
406193326Sed  }
407193326Sed
408251662Sdim  /// \brief Retrieve the access specifier for this declaration, even though
409251662Sdim  /// it may not yet have been properly set.
410251662Sdim  AccessSpecifier getAccessUnsafe() const {
411251662Sdim    return AccessSpecifier(Access);
412251662Sdim  }
413251662Sdim
414193326Sed  bool hasAttrs() const { return HasAttrs; }
415234353Sdim  void setAttrs(const AttrVec& Attrs) {
416234353Sdim    return setAttrsImpl(Attrs, getASTContext());
417234353Sdim  }
418218893Sdim  AttrVec &getAttrs() {
419212904Sdim    return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
420193326Sed  }
421212904Sdim  const AttrVec &getAttrs() const;
422195341Sed  void swapAttrs(Decl *D);
423212904Sdim  void dropAttrs();
424193326Sed
425212904Sdim  void addAttr(Attr *A) {
426212904Sdim    if (hasAttrs())
427212904Sdim      getAttrs().push_back(A);
428212904Sdim    else
429212904Sdim      setAttrs(AttrVec(1, A));
430193326Sed  }
431198092Srdivacky
432212904Sdim  typedef AttrVec::const_iterator attr_iterator;
433212904Sdim
434212904Sdim  // FIXME: Do not rely on iterators having comparable singular values.
435212904Sdim  //        Note that this should error out if they do not.
436212904Sdim  attr_iterator attr_begin() const {
437212904Sdim    return hasAttrs() ? getAttrs().begin() : 0;
438212904Sdim  }
439212904Sdim  attr_iterator attr_end() const {
440212904Sdim    return hasAttrs() ? getAttrs().end() : 0;
441212904Sdim  }
442234353Sdim
443212904Sdim  template <typename T>
444224145Sdim  void dropAttr() {
445224145Sdim    if (!HasAttrs) return;
446234353Sdim
447243830Sdim    AttrVec &Vec = getAttrs();
448243830Sdim    Vec.erase(std::remove_if(Vec.begin(), Vec.end(), isa<T, Attr*>), Vec.end());
449243830Sdim
450243830Sdim    if (Vec.empty())
451224145Sdim      HasAttrs = false;
452224145Sdim  }
453234353Sdim
454224145Sdim  template <typename T>
455212904Sdim  specific_attr_iterator<T> specific_attr_begin() const {
456212904Sdim    return specific_attr_iterator<T>(attr_begin());
457212904Sdim  }
458212904Sdim  template <typename T>
459212904Sdim  specific_attr_iterator<T> specific_attr_end() const {
460212904Sdim    return specific_attr_iterator<T>(attr_end());
461212904Sdim  }
462212904Sdim
463212904Sdim  template<typename T> T *getAttr() const {
464212904Sdim    return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : 0;
465212904Sdim  }
466195341Sed  template<typename T> bool hasAttr() const {
467212904Sdim    return hasAttrs() && hasSpecificAttr<T>(getAttrs());
468193326Sed  }
469198092Srdivacky
470212904Sdim  /// getMaxAlignment - return the maximum alignment specified by attributes
471212904Sdim  /// on this decl, 0 if there are none.
472249423Sdim  unsigned getMaxAlignment() const;
473212904Sdim
474193326Sed  /// setInvalidDecl - Indicates the Decl had a semantic error. This
475193326Sed  /// allows for graceful error recovery.
476204793Srdivacky  void setInvalidDecl(bool Invalid = true);
477193326Sed  bool isInvalidDecl() const { return (bool) InvalidDecl; }
478193326Sed
479193326Sed  /// isImplicit - Indicates whether the declaration was implicitly
480193326Sed  /// generated by the implementation. If false, this declaration
481193326Sed  /// was written explicitly in the source code.
482193326Sed  bool isImplicit() const { return Implicit; }
483193326Sed  void setImplicit(bool I = true) { Implicit = I; }
484198092Srdivacky
485194613Sed  /// \brief Whether this declaration was used, meaning that a definition
486194613Sed  /// is required.
487210299Sed  ///
488210299Sed  /// \param CheckUsedAttr When true, also consider the "used" attribute
489210299Sed  /// (in addition to the "used" bit set by \c setUsed()) when determining
490210299Sed  /// whether the function is used.
491210299Sed  bool isUsed(bool CheckUsedAttr = true) const;
492212904Sdim
493194613Sed  void setUsed(bool U = true) { Used = U; }
494198092Srdivacky
495221345Sdim  /// \brief Whether this declaration was referenced.
496221345Sdim  bool isReferenced() const;
497221345Sdim
498221345Sdim  void setReferenced(bool R = true) { Referenced = R; }
499221345Sdim
500234353Sdim  /// \brief Whether this declaration is a top-level declaration (function,
501234353Sdim  /// global variable, etc.) that is lexically inside an objc container
502234353Sdim  /// definition.
503234353Sdim  bool isTopLevelDeclInObjCContainer() const {
504234353Sdim    return NextInContextAndBits.getInt() & TopLevelDeclInObjCContainerFlag;
505234353Sdim  }
506234353Sdim
507234353Sdim  void setTopLevelDeclInObjCContainer(bool V = true) {
508234353Sdim    unsigned Bits = NextInContextAndBits.getInt();
509234353Sdim    if (V)
510234353Sdim      Bits |= TopLevelDeclInObjCContainerFlag;
511234353Sdim    else
512234353Sdim      Bits &= ~TopLevelDeclInObjCContainerFlag;
513234353Sdim    NextInContextAndBits.setInt(Bits);
514234353Sdim  }
515234353Sdim
516234353Sdimprotected:
517234353Sdim  /// \brief Whether this declaration was marked as being private to the
518234353Sdim  /// module in which it was defined.
519234353Sdim  bool isModulePrivate() const {
520234353Sdim    return NextInContextAndBits.getInt() & ModulePrivateFlag;
521234353Sdim  }
522234353Sdim
523234353Sdim  /// \brief Specify whether this declaration was marked as being private
524234353Sdim  /// to the module in which it was defined.
525234353Sdim  void setModulePrivate(bool MP = true) {
526234353Sdim    unsigned Bits = NextInContextAndBits.getInt();
527234353Sdim    if (MP)
528234353Sdim      Bits |= ModulePrivateFlag;
529234353Sdim    else
530234353Sdim      Bits &= ~ModulePrivateFlag;
531234353Sdim    NextInContextAndBits.setInt(Bits);
532234353Sdim  }
533234353Sdim
534234353Sdim  /// \brief Set the owning module ID.
535234353Sdim  void setOwningModuleID(unsigned ID) {
536234353Sdim    assert(isFromASTFile() && "Only works on a deserialized declaration");
537234353Sdim    *((unsigned*)this - 2) = ID;
538234353Sdim  }
539234353Sdim
540234353Sdimpublic:
541234353Sdim
542221345Sdim  /// \brief Determine the availability of the given declaration.
543221345Sdim  ///
544221345Sdim  /// This routine will determine the most restrictive availability of
545221345Sdim  /// the given declaration (e.g., preferring 'unavailable' to
546221345Sdim  /// 'deprecated').
547221345Sdim  ///
548221345Sdim  /// \param Message If non-NULL and the result is not \c
549221345Sdim  /// AR_Available, will be set to a (possibly empty) message
550221345Sdim  /// describing why the declaration has not been introduced, is
551221345Sdim  /// deprecated, or is unavailable.
552221345Sdim  AvailabilityResult getAvailability(std::string *Message = 0) const;
553221345Sdim
554221345Sdim  /// \brief Determine whether this declaration is marked 'deprecated'.
555221345Sdim  ///
556221345Sdim  /// \param Message If non-NULL and the declaration is deprecated,
557221345Sdim  /// this will be set to the message describing why the declaration
558221345Sdim  /// was deprecated (which may be empty).
559221345Sdim  bool isDeprecated(std::string *Message = 0) const {
560221345Sdim    return getAvailability(Message) == AR_Deprecated;
561221345Sdim  }
562221345Sdim
563221345Sdim  /// \brief Determine whether this declaration is marked 'unavailable'.
564221345Sdim  ///
565221345Sdim  /// \param Message If non-NULL and the declaration is unavailable,
566221345Sdim  /// this will be set to the message describing why the declaration
567221345Sdim  /// was made unavailable (which may be empty).
568221345Sdim  bool isUnavailable(std::string *Message = 0) const {
569221345Sdim    return getAvailability(Message) == AR_Unavailable;
570221345Sdim  }
571221345Sdim
572221345Sdim  /// \brief Determine whether this is a weak-imported symbol.
573221345Sdim  ///
574221345Sdim  /// Weak-imported symbols are typically marked with the
575221345Sdim  /// 'weak_import' attribute, but may also be marked with an
576221345Sdim  /// 'availability' attribute where we're targing a platform prior to
577221345Sdim  /// the introduction of this feature.
578221345Sdim  bool isWeakImported() const;
579221345Sdim
580221345Sdim  /// \brief Determines whether this symbol can be weak-imported,
581221345Sdim  /// e.g., whether it would be well-formed to add the weak_import
582221345Sdim  /// attribute.
583221345Sdim  ///
584221345Sdim  /// \param IsDefinition Set to \c true to indicate that this
585221345Sdim  /// declaration cannot be weak-imported because it has a definition.
586221345Sdim  bool canBeWeakImported(bool &IsDefinition) const;
587221345Sdim
588226633Sdim  /// \brief Determine whether this declaration came from an AST file (such as
589226633Sdim  /// a precompiled header or module) rather than having been parsed.
590226633Sdim  bool isFromASTFile() const { return FromASTFile; }
591212904Sdim
592234353Sdim  /// \brief Retrieve the global declaration ID associated with this
593234353Sdim  /// declaration, which specifies where in the
594234353Sdim  unsigned getGlobalID() const {
595234353Sdim    if (isFromASTFile())
596234353Sdim      return *((const unsigned*)this - 1);
597234353Sdim    return 0;
598212904Sdim  }
599234353Sdim
600234353Sdim  /// \brief Retrieve the global ID of the module that owns this particular
601234353Sdim  /// declaration.
602234353Sdim  unsigned getOwningModuleID() const {
603234353Sdim    if (isFromASTFile())
604234353Sdim      return *((const unsigned*)this - 2);
605234353Sdim
606234353Sdim    return 0;
607234353Sdim  }
608249423Sdim
609249423Sdimprivate:
610249423Sdim  Module *getOwningModuleSlow() const;
611249423Sdim
612249423Sdimpublic:
613249423Sdim  Module *getOwningModule() const {
614249423Sdim    if (!isFromASTFile())
615249423Sdim      return 0;
616249423Sdim
617249423Sdim    return getOwningModuleSlow();
618249423Sdim  }
619249423Sdim
620193326Sed  unsigned getIdentifierNamespace() const {
621193326Sed    return IdentifierNamespace;
622193326Sed  }
623193326Sed  bool isInIdentifierNamespace(unsigned NS) const {
624193326Sed    return getIdentifierNamespace() & NS;
625193326Sed  }
626193326Sed  static unsigned getIdentifierNamespaceForKind(Kind DK);
627193326Sed
628207619Srdivacky  bool hasTagIdentifierNamespace() const {
629207619Srdivacky    return isTagIdentifierNamespace(getIdentifierNamespace());
630207619Srdivacky  }
631207619Srdivacky  static bool isTagIdentifierNamespace(unsigned NS) {
632207619Srdivacky    // TagDecls have Tag and Type set and may also have TagFriend.
633207619Srdivacky    return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
634207619Srdivacky  }
635198092Srdivacky
636193326Sed  /// getLexicalDeclContext - The declaration context where this Decl was
637193326Sed  /// lexically declared (LexicalDC). May be different from
638193326Sed  /// getDeclContext() (SemanticDC).
639193326Sed  /// e.g.:
640193326Sed  ///
641193326Sed  ///   namespace A {
642193326Sed  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
643193326Sed  ///   }
644193326Sed  ///   void A::f(); // SemanticDC == namespace 'A'
645193326Sed  ///                // LexicalDC == global namespace
646193326Sed  DeclContext *getLexicalDeclContext() {
647193326Sed    if (isInSemaDC())
648193326Sed      return getSemanticDC();
649193326Sed    return getMultipleDC()->LexicalDC;
650193326Sed  }
651193326Sed  const DeclContext *getLexicalDeclContext() const {
652193326Sed    return const_cast<Decl*>(this)->getLexicalDeclContext();
653193326Sed  }
654194613Sed
655204643Srdivacky  virtual bool isOutOfLine() const {
656194613Sed    return getLexicalDeclContext() != getDeclContext();
657194613Sed  }
658198092Srdivacky
659193326Sed  /// setDeclContext - Set both the semantic and lexical DeclContext
660193326Sed  /// to DC.
661193326Sed  void setDeclContext(DeclContext *DC);
662193326Sed
663193326Sed  void setLexicalDeclContext(DeclContext *DC);
664193326Sed
665212904Sdim  /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
666212904Sdim  /// scoped decl is defined outside the current function or method.  This is
667212904Sdim  /// roughly global variables and functions, but also handles enums (which
668212904Sdim  /// could be defined inside or outside a function etc).
669226633Sdim  bool isDefinedOutsideFunctionOrMethod() const {
670226633Sdim    return getParentFunctionOrMethod() == 0;
671226633Sdim  }
672193326Sed
673226633Sdim  /// \brief If this decl is defined inside a function/method/block it returns
674226633Sdim  /// the corresponding DeclContext, otherwise it returns null.
675226633Sdim  const DeclContext *getParentFunctionOrMethod() const;
676226633Sdim  DeclContext *getParentFunctionOrMethod() {
677226633Sdim    return const_cast<DeclContext*>(
678226633Sdim                    const_cast<const Decl*>(this)->getParentFunctionOrMethod());
679226633Sdim  }
680226633Sdim
681198092Srdivacky  /// \brief Retrieves the "canonical" declaration of the given declaration.
682198092Srdivacky  virtual Decl *getCanonicalDecl() { return this; }
683198092Srdivacky  const Decl *getCanonicalDecl() const {
684198092Srdivacky    return const_cast<Decl*>(this)->getCanonicalDecl();
685198092Srdivacky  }
686198092Srdivacky
687198092Srdivacky  /// \brief Whether this particular Decl is a canonical one.
688198092Srdivacky  bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
689234353Sdim
690198092Srdivackyprotected:
691198092Srdivacky  /// \brief Returns the next redeclaration or itself if this is the only decl.
692198092Srdivacky  ///
693198092Srdivacky  /// Decl subclasses that can be redeclared should override this method so that
694198092Srdivacky  /// Decl::redecl_iterator can iterate over them.
695198092Srdivacky  virtual Decl *getNextRedeclaration() { return this; }
696198092Srdivacky
697234353Sdim  /// \brief Implementation of getPreviousDecl(), to be overridden by any
698234353Sdim  /// subclass that has a redeclaration chain.
699234353Sdim  virtual Decl *getPreviousDeclImpl() { return 0; }
700234353Sdim
701234353Sdim  /// \brief Implementation of getMostRecentDecl(), to be overridden by any
702234353Sdim  /// subclass that has a redeclaration chain.
703234353Sdim  virtual Decl *getMostRecentDeclImpl() { return this; }
704234353Sdim
705198092Srdivackypublic:
706198092Srdivacky  /// \brief Iterates through all the redeclarations of the same decl.
707198092Srdivacky  class redecl_iterator {
708198092Srdivacky    /// Current - The current declaration.
709198092Srdivacky    Decl *Current;
710198092Srdivacky    Decl *Starter;
711198092Srdivacky
712198092Srdivacky  public:
713239462Sdim    typedef Decl *value_type;
714239462Sdim    typedef const value_type &reference;
715239462Sdim    typedef const value_type *pointer;
716198092Srdivacky    typedef std::forward_iterator_tag iterator_category;
717239462Sdim    typedef std::ptrdiff_t difference_type;
718198092Srdivacky
719198092Srdivacky    redecl_iterator() : Current(0) { }
720198092Srdivacky    explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
721198092Srdivacky
722198092Srdivacky    reference operator*() const { return Current; }
723239462Sdim    value_type operator->() const { return Current; }
724198092Srdivacky
725198092Srdivacky    redecl_iterator& operator++() {
726198092Srdivacky      assert(Current && "Advancing while iterator has reached end");
727198092Srdivacky      // Get either previous decl or latest decl.
728198092Srdivacky      Decl *Next = Current->getNextRedeclaration();
729198092Srdivacky      assert(Next && "Should return next redeclaration or itself, never null!");
730198092Srdivacky      Current = (Next != Starter ? Next : 0);
731198092Srdivacky      return *this;
732198092Srdivacky    }
733198092Srdivacky
734198092Srdivacky    redecl_iterator operator++(int) {
735198092Srdivacky      redecl_iterator tmp(*this);
736198092Srdivacky      ++(*this);
737198092Srdivacky      return tmp;
738198092Srdivacky    }
739198092Srdivacky
740198092Srdivacky    friend bool operator==(redecl_iterator x, redecl_iterator y) {
741198092Srdivacky      return x.Current == y.Current;
742198092Srdivacky    }
743198092Srdivacky    friend bool operator!=(redecl_iterator x, redecl_iterator y) {
744198092Srdivacky      return x.Current != y.Current;
745198092Srdivacky    }
746198092Srdivacky  };
747198092Srdivacky
748198092Srdivacky  /// \brief Returns iterator for all the redeclarations of the same decl.
749198092Srdivacky  /// It will iterate at least once (when this decl is the only one).
750198092Srdivacky  redecl_iterator redecls_begin() const {
751198092Srdivacky    return redecl_iterator(const_cast<Decl*>(this));
752198092Srdivacky  }
753198092Srdivacky  redecl_iterator redecls_end() const { return redecl_iterator(); }
754198092Srdivacky
755234353Sdim  /// \brief Retrieve the previous declaration that declares the same entity
756234353Sdim  /// as this declaration, or NULL if there is no previous declaration.
757234353Sdim  Decl *getPreviousDecl() { return getPreviousDeclImpl(); }
758234353Sdim
759234353Sdim  /// \brief Retrieve the most recent declaration that declares the same entity
760234353Sdim  /// as this declaration, or NULL if there is no previous declaration.
761234353Sdim  const Decl *getPreviousDecl() const {
762234353Sdim    return const_cast<Decl *>(this)->getPreviousDeclImpl();
763234353Sdim  }
764234353Sdim
765234353Sdim  /// \brief Retrieve the most recent declaration that declares the same entity
766234353Sdim  /// as this declaration (which may be this declaration).
767234353Sdim  Decl *getMostRecentDecl() { return getMostRecentDeclImpl(); }
768234353Sdim
769234353Sdim  /// \brief Retrieve the most recent declaration that declares the same entity
770234353Sdim  /// as this declaration (which may be this declaration).
771234353Sdim  const Decl *getMostRecentDecl() const {
772234353Sdim    return const_cast<Decl *>(this)->getMostRecentDeclImpl();
773234353Sdim  }
774234353Sdim
775193326Sed  /// getBody - If this Decl represents a declaration for a body of code,
776193326Sed  ///  such as a function or method definition, this method returns the
777193326Sed  ///  top-level Stmt* of that body.  Otherwise this method returns null.
778195341Sed  virtual Stmt* getBody() const { return 0; }
779193326Sed
780210299Sed  /// \brief Returns true if this Decl represents a declaration for a body of
781210299Sed  /// code, such as a function or method definition.
782210299Sed  virtual bool hasBody() const { return getBody() != 0; }
783193326Sed
784193326Sed  /// getBodyRBrace - Gets the right brace of the body, if a body exists.
785193326Sed  /// This works whether the body is a CompoundStmt or a CXXTryStmt.
786195341Sed  SourceLocation getBodyRBrace() const;
787193326Sed
788193326Sed  // global temp stats (until we have a per-module visitor)
789210299Sed  static void add(Kind k);
790234353Sdim  static void EnableStatistics();
791193326Sed  static void PrintStats();
792198092Srdivacky
793194179Sed  /// isTemplateParameter - Determines whether this declaration is a
794193326Sed  /// template parameter.
795193326Sed  bool isTemplateParameter() const;
796198092Srdivacky
797194179Sed  /// isTemplateParameter - Determines whether this declaration is a
798194179Sed  /// template parameter pack.
799194179Sed  bool isTemplateParameterPack() const;
800193326Sed
801218893Sdim  /// \brief Whether this declaration is a parameter pack.
802218893Sdim  bool isParameterPack() const;
803234353Sdim
804226633Sdim  /// \brief returns true if this declaration is a template
805226633Sdim  bool isTemplateDecl() const;
806226633Sdim
807195099Sed  /// \brief Whether this declaration is a function or function template.
808195099Sed  bool isFunctionOrFunctionTemplate() const;
809198092Srdivacky
810198092Srdivacky  /// \brief Changes the namespace of this declaration to reflect that it's
811198092Srdivacky  /// the object of a friend declaration.
812198092Srdivacky  ///
813198092Srdivacky  /// These declarations appear in the lexical context of the friending
814198092Srdivacky  /// class, but in the semantic context of the actual entity.  This property
815198092Srdivacky  /// applies only to a specific decl object;  other redeclarations of the
816198092Srdivacky  /// same entity may not (and probably don't) share this property.
817198092Srdivacky  void setObjectOfFriendDecl(bool PreviouslyDeclared) {
818198092Srdivacky    unsigned OldNS = IdentifierNamespace;
819206084Srdivacky    assert((OldNS & (IDNS_Tag | IDNS_Ordinary |
820206084Srdivacky                     IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
821206084Srdivacky           "namespace includes neither ordinary nor tag");
822207619Srdivacky    assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |
823206084Srdivacky                       IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
824206084Srdivacky           "namespace includes other than ordinary or tag");
825198092Srdivacky
826206084Srdivacky    IdentifierNamespace = 0;
827206084Srdivacky    if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
828198092Srdivacky      IdentifierNamespace |= IDNS_TagFriend;
829207619Srdivacky      if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Tag | IDNS_Type;
830206084Srdivacky    }
831206084Srdivacky
832206084Srdivacky    if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend)) {
833198092Srdivacky      IdentifierNamespace |= IDNS_OrdinaryFriend;
834206084Srdivacky      if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Ordinary;
835206084Srdivacky    }
836198092Srdivacky  }
837198092Srdivacky
838198092Srdivacky  enum FriendObjectKind {
839198092Srdivacky    FOK_None, // not a friend object
840198092Srdivacky    FOK_Declared, // a friend of a previously-declared entity
841198092Srdivacky    FOK_Undeclared // a friend of a previously-undeclared entity
842198092Srdivacky  };
843198092Srdivacky
844198092Srdivacky  /// \brief Determines whether this declaration is the object of a
845198092Srdivacky  /// friend declaration and, if so, what kind.
846198092Srdivacky  ///
847198092Srdivacky  /// There is currently no direct way to find the associated FriendDecl.
848198092Srdivacky  FriendObjectKind getFriendObjectKind() const {
849198092Srdivacky    unsigned mask
850198092Srdivacky      = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
851198092Srdivacky    if (!mask) return FOK_None;
852234353Sdim    return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ?
853198092Srdivacky              FOK_Declared : FOK_Undeclared);
854198092Srdivacky  }
855198092Srdivacky
856207619Srdivacky  /// Specifies that this declaration is a C++ overloaded non-member.
857207619Srdivacky  void setNonMemberOperator() {
858207619Srdivacky    assert(getKind() == Function || getKind() == FunctionTemplate);
859207619Srdivacky    assert((IdentifierNamespace & IDNS_Ordinary) &&
860207619Srdivacky           "visible non-member operators should be in ordinary namespace");
861207619Srdivacky    IdentifierNamespace |= IDNS_NonMemberOperator;
862207619Srdivacky  }
863207619Srdivacky
864203955Srdivacky  static bool classofKind(Kind K) { return true; }
865193326Sed  static DeclContext *castToDeclContext(const Decl *);
866193326Sed  static Decl *castFromDeclContext(const DeclContext *);
867198092Srdivacky
868226633Sdim  void print(raw_ostream &Out, unsigned Indentation = 0,
869226633Sdim             bool PrintInstantiation = false) const;
870226633Sdim  void print(raw_ostream &Out, const PrintingPolicy &Policy,
871226633Sdim             unsigned Indentation = 0, bool PrintInstantiation = false) const;
872193326Sed  static void printGroup(Decl** Begin, unsigned NumDecls,
873226633Sdim                         raw_ostream &Out, const PrintingPolicy &Policy,
874193326Sed                         unsigned Indentation = 0);
875239462Sdim  // Debuggers don't usually respect default arguments.
876234353Sdim  LLVM_ATTRIBUTE_USED void dump() const;
877249423Sdim  // Same as dump(), but forces color printing.
878249423Sdim  LLVM_ATTRIBUTE_USED void dumpColor() const;
879239462Sdim  void dump(raw_ostream &Out) const;
880239462Sdim  // Debuggers don't usually respect default arguments.
881234353Sdim  LLVM_ATTRIBUTE_USED void dumpXML() const;
882226633Sdim  void dumpXML(raw_ostream &OS) const;
883193326Sed
884193326Sedprivate:
885234353Sdim  void setAttrsImpl(const AttrVec& Attrs, ASTContext &Ctx);
886234353Sdim  void setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
887234353Sdim                           ASTContext &Ctx);
888193326Sed
889218893Sdimprotected:
890218893Sdim  ASTMutationListener *getASTMutationListener() const;
891193326Sed};
892193326Sed
893234353Sdim/// \brief Determine whether two declarations declare the same entity.
894234353Sdiminline bool declaresSameEntity(const Decl *D1, const Decl *D2) {
895234353Sdim  if (!D1 || !D2)
896234353Sdim    return false;
897234353Sdim
898234353Sdim  if (D1 == D2)
899234353Sdim    return true;
900234353Sdim
901234353Sdim  return D1->getCanonicalDecl() == D2->getCanonicalDecl();
902234353Sdim}
903234353Sdim
904193326Sed/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
905193326Sed/// doing something to a specific decl.
906193326Sedclass PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
907199990Srdivacky  const Decl *TheDecl;
908193326Sed  SourceLocation Loc;
909193326Sed  SourceManager &SM;
910193326Sed  const char *Message;
911193326Sedpublic:
912199990Srdivacky  PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
913193326Sed                       SourceManager &sm, const char *Msg)
914193326Sed  : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
915198092Srdivacky
916226633Sdim  virtual void print(raw_ostream &OS) const;
917198092Srdivacky};
918193326Sed
919249423Sdimtypedef llvm::MutableArrayRef<NamedDecl*> DeclContextLookupResult;
920198092Srdivacky
921249423Sdimtypedef ArrayRef<NamedDecl *> DeclContextLookupConstResult;
922212904Sdim
923193326Sed/// DeclContext - This is used only as base class of specific decl types that
924193326Sed/// can act as declaration contexts. These decls are (only the top classes
925193326Sed/// that directly derive from DeclContext are mentioned, not their subclasses):
926193326Sed///
927193326Sed///   TranslationUnitDecl
928193326Sed///   NamespaceDecl
929193326Sed///   FunctionDecl
930193326Sed///   TagDecl
931193326Sed///   ObjCMethodDecl
932193326Sed///   ObjCContainerDecl
933193326Sed///   LinkageSpecDecl
934193326Sed///   BlockDecl
935193326Sed///
936193326Sedclass DeclContext {
937193326Sed  /// DeclKind - This indicates which class this is.
938218893Sdim  unsigned DeclKind : 8;
939193326Sed
940193326Sed  /// \brief Whether this declaration context also has some external
941193326Sed  /// storage that contains additional declarations that are lexically
942193326Sed  /// part of this context.
943249423Sdim  mutable bool ExternalLexicalStorage : 1;
944193326Sed
945193326Sed  /// \brief Whether this declaration context also has some external
946193326Sed  /// storage that contains additional declarations that are visible
947193326Sed  /// in this context.
948249423Sdim  mutable bool ExternalVisibleStorage : 1;
949193326Sed
950249423Sdim  /// \brief Whether this declaration context has had external visible
951249423Sdim  /// storage added since the last lookup. In this case, \c LookupPtr's
952249423Sdim  /// invariant may not hold and needs to be fixed before we perform
953249423Sdim  /// another lookup.
954249423Sdim  mutable bool NeedToReconcileExternalVisibleStorage : 1;
955249423Sdim
956193326Sed  /// \brief Pointer to the data structure used to lookup declarations
957206084Srdivacky  /// within this context (or a DependentStoredDeclsMap if this is a
958234353Sdim  /// dependent context), and a bool indicating whether we have lazily
959234353Sdim  /// omitted any declarations from the map. We maintain the invariant
960249423Sdim  /// that, if the map contains an entry for a DeclarationName (and we
961249423Sdim  /// haven't lazily omitted anything), then it contains all relevant
962249423Sdim  /// entries for that name.
963234353Sdim  mutable llvm::PointerIntPair<StoredDeclsMap*, 1, bool> LookupPtr;
964193326Sed
965218893Sdimprotected:
966193326Sed  /// FirstDecl - The first declaration stored within this declaration
967193326Sed  /// context.
968193326Sed  mutable Decl *FirstDecl;
969193326Sed
970193326Sed  /// LastDecl - The last declaration stored within this declaration
971193326Sed  /// context. FIXME: We could probably cache this value somewhere
972193326Sed  /// outside of the DeclContext, to reduce the size of DeclContext by
973193326Sed  /// another pointer.
974193326Sed  mutable Decl *LastDecl;
975193326Sed
976210299Sed  friend class ExternalASTSource;
977234353Sdim  friend class ASTWriter;
978210299Sed
979218893Sdim  /// \brief Build up a chain of declarations.
980218893Sdim  ///
981218893Sdim  /// \returns the first/last pair of declarations.
982218893Sdim  static std::pair<Decl *, Decl *>
983234353Sdim  BuildDeclChain(ArrayRef<Decl*> Decls, bool FieldsAlreadyLoaded);
984218893Sdim
985249423Sdim  DeclContext(Decl::Kind K)
986249423Sdim      : DeclKind(K), ExternalLexicalStorage(false),
987249423Sdim        ExternalVisibleStorage(false),
988249423Sdim        NeedToReconcileExternalVisibleStorage(false), LookupPtr(0, false),
989249423Sdim        FirstDecl(0), LastDecl(0) {}
990193326Sed
991193326Sedpublic:
992193326Sed  ~DeclContext();
993193326Sed
994193326Sed  Decl::Kind getDeclKind() const {
995218893Sdim    return static_cast<Decl::Kind>(DeclKind);
996193326Sed  }
997193326Sed  const char *getDeclKindName() const;
998193326Sed
999193326Sed  /// getParent - Returns the containing DeclContext.
1000193326Sed  DeclContext *getParent() {
1001193326Sed    return cast<Decl>(this)->getDeclContext();
1002193326Sed  }
1003193326Sed  const DeclContext *getParent() const {
1004193326Sed    return const_cast<DeclContext*>(this)->getParent();
1005193326Sed  }
1006198092Srdivacky
1007193326Sed  /// getLexicalParent - Returns the containing lexical DeclContext. May be
1008193326Sed  /// different from getParent, e.g.:
1009193326Sed  ///
1010193326Sed  ///   namespace A {
1011193326Sed  ///      struct S;
1012193326Sed  ///   }
1013193326Sed  ///   struct A::S {}; // getParent() == namespace 'A'
1014193326Sed  ///                   // getLexicalParent() == translation unit
1015193326Sed  ///
1016193326Sed  DeclContext *getLexicalParent() {
1017193326Sed    return cast<Decl>(this)->getLexicalDeclContext();
1018193326Sed  }
1019193326Sed  const DeclContext *getLexicalParent() const {
1020193326Sed    return const_cast<DeclContext*>(this)->getLexicalParent();
1021198092Srdivacky  }
1022195341Sed
1023198092Srdivacky  DeclContext *getLookupParent();
1024234353Sdim
1025198092Srdivacky  const DeclContext *getLookupParent() const {
1026198092Srdivacky    return const_cast<DeclContext*>(this)->getLookupParent();
1027198092Srdivacky  }
1028234353Sdim
1029195341Sed  ASTContext &getParentASTContext() const {
1030195341Sed    return cast<Decl>(this)->getASTContext();
1031195341Sed  }
1032195341Sed
1033219077Sdim  bool isClosure() const {
1034219077Sdim    return DeclKind == Decl::Block;
1035219077Sdim  }
1036219077Sdim
1037226633Sdim  bool isObjCContainer() const {
1038226633Sdim    switch (DeclKind) {
1039226633Sdim        case Decl::ObjCCategory:
1040226633Sdim        case Decl::ObjCCategoryImpl:
1041226633Sdim        case Decl::ObjCImplementation:
1042226633Sdim        case Decl::ObjCInterface:
1043226633Sdim        case Decl::ObjCProtocol:
1044226633Sdim            return true;
1045226633Sdim    }
1046226633Sdim    return false;
1047226633Sdim  }
1048226633Sdim
1049193326Sed  bool isFunctionOrMethod() const {
1050193326Sed    switch (DeclKind) {
1051193326Sed    case Decl::Block:
1052251662Sdim    case Decl::Captured:
1053193326Sed    case Decl::ObjCMethod:
1054193326Sed      return true;
1055193326Sed    default:
1056210299Sed      return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction;
1057193326Sed    }
1058193326Sed  }
1059193326Sed
1060193326Sed  bool isFileContext() const {
1061193326Sed    return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
1062193326Sed  }
1063193326Sed
1064193326Sed  bool isTranslationUnit() const {
1065193326Sed    return DeclKind == Decl::TranslationUnit;
1066193326Sed  }
1067193326Sed
1068193326Sed  bool isRecord() const {
1069210299Sed    return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord;
1070193326Sed  }
1071193326Sed
1072193326Sed  bool isNamespace() const {
1073193326Sed    return DeclKind == Decl::Namespace;
1074193326Sed  }
1075193326Sed
1076212904Sdim  bool isInlineNamespace() const;
1077212904Sdim
1078193326Sed  /// \brief Determines whether this context is dependent on a
1079193326Sed  /// template parameter.
1080193326Sed  bool isDependentContext() const;
1081193326Sed
1082193326Sed  /// isTransparentContext - Determines whether this context is a
1083193326Sed  /// "transparent" context, meaning that the members declared in this
1084193326Sed  /// context are semantically declared in the nearest enclosing
1085193326Sed  /// non-transparent (opaque) context but are lexically declared in
1086193326Sed  /// this context. For example, consider the enumerators of an
1087198092Srdivacky  /// enumeration type:
1088193326Sed  /// @code
1089193326Sed  /// enum E {
1090198092Srdivacky  ///   Val1
1091193326Sed  /// };
1092193326Sed  /// @endcode
1093193326Sed  /// Here, E is a transparent context, so its enumerator (Val1) will
1094193326Sed  /// appear (semantically) that it is in the same context of E.
1095193326Sed  /// Examples of transparent contexts include: enumerations (except for
1096212904Sdim  /// C++0x scoped enums), and C++ linkage specifications.
1097193326Sed  bool isTransparentContext() const;
1098193326Sed
1099198092Srdivacky  /// \brief Determine whether this declaration context is equivalent
1100198092Srdivacky  /// to the declaration context DC.
1101212904Sdim  bool Equals(const DeclContext *DC) const {
1102205219Srdivacky    return DC && this->getPrimaryContext() == DC->getPrimaryContext();
1103193326Sed  }
1104193326Sed
1105198092Srdivacky  /// \brief Determine whether this declaration context encloses the
1106198092Srdivacky  /// declaration context DC.
1107212904Sdim  bool Encloses(const DeclContext *DC) const;
1108198092Srdivacky
1109234353Sdim  /// \brief Find the nearest non-closure ancestor of this context,
1110234353Sdim  /// i.e. the innermost semantic parent of this context which is not
1111234353Sdim  /// a closure.  A context may be its own non-closure ancestor.
1112251662Sdim  Decl *getNonClosureAncestor();
1113251662Sdim  const Decl *getNonClosureAncestor() const {
1114234353Sdim    return const_cast<DeclContext*>(this)->getNonClosureAncestor();
1115234353Sdim  }
1116234353Sdim
1117193326Sed  /// getPrimaryContext - There may be many different
1118193326Sed  /// declarations of the same entity (including forward declarations
1119193326Sed  /// of classes, multiple definitions of namespaces, etc.), each with
1120193326Sed  /// a different set of declarations. This routine returns the
1121193326Sed  /// "primary" DeclContext structure, which will contain the
1122193326Sed  /// information needed to perform name lookup into this context.
1123193326Sed  DeclContext *getPrimaryContext();
1124206084Srdivacky  const DeclContext *getPrimaryContext() const {
1125206084Srdivacky    return const_cast<DeclContext*>(this)->getPrimaryContext();
1126206084Srdivacky  }
1127193326Sed
1128212904Sdim  /// getRedeclContext - Retrieve the context in which an entity conflicts with
1129212904Sdim  /// other entities of the same name, or where it is a redeclaration if the
1130212904Sdim  /// two entities are compatible. This skips through transparent contexts.
1131212904Sdim  DeclContext *getRedeclContext();
1132212904Sdim  const DeclContext *getRedeclContext() const {
1133212904Sdim    return const_cast<DeclContext *>(this)->getRedeclContext();
1134193326Sed  }
1135198092Srdivacky
1136193326Sed  /// \brief Retrieve the nearest enclosing namespace context.
1137193326Sed  DeclContext *getEnclosingNamespaceContext();
1138193326Sed  const DeclContext *getEnclosingNamespaceContext() const {
1139193326Sed    return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
1140193326Sed  }
1141193326Sed
1142212904Sdim  /// \brief Test if this context is part of the enclosing namespace set of
1143212904Sdim  /// the context NS, as defined in C++0x [namespace.def]p9. If either context
1144212904Sdim  /// isn't a namespace, this is equivalent to Equals().
1145212904Sdim  ///
1146212904Sdim  /// The enclosing namespace set of a namespace is the namespace and, if it is
1147212904Sdim  /// inline, its enclosing namespace, recursively.
1148212904Sdim  bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
1149212904Sdim
1150239462Sdim  /// \brief Collects all of the declaration contexts that are semantically
1151234353Sdim  /// connected to this declaration context.
1152234353Sdim  ///
1153234353Sdim  /// For declaration contexts that have multiple semantically connected but
1154234353Sdim  /// syntactically distinct contexts, such as C++ namespaces, this routine
1155234353Sdim  /// retrieves the complete set of such declaration contexts in source order.
1156234353Sdim  /// For example, given:
1157234353Sdim  ///
1158234353Sdim  /// \code
1159193326Sed  /// namespace N {
1160193326Sed  ///   int x;
1161193326Sed  /// }
1162193326Sed  /// namespace N {
1163193326Sed  ///   int y;
1164193326Sed  /// }
1165234353Sdim  /// \endcode
1166234353Sdim  ///
1167234353Sdim  /// The \c Contexts parameter will contain both definitions of N.
1168234353Sdim  ///
1169234353Sdim  /// \param Contexts Will be cleared and set to the set of declaration
1170234353Sdim  /// contexts that are semanticaly connected to this declaration context,
1171234353Sdim  /// in source order, including this context (which may be the only result,
1172234353Sdim  /// for non-namespace contexts).
1173249423Sdim  void collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts);
1174193326Sed
1175193326Sed  /// decl_iterator - Iterates through the declarations stored
1176193326Sed  /// within this context.
1177193326Sed  class decl_iterator {
1178193326Sed    /// Current - The current declaration.
1179193326Sed    Decl *Current;
1180193326Sed
1181193326Sed  public:
1182239462Sdim    typedef Decl *value_type;
1183239462Sdim    typedef const value_type &reference;
1184239462Sdim    typedef const value_type *pointer;
1185193326Sed    typedef std::forward_iterator_tag iterator_category;
1186193326Sed    typedef std::ptrdiff_t            difference_type;
1187193326Sed
1188193326Sed    decl_iterator() : Current(0) { }
1189193326Sed    explicit decl_iterator(Decl *C) : Current(C) { }
1190193326Sed
1191193326Sed    reference operator*() const { return Current; }
1192239462Sdim    // This doesn't meet the iterator requirements, but it's convenient
1193239462Sdim    value_type operator->() const { return Current; }
1194193326Sed
1195193326Sed    decl_iterator& operator++() {
1196193326Sed      Current = Current->getNextDeclInContext();
1197193326Sed      return *this;
1198193326Sed    }
1199193326Sed
1200193326Sed    decl_iterator operator++(int) {
1201193326Sed      decl_iterator tmp(*this);
1202193326Sed      ++(*this);
1203193326Sed      return tmp;
1204193326Sed    }
1205193326Sed
1206198092Srdivacky    friend bool operator==(decl_iterator x, decl_iterator y) {
1207193326Sed      return x.Current == y.Current;
1208193326Sed    }
1209198092Srdivacky    friend bool operator!=(decl_iterator x, decl_iterator y) {
1210193326Sed      return x.Current != y.Current;
1211193326Sed    }
1212193326Sed  };
1213193326Sed
1214193326Sed  /// decls_begin/decls_end - Iterate over the declarations stored in
1215198092Srdivacky  /// this context.
1216195341Sed  decl_iterator decls_begin() const;
1217239462Sdim  decl_iterator decls_end() const { return decl_iterator(); }
1218195341Sed  bool decls_empty() const;
1219193326Sed
1220212904Sdim  /// noload_decls_begin/end - Iterate over the declarations stored in this
1221212904Sdim  /// context that are currently loaded; don't attempt to retrieve anything
1222212904Sdim  /// from an external source.
1223212904Sdim  decl_iterator noload_decls_begin() const;
1224239462Sdim  decl_iterator noload_decls_end() const { return decl_iterator(); }
1225212904Sdim
1226193326Sed  /// specific_decl_iterator - Iterates over a subrange of
1227193326Sed  /// declarations stored in a DeclContext, providing only those that
1228193326Sed  /// are of type SpecificDecl (or a class derived from it). This
1229193326Sed  /// iterator is used, for example, to provide iteration over just
1230193326Sed  /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
1231193326Sed  template<typename SpecificDecl>
1232193326Sed  class specific_decl_iterator {
1233193326Sed    /// Current - The current, underlying declaration iterator, which
1234193326Sed    /// will either be NULL or will point to a declaration of
1235193326Sed    /// type SpecificDecl.
1236193326Sed    DeclContext::decl_iterator Current;
1237198092Srdivacky
1238193326Sed    /// SkipToNextDecl - Advances the current position up to the next
1239193326Sed    /// declaration of type SpecificDecl that also meets the criteria
1240193326Sed    /// required by Acceptable.
1241193326Sed    void SkipToNextDecl() {
1242193326Sed      while (*Current && !isa<SpecificDecl>(*Current))
1243193326Sed        ++Current;
1244193326Sed    }
1245193326Sed
1246193326Sed  public:
1247239462Sdim    typedef SpecificDecl *value_type;
1248239462Sdim    // TODO: Add reference and pointer typedefs (with some appropriate proxy
1249239462Sdim    // type) if we ever have a need for them.
1250239462Sdim    typedef void reference;
1251239462Sdim    typedef void pointer;
1252193326Sed    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1253193326Sed      difference_type;
1254193326Sed    typedef std::forward_iterator_tag iterator_category;
1255193326Sed
1256193326Sed    specific_decl_iterator() : Current() { }
1257193326Sed
1258193326Sed    /// specific_decl_iterator - Construct a new iterator over a
1259193326Sed    /// subset of the declarations the range [C,
1260193326Sed    /// end-of-declarations). If A is non-NULL, it is a pointer to a
1261193326Sed    /// member function of SpecificDecl that should return true for
1262193326Sed    /// all of the SpecificDecl instances that will be in the subset
1263193326Sed    /// of iterators. For example, if you want Objective-C instance
1264193326Sed    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1265193326Sed    /// &ObjCMethodDecl::isInstanceMethod.
1266193326Sed    explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1267193326Sed      SkipToNextDecl();
1268193326Sed    }
1269193326Sed
1270239462Sdim    value_type operator*() const { return cast<SpecificDecl>(*Current); }
1271239462Sdim    // This doesn't meet the iterator requirements, but it's convenient
1272239462Sdim    value_type operator->() const { return **this; }
1273193326Sed
1274193326Sed    specific_decl_iterator& operator++() {
1275193326Sed      ++Current;
1276193326Sed      SkipToNextDecl();
1277193326Sed      return *this;
1278193326Sed    }
1279193326Sed
1280193326Sed    specific_decl_iterator operator++(int) {
1281193326Sed      specific_decl_iterator tmp(*this);
1282193326Sed      ++(*this);
1283193326Sed      return tmp;
1284193326Sed    }
1285198092Srdivacky
1286234353Sdim    friend bool operator==(const specific_decl_iterator& x,
1287234353Sdim                           const specific_decl_iterator& y) {
1288193326Sed      return x.Current == y.Current;
1289193326Sed    }
1290198092Srdivacky
1291234353Sdim    friend bool operator!=(const specific_decl_iterator& x,
1292234353Sdim                           const specific_decl_iterator& y) {
1293193326Sed      return x.Current != y.Current;
1294193326Sed    }
1295193326Sed  };
1296193326Sed
1297193326Sed  /// \brief Iterates over a filtered subrange of declarations stored
1298193326Sed  /// in a DeclContext.
1299193326Sed  ///
1300193326Sed  /// This iterator visits only those declarations that are of type
1301193326Sed  /// SpecificDecl (or a class derived from it) and that meet some
1302193326Sed  /// additional run-time criteria. This iterator is used, for
1303193326Sed  /// example, to provide access to the instance methods within an
1304193326Sed  /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
1305193326Sed  /// Acceptable = ObjCMethodDecl::isInstanceMethod).
1306193326Sed  template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
1307193326Sed  class filtered_decl_iterator {
1308193326Sed    /// Current - The current, underlying declaration iterator, which
1309193326Sed    /// will either be NULL or will point to a declaration of
1310193326Sed    /// type SpecificDecl.
1311193326Sed    DeclContext::decl_iterator Current;
1312198092Srdivacky
1313193326Sed    /// SkipToNextDecl - Advances the current position up to the next
1314193326Sed    /// declaration of type SpecificDecl that also meets the criteria
1315193326Sed    /// required by Acceptable.
1316193326Sed    void SkipToNextDecl() {
1317193326Sed      while (*Current &&
1318193326Sed             (!isa<SpecificDecl>(*Current) ||
1319193326Sed              (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
1320193326Sed        ++Current;
1321193326Sed    }
1322193326Sed
1323193326Sed  public:
1324239462Sdim    typedef SpecificDecl *value_type;
1325239462Sdim    // TODO: Add reference and pointer typedefs (with some appropriate proxy
1326239462Sdim    // type) if we ever have a need for them.
1327239462Sdim    typedef void reference;
1328239462Sdim    typedef void pointer;
1329193326Sed    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1330193326Sed      difference_type;
1331193326Sed    typedef std::forward_iterator_tag iterator_category;
1332193326Sed
1333193326Sed    filtered_decl_iterator() : Current() { }
1334193326Sed
1335239462Sdim    /// filtered_decl_iterator - Construct a new iterator over a
1336193326Sed    /// subset of the declarations the range [C,
1337193326Sed    /// end-of-declarations). If A is non-NULL, it is a pointer to a
1338193326Sed    /// member function of SpecificDecl that should return true for
1339193326Sed    /// all of the SpecificDecl instances that will be in the subset
1340193326Sed    /// of iterators. For example, if you want Objective-C instance
1341193326Sed    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1342193326Sed    /// &ObjCMethodDecl::isInstanceMethod.
1343193326Sed    explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1344193326Sed      SkipToNextDecl();
1345193326Sed    }
1346193326Sed
1347239462Sdim    value_type operator*() const { return cast<SpecificDecl>(*Current); }
1348239462Sdim    value_type operator->() const { return cast<SpecificDecl>(*Current); }
1349193326Sed
1350193326Sed    filtered_decl_iterator& operator++() {
1351193326Sed      ++Current;
1352193326Sed      SkipToNextDecl();
1353193326Sed      return *this;
1354193326Sed    }
1355193326Sed
1356193326Sed    filtered_decl_iterator operator++(int) {
1357193326Sed      filtered_decl_iterator tmp(*this);
1358193326Sed      ++(*this);
1359193326Sed      return tmp;
1360193326Sed    }
1361198092Srdivacky
1362234353Sdim    friend bool operator==(const filtered_decl_iterator& x,
1363234353Sdim                           const filtered_decl_iterator& y) {
1364193326Sed      return x.Current == y.Current;
1365193326Sed    }
1366198092Srdivacky
1367234353Sdim    friend bool operator!=(const filtered_decl_iterator& x,
1368234353Sdim                           const filtered_decl_iterator& y) {
1369193326Sed      return x.Current != y.Current;
1370193326Sed    }
1371193326Sed  };
1372193326Sed
1373193326Sed  /// @brief Add the declaration D into this context.
1374193326Sed  ///
1375193326Sed  /// This routine should be invoked when the declaration D has first
1376193326Sed  /// been declared, to place D into the context where it was
1377193326Sed  /// (lexically) defined. Every declaration must be added to one
1378193326Sed  /// (and only one!) context, where it can be visited via
1379193326Sed  /// [decls_begin(), decls_end()). Once a declaration has been added
1380193326Sed  /// to its lexical context, the corresponding DeclContext owns the
1381193326Sed  /// declaration.
1382193326Sed  ///
1383193326Sed  /// If D is also a NamedDecl, it will be made visible within its
1384193326Sed  /// semantic context via makeDeclVisibleInContext.
1385195341Sed  void addDecl(Decl *D);
1386193326Sed
1387234353Sdim  /// @brief Add the declaration D into this context, but suppress
1388234353Sdim  /// searches for external declarations with the same name.
1389234353Sdim  ///
1390234353Sdim  /// Although analogous in function to addDecl, this removes an
1391234353Sdim  /// important check.  This is only useful if the Decl is being
1392234353Sdim  /// added in response to an external search; in all other cases,
1393234353Sdim  /// addDecl() is the right function to use.
1394234353Sdim  /// See the ASTImporter for use cases.
1395234353Sdim  void addDeclInternal(Decl *D);
1396234353Sdim
1397198092Srdivacky  /// @brief Add the declaration D to this context without modifying
1398198092Srdivacky  /// any lookup tables.
1399198092Srdivacky  ///
1400198092Srdivacky  /// This is useful for some operations in dependent contexts where
1401198092Srdivacky  /// the semantic context might not be dependent;  this basically
1402198092Srdivacky  /// only happens with friends.
1403198092Srdivacky  void addHiddenDecl(Decl *D);
1404198092Srdivacky
1405200583Srdivacky  /// @brief Removes a declaration from this context.
1406200583Srdivacky  void removeDecl(Decl *D);
1407251662Sdim
1408251662Sdim  /// @brief Checks whether a declaration is in this context.
1409251662Sdim  bool containsDecl(Decl *D) const;
1410200583Srdivacky
1411193326Sed  /// lookup_iterator - An iterator that provides access to the results
1412193326Sed  /// of looking up a name within this context.
1413193326Sed  typedef NamedDecl **lookup_iterator;
1414193326Sed
1415193326Sed  /// lookup_const_iterator - An iterator that provides non-mutable
1416193326Sed  /// access to the results of lookup up a name within this context.
1417193326Sed  typedef NamedDecl * const * lookup_const_iterator;
1418193326Sed
1419212904Sdim  typedef DeclContextLookupResult lookup_result;
1420212904Sdim  typedef DeclContextLookupConstResult lookup_const_result;
1421193326Sed
1422193326Sed  /// lookup - Find the declarations (if any) with the given Name in
1423193326Sed  /// this context. Returns a range of iterators that contains all of
1424193326Sed  /// the declarations with this name, with object, function, member,
1425193326Sed  /// and enumerator names preceding any tag name. Note that this
1426193326Sed  /// routine will not look into parent contexts.
1427195341Sed  lookup_result lookup(DeclarationName Name);
1428239462Sdim  lookup_const_result lookup(DeclarationName Name) const {
1429239462Sdim    return const_cast<DeclContext*>(this)->lookup(Name);
1430239462Sdim  }
1431193326Sed
1432226633Sdim  /// \brief A simplistic name lookup mechanism that performs name lookup
1433226633Sdim  /// into this declaration context without consulting the external source.
1434226633Sdim  ///
1435234353Sdim  /// This function should almost never be used, because it subverts the
1436226633Sdim  /// usual relationship between a DeclContext and the external source.
1437226633Sdim  /// See the ASTImporter for the (few, but important) use cases.
1438234353Sdim  void localUncachedLookup(DeclarationName Name,
1439249423Sdim                           SmallVectorImpl<NamedDecl *> &Results);
1440234353Sdim
1441193326Sed  /// @brief Makes a declaration visible within this context.
1442193326Sed  ///
1443193326Sed  /// This routine makes the declaration D visible to name lookup
1444193326Sed  /// within this context and, if this is a transparent context,
1445193326Sed  /// within its parent contexts up to the first enclosing
1446193326Sed  /// non-transparent context. Making a declaration visible within a
1447193326Sed  /// context does not transfer ownership of a declaration, and a
1448193326Sed  /// declaration can be visible in many contexts that aren't its
1449193326Sed  /// lexical context.
1450193326Sed  ///
1451193326Sed  /// If D is a redeclaration of an existing declaration that is
1452193326Sed  /// visible from this context, as determined by
1453193326Sed  /// NamedDecl::declarationReplaces, the previous declaration will be
1454193326Sed  /// replaced with D.
1455234353Sdim  void makeDeclVisibleInContext(NamedDecl *D);
1456193326Sed
1457234353Sdim  /// all_lookups_iterator - An iterator that provides a view over the results
1458234353Sdim  /// of looking up every possible name.
1459234353Sdim  class all_lookups_iterator;
1460234353Sdim
1461234353Sdim  all_lookups_iterator lookups_begin() const;
1462234353Sdim
1463234353Sdim  all_lookups_iterator lookups_end() const;
1464234353Sdim
1465193326Sed  /// udir_iterator - Iterates through the using-directives stored
1466193326Sed  /// within this context.
1467193326Sed  typedef UsingDirectiveDecl * const * udir_iterator;
1468198092Srdivacky
1469193326Sed  typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
1470193326Sed
1471195341Sed  udir_iterator_range getUsingDirectives() const;
1472193326Sed
1473195341Sed  udir_iterator using_directives_begin() const {
1474195341Sed    return getUsingDirectives().first;
1475193326Sed  }
1476193326Sed
1477195341Sed  udir_iterator using_directives_end() const {
1478195341Sed    return getUsingDirectives().second;
1479193326Sed  }
1480193326Sed
1481206084Srdivacky  // These are all defined in DependentDiagnostic.h.
1482206084Srdivacky  class ddiag_iterator;
1483206084Srdivacky  inline ddiag_iterator ddiag_begin() const;
1484206084Srdivacky  inline ddiag_iterator ddiag_end() const;
1485206084Srdivacky
1486193326Sed  // Low-level accessors
1487243830Sdim
1488243830Sdim  /// \brief Mark the lookup table as needing to be built.  This should be
1489249423Sdim  /// used only if setHasExternalLexicalStorage() has been called on any
1490249423Sdim  /// decl context for which this is the primary context.
1491243830Sdim  void setMustBuildLookupTable() {
1492243830Sdim    LookupPtr.setInt(true);
1493243830Sdim  }
1494193326Sed
1495193326Sed  /// \brief Retrieve the internal representation of the lookup structure.
1496234353Sdim  /// This may omit some names if we are lazily building the structure.
1497234353Sdim  StoredDeclsMap *getLookupPtr() const { return LookupPtr.getPointer(); }
1498193326Sed
1499234353Sdim  /// \brief Ensure the lookup structure is fully-built and return it.
1500234353Sdim  StoredDeclsMap *buildLookup();
1501234353Sdim
1502193326Sed  /// \brief Whether this DeclContext has external storage containing
1503193326Sed  /// additional declarations that are lexically in this context.
1504193326Sed  bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
1505193326Sed
1506193326Sed  /// \brief State whether this DeclContext has external storage for
1507193326Sed  /// declarations lexically in this context.
1508198092Srdivacky  void setHasExternalLexicalStorage(bool ES = true) {
1509198092Srdivacky    ExternalLexicalStorage = ES;
1510193326Sed  }
1511193326Sed
1512193326Sed  /// \brief Whether this DeclContext has external storage containing
1513193326Sed  /// additional declarations that are visible in this context.
1514193326Sed  bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
1515193326Sed
1516193326Sed  /// \brief State whether this DeclContext has external storage for
1517193326Sed  /// declarations visible in this context.
1518198092Srdivacky  void setHasExternalVisibleStorage(bool ES = true) {
1519198092Srdivacky    ExternalVisibleStorage = ES;
1520249423Sdim    if (ES && LookupPtr.getPointer())
1521249423Sdim      NeedToReconcileExternalVisibleStorage = true;
1522193326Sed  }
1523193326Sed
1524226633Sdim  /// \brief Determine whether the given declaration is stored in the list of
1525226633Sdim  /// declarations lexically within this context.
1526226633Sdim  bool isDeclInLexicalTraversal(const Decl *D) const {
1527234353Sdim    return D && (D->NextInContextAndBits.getPointer() || D == FirstDecl ||
1528234353Sdim                 D == LastDecl);
1529226633Sdim  }
1530234353Sdim
1531193326Sed  static bool classof(const Decl *D);
1532193326Sed  static bool classof(const DeclContext *D) { return true; }
1533193326Sed
1534234353Sdim  LLVM_ATTRIBUTE_USED void dumpDeclContext() const;
1535200583Srdivacky
1536193326Sedprivate:
1537249423Sdim  void reconcileExternalVisibleStorage();
1538195341Sed  void LoadLexicalDeclsFromExternalStorage() const;
1539193326Sed
1540234353Sdim  /// @brief Makes a declaration visible within this context, but
1541234353Sdim  /// suppresses searches for external declarations with the same
1542234353Sdim  /// name.
1543234353Sdim  ///
1544234353Sdim  /// Analogous to makeDeclVisibleInContext, but for the exclusive
1545234353Sdim  /// use of addDeclInternal().
1546234353Sdim  void makeDeclVisibleInContextInternal(NamedDecl *D);
1547234353Sdim
1548206084Srdivacky  friend class DependentDiagnostic;
1549206084Srdivacky  StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
1550206084Srdivacky
1551234353Sdim  void buildLookupImpl(DeclContext *DCtx);
1552234353Sdim  void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1553234353Sdim                                         bool Rediscoverable);
1554234353Sdim  void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal);
1555193326Sed};
1556193326Sed
1557193326Sedinline bool Decl::isTemplateParameter() const {
1558202879Srdivacky  return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
1559202879Srdivacky         getKind() == TemplateTemplateParm;
1560193326Sed}
1561193326Sed
1562203955Srdivacky// Specialization selected when ToTy is not a known subclass of DeclContext.
1563203955Srdivackytemplate <class ToTy,
1564203955Srdivacky          bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value>
1565203955Srdivackystruct cast_convert_decl_context {
1566203955Srdivacky  static const ToTy *doit(const DeclContext *Val) {
1567203955Srdivacky    return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
1568203955Srdivacky  }
1569203955Srdivacky
1570203955Srdivacky  static ToTy *doit(DeclContext *Val) {
1571203955Srdivacky    return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
1572203955Srdivacky  }
1573203955Srdivacky};
1574203955Srdivacky
1575203955Srdivacky// Specialization selected when ToTy is a known subclass of DeclContext.
1576203955Srdivackytemplate <class ToTy>
1577203955Srdivackystruct cast_convert_decl_context<ToTy, true> {
1578203955Srdivacky  static const ToTy *doit(const DeclContext *Val) {
1579203955Srdivacky    return static_cast<const ToTy*>(Val);
1580203955Srdivacky  }
1581203955Srdivacky
1582203955Srdivacky  static ToTy *doit(DeclContext *Val) {
1583203955Srdivacky    return static_cast<ToTy*>(Val);
1584203955Srdivacky  }
1585203955Srdivacky};
1586203955Srdivacky
1587203955Srdivacky
1588193326Sed} // end clang.
1589193326Sed
1590193326Sednamespace llvm {
1591193326Sed
1592203955Srdivacky/// isa<T>(DeclContext*)
1593223017Sdimtemplate <typename To>
1594223017Sdimstruct isa_impl<To, ::clang::DeclContext> {
1595193326Sed  static bool doit(const ::clang::DeclContext &Val) {
1596223017Sdim    return To::classofKind(Val.getDeclKind());
1597193326Sed  }
1598193326Sed};
1599193326Sed
1600203955Srdivacky/// cast<T>(DeclContext*)
1601203955Srdivackytemplate<class ToTy>
1602203955Srdivackystruct cast_convert_val<ToTy,
1603203955Srdivacky                        const ::clang::DeclContext,const ::clang::DeclContext> {
1604203955Srdivacky  static const ToTy &doit(const ::clang::DeclContext &Val) {
1605203955Srdivacky    return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1606203955Srdivacky  }
1607203955Srdivacky};
1608203955Srdivackytemplate<class ToTy>
1609203955Srdivackystruct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
1610203955Srdivacky  static ToTy &doit(::clang::DeclContext &Val) {
1611203955Srdivacky    return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1612203955Srdivacky  }
1613203955Srdivacky};
1614203955Srdivackytemplate<class ToTy>
1615203955Srdivackystruct cast_convert_val<ToTy,
1616203955Srdivacky                     const ::clang::DeclContext*, const ::clang::DeclContext*> {
1617203955Srdivacky  static const ToTy *doit(const ::clang::DeclContext *Val) {
1618203955Srdivacky    return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1619203955Srdivacky  }
1620203955Srdivacky};
1621203955Srdivackytemplate<class ToTy>
1622203955Srdivackystruct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
1623203955Srdivacky  static ToTy *doit(::clang::DeclContext *Val) {
1624203955Srdivacky    return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1625203955Srdivacky  }
1626203955Srdivacky};
1627203955Srdivacky
1628193326Sed/// Implement cast_convert_val for Decl -> DeclContext conversions.
1629193326Sedtemplate<class FromTy>
1630193326Sedstruct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
1631193326Sed  static ::clang::DeclContext &doit(const FromTy &Val) {
1632193326Sed    return *FromTy::castToDeclContext(&Val);
1633193326Sed  }
1634193326Sed};
1635193326Sed
1636193326Sedtemplate<class FromTy>
1637193326Sedstruct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
1638193326Sed  static ::clang::DeclContext *doit(const FromTy *Val) {
1639193326Sed    return FromTy::castToDeclContext(Val);
1640193326Sed  }
1641193326Sed};
1642193326Sed
1643193326Sedtemplate<class FromTy>
1644193326Sedstruct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
1645193326Sed  static const ::clang::DeclContext &doit(const FromTy &Val) {
1646193326Sed    return *FromTy::castToDeclContext(&Val);
1647193326Sed  }
1648193326Sed};
1649193326Sed
1650193326Sedtemplate<class FromTy>
1651193326Sedstruct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
1652193326Sed  static const ::clang::DeclContext *doit(const FromTy *Val) {
1653193326Sed    return FromTy::castToDeclContext(Val);
1654193326Sed  }
1655193326Sed};
1656193326Sed
1657193326Sed} // end namespace llvm
1658193326Sed
1659193326Sed#endif
1660