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