DeclBase.h revision 0a0d2b179085a52c10402feebeb6db8b4d96a140
1//===-- DeclBase.h - Base Classes for representing declarations -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the Decl and DeclContext interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_DECLBASE_H
15#define LLVM_CLANG_AST_DECLBASE_H
16
17#include "clang/AST/Attr.h"
18#include "clang/AST/Type.h"
19#include "clang/Basic/Specifiers.h"
20#include "llvm/Support/PrettyStackTrace.h"
21#include "llvm/ADT/PointerUnion.h"
22
23namespace clang {
24class DeclContext;
25class TranslationUnitDecl;
26class NamespaceDecl;
27class UsingDirectiveDecl;
28class NamedDecl;
29class FunctionDecl;
30class CXXRecordDecl;
31class EnumDecl;
32class ObjCMethodDecl;
33class ObjCContainerDecl;
34class ObjCInterfaceDecl;
35class ObjCCategoryDecl;
36class ObjCProtocolDecl;
37class ObjCImplementationDecl;
38class ObjCCategoryImplDecl;
39class ObjCImplDecl;
40class LinkageSpecDecl;
41class BlockDecl;
42class DeclarationName;
43class CompoundStmt;
44class StoredDeclsMap;
45class DependentDiagnostic;
46class ASTMutationListener;
47}
48
49namespace llvm {
50// DeclContext* is only 4-byte aligned on 32-bit systems.
51template<>
52  class PointerLikeTypeTraits<clang::DeclContext*> {
53  typedef clang::DeclContext* PT;
54public:
55  static inline void *getAsVoidPointer(PT P) { return P; }
56  static inline PT getFromVoidPointer(void *P) {
57    return static_cast<PT>(P);
58  }
59  enum { NumLowBitsAvailable = 2 };
60};
61}
62
63namespace clang {
64
65  /// \brief Captures the result of checking the availability of a
66  /// declaration.
67  enum AvailabilityResult {
68    AR_Available = 0,
69    AR_NotYetIntroduced,
70    AR_Deprecated,
71    AR_Unavailable
72  };
73
74/// Decl - This represents one declaration (or definition), e.g. a variable,
75/// typedef, function, struct, etc.
76///
77class Decl {
78public:
79  /// \brief Lists the kind of concrete classes of Decl.
80  enum Kind {
81#define DECL(DERIVED, BASE) DERIVED,
82#define ABSTRACT_DECL(DECL)
83#define DECL_RANGE(BASE, START, END) \
84        first##BASE = START, last##BASE = END,
85#define LAST_DECL_RANGE(BASE, START, END) \
86        first##BASE = START, last##BASE = END
87#include "clang/AST/DeclNodes.inc"
88  };
89
90  /// \brief A placeholder type used to construct an empty shell of a
91  /// decl-derived type that will be filled in later (e.g., by some
92  /// deserialization method).
93  struct EmptyShell { };
94
95  /// IdentifierNamespace - The different namespaces in which
96  /// declarations may appear.  According to C99 6.2.3, there are
97  /// four namespaces, labels, tags, members and ordinary
98  /// identifiers.  C++ describes lookup completely differently:
99  /// certain lookups merely "ignore" certain kinds of declarations,
100  /// usually based on whether the declaration is of a type, etc.
101  ///
102  /// These are meant as bitmasks, so that searches in
103  /// C++ can look into the "tag" namespace during ordinary lookup.
104  ///
105  /// Decl currently provides 15 bits of IDNS bits.
106  enum IdentifierNamespace {
107    /// Labels, declared with 'x:' and referenced with 'goto x'.
108    IDNS_Label               = 0x0001,
109
110    /// Tags, declared with 'struct foo;' and referenced with
111    /// 'struct foo'.  All tags are also types.  This is what
112    /// elaborated-type-specifiers look for in C.
113    IDNS_Tag                 = 0x0002,
114
115    /// Types, declared with 'struct foo', typedefs, etc.
116    /// This is what elaborated-type-specifiers look for in C++,
117    /// but note that it's ill-formed to find a non-tag.
118    IDNS_Type                = 0x0004,
119
120    /// Members, declared with object declarations within tag
121    /// definitions.  In C, these can only be found by "qualified"
122    /// lookup in member expressions.  In C++, they're found by
123    /// normal lookup.
124    IDNS_Member              = 0x0008,
125
126    /// Namespaces, declared with 'namespace foo {}'.
127    /// Lookup for nested-name-specifiers find these.
128    IDNS_Namespace           = 0x0010,
129
130    /// Ordinary names.  In C, everything that's not a label, tag,
131    /// or member ends up here.
132    IDNS_Ordinary            = 0x0020,
133
134    /// Objective C @protocol.
135    IDNS_ObjCProtocol        = 0x0040,
136
137    /// This declaration is a friend function.  A friend function
138    /// declaration is always in this namespace but may also be in
139    /// IDNS_Ordinary if it was previously declared.
140    IDNS_OrdinaryFriend      = 0x0080,
141
142    /// This declaration is a friend class.  A friend class
143    /// declaration is always in this namespace but may also be in
144    /// IDNS_Tag|IDNS_Type if it was previously declared.
145    IDNS_TagFriend           = 0x0100,
146
147    /// This declaration is a using declaration.  A using declaration
148    /// *introduces* a number of other declarations into the current
149    /// scope, and those declarations use the IDNS of their targets,
150    /// but the actual using declarations go in this namespace.
151    IDNS_Using               = 0x0200,
152
153    /// This declaration is a C++ operator declared in a non-class
154    /// context.  All such operators are also in IDNS_Ordinary.
155    /// C++ lexical operator lookup looks for these.
156    IDNS_NonMemberOperator   = 0x0400
157  };
158
159  /// ObjCDeclQualifier - Qualifier used on types in method declarations
160  /// for remote messaging. They are meant for the arguments though and
161  /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
162  enum ObjCDeclQualifier {
163    OBJC_TQ_None = 0x0,
164    OBJC_TQ_In = 0x1,
165    OBJC_TQ_Inout = 0x2,
166    OBJC_TQ_Out = 0x4,
167    OBJC_TQ_Bycopy = 0x8,
168    OBJC_TQ_Byref = 0x10,
169    OBJC_TQ_Oneway = 0x20
170  };
171
172private:
173  /// NextDeclInContext - The next declaration within the same lexical
174  /// DeclContext. These pointers form the linked list that is
175  /// traversed via DeclContext's decls_begin()/decls_end().
176  Decl *NextDeclInContext;
177
178  friend class DeclContext;
179
180  struct MultipleDC {
181    DeclContext *SemanticDC;
182    DeclContext *LexicalDC;
183  };
184
185
186  /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
187  /// For declarations that don't contain C++ scope specifiers, it contains
188  /// the DeclContext where the Decl was declared.
189  /// For declarations with C++ scope specifiers, it contains a MultipleDC*
190  /// with the context where it semantically belongs (SemanticDC) and the
191  /// context where it was lexically declared (LexicalDC).
192  /// e.g.:
193  ///
194  ///   namespace A {
195  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
196  ///   }
197  ///   void A::f(); // SemanticDC == namespace 'A'
198  ///                // LexicalDC == global namespace
199  llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
200
201  inline bool isInSemaDC() const    { return DeclCtx.is<DeclContext*>(); }
202  inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
203  inline MultipleDC *getMultipleDC() const {
204    return DeclCtx.get<MultipleDC*>();
205  }
206  inline DeclContext *getSemanticDC() const {
207    return DeclCtx.get<DeclContext*>();
208  }
209
210  /// Loc - The location of this decl.
211  SourceLocation Loc;
212
213  /// DeclKind - This indicates which class this is.
214  unsigned DeclKind : 8;
215
216  /// InvalidDecl - This indicates a semantic error occurred.
217  unsigned InvalidDecl :  1;
218
219  /// HasAttrs - This indicates whether the decl has attributes or not.
220  unsigned HasAttrs : 1;
221
222  /// Implicit - Whether this declaration was implicitly generated by
223  /// the implementation rather than explicitly written by the user.
224  unsigned Implicit : 1;
225
226  /// \brief Whether this declaration was "used", meaning that a definition is
227  /// required.
228  unsigned Used : 1;
229
230protected:
231  /// Access - Used by C++ decls for the access specifier.
232  // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
233  unsigned Access : 2;
234  friend class CXXClassMemberWrapper;
235
236  /// PCHLevel - the "level" of AST file from which this declaration was built.
237  unsigned PCHLevel : 2;
238
239  /// ChangedAfterLoad - if this declaration has changed since being loaded
240  unsigned ChangedAfterLoad : 1;
241
242  /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
243  unsigned IdentifierNamespace : 12;
244
245  /// \brief Whether the \c CachedLinkage field is active.
246  ///
247  /// This field is only valid for NamedDecls subclasses.
248  mutable unsigned HasCachedLinkage : 1;
249
250  /// \brief If \c HasCachedLinkage, the linkage of this declaration.
251  ///
252  /// This field is only valid for NamedDecls subclasses.
253  mutable unsigned CachedLinkage : 2;
254
255
256private:
257  void CheckAccessDeclContext() const;
258
259protected:
260
261  Decl(Kind DK, DeclContext *DC, SourceLocation L)
262    : NextDeclInContext(0), DeclCtx(DC),
263      Loc(L), DeclKind(DK), InvalidDecl(0),
264      HasAttrs(false), Implicit(false), Used(false),
265      Access(AS_none), PCHLevel(0), ChangedAfterLoad(false),
266      IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
267      HasCachedLinkage(0)
268  {
269    if (Decl::CollectingStats()) add(DK);
270  }
271
272  Decl(Kind DK, EmptyShell Empty)
273    : NextDeclInContext(0), DeclKind(DK), InvalidDecl(0),
274      HasAttrs(false), Implicit(false), Used(false),
275      Access(AS_none), PCHLevel(0), ChangedAfterLoad(false),
276      IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
277      HasCachedLinkage(0)
278  {
279    if (Decl::CollectingStats()) add(DK);
280  }
281
282  virtual ~Decl();
283
284public:
285
286  /// \brief Source range that this declaration covers.
287  virtual SourceRange getSourceRange() const {
288    return SourceRange(getLocation(), getLocation());
289  }
290  SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
291  SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
292
293  SourceLocation getLocation() const { return Loc; }
294  void setLocation(SourceLocation L) { Loc = L; }
295
296  Kind getKind() const { return static_cast<Kind>(DeclKind); }
297  const char *getDeclKindName() const;
298
299  Decl *getNextDeclInContext() { return NextDeclInContext; }
300  const Decl *getNextDeclInContext() const { return NextDeclInContext; }
301
302  DeclContext *getDeclContext() {
303    if (isInSemaDC())
304      return getSemanticDC();
305    return getMultipleDC()->SemanticDC;
306  }
307  const DeclContext *getDeclContext() const {
308    return const_cast<Decl*>(this)->getDeclContext();
309  }
310
311  /// Finds the innermost non-closure context of this declaration.
312  /// That is, walk out the DeclContext chain, skipping any blocks.
313  DeclContext *getNonClosureContext();
314  const DeclContext *getNonClosureContext() const {
315    return const_cast<Decl*>(this)->getNonClosureContext();
316  }
317
318  TranslationUnitDecl *getTranslationUnitDecl();
319  const TranslationUnitDecl *getTranslationUnitDecl() const {
320    return const_cast<Decl*>(this)->getTranslationUnitDecl();
321  }
322
323  bool isInAnonymousNamespace() const;
324
325  ASTContext &getASTContext() const;
326
327  void setAccess(AccessSpecifier AS) {
328    Access = AS;
329#ifndef NDEBUG
330    CheckAccessDeclContext();
331#endif
332  }
333
334  AccessSpecifier getAccess() const {
335#ifndef NDEBUG
336    CheckAccessDeclContext();
337#endif
338    return AccessSpecifier(Access);
339  }
340
341  bool hasAttrs() const { return HasAttrs; }
342  void setAttrs(const AttrVec& Attrs);
343  AttrVec &getAttrs() {
344    return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
345  }
346  const AttrVec &getAttrs() const;
347  void swapAttrs(Decl *D);
348  void dropAttrs();
349
350  void addAttr(Attr *A) {
351    if (hasAttrs())
352      getAttrs().push_back(A);
353    else
354      setAttrs(AttrVec(1, A));
355  }
356
357  typedef AttrVec::const_iterator attr_iterator;
358
359  // FIXME: Do not rely on iterators having comparable singular values.
360  //        Note that this should error out if they do not.
361  attr_iterator attr_begin() const {
362    return hasAttrs() ? getAttrs().begin() : 0;
363  }
364  attr_iterator attr_end() const {
365    return hasAttrs() ? getAttrs().end() : 0;
366  }
367
368  template <typename T>
369  specific_attr_iterator<T> specific_attr_begin() const {
370    return specific_attr_iterator<T>(attr_begin());
371  }
372  template <typename T>
373  specific_attr_iterator<T> specific_attr_end() const {
374    return specific_attr_iterator<T>(attr_end());
375  }
376
377  template<typename T> T *getAttr() const {
378    return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : 0;
379  }
380  template<typename T> bool hasAttr() const {
381    return hasAttrs() && hasSpecificAttr<T>(getAttrs());
382  }
383
384  /// getMaxAlignment - return the maximum alignment specified by attributes
385  /// on this decl, 0 if there are none.
386  unsigned getMaxAlignment() const {
387    return hasAttrs() ? getMaxAttrAlignment(getAttrs(), getASTContext()) : 0;
388  }
389
390  /// setInvalidDecl - Indicates the Decl had a semantic error. This
391  /// allows for graceful error recovery.
392  void setInvalidDecl(bool Invalid = true);
393  bool isInvalidDecl() const { return (bool) InvalidDecl; }
394
395  /// isImplicit - Indicates whether the declaration was implicitly
396  /// generated by the implementation. If false, this declaration
397  /// was written explicitly in the source code.
398  bool isImplicit() const { return Implicit; }
399  void setImplicit(bool I = true) { Implicit = I; }
400
401  /// \brief Whether this declaration was used, meaning that a definition
402  /// is required.
403  ///
404  /// \param CheckUsedAttr When true, also consider the "used" attribute
405  /// (in addition to the "used" bit set by \c setUsed()) when determining
406  /// whether the function is used.
407  bool isUsed(bool CheckUsedAttr = true) const;
408
409  void setUsed(bool U = true) { Used = U; }
410
411  /// \brief Determine the availability of the given declaration.
412  ///
413  /// This routine will determine the most restrictive availability of
414  /// the given declaration (e.g., preferring 'unavailable' to
415  /// 'deprecated').
416  ///
417  /// \param Message If non-NULL and the result is not \c
418  /// AR_Available, will be set to a (possibly empty) message
419  /// describing why the declaration has not been introduced, is
420  /// deprecated, or is unavailable.
421  AvailabilityResult getAvailability(std::string *Message = 0) const;
422
423  /// \brief Determine whether this declaration is marked 'deprecated'.
424  ///
425  /// \param Message If non-NULL and the declaration is deprecated,
426  /// this will be set to the message describing why the declaration
427  /// was deprecated (which may be empty).
428  bool isDeprecated(std::string *Message = 0) const {
429    return getAvailability(Message) == AR_Deprecated;
430  }
431
432  /// \brief Determine whether this declaration is marked 'unavailable'.
433  ///
434  /// \param Message If non-NULL and the declaration is unavailable,
435  /// this will be set to the message describing why the declaration
436  /// was made unavailable (which may be empty).
437  bool isUnavailable(std::string *Message = 0) const {
438    return getAvailability(Message) == AR_Unavailable;
439  }
440
441  /// \brief Determine whether this is a weak-imported symbol.
442  ///
443  /// Weak-imported symbols are typically marked with the
444  /// 'weak_import' attribute, but may also be marked with an
445  /// 'availability' attribute where we're targing a platform prior to
446  /// the introduction of this feature.
447  bool isWeakImported() const;
448
449  /// \brief Determines whether this symbol can be weak-imported,
450  /// e.g., whether it would be well-formed to add the weak_import
451  /// attribute.
452  ///
453  /// \param IsDefinition Set to \c true to indicate that this
454  /// declaration cannot be weak-imported because it has a definition.
455  bool canBeWeakImported(bool &IsDefinition) const;
456
457  /// \brief Retrieve the level of precompiled header from which this
458  /// declaration was generated.
459  ///
460  /// The PCH level of a declaration describes where the declaration originated
461  /// from. A PCH level of 0 indicates that the declaration was parsed from
462  /// source. A PCH level of 1 indicates that the declaration was loaded from
463  /// a top-level AST file. A PCH level 2 indicates that the declaration was
464  /// loaded from a PCH file the AST file depends on, and so on.
465  unsigned getPCHLevel() const { return PCHLevel; }
466
467  /// \brief The maximum PCH level that any declaration may have.
468  static const unsigned MaxPCHLevel = 3;
469
470  /// \brief Set the PCH level of this declaration.
471  void setPCHLevel(unsigned Level) {
472    assert(Level <= MaxPCHLevel && "PCH level exceeds the maximum");
473    PCHLevel = Level;
474  }
475
476  /// \brief Query whether this declaration was changed in a significant way
477  /// since being loaded from an AST file.
478  ///
479  /// In an epic violation of layering, what is "significant" is entirely
480  /// up to the serialization system, but implemented in AST and Sema.
481  bool isChangedSinceDeserialization() const { return ChangedAfterLoad; }
482
483  /// \brief Mark this declaration as having changed since deserialization, or
484  /// reset the flag.
485  void setChangedSinceDeserialization(bool Changed) {
486    ChangedAfterLoad = Changed;
487  }
488
489  unsigned getIdentifierNamespace() const {
490    return IdentifierNamespace;
491  }
492  bool isInIdentifierNamespace(unsigned NS) const {
493    return getIdentifierNamespace() & NS;
494  }
495  static unsigned getIdentifierNamespaceForKind(Kind DK);
496
497  bool hasTagIdentifierNamespace() const {
498    return isTagIdentifierNamespace(getIdentifierNamespace());
499  }
500  static bool isTagIdentifierNamespace(unsigned NS) {
501    // TagDecls have Tag and Type set and may also have TagFriend.
502    return (NS & ~IDNS_TagFriend) == (IDNS_Tag | IDNS_Type);
503  }
504
505  /// getLexicalDeclContext - The declaration context where this Decl was
506  /// lexically declared (LexicalDC). May be different from
507  /// getDeclContext() (SemanticDC).
508  /// e.g.:
509  ///
510  ///   namespace A {
511  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
512  ///   }
513  ///   void A::f(); // SemanticDC == namespace 'A'
514  ///                // LexicalDC == global namespace
515  DeclContext *getLexicalDeclContext() {
516    if (isInSemaDC())
517      return getSemanticDC();
518    return getMultipleDC()->LexicalDC;
519  }
520  const DeclContext *getLexicalDeclContext() const {
521    return const_cast<Decl*>(this)->getLexicalDeclContext();
522  }
523
524  virtual bool isOutOfLine() const {
525    return getLexicalDeclContext() != getDeclContext();
526  }
527
528  /// setDeclContext - Set both the semantic and lexical DeclContext
529  /// to DC.
530  void setDeclContext(DeclContext *DC);
531
532  void setLexicalDeclContext(DeclContext *DC);
533
534  /// isDefinedOutsideFunctionOrMethod - This predicate returns true if this
535  /// scoped decl is defined outside the current function or method.  This is
536  /// roughly global variables and functions, but also handles enums (which
537  /// could be defined inside or outside a function etc).
538  bool isDefinedOutsideFunctionOrMethod() const;
539
540  /// \brief Retrieves the "canonical" declaration of the given declaration.
541  virtual Decl *getCanonicalDecl() { return this; }
542  const Decl *getCanonicalDecl() const {
543    return const_cast<Decl*>(this)->getCanonicalDecl();
544  }
545
546  /// \brief Whether this particular Decl is a canonical one.
547  bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
548
549protected:
550  /// \brief Returns the next redeclaration or itself if this is the only decl.
551  ///
552  /// Decl subclasses that can be redeclared should override this method so that
553  /// Decl::redecl_iterator can iterate over them.
554  virtual Decl *getNextRedeclaration() { return this; }
555
556public:
557  /// \brief Iterates through all the redeclarations of the same decl.
558  class redecl_iterator {
559    /// Current - The current declaration.
560    Decl *Current;
561    Decl *Starter;
562
563  public:
564    typedef Decl*                     value_type;
565    typedef Decl*                     reference;
566    typedef Decl*                     pointer;
567    typedef std::forward_iterator_tag iterator_category;
568    typedef std::ptrdiff_t            difference_type;
569
570    redecl_iterator() : Current(0) { }
571    explicit redecl_iterator(Decl *C) : Current(C), Starter(C) { }
572
573    reference operator*() const { return Current; }
574    pointer operator->() const { return Current; }
575
576    redecl_iterator& operator++() {
577      assert(Current && "Advancing while iterator has reached end");
578      // Get either previous decl or latest decl.
579      Decl *Next = Current->getNextRedeclaration();
580      assert(Next && "Should return next redeclaration or itself, never null!");
581      Current = (Next != Starter ? Next : 0);
582      return *this;
583    }
584
585    redecl_iterator operator++(int) {
586      redecl_iterator tmp(*this);
587      ++(*this);
588      return tmp;
589    }
590
591    friend bool operator==(redecl_iterator x, redecl_iterator y) {
592      return x.Current == y.Current;
593    }
594    friend bool operator!=(redecl_iterator x, redecl_iterator y) {
595      return x.Current != y.Current;
596    }
597  };
598
599  /// \brief Returns iterator for all the redeclarations of the same decl.
600  /// It will iterate at least once (when this decl is the only one).
601  redecl_iterator redecls_begin() const {
602    return redecl_iterator(const_cast<Decl*>(this));
603  }
604  redecl_iterator redecls_end() const { return redecl_iterator(); }
605
606  /// getBody - If this Decl represents a declaration for a body of code,
607  ///  such as a function or method definition, this method returns the
608  ///  top-level Stmt* of that body.  Otherwise this method returns null.
609  virtual Stmt* getBody() const { return 0; }
610
611  /// \brief Returns true if this Decl represents a declaration for a body of
612  /// code, such as a function or method definition.
613  virtual bool hasBody() const { return getBody() != 0; }
614
615  /// getBodyRBrace - Gets the right brace of the body, if a body exists.
616  /// This works whether the body is a CompoundStmt or a CXXTryStmt.
617  SourceLocation getBodyRBrace() const;
618
619  // global temp stats (until we have a per-module visitor)
620  static void add(Kind k);
621  static bool CollectingStats(bool Enable = false);
622  static void PrintStats();
623
624  /// isTemplateParameter - Determines whether this declaration is a
625  /// template parameter.
626  bool isTemplateParameter() const;
627
628  /// isTemplateParameter - Determines whether this declaration is a
629  /// template parameter pack.
630  bool isTemplateParameterPack() const;
631
632  /// \brief Whether this declaration is a parameter pack.
633  bool isParameterPack() const;
634
635  /// \brief Whether this declaration is a function or function template.
636  bool isFunctionOrFunctionTemplate() const;
637
638  /// \brief Changes the namespace of this declaration to reflect that it's
639  /// the object of a friend declaration.
640  ///
641  /// These declarations appear in the lexical context of the friending
642  /// class, but in the semantic context of the actual entity.  This property
643  /// applies only to a specific decl object;  other redeclarations of the
644  /// same entity may not (and probably don't) share this property.
645  void setObjectOfFriendDecl(bool PreviouslyDeclared) {
646    unsigned OldNS = IdentifierNamespace;
647    assert((OldNS & (IDNS_Tag | IDNS_Ordinary |
648                     IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
649           "namespace includes neither ordinary nor tag");
650    assert(!(OldNS & ~(IDNS_Tag | IDNS_Ordinary | IDNS_Type |
651                       IDNS_TagFriend | IDNS_OrdinaryFriend)) &&
652           "namespace includes other than ordinary or tag");
653
654    IdentifierNamespace = 0;
655    if (OldNS & (IDNS_Tag | IDNS_TagFriend)) {
656      IdentifierNamespace |= IDNS_TagFriend;
657      if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Tag | IDNS_Type;
658    }
659
660    if (OldNS & (IDNS_Ordinary | IDNS_OrdinaryFriend)) {
661      IdentifierNamespace |= IDNS_OrdinaryFriend;
662      if (PreviouslyDeclared) IdentifierNamespace |= IDNS_Ordinary;
663    }
664  }
665
666  enum FriendObjectKind {
667    FOK_None, // not a friend object
668    FOK_Declared, // a friend of a previously-declared entity
669    FOK_Undeclared // a friend of a previously-undeclared entity
670  };
671
672  /// \brief Determines whether this declaration is the object of a
673  /// friend declaration and, if so, what kind.
674  ///
675  /// There is currently no direct way to find the associated FriendDecl.
676  FriendObjectKind getFriendObjectKind() const {
677    unsigned mask
678      = (IdentifierNamespace & (IDNS_TagFriend | IDNS_OrdinaryFriend));
679    if (!mask) return FOK_None;
680    return (IdentifierNamespace & (IDNS_Tag | IDNS_Ordinary) ?
681              FOK_Declared : FOK_Undeclared);
682  }
683
684  /// Specifies that this declaration is a C++ overloaded non-member.
685  void setNonMemberOperator() {
686    assert(getKind() == Function || getKind() == FunctionTemplate);
687    assert((IdentifierNamespace & IDNS_Ordinary) &&
688           "visible non-member operators should be in ordinary namespace");
689    IdentifierNamespace |= IDNS_NonMemberOperator;
690  }
691
692  // Implement isa/cast/dyncast/etc.
693  static bool classof(const Decl *) { return true; }
694  static bool classofKind(Kind K) { return true; }
695  static DeclContext *castToDeclContext(const Decl *);
696  static Decl *castFromDeclContext(const DeclContext *);
697
698  void print(llvm::raw_ostream &Out, unsigned Indentation = 0) const;
699  void print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
700             unsigned Indentation = 0) const;
701  static void printGroup(Decl** Begin, unsigned NumDecls,
702                         llvm::raw_ostream &Out, const PrintingPolicy &Policy,
703                         unsigned Indentation = 0);
704  void dump() const;
705  void dumpXML() const;
706  void dumpXML(llvm::raw_ostream &OS) const;
707
708private:
709  const Attr *getAttrsImpl() const;
710
711protected:
712  ASTMutationListener *getASTMutationListener() const;
713};
714
715/// PrettyStackTraceDecl - If a crash occurs, indicate that it happened when
716/// doing something to a specific decl.
717class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
718  const Decl *TheDecl;
719  SourceLocation Loc;
720  SourceManager &SM;
721  const char *Message;
722public:
723  PrettyStackTraceDecl(const Decl *theDecl, SourceLocation L,
724                       SourceManager &sm, const char *Msg)
725  : TheDecl(theDecl), Loc(L), SM(sm), Message(Msg) {}
726
727  virtual void print(llvm::raw_ostream &OS) const;
728};
729
730class DeclContextLookupResult
731  : public std::pair<NamedDecl**,NamedDecl**> {
732public:
733  DeclContextLookupResult(NamedDecl **I, NamedDecl **E)
734    : std::pair<NamedDecl**,NamedDecl**>(I, E) {}
735  DeclContextLookupResult()
736    : std::pair<NamedDecl**,NamedDecl**>() {}
737
738  using std::pair<NamedDecl**,NamedDecl**>::operator=;
739};
740
741class DeclContextLookupConstResult
742  : public std::pair<NamedDecl*const*, NamedDecl*const*> {
743public:
744  DeclContextLookupConstResult(std::pair<NamedDecl**,NamedDecl**> R)
745    : std::pair<NamedDecl*const*, NamedDecl*const*>(R) {}
746  DeclContextLookupConstResult(NamedDecl * const *I, NamedDecl * const *E)
747    : std::pair<NamedDecl*const*, NamedDecl*const*>(I, E) {}
748  DeclContextLookupConstResult()
749    : std::pair<NamedDecl*const*, NamedDecl*const*>() {}
750
751  using std::pair<NamedDecl*const*,NamedDecl*const*>::operator=;
752};
753
754/// DeclContext - This is used only as base class of specific decl types that
755/// can act as declaration contexts. These decls are (only the top classes
756/// that directly derive from DeclContext are mentioned, not their subclasses):
757///
758///   TranslationUnitDecl
759///   NamespaceDecl
760///   FunctionDecl
761///   TagDecl
762///   ObjCMethodDecl
763///   ObjCContainerDecl
764///   LinkageSpecDecl
765///   BlockDecl
766///
767class DeclContext {
768  /// DeclKind - This indicates which class this is.
769  unsigned DeclKind : 8;
770
771  /// \brief Whether this declaration context also has some external
772  /// storage that contains additional declarations that are lexically
773  /// part of this context.
774  mutable unsigned ExternalLexicalStorage : 1;
775
776  /// \brief Whether this declaration context also has some external
777  /// storage that contains additional declarations that are visible
778  /// in this context.
779  mutable unsigned ExternalVisibleStorage : 1;
780
781  /// \brief Pointer to the data structure used to lookup declarations
782  /// within this context (or a DependentStoredDeclsMap if this is a
783  /// dependent context).
784  mutable StoredDeclsMap *LookupPtr;
785
786protected:
787  /// FirstDecl - The first declaration stored within this declaration
788  /// context.
789  mutable Decl *FirstDecl;
790
791  /// LastDecl - The last declaration stored within this declaration
792  /// context. FIXME: We could probably cache this value somewhere
793  /// outside of the DeclContext, to reduce the size of DeclContext by
794  /// another pointer.
795  mutable Decl *LastDecl;
796
797  friend class ExternalASTSource;
798
799  /// \brief Build up a chain of declarations.
800  ///
801  /// \returns the first/last pair of declarations.
802  static std::pair<Decl *, Decl *>
803  BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls);
804
805   DeclContext(Decl::Kind K)
806     : DeclKind(K), ExternalLexicalStorage(false),
807       ExternalVisibleStorage(false), LookupPtr(0), FirstDecl(0),
808       LastDecl(0) { }
809
810public:
811  ~DeclContext();
812
813  Decl::Kind getDeclKind() const {
814    return static_cast<Decl::Kind>(DeclKind);
815  }
816  const char *getDeclKindName() const;
817
818  /// getParent - Returns the containing DeclContext.
819  DeclContext *getParent() {
820    return cast<Decl>(this)->getDeclContext();
821  }
822  const DeclContext *getParent() const {
823    return const_cast<DeclContext*>(this)->getParent();
824  }
825
826  /// getLexicalParent - Returns the containing lexical DeclContext. May be
827  /// different from getParent, e.g.:
828  ///
829  ///   namespace A {
830  ///      struct S;
831  ///   }
832  ///   struct A::S {}; // getParent() == namespace 'A'
833  ///                   // getLexicalParent() == translation unit
834  ///
835  DeclContext *getLexicalParent() {
836    return cast<Decl>(this)->getLexicalDeclContext();
837  }
838  const DeclContext *getLexicalParent() const {
839    return const_cast<DeclContext*>(this)->getLexicalParent();
840  }
841
842  DeclContext *getLookupParent();
843
844  const DeclContext *getLookupParent() const {
845    return const_cast<DeclContext*>(this)->getLookupParent();
846  }
847
848  ASTContext &getParentASTContext() const {
849    return cast<Decl>(this)->getASTContext();
850  }
851
852  bool isClosure() const {
853    return DeclKind == Decl::Block;
854  }
855
856  bool isFunctionOrMethod() const {
857    switch (DeclKind) {
858    case Decl::Block:
859    case Decl::ObjCMethod:
860      return true;
861    default:
862      return DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction;
863    }
864  }
865
866  bool isFileContext() const {
867    return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
868  }
869
870  bool isTranslationUnit() const {
871    return DeclKind == Decl::TranslationUnit;
872  }
873
874  bool isRecord() const {
875    return DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord;
876  }
877
878  bool isNamespace() const {
879    return DeclKind == Decl::Namespace;
880  }
881
882  bool isInlineNamespace() const;
883
884  /// \brief Determines whether this context is dependent on a
885  /// template parameter.
886  bool isDependentContext() const;
887
888  /// isTransparentContext - Determines whether this context is a
889  /// "transparent" context, meaning that the members declared in this
890  /// context are semantically declared in the nearest enclosing
891  /// non-transparent (opaque) context but are lexically declared in
892  /// this context. For example, consider the enumerators of an
893  /// enumeration type:
894  /// @code
895  /// enum E {
896  ///   Val1
897  /// };
898  /// @endcode
899  /// Here, E is a transparent context, so its enumerator (Val1) will
900  /// appear (semantically) that it is in the same context of E.
901  /// Examples of transparent contexts include: enumerations (except for
902  /// C++0x scoped enums), and C++ linkage specifications.
903  bool isTransparentContext() const;
904
905  /// \brief Determines whether this context is, or is nested within,
906  /// a C++ extern "C" linkage spec.
907  bool isExternCContext() const;
908
909  /// \brief Determine whether this declaration context is equivalent
910  /// to the declaration context DC.
911  bool Equals(const DeclContext *DC) const {
912    return DC && this->getPrimaryContext() == DC->getPrimaryContext();
913  }
914
915  /// \brief Determine whether this declaration context encloses the
916  /// declaration context DC.
917  bool Encloses(const DeclContext *DC) const;
918
919  /// getPrimaryContext - There may be many different
920  /// declarations of the same entity (including forward declarations
921  /// of classes, multiple definitions of namespaces, etc.), each with
922  /// a different set of declarations. This routine returns the
923  /// "primary" DeclContext structure, which will contain the
924  /// information needed to perform name lookup into this context.
925  DeclContext *getPrimaryContext();
926  const DeclContext *getPrimaryContext() const {
927    return const_cast<DeclContext*>(this)->getPrimaryContext();
928  }
929
930  /// getRedeclContext - Retrieve the context in which an entity conflicts with
931  /// other entities of the same name, or where it is a redeclaration if the
932  /// two entities are compatible. This skips through transparent contexts.
933  DeclContext *getRedeclContext();
934  const DeclContext *getRedeclContext() const {
935    return const_cast<DeclContext *>(this)->getRedeclContext();
936  }
937
938  /// \brief Retrieve the nearest enclosing namespace context.
939  DeclContext *getEnclosingNamespaceContext();
940  const DeclContext *getEnclosingNamespaceContext() const {
941    return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
942  }
943
944  /// \brief Test if this context is part of the enclosing namespace set of
945  /// the context NS, as defined in C++0x [namespace.def]p9. If either context
946  /// isn't a namespace, this is equivalent to Equals().
947  ///
948  /// The enclosing namespace set of a namespace is the namespace and, if it is
949  /// inline, its enclosing namespace, recursively.
950  bool InEnclosingNamespaceSetOf(const DeclContext *NS) const;
951
952  /// getNextContext - If this is a DeclContext that may have other
953  /// DeclContexts that are semantically connected but syntactically
954  /// different, such as C++ namespaces, this routine retrieves the
955  /// next DeclContext in the link. Iteration through the chain of
956  /// DeclContexts should begin at the primary DeclContext and
957  /// continue until this function returns NULL. For example, given:
958  /// @code
959  /// namespace N {
960  ///   int x;
961  /// }
962  /// namespace N {
963  ///   int y;
964  /// }
965  /// @endcode
966  /// The first occurrence of namespace N will be the primary
967  /// DeclContext. Its getNextContext will return the second
968  /// occurrence of namespace N.
969  DeclContext *getNextContext();
970
971  /// decl_iterator - Iterates through the declarations stored
972  /// within this context.
973  class decl_iterator {
974    /// Current - The current declaration.
975    Decl *Current;
976
977  public:
978    typedef Decl*                     value_type;
979    typedef Decl*                     reference;
980    typedef Decl*                     pointer;
981    typedef std::forward_iterator_tag iterator_category;
982    typedef std::ptrdiff_t            difference_type;
983
984    decl_iterator() : Current(0) { }
985    explicit decl_iterator(Decl *C) : Current(C) { }
986
987    reference operator*() const { return Current; }
988    pointer operator->() const { return Current; }
989
990    decl_iterator& operator++() {
991      Current = Current->getNextDeclInContext();
992      return *this;
993    }
994
995    decl_iterator operator++(int) {
996      decl_iterator tmp(*this);
997      ++(*this);
998      return tmp;
999    }
1000
1001    friend bool operator==(decl_iterator x, decl_iterator y) {
1002      return x.Current == y.Current;
1003    }
1004    friend bool operator!=(decl_iterator x, decl_iterator y) {
1005      return x.Current != y.Current;
1006    }
1007  };
1008
1009  /// decls_begin/decls_end - Iterate over the declarations stored in
1010  /// this context.
1011  decl_iterator decls_begin() const;
1012  decl_iterator decls_end() const;
1013  bool decls_empty() const;
1014
1015  /// noload_decls_begin/end - Iterate over the declarations stored in this
1016  /// context that are currently loaded; don't attempt to retrieve anything
1017  /// from an external source.
1018  decl_iterator noload_decls_begin() const;
1019  decl_iterator noload_decls_end() const;
1020
1021  /// specific_decl_iterator - Iterates over a subrange of
1022  /// declarations stored in a DeclContext, providing only those that
1023  /// are of type SpecificDecl (or a class derived from it). This
1024  /// iterator is used, for example, to provide iteration over just
1025  /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
1026  template<typename SpecificDecl>
1027  class specific_decl_iterator {
1028    /// Current - The current, underlying declaration iterator, which
1029    /// will either be NULL or will point to a declaration of
1030    /// type SpecificDecl.
1031    DeclContext::decl_iterator Current;
1032
1033    /// SkipToNextDecl - Advances the current position up to the next
1034    /// declaration of type SpecificDecl that also meets the criteria
1035    /// required by Acceptable.
1036    void SkipToNextDecl() {
1037      while (*Current && !isa<SpecificDecl>(*Current))
1038        ++Current;
1039    }
1040
1041  public:
1042    typedef SpecificDecl* value_type;
1043    typedef SpecificDecl* reference;
1044    typedef SpecificDecl* pointer;
1045    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1046      difference_type;
1047    typedef std::forward_iterator_tag iterator_category;
1048
1049    specific_decl_iterator() : Current() { }
1050
1051    /// specific_decl_iterator - Construct a new iterator over a
1052    /// subset of the declarations the range [C,
1053    /// end-of-declarations). If A is non-NULL, it is a pointer to a
1054    /// member function of SpecificDecl that should return true for
1055    /// all of the SpecificDecl instances that will be in the subset
1056    /// of iterators. For example, if you want Objective-C instance
1057    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1058    /// &ObjCMethodDecl::isInstanceMethod.
1059    explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1060      SkipToNextDecl();
1061    }
1062
1063    reference operator*() const { return cast<SpecificDecl>(*Current); }
1064    pointer operator->() const { return cast<SpecificDecl>(*Current); }
1065
1066    specific_decl_iterator& operator++() {
1067      ++Current;
1068      SkipToNextDecl();
1069      return *this;
1070    }
1071
1072    specific_decl_iterator operator++(int) {
1073      specific_decl_iterator tmp(*this);
1074      ++(*this);
1075      return tmp;
1076    }
1077
1078    friend bool
1079    operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
1080      return x.Current == y.Current;
1081    }
1082
1083    friend bool
1084    operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
1085      return x.Current != y.Current;
1086    }
1087  };
1088
1089  /// \brief Iterates over a filtered subrange of declarations stored
1090  /// in a DeclContext.
1091  ///
1092  /// This iterator visits only those declarations that are of type
1093  /// SpecificDecl (or a class derived from it) and that meet some
1094  /// additional run-time criteria. This iterator is used, for
1095  /// example, to provide access to the instance methods within an
1096  /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
1097  /// Acceptable = ObjCMethodDecl::isInstanceMethod).
1098  template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
1099  class filtered_decl_iterator {
1100    /// Current - The current, underlying declaration iterator, which
1101    /// will either be NULL or will point to a declaration of
1102    /// type SpecificDecl.
1103    DeclContext::decl_iterator Current;
1104
1105    /// SkipToNextDecl - Advances the current position up to the next
1106    /// declaration of type SpecificDecl that also meets the criteria
1107    /// required by Acceptable.
1108    void SkipToNextDecl() {
1109      while (*Current &&
1110             (!isa<SpecificDecl>(*Current) ||
1111              (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
1112        ++Current;
1113    }
1114
1115  public:
1116    typedef SpecificDecl* value_type;
1117    typedef SpecificDecl* reference;
1118    typedef SpecificDecl* pointer;
1119    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
1120      difference_type;
1121    typedef std::forward_iterator_tag iterator_category;
1122
1123    filtered_decl_iterator() : Current() { }
1124
1125    /// specific_decl_iterator - Construct a new iterator over a
1126    /// subset of the declarations the range [C,
1127    /// end-of-declarations). If A is non-NULL, it is a pointer to a
1128    /// member function of SpecificDecl that should return true for
1129    /// all of the SpecificDecl instances that will be in the subset
1130    /// of iterators. For example, if you want Objective-C instance
1131    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
1132    /// &ObjCMethodDecl::isInstanceMethod.
1133    explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
1134      SkipToNextDecl();
1135    }
1136
1137    reference operator*() const { return cast<SpecificDecl>(*Current); }
1138    pointer operator->() const { return cast<SpecificDecl>(*Current); }
1139
1140    filtered_decl_iterator& operator++() {
1141      ++Current;
1142      SkipToNextDecl();
1143      return *this;
1144    }
1145
1146    filtered_decl_iterator operator++(int) {
1147      filtered_decl_iterator tmp(*this);
1148      ++(*this);
1149      return tmp;
1150    }
1151
1152    friend bool
1153    operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
1154      return x.Current == y.Current;
1155    }
1156
1157    friend bool
1158    operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
1159      return x.Current != y.Current;
1160    }
1161  };
1162
1163  /// @brief Add the declaration D into this context.
1164  ///
1165  /// This routine should be invoked when the declaration D has first
1166  /// been declared, to place D into the context where it was
1167  /// (lexically) defined. Every declaration must be added to one
1168  /// (and only one!) context, where it can be visited via
1169  /// [decls_begin(), decls_end()). Once a declaration has been added
1170  /// to its lexical context, the corresponding DeclContext owns the
1171  /// declaration.
1172  ///
1173  /// If D is also a NamedDecl, it will be made visible within its
1174  /// semantic context via makeDeclVisibleInContext.
1175  void addDecl(Decl *D);
1176
1177  /// @brief Add the declaration D to this context without modifying
1178  /// any lookup tables.
1179  ///
1180  /// This is useful for some operations in dependent contexts where
1181  /// the semantic context might not be dependent;  this basically
1182  /// only happens with friends.
1183  void addHiddenDecl(Decl *D);
1184
1185  /// @brief Removes a declaration from this context.
1186  void removeDecl(Decl *D);
1187
1188  /// lookup_iterator - An iterator that provides access to the results
1189  /// of looking up a name within this context.
1190  typedef NamedDecl **lookup_iterator;
1191
1192  /// lookup_const_iterator - An iterator that provides non-mutable
1193  /// access to the results of lookup up a name within this context.
1194  typedef NamedDecl * const * lookup_const_iterator;
1195
1196  typedef DeclContextLookupResult lookup_result;
1197  typedef DeclContextLookupConstResult lookup_const_result;
1198
1199  /// lookup - Find the declarations (if any) with the given Name in
1200  /// this context. Returns a range of iterators that contains all of
1201  /// the declarations with this name, with object, function, member,
1202  /// and enumerator names preceding any tag name. Note that this
1203  /// routine will not look into parent contexts.
1204  lookup_result lookup(DeclarationName Name);
1205  lookup_const_result lookup(DeclarationName Name) const;
1206
1207  /// @brief Makes a declaration visible within this context.
1208  ///
1209  /// This routine makes the declaration D visible to name lookup
1210  /// within this context and, if this is a transparent context,
1211  /// within its parent contexts up to the first enclosing
1212  /// non-transparent context. Making a declaration visible within a
1213  /// context does not transfer ownership of a declaration, and a
1214  /// declaration can be visible in many contexts that aren't its
1215  /// lexical context.
1216  ///
1217  /// If D is a redeclaration of an existing declaration that is
1218  /// visible from this context, as determined by
1219  /// NamedDecl::declarationReplaces, the previous declaration will be
1220  /// replaced with D.
1221  ///
1222  /// @param Recoverable true if it's okay to not add this decl to
1223  /// the lookup tables because it can be easily recovered by walking
1224  /// the declaration chains.
1225  void makeDeclVisibleInContext(NamedDecl *D, bool Recoverable = true);
1226
1227  /// \brief Deserialize all the visible declarations from external storage.
1228  ///
1229  /// Name lookup deserializes visible declarations lazily, thus a DeclContext
1230  /// may not have a complete name lookup table. This function deserializes
1231  /// the rest of visible declarations from the external storage and completes
1232  /// the name lookup table.
1233  void MaterializeVisibleDeclsFromExternalStorage();
1234
1235  /// udir_iterator - Iterates through the using-directives stored
1236  /// within this context.
1237  typedef UsingDirectiveDecl * const * udir_iterator;
1238
1239  typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
1240
1241  udir_iterator_range getUsingDirectives() const;
1242
1243  udir_iterator using_directives_begin() const {
1244    return getUsingDirectives().first;
1245  }
1246
1247  udir_iterator using_directives_end() const {
1248    return getUsingDirectives().second;
1249  }
1250
1251  // These are all defined in DependentDiagnostic.h.
1252  class ddiag_iterator;
1253  inline ddiag_iterator ddiag_begin() const;
1254  inline ddiag_iterator ddiag_end() const;
1255
1256  // Low-level accessors
1257
1258  /// \brief Retrieve the internal representation of the lookup structure.
1259  StoredDeclsMap* getLookupPtr() const { return LookupPtr; }
1260
1261  /// \brief Whether this DeclContext has external storage containing
1262  /// additional declarations that are lexically in this context.
1263  bool hasExternalLexicalStorage() const { return ExternalLexicalStorage; }
1264
1265  /// \brief State whether this DeclContext has external storage for
1266  /// declarations lexically in this context.
1267  void setHasExternalLexicalStorage(bool ES = true) {
1268    ExternalLexicalStorage = ES;
1269  }
1270
1271  /// \brief Whether this DeclContext has external storage containing
1272  /// additional declarations that are visible in this context.
1273  bool hasExternalVisibleStorage() const { return ExternalVisibleStorage; }
1274
1275  /// \brief State whether this DeclContext has external storage for
1276  /// declarations visible in this context.
1277  void setHasExternalVisibleStorage(bool ES = true) {
1278    ExternalVisibleStorage = ES;
1279  }
1280
1281  static bool classof(const Decl *D);
1282  static bool classof(const DeclContext *D) { return true; }
1283#define DECL(NAME, BASE)
1284#define DECL_CONTEXT(NAME) \
1285  static bool classof(const NAME##Decl *D) { return true; }
1286#include "clang/AST/DeclNodes.inc"
1287
1288  void dumpDeclContext() const;
1289
1290private:
1291  void LoadLexicalDeclsFromExternalStorage() const;
1292
1293  friend class DependentDiagnostic;
1294  StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const;
1295
1296  void buildLookup(DeclContext *DCtx);
1297  void makeDeclVisibleInContextImpl(NamedDecl *D);
1298};
1299
1300inline bool Decl::isTemplateParameter() const {
1301  return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm ||
1302         getKind() == TemplateTemplateParm;
1303}
1304
1305// Specialization selected when ToTy is not a known subclass of DeclContext.
1306template <class ToTy,
1307          bool IsKnownSubtype = ::llvm::is_base_of< DeclContext, ToTy>::value>
1308struct cast_convert_decl_context {
1309  static const ToTy *doit(const DeclContext *Val) {
1310    return static_cast<const ToTy*>(Decl::castFromDeclContext(Val));
1311  }
1312
1313  static ToTy *doit(DeclContext *Val) {
1314    return static_cast<ToTy*>(Decl::castFromDeclContext(Val));
1315  }
1316};
1317
1318// Specialization selected when ToTy is a known subclass of DeclContext.
1319template <class ToTy>
1320struct cast_convert_decl_context<ToTy, true> {
1321  static const ToTy *doit(const DeclContext *Val) {
1322    return static_cast<const ToTy*>(Val);
1323  }
1324
1325  static ToTy *doit(DeclContext *Val) {
1326    return static_cast<ToTy*>(Val);
1327  }
1328};
1329
1330
1331} // end clang.
1332
1333namespace llvm {
1334
1335/// isa<T>(DeclContext*)
1336template<class ToTy>
1337struct isa_impl_wrap<ToTy,
1338                     const ::clang::DeclContext,const ::clang::DeclContext> {
1339  static bool doit(const ::clang::DeclContext &Val) {
1340    return ToTy::classofKind(Val.getDeclKind());
1341  }
1342};
1343template<class ToTy>
1344struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext>
1345  : public isa_impl_wrap<ToTy,
1346                      const ::clang::DeclContext,const ::clang::DeclContext> {};
1347
1348/// cast<T>(DeclContext*)
1349template<class ToTy>
1350struct cast_convert_val<ToTy,
1351                        const ::clang::DeclContext,const ::clang::DeclContext> {
1352  static const ToTy &doit(const ::clang::DeclContext &Val) {
1353    return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1354  }
1355};
1356template<class ToTy>
1357struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext> {
1358  static ToTy &doit(::clang::DeclContext &Val) {
1359    return *::clang::cast_convert_decl_context<ToTy>::doit(&Val);
1360  }
1361};
1362template<class ToTy>
1363struct cast_convert_val<ToTy,
1364                     const ::clang::DeclContext*, const ::clang::DeclContext*> {
1365  static const ToTy *doit(const ::clang::DeclContext *Val) {
1366    return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1367  }
1368};
1369template<class ToTy>
1370struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*> {
1371  static ToTy *doit(::clang::DeclContext *Val) {
1372    return ::clang::cast_convert_decl_context<ToTy>::doit(Val);
1373  }
1374};
1375
1376/// Implement cast_convert_val for Decl -> DeclContext conversions.
1377template<class FromTy>
1378struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
1379  static ::clang::DeclContext &doit(const FromTy &Val) {
1380    return *FromTy::castToDeclContext(&Val);
1381  }
1382};
1383
1384template<class FromTy>
1385struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
1386  static ::clang::DeclContext *doit(const FromTy *Val) {
1387    return FromTy::castToDeclContext(Val);
1388  }
1389};
1390
1391template<class FromTy>
1392struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
1393  static const ::clang::DeclContext &doit(const FromTy &Val) {
1394    return *FromTy::castToDeclContext(&Val);
1395  }
1396};
1397
1398template<class FromTy>
1399struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
1400  static const ::clang::DeclContext *doit(const FromTy *Val) {
1401    return FromTy::castToDeclContext(Val);
1402  }
1403};
1404
1405} // end namespace llvm
1406
1407#endif
1408