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