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