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