IdentifierResolver.h revision 7cbc558ffda5877ec4d2e432534e3d3d4ac10050
1//===- IdentifierResolver.h - Lexical Scope Name lookup ---------*- 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 IdentifierResolver class, which is used for lexical
11// scoped lookup, based on declaration names.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H
16#define LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H
17
18#include "clang/Basic/IdentifierTable.h"
19
20namespace clang {
21
22class ASTContext;
23class Decl;
24class DeclContext;
25class DeclarationName;
26class NamedDecl;
27class Scope;
28
29/// IdentifierResolver - Keeps track of shadowed decls on enclosing
30/// scopes.  It manages the shadowing chains of declaration names and
31/// implements efficent decl lookup based on a declaration name.
32class IdentifierResolver {
33
34  /// IdDeclInfo - Keeps track of information about decls associated
35  /// to a particular declaration name. IdDeclInfos are lazily
36  /// constructed and assigned to a declaration name the first time a
37  /// decl with that declaration name is shadowed in some scope.
38  class IdDeclInfo {
39  public:
40    typedef llvm::SmallVector<NamedDecl*, 2> DeclsTy;
41
42    inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
43    inline DeclsTy::iterator decls_end() { return Decls.end(); }
44
45    void AddDecl(NamedDecl *D) { Decls.push_back(D); }
46
47    /// RemoveDecl - Remove the decl from the scope chain.
48    /// The decl must already be part of the decl chain.
49    void RemoveDecl(NamedDecl *D);
50
51    /// Replaces the Old declaration with the New declaration. If the
52    /// replacement is successful, returns true. If the old
53    /// declaration was not found, returns false.
54    bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
55
56    /// \brief Insert the given declaration at the given position in the list.
57    void InsertDecl(DeclsTy::iterator Pos, NamedDecl *D) {
58      Decls.insert(Pos, D);
59    }
60
61  private:
62    DeclsTy Decls;
63  };
64
65public:
66
67  /// iterator - Iterate over the decls of a specified declaration name.
68  /// It will walk or not the parent declaration contexts depending on how
69  /// it was instantiated.
70  class iterator {
71  public:
72    typedef NamedDecl *             value_type;
73    typedef NamedDecl *             reference;
74    typedef NamedDecl *             pointer;
75    typedef std::input_iterator_tag iterator_category;
76    typedef std::ptrdiff_t          difference_type;
77
78    /// Ptr - There are 3 forms that 'Ptr' represents:
79    /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
80    /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
81    ///    same declaration context. (Ptr & 0x3 == 0x1)
82    /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
83    ///    declaration contexts too. (Ptr & 0x3 == 0x3)
84    uintptr_t Ptr;
85    typedef IdDeclInfo::DeclsTy::iterator BaseIter;
86
87    /// A single NamedDecl. (Ptr & 0x1 == 0)
88    iterator(NamedDecl *D) {
89      Ptr = reinterpret_cast<uintptr_t>(D);
90      assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
91    }
92    /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
93    /// contexts depending on 'LookInParentCtx'.
94    iterator(BaseIter I) {
95      Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
96    }
97
98    bool isIterator() const { return (Ptr & 0x1); }
99
100    BaseIter getIterator() const {
101      assert(isIterator() && "Ptr not an iterator!");
102      return reinterpret_cast<BaseIter>(Ptr & ~0x3);
103    }
104
105    friend class IdentifierResolver;
106
107    void incrementSlowCase();
108  public:
109    iterator() : Ptr(0) {}
110
111    NamedDecl *operator*() const {
112      if (isIterator())
113        return *getIterator();
114      else
115        return reinterpret_cast<NamedDecl*>(Ptr);
116    }
117
118    bool operator==(const iterator &RHS) const {
119      return Ptr == RHS.Ptr;
120    }
121    bool operator!=(const iterator &RHS) const {
122      return Ptr != RHS.Ptr;
123    }
124
125    // Preincrement.
126    iterator& operator++() {
127      if (!isIterator()) // common case.
128        Ptr = 0;
129      else
130        incrementSlowCase();
131      return *this;
132    }
133
134    uintptr_t getAsOpaqueValue() const { return Ptr; }
135
136    static iterator getFromOpaqueValue(uintptr_t P) {
137      iterator Result;
138      Result.Ptr = P;
139      return Result;
140    }
141  };
142
143  /// begin - Returns an iterator for decls with the name 'Name'.
144  static iterator begin(DeclarationName Name);
145
146  /// end - Returns an iterator that has 'finished'.
147  static iterator end() {
148    return iterator();
149  }
150
151  /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
152  /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
153  /// true if 'D' belongs to the given declaration context.
154  ///
155  /// \param ExplicitInstantiationOrSpecialization When true, we are checking
156  /// whether the declaration is in scope for the purposes of explicit template
157  /// instantiation or specialization. The default is false.
158  bool isDeclInScope(Decl *D, DeclContext *Ctx, ASTContext &Context,
159                     Scope *S = 0,
160                     bool ExplicitInstantiationOrSpecialization = false) const;
161
162  /// AddDecl - Link the decl to its shadowed decl chain.
163  void AddDecl(NamedDecl *D);
164
165  /// RemoveDecl - Unlink the decl from its shadowed decl chain.
166  /// The decl must already be part of the decl chain.
167  void RemoveDecl(NamedDecl *D);
168
169  /// Replace the decl Old with the new declaration New on its
170  /// identifier chain. Returns true if the old declaration was found
171  /// (and, therefore, replaced).
172  bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
173
174  /// \brief Insert the given declaration prior to the given iterator
175  /// position
176  void InsertDecl(iterator Pos, NamedDecl *D);
177
178  /// \brief Link the declaration into the chain of declarations for
179  /// the given identifier.
180  ///
181  /// This is a lower-level routine used by the AST reader to link a
182  /// declaration into a specific IdentifierInfo before the
183  /// declaration actually has a name.
184  void AddDeclToIdentifierChain(IdentifierInfo *II, NamedDecl *D);
185
186  explicit IdentifierResolver(const LangOptions &LangOpt);
187  ~IdentifierResolver();
188
189private:
190  const LangOptions &LangOpt;
191
192  class IdDeclInfoMap;
193  IdDeclInfoMap *IdDeclInfos;
194
195  /// FETokenInfo contains a Decl pointer if lower bit == 0.
196  static inline bool isDeclPtr(void *Ptr) {
197    return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
198  }
199
200  /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
201  static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
202    assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
203          && "Ptr not a IdDeclInfo* !");
204    return reinterpret_cast<IdDeclInfo*>(
205                    reinterpret_cast<uintptr_t>(Ptr) & ~0x1
206                                                            );
207  }
208};
209
210} // end namespace clang
211
212#endif
213