clang  8.0.0svn
IdentifierResolver.h
Go to the documentation of this file.
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_SEMA_IDENTIFIERRESOLVER_H
16 #define LLVM_CLANG_SEMA_IDENTIFIERRESOLVER_H
17 
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include <cassert>
21 #include <cstddef>
22 #include <cstdint>
23 #include <iterator>
24 
25 namespace clang {
26 
27 class Decl;
28 class DeclarationName;
29 class DeclContext;
30 class IdentifierInfo;
31 class LangOptions;
32 class NamedDecl;
33 class Preprocessor;
34 class Scope;
35 
36 /// IdentifierResolver - Keeps track of shadowed decls on enclosing
37 /// scopes. It manages the shadowing chains of declaration names and
38 /// implements efficient decl lookup based on a declaration name.
40  /// IdDeclInfo - Keeps track of information about decls associated
41  /// to a particular declaration name. IdDeclInfos are lazily
42  /// constructed and assigned to a declaration name the first time a
43  /// decl with that declaration name is shadowed in some scope.
44  class IdDeclInfo {
45  public:
46  using DeclsTy = SmallVector<NamedDecl *, 2>;
47 
48  DeclsTy::iterator decls_begin() { return Decls.begin(); }
49  DeclsTy::iterator decls_end() { return Decls.end(); }
50 
51  void AddDecl(NamedDecl *D) { Decls.push_back(D); }
52 
53  /// RemoveDecl - Remove the decl from the scope chain.
54  /// The decl must already be part of the decl chain.
55  void RemoveDecl(NamedDecl *D);
56 
57  /// Insert the given declaration at the given position in the list.
58  void InsertDecl(DeclsTy::iterator Pos, NamedDecl *D) {
59  Decls.insert(Pos, D);
60  }
61 
62  private:
63  DeclsTy Decls;
64  };
65 
66 public:
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  friend class IdentifierResolver;
73 
74  using value_type = NamedDecl *;
75  using reference = NamedDecl *;
76  using pointer = NamedDecl *;
77  using iterator_category = std::input_iterator_tag;
79 
80  /// Ptr - There are 2 forms that 'Ptr' represents:
81  /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
82  /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
83  /// same declaration context. (Ptr & 0x1 == 0x1)
84  uintptr_t Ptr = 0;
85  using BaseIter = IdDeclInfo::DeclsTy::iterator;
86 
87  /// A single NamedDecl. (Ptr & 0x1 == 0)
89  Ptr = reinterpret_cast<uintptr_t>(D);
90  assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
91  }
92 
93  /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
94  /// contexts depending on 'LookInParentCtx'.
96  Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
97  }
98 
99  bool isIterator() const { return (Ptr & 0x1); }
100 
102  assert(isIterator() && "Ptr not an iterator!");
103  return reinterpret_cast<BaseIter>(Ptr & ~0x1);
104  }
105 
106  void incrementSlowCase();
107 
108  public:
109  iterator() = default;
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.
127  if (!isIterator()) // common case.
128  Ptr = 0;
129  else
130  incrementSlowCase();
131  return *this;
132  }
133  };
134 
135  explicit IdentifierResolver(Preprocessor &PP);
137 
138  /// begin - Returns an iterator for decls with the name 'Name'.
140 
141  /// end - Returns an iterator that has 'finished'.
143  return iterator();
144  }
145 
146  /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
147  /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
148  /// true if 'D' belongs to the given declaration context.
149  ///
150  /// \param AllowInlineNamespace If \c true, we are checking whether a prior
151  /// declaration is in scope in a declaration that requires a prior
152  /// declaration (because it is either explicitly qualified or is a
153  /// template instantiation or specialization). In this case, a
154  /// declaration is in scope if it's in the inline namespace set of the
155  /// context.
156  bool isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S = nullptr,
157  bool AllowInlineNamespace = false) const;
158 
159  /// AddDecl - Link the decl to its shadowed decl chain.
160  void AddDecl(NamedDecl *D);
161 
162  /// RemoveDecl - Unlink the decl from its shadowed decl chain.
163  /// The decl must already be part of the decl chain.
164  void RemoveDecl(NamedDecl *D);
165 
166  /// Insert the given declaration after the given iterator
167  /// position.
168  void InsertDeclAfter(iterator Pos, NamedDecl *D);
169 
170  /// Try to add the given declaration to the top level scope, if it
171  /// (or a redeclaration of it) hasn't already been added.
172  ///
173  /// \param D The externally-produced declaration to add.
174  ///
175  /// \param Name The name of the externally-produced declaration.
176  ///
177  /// \returns true if the declaration was added, false otherwise.
179 
180 private:
181  const LangOptions &LangOpt;
182  Preprocessor &PP;
183 
184  class IdDeclInfoMap;
185  IdDeclInfoMap *IdDeclInfos;
186 
187  void updatingIdentifier(IdentifierInfo &II);
188  void readingIdentifier(IdentifierInfo &II);
189 
190  /// FETokenInfo contains a Decl pointer if lower bit == 0.
191  static inline bool isDeclPtr(void *Ptr) {
192  return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
193  }
194 
195  /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
196  static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
197  assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
198  && "Ptr not a IdDeclInfo* !");
199  return reinterpret_cast<IdDeclInfo*>(
200  reinterpret_cast<uintptr_t>(Ptr) & ~0x1);
201  }
202 };
203 
204 } // namespace clang
205 
206 #endif // LLVM_CLANG_SEMA_IDENTIFIERRESOLVER_H
iterator(NamedDecl *D)
A single NamedDecl. (Ptr & 0x1 == 0)
bool tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name)
Try to add the given declaration to the top level scope, if it (or a redeclaration of it) hasn&#39;t alre...
bool isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S=nullptr, bool AllowInlineNamespace=false) const
isDeclInScope - If &#39;Ctx&#39; is a function/method, isDeclInScope returns true if &#39;D&#39; is in Scope &#39;S&#39;...
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
iterator begin(DeclarationName Name)
begin - Returns an iterator for decls with the name &#39;Name&#39;.
One of these records is kept for each identifier that is lexed.
bool operator!=(const iterator &RHS) const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
iterator end()
end - Returns an iterator that has &#39;finished&#39;.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
void RemoveDecl(NamedDecl *D)
RemoveDecl - Unlink the decl from its shadowed decl chain.
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
IdDeclInfoMap - Associates IdDeclInfos with declaration names.
IdDeclInfo::DeclsTy::iterator BaseIter
std::input_iterator_tag iterator_category
IdentifierResolver - Keeps track of shadowed decls on enclosing scopes.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c.h:82
__PTRDIFF_TYPE__ ptrdiff_t
A signed integer type that is the result of subtracting two pointers.
Definition: opencl-c.h:68
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1268
IdentifierResolver(Preprocessor &PP)
bool operator==(const iterator &RHS) const
The name of a declaration.
void InsertDeclAfter(iterator Pos, NamedDecl *D)
Insert the given declaration after the given iterator position.
void AddDecl(NamedDecl *D)
AddDecl - Link the decl to its shadowed decl chain.
iterator(BaseIter I)
A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration contexts depending on &#39;LookI...
uintptr_t Ptr
Ptr - There are 2 forms that &#39;Ptr&#39; represents: 1) A single NamedDecl.
This represents a decl that may have a name.
Definition: Decl.h:248
bool isIterator(const CXXRecordDecl *CRD)
iterator - Iterate over the decls of a specified declaration name.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:127