DeclBase.h revision 65100792a69a16895bd80f1d639b99e7ad903386
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// FIXME: Layering violation
20#include "clang/Parse/AccessSpecifier.h"
21
22namespace clang {
23class DeclContext;
24class TranslationUnitDecl;
25class NamespaceDecl;
26class UsingDirectiveDecl;
27class NamedDecl;
28class FunctionDecl;
29class CXXRecordDecl;
30class EnumDecl;
31class ObjCMethodDecl;
32class ObjCContainerDecl;
33class ObjCInterfaceDecl;
34class ObjCCategoryDecl;
35class ObjCProtocolDecl;
36class ObjCImplementationDecl;
37class ObjCCategoryImplDecl;
38class LinkageSpecDecl;
39class BlockDecl;
40class DeclarationName;
41
42/// Decl - This represents one declaration (or definition), e.g. a variable,
43/// typedef, function, struct, etc.
44///
45class Decl {
46public:
47  /// \brief Lists the kind of concrete classes of Decl.
48  enum Kind {
49#define DECL(Derived, Base) Derived,
50#define DECL_RANGE(CommonBase, Start, End) \
51    CommonBase##First = Start, CommonBase##Last = End,
52#define LAST_DECL_RANGE(CommonBase, Start, End) \
53    CommonBase##First = Start, CommonBase##Last = End
54#include "clang/AST/DeclNodes.def"
55  };
56
57  /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces,
58  /// labels, tags, members and ordinary identifiers. These are meant
59  /// as bitmasks, so that searches in C++ can look into the "tag" namespace
60  /// during ordinary lookup.
61  enum IdentifierNamespace {
62    IDNS_Label = 0x1,
63    IDNS_Tag = 0x2,
64    IDNS_Member = 0x4,
65    IDNS_Ordinary = 0x8,
66    IDNS_Protocol = 0x10
67  };
68
69  /// ObjCDeclQualifier - Qualifier used on types in method declarations
70  /// for remote messaging. They are meant for the arguments though and
71  /// applied to the Decls (ObjCMethodDecl and ParmVarDecl).
72  enum ObjCDeclQualifier {
73    OBJC_TQ_None = 0x0,
74    OBJC_TQ_In = 0x1,
75    OBJC_TQ_Inout = 0x2,
76    OBJC_TQ_Out = 0x4,
77    OBJC_TQ_Bycopy = 0x8,
78    OBJC_TQ_Byref = 0x10,
79    OBJC_TQ_Oneway = 0x20
80  };
81
82private:
83  /// Loc - The location that this decl.
84  SourceLocation Loc;
85
86  /// NextDeclarator - If this decl was part of a multi-declarator declaration,
87  /// such as "int X, Y, *Z;" this indicates Decl for the next declarator.
88  Decl *NextDeclarator;
89
90  /// NextDeclInScope - The next declaration within the same lexical
91  /// DeclContext. These pointers form the linked list that is
92  /// traversed via DeclContext's decls_begin()/decls_end().
93  /// FIXME: If NextDeclarator is non-NULL, will it always be the same
94  /// as NextDeclInScope? If so, we can use a
95  /// PointerIntPair<Decl*, 1> to make Decl smaller.
96  Decl *NextDeclInScope;
97
98  friend class DeclContext;
99
100  /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
101  /// For declarations that don't contain C++ scope specifiers, it contains
102  /// the DeclContext where the Decl was declared.
103  /// For declarations with C++ scope specifiers, it contains a MultipleDC*
104  /// with the context where it semantically belongs (SemanticDC) and the
105  /// context where it was lexically declared (LexicalDC).
106  /// e.g.:
107  ///
108  ///   namespace A {
109  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
110  ///   }
111  ///   void A::f(); // SemanticDC == namespace 'A'
112  ///                // LexicalDC == global namespace
113  uintptr_t DeclCtx;
114
115  struct MultipleDC {
116    DeclContext *SemanticDC;
117    DeclContext *LexicalDC;
118  };
119
120  inline bool isInSemaDC() const { return (DeclCtx & 0x1) == 0; }
121  inline bool isOutOfSemaDC() const { return (DeclCtx & 0x1) != 0; }
122  inline MultipleDC *getMultipleDC() const {
123    return reinterpret_cast<MultipleDC*>(DeclCtx & ~0x1);
124  }
125
126  /// DeclKind - This indicates which class this is.
127  Kind DeclKind   :  8;
128
129  /// InvalidDecl - This indicates a semantic error occurred.
130  unsigned int InvalidDecl :  1;
131
132  /// HasAttrs - This indicates whether the decl has attributes or not.
133  unsigned int HasAttrs : 1;
134
135  /// Implicit - Whether this declaration was implicitly generated by
136  /// the implementation rather than explicitly written by the user.
137  bool Implicit : 1;
138
139protected:
140  /// Access - Used by C++ decls for the access specifier.
141  // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
142  unsigned Access : 2;
143  friend class CXXClassMemberWrapper;
144
145  Decl(Kind DK, DeclContext *DC, SourceLocation L)
146    : Loc(L), NextDeclarator(0), NextDeclInScope(0),
147      DeclCtx(reinterpret_cast<uintptr_t>(DC)), DeclKind(DK), InvalidDecl(0),
148      HasAttrs(false), Implicit(false) {
149    if (Decl::CollectingStats()) addDeclKind(DK);
150  }
151
152  virtual ~Decl();
153
154  /// setDeclContext - Set both the semantic and lexical DeclContext
155  /// to DC.
156  void setDeclContext(DeclContext *DC);
157
158public:
159  SourceLocation getLocation() const { return Loc; }
160  void setLocation(SourceLocation L) { Loc = L; }
161
162  Kind getKind() const { return DeclKind; }
163  const char *getDeclKindName() const;
164
165  const DeclContext *getDeclContext() const {
166    if (isInSemaDC())
167      return reinterpret_cast<DeclContext*>(DeclCtx);
168    return getMultipleDC()->SemanticDC;
169  }
170  DeclContext *getDeclContext() {
171    return const_cast<DeclContext*>(
172                         const_cast<const Decl*>(this)->getDeclContext());
173  }
174
175  void setAccess(AccessSpecifier AS) { Access = AS; }
176  AccessSpecifier getAccess() const { return AccessSpecifier(Access); }
177
178  bool hasAttrs() const { return HasAttrs; }
179  void addAttr(Attr *attr);
180  const Attr *getAttrs() const;
181  void swapAttrs(Decl *D);
182  void invalidateAttrs();
183
184  template<typename T> const T *getAttr() const {
185    for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())
186      if (const T *V = dyn_cast<T>(attr))
187        return V;
188
189    return 0;
190  }
191
192  /// setInvalidDecl - Indicates the Decl had a semantic error. This
193  /// allows for graceful error recovery.
194  void setInvalidDecl() { InvalidDecl = 1; }
195  bool isInvalidDecl() const { return (bool) InvalidDecl; }
196
197  /// isImplicit - Indicates whether the declaration was implicitly
198  /// generated by the implementation. If false, this declaration
199  /// was written explicitly in the source code.
200  bool isImplicit() const { return Implicit; }
201  void setImplicit(bool I = true) { Implicit = I; }
202
203  unsigned getIdentifierNamespace() const {
204    switch (DeclKind) {
205    default:
206      if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
207        return IDNS_Ordinary;
208      assert(0 && "Unknown decl kind!");
209    case OverloadedFunction:
210    case Typedef:
211    case EnumConstant:
212    case Var:
213    case CXXClassVar:
214    case ImplicitParam:
215    case ParmVar:
216    case OriginalParmVar:
217    case NonTypeTemplateParm:
218    case ObjCMethod:
219    case ObjCContainer:
220    case ObjCCategory:
221    case ObjCInterface:
222    case ObjCCategoryImpl:
223    case ObjCProperty:
224    case ObjCCompatibleAlias:
225      return IDNS_Ordinary;
226
227    case ObjCProtocol:
228      return IDNS_Protocol;
229
230    case Field:
231    case ObjCAtDefsField:
232    case ObjCIvar:
233      return IDNS_Member;
234
235    case Record:
236    case CXXRecord:
237    case Enum:
238    case TemplateTypeParm:
239      return IDNS_Tag;
240
241    case Namespace:
242    case Template:
243    case FunctionTemplate:
244    case ClassTemplate:
245    case TemplateTemplateParm:
246      return IDNS_Tag | IDNS_Ordinary;
247
248    case ClassTemplateSpecialization:
249      return 0;
250    }
251  }
252
253  bool isInIdentifierNamespace(unsigned NS) const {
254    return getIdentifierNamespace() & NS;
255  }
256
257  /// getLexicalDeclContext - The declaration context where this Decl was
258  /// lexically declared (LexicalDC). May be different from
259  /// getDeclContext() (SemanticDC).
260  /// e.g.:
261  ///
262  ///   namespace A {
263  ///      void f(); // SemanticDC == LexicalDC == 'namespace A'
264  ///   }
265  ///   void A::f(); // SemanticDC == namespace 'A'
266  ///                // LexicalDC == global namespace
267  const DeclContext *getLexicalDeclContext() const {
268    if (isInSemaDC())
269      return reinterpret_cast<DeclContext*>(DeclCtx);
270    return getMultipleDC()->LexicalDC;
271  }
272  DeclContext *getLexicalDeclContext() {
273    return const_cast<DeclContext*>(
274                  const_cast<const Decl*>(this)->getLexicalDeclContext());
275  }
276
277  void setLexicalDeclContext(DeclContext *DC);
278
279  /// getNextDeclarator - If this decl was part of a multi-declarator
280  /// declaration, such as "int X, Y, *Z;" this returns the decl for the next
281  /// declarator.  Otherwise it returns null.
282  Decl *getNextDeclarator() { return NextDeclarator; }
283  const Decl *getNextDeclarator() const { return NextDeclarator; }
284  void setNextDeclarator(Decl *N) { NextDeclarator = N; }
285
286  // isDefinedOutsideFunctionOrMethod - This predicate returns true if this
287  // scoped decl is defined outside the current function or method.  This is
288  // roughly global variables and functions, but also handles enums (which could
289  // be defined inside or outside a function etc).
290  bool isDefinedOutsideFunctionOrMethod() const;
291
292  // getBody - If this Decl represents a declaration for a body of code,
293  //  such as a function or method definition, this method returns the top-level
294  //  Stmt* of that body.  Otherwise this method returns null.
295  virtual Stmt* getBody() const { return 0; }
296
297  // global temp stats (until we have a per-module visitor)
298  static void addDeclKind(Kind k);
299  static bool CollectingStats(bool Enable = false);
300  static void PrintStats();
301
302  /// isTemplateParameter - Determines whether this declartion is a
303  /// template parameter.
304  bool isTemplateParameter() const;
305
306  // Implement isa/cast/dyncast/etc.
307  static bool classof(const Decl *) { return true; }
308  static DeclContext *castToDeclContext(const Decl *);
309  static Decl *castFromDeclContext(const DeclContext *);
310
311  /// Emit - Serialize this Decl to Bitcode.
312  void Emit(llvm::Serializer& S) const;
313
314  /// Create - Deserialize a Decl from Bitcode.
315  static Decl* Create(llvm::Deserializer& D, ASTContext& C);
316
317  /// Destroy - Call destructors and release memory.
318  virtual void Destroy(ASTContext& C);
319
320protected:
321  /// EmitImpl - Provides the subclass-specific serialization logic for
322  ///   serializing out a decl.
323  virtual void EmitImpl(llvm::Serializer& S) const {
324    // FIXME: This will eventually be a pure virtual function.
325    assert (false && "Not implemented.");
326  }
327};
328
329/// DeclContext - This is used only as base class of specific decl types that
330/// can act as declaration contexts. These decls are (only the top classes
331/// that directly derive from DeclContext are mentioned, not their subclasses):
332///
333///   TranslationUnitDecl
334///   NamespaceDecl
335///   FunctionDecl
336///   TagDecl
337///   ObjCMethodDecl
338///   ObjCContainerDecl
339///   ObjCCategoryImplDecl
340///   ObjCImplementationDecl
341///   LinkageSpecDecl
342///   BlockDecl
343///
344class DeclContext {
345  /// DeclKind - This indicates which class this is.
346  Decl::Kind DeclKind   :  8;
347
348  /// LookupPtrKind - Describes what kind of pointer LookupPtr
349  /// actually is.
350  enum LookupPtrKind {
351    /// LookupIsMap - Indicates that LookupPtr is actually a map.
352    LookupIsMap = 7
353  };
354
355  /// LookupPtr - Pointer to a data structure used to lookup
356  /// declarations within this context. If the context contains fewer
357  /// than seven declarations, the number of declarations is provided
358  /// in the 3 lowest-order bits and the upper bits are treated as a
359  /// pointer to an array of NamedDecl pointers. If the context
360  /// contains seven or more declarations, the upper bits are treated
361  /// as a pointer to a DenseMap<DeclarationName, std::vector<NamedDecl*>>.
362  /// FIXME: We need a better data structure for this.
363  llvm::PointerIntPair<void*, 3> LookupPtr;
364
365  /// FirstDecl - The first declaration stored within this declaration
366  /// context.
367  Decl *FirstDecl;
368
369  /// LastDecl - The last declaration stored within this declaration
370  /// context. FIXME: We could probably cache this value somewhere
371  /// outside of the DeclContext, to reduce the size of DeclContext by
372  /// another pointer.
373  Decl *LastDecl;
374
375  /// isLookupMap - Determine if the lookup structure is a
376  /// DenseMap. Othewise, it is an array.
377  bool isLookupMap() const { return LookupPtr.getInt() == LookupIsMap; }
378
379  static Decl *getNextDeclInScope(Decl *D) { return D->NextDeclInScope; }
380
381protected:
382   DeclContext(Decl::Kind K)
383     : DeclKind(K), LookupPtr(), FirstDecl(0), LastDecl(0) { }
384
385  void DestroyDecls(ASTContext &C);
386
387public:
388  ~DeclContext();
389
390  Decl::Kind getDeclKind() const {
391    return DeclKind;
392  }
393  const char *getDeclKindName() const;
394
395  /// getParent - Returns the containing DeclContext.
396  const DeclContext *getParent() const {
397    return cast<Decl>(this)->getDeclContext();
398  }
399  DeclContext *getParent() {
400    return const_cast<DeclContext*>(
401                             const_cast<const DeclContext*>(this)->getParent());
402  }
403
404  /// getLexicalParent - Returns the containing lexical DeclContext. May be
405  /// different from getParent, e.g.:
406  ///
407  ///   namespace A {
408  ///      struct S;
409  ///   }
410  ///   struct A::S {}; // getParent() == namespace 'A'
411  ///                   // getLexicalParent() == translation unit
412  ///
413  const DeclContext *getLexicalParent() const {
414    return cast<Decl>(this)->getLexicalDeclContext();
415  }
416  DeclContext *getLexicalParent() {
417    return const_cast<DeclContext*>(
418                      const_cast<const DeclContext*>(this)->getLexicalParent());
419  }
420
421  bool isFunctionOrMethod() const {
422    switch (DeclKind) {
423      case Decl::Block:
424      case Decl::ObjCMethod:
425        return true;
426
427      default:
428       if (DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast)
429         return true;
430        return false;
431    }
432  }
433
434  bool isFileContext() const {
435    return DeclKind == Decl::TranslationUnit || DeclKind == Decl::Namespace;
436  }
437
438  bool isTranslationUnit() const {
439    return DeclKind == Decl::TranslationUnit;
440  }
441
442  bool isRecord() const {
443    return DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast;
444  }
445
446  bool isNamespace() const {
447    return DeclKind == Decl::Namespace;
448  }
449
450  /// isTransparentContext - Determines whether this context is a
451  /// "transparent" context, meaning that the members declared in this
452  /// context are semantically declared in the nearest enclosing
453  /// non-transparent (opaque) context but are lexically declared in
454  /// this context. For example, consider the enumerators of an
455  /// enumeration type:
456  /// @code
457  /// enum E {
458  ///   Val1
459  /// };
460  /// @endcode
461  /// Here, E is a transparent context, so its enumerator (Val1) will
462  /// appear (semantically) that it is in the same context of E.
463  /// Examples of transparent contexts include: enumerations (except for
464  /// C++0x scoped enums), C++ linkage specifications, and C++0x
465  /// inline namespaces.
466  bool isTransparentContext() const;
467
468  bool Encloses(DeclContext *DC) const {
469    for (; DC; DC = DC->getParent())
470      if (DC == this)
471        return true;
472    return false;
473  }
474
475  /// getPrimaryContext - There may be many different
476  /// declarations of the same entity (including forward declarations
477  /// of classes, multiple definitions of namespaces, etc.), each with
478  /// a different set of declarations. This routine returns the
479  /// "primary" DeclContext structure, which will contain the
480  /// information needed to perform name lookup into this context.
481  DeclContext *getPrimaryContext();
482
483  /// getLookupContext - Retrieve the innermost non-transparent
484  /// context of this context, which corresponds to the innermost
485  /// location from which name lookup can find the entities in this
486  /// context.
487  DeclContext *getLookupContext() {
488    return const_cast<DeclContext *>(
489             const_cast<const DeclContext *>(this)->getLookupContext());
490  }
491  const DeclContext *getLookupContext() const;
492
493  /// \brief Retrieve the nearest enclosing namespace context.
494  DeclContext *getEnclosingNamespaceContext();
495  const DeclContext *getEnclosingNamespaceContext() const {
496    return const_cast<DeclContext *>(this)->getEnclosingNamespaceContext();
497  }
498
499  /// getNextContext - If this is a DeclContext that may have other
500  /// DeclContexts that are semantically connected but syntactically
501  /// different, such as C++ namespaces, this routine retrieves the
502  /// next DeclContext in the link. Iteration through the chain of
503  /// DeclContexts should begin at the primary DeclContext and
504  /// continue until this function returns NULL. For example, given:
505  /// @code
506  /// namespace N {
507  ///   int x;
508  /// }
509  /// namespace N {
510  ///   int y;
511  /// }
512  /// @endcode
513  /// The first occurrence of namespace N will be the primary
514  /// DeclContext. Its getNextContext will return the second
515  /// occurrence of namespace N.
516  DeclContext *getNextContext();
517
518  /// decl_iterator - Iterates through the declarations stored
519  /// within this context.
520  class decl_iterator {
521    /// Current - The current declaration.
522    Decl *Current;
523
524  public:
525    typedef Decl*                     value_type;
526    typedef Decl*                     reference;
527    typedef Decl*                     pointer;
528    typedef std::forward_iterator_tag iterator_category;
529    typedef std::ptrdiff_t            difference_type;
530
531    decl_iterator() : Current(0) { }
532    explicit decl_iterator(Decl *C) : Current(C) { }
533
534    reference operator*() const { return Current; }
535    pointer operator->() const { return Current; }
536
537    decl_iterator& operator++();
538
539    decl_iterator operator++(int) {
540      decl_iterator tmp(*this);
541      ++(*this);
542      return tmp;
543    }
544
545    friend bool operator==(decl_iterator x, decl_iterator y) {
546      return x.Current == y.Current;
547    }
548    friend bool operator!=(decl_iterator x, decl_iterator y) {
549      return x.Current != y.Current;
550    }
551  };
552
553  /// decls_begin/decls_end - Iterate over the declarations stored in
554  /// this context.
555  decl_iterator decls_begin() const { return decl_iterator(FirstDecl); }
556  decl_iterator decls_end()   const { return decl_iterator(); }
557
558  /// specific_decl_iterator - Iterates over a subrange of
559  /// declarations stored in a DeclContext, providing only those that
560  /// are of type SpecificDecl (or a class derived from it). This
561  /// iterator is used, for example, to provide iteration over just
562  /// the fields within a RecordDecl (with SpecificDecl = FieldDecl).
563  template<typename SpecificDecl>
564  class specific_decl_iterator {
565    /// Current - The current, underlying declaration iterator, which
566    /// will either be NULL or will point to a declaration of
567    /// type SpecificDecl.
568    DeclContext::decl_iterator Current;
569
570    /// SkipToNextDecl - Advances the current position up to the next
571    /// declaration of type SpecificDecl that also meets the criteria
572    /// required by Acceptable.
573    void SkipToNextDecl() {
574      while (*Current && !isa<SpecificDecl>(*Current))
575        ++Current;
576    }
577
578  public:
579    typedef SpecificDecl* value_type;
580    typedef SpecificDecl* reference;
581    typedef SpecificDecl* pointer;
582    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
583      difference_type;
584    typedef std::forward_iterator_tag iterator_category;
585
586    specific_decl_iterator() : Current() { }
587
588    /// specific_decl_iterator - Construct a new iterator over a
589    /// subset of the declarations the range [C,
590    /// end-of-declarations). If A is non-NULL, it is a pointer to a
591    /// member function of SpecificDecl that should return true for
592    /// all of the SpecificDecl instances that will be in the subset
593    /// of iterators. For example, if you want Objective-C instance
594    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
595    /// &ObjCMethodDecl::isInstanceMethod.
596    explicit specific_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
597      SkipToNextDecl();
598    }
599
600    reference operator*() const { return cast<SpecificDecl>(*Current); }
601    pointer operator->() const { return cast<SpecificDecl>(*Current); }
602
603    specific_decl_iterator& operator++() {
604      ++Current;
605      SkipToNextDecl();
606      return *this;
607    }
608
609    specific_decl_iterator operator++(int) {
610      specific_decl_iterator tmp(*this);
611      ++(*this);
612      return tmp;
613    }
614
615    friend bool
616    operator==(const specific_decl_iterator& x, const specific_decl_iterator& y) {
617      return x.Current == y.Current;
618    }
619
620    friend bool
621    operator!=(const specific_decl_iterator& x, const specific_decl_iterator& y) {
622      return x.Current != y.Current;
623    }
624  };
625
626  /// \brief Iterates over a filtered subrange of declarations stored
627  /// in a DeclContext.
628  ///
629  /// This iterator visits only those declarations that are of type
630  /// SpecificDecl (or a class derived from it) and that meet some
631  /// additional run-time criteria. This iterator is used, for
632  /// example, to provide access to the instance methods within an
633  /// Objective-C interface (with SpecificDecl = ObjCMethodDecl and
634  /// Acceptable = ObjCMethodDecl::isInstanceMethod).
635  template<typename SpecificDecl, bool (SpecificDecl::*Acceptable)() const>
636  class filtered_decl_iterator {
637    /// Current - The current, underlying declaration iterator, which
638    /// will either be NULL or will point to a declaration of
639    /// type SpecificDecl.
640    DeclContext::decl_iterator Current;
641
642    /// SkipToNextDecl - Advances the current position up to the next
643    /// declaration of type SpecificDecl that also meets the criteria
644    /// required by Acceptable.
645    void SkipToNextDecl() {
646      while (*Current &&
647             (!isa<SpecificDecl>(*Current) ||
648              (Acceptable && !(cast<SpecificDecl>(*Current)->*Acceptable)())))
649        ++Current;
650    }
651
652  public:
653    typedef SpecificDecl* value_type;
654    typedef SpecificDecl* reference;
655    typedef SpecificDecl* pointer;
656    typedef std::iterator_traits<DeclContext::decl_iterator>::difference_type
657      difference_type;
658    typedef std::forward_iterator_tag iterator_category;
659
660    filtered_decl_iterator() : Current() { }
661
662    /// specific_decl_iterator - Construct a new iterator over a
663    /// subset of the declarations the range [C,
664    /// end-of-declarations). If A is non-NULL, it is a pointer to a
665    /// member function of SpecificDecl that should return true for
666    /// all of the SpecificDecl instances that will be in the subset
667    /// of iterators. For example, if you want Objective-C instance
668    /// methods, SpecificDecl will be ObjCMethodDecl and A will be
669    /// &ObjCMethodDecl::isInstanceMethod.
670    explicit filtered_decl_iterator(DeclContext::decl_iterator C) : Current(C) {
671      SkipToNextDecl();
672    }
673
674    reference operator*() const { return cast<SpecificDecl>(*Current); }
675    pointer operator->() const { return cast<SpecificDecl>(*Current); }
676
677    filtered_decl_iterator& operator++() {
678      ++Current;
679      SkipToNextDecl();
680      return *this;
681    }
682
683    filtered_decl_iterator operator++(int) {
684      filtered_decl_iterator tmp(*this);
685      ++(*this);
686      return tmp;
687    }
688
689    friend bool
690    operator==(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
691      return x.Current == y.Current;
692    }
693
694    friend bool
695    operator!=(const filtered_decl_iterator& x, const filtered_decl_iterator& y) {
696      return x.Current != y.Current;
697    }
698  };
699
700  /// @brief Add the declaration D into this context.
701  ///
702  /// This routine should be invoked when the declaration D has first
703  /// been declared, to place D into the context where it was
704  /// (lexically) defined. Every declaration must be added to one
705  /// (and only one!) context, where it can be visited via
706  /// [decls_begin(), decls_end()). Once a declaration has been added
707  /// to its lexical context, the corresponding DeclContext owns the
708  /// declaration.
709  ///
710  /// If D is also a NamedDecl, it will be made visible within its
711  /// semantic context via makeDeclVisibleInContext.
712  void addDecl(Decl *D);
713
714  /// lookup_iterator - An iterator that provides access to the results
715  /// of looking up a name within this context.
716  typedef NamedDecl **lookup_iterator;
717
718  /// lookup_const_iterator - An iterator that provides non-mutable
719  /// access to the results of lookup up a name within this context.
720  typedef NamedDecl * const * lookup_const_iterator;
721
722  typedef std::pair<lookup_iterator, lookup_iterator> lookup_result;
723  typedef std::pair<lookup_const_iterator, lookup_const_iterator>
724    lookup_const_result;
725
726  /// lookup - Find the declarations (if any) with the given Name in
727  /// this context. Returns a range of iterators that contains all of
728  /// the declarations with this name, with object, function, member,
729  /// and enumerator names preceding any tag name. Note that this
730  /// routine will not look into parent contexts.
731  lookup_result lookup(DeclarationName Name);
732  lookup_const_result lookup(DeclarationName Name) const;
733
734  /// @brief Makes a declaration visible within this context.
735  ///
736  /// This routine makes the declaration D visible to name lookup
737  /// within this context and, if this is a transparent context,
738  /// within its parent contexts up to the first enclosing
739  /// non-transparent context. Making a declaration visible within a
740  /// context does not transfer ownership of a declaration, and a
741  /// declaration can be visible in many contexts that aren't its
742  /// lexical context.
743  ///
744  /// If D is a redeclaration of an existing declaration that is
745  /// visible from this context, as determined by
746  /// NamedDecl::declarationReplaces, the previous declaration will be
747  /// replaced with D.
748  void makeDeclVisibleInContext(NamedDecl *D);
749
750  /// udir_iterator - Iterates through the using-directives stored
751  /// within this context.
752  typedef UsingDirectiveDecl * const * udir_iterator;
753
754  typedef std::pair<udir_iterator, udir_iterator> udir_iterator_range;
755
756  udir_iterator_range getUsingDirectives() const;
757
758  udir_iterator using_directives_begin() const {
759    return getUsingDirectives().first;
760  }
761
762  udir_iterator using_directives_end() const {
763    return getUsingDirectives().second;
764  }
765
766  static bool classof(const Decl *D);
767  static bool classof(const DeclContext *D) { return true; }
768#define DECL_CONTEXT(Name) \
769  static bool classof(const Name##Decl *D) { return true; }
770#include "clang/AST/DeclNodes.def"
771
772private:
773  void buildLookup(DeclContext *DCtx);
774  void makeDeclVisibleInContextImpl(NamedDecl *D);
775
776  void EmitOutRec(llvm::Serializer& S) const;
777  void ReadOutRec(llvm::Deserializer& D, ASTContext& C);
778
779  friend class Decl;
780};
781
782inline bool Decl::isTemplateParameter() const {
783  return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm;
784}
785
786inline bool Decl::isDefinedOutsideFunctionOrMethod() const {
787  if (getDeclContext())
788    return !getDeclContext()->getLookupContext()->isFunctionOrMethod();
789  else
790    return true;
791}
792
793inline DeclContext::decl_iterator& DeclContext::decl_iterator::operator++() {
794  Current = getNextDeclInScope(Current);
795  return *this;
796}
797
798} // end clang.
799
800namespace llvm {
801
802/// Implement a isa_impl_wrap specialization to check whether a DeclContext is
803/// a specific Decl.
804template<class ToTy>
805struct isa_impl_wrap<ToTy,
806                     const ::clang::DeclContext,const ::clang::DeclContext> {
807  static bool doit(const ::clang::DeclContext &Val) {
808    return ToTy::classof(::clang::Decl::castFromDeclContext(&Val));
809  }
810};
811template<class ToTy>
812struct isa_impl_wrap<ToTy, ::clang::DeclContext, ::clang::DeclContext>
813  : public isa_impl_wrap<ToTy,
814                      const ::clang::DeclContext,const ::clang::DeclContext> {};
815
816/// Implement cast_convert_val for Decl -> DeclContext conversions.
817template<class FromTy>
818struct cast_convert_val< ::clang::DeclContext, FromTy, FromTy> {
819  static ::clang::DeclContext &doit(const FromTy &Val) {
820    return *FromTy::castToDeclContext(&Val);
821  }
822};
823
824template<class FromTy>
825struct cast_convert_val< ::clang::DeclContext, FromTy*, FromTy*> {
826  static ::clang::DeclContext *doit(const FromTy *Val) {
827    return FromTy::castToDeclContext(Val);
828  }
829};
830
831template<class FromTy>
832struct cast_convert_val< const ::clang::DeclContext, FromTy, FromTy> {
833  static const ::clang::DeclContext &doit(const FromTy &Val) {
834    return *FromTy::castToDeclContext(&Val);
835  }
836};
837
838template<class FromTy>
839struct cast_convert_val< const ::clang::DeclContext, FromTy*, FromTy*> {
840  static const ::clang::DeclContext *doit(const FromTy *Val) {
841    return FromTy::castToDeclContext(Val);
842  }
843};
844
845/// Implement cast_convert_val for DeclContext -> Decl conversions.
846template<class ToTy>
847struct cast_convert_val<ToTy,
848                        const ::clang::DeclContext,const ::clang::DeclContext> {
849  static ToTy &doit(const ::clang::DeclContext &Val) {
850    return *reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(&Val));
851  }
852};
853template<class ToTy>
854struct cast_convert_val<ToTy, ::clang::DeclContext, ::clang::DeclContext>
855  : public cast_convert_val<ToTy,
856                      const ::clang::DeclContext,const ::clang::DeclContext> {};
857
858template<class ToTy>
859struct cast_convert_val<ToTy,
860                     const ::clang::DeclContext*, const ::clang::DeclContext*> {
861  static ToTy *doit(const ::clang::DeclContext *Val) {
862    return reinterpret_cast<ToTy*>(ToTy::castFromDeclContext(Val));
863  }
864};
865template<class ToTy>
866struct cast_convert_val<ToTy, ::clang::DeclContext*, ::clang::DeclContext*>
867  : public cast_convert_val<ToTy,
868                    const ::clang::DeclContext*,const ::clang::DeclContext*> {};
869
870} // end namespace llvm
871
872#endif
873