clang  6.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 
19 #include "llvm/ADT/SmallVector.h"
20 
21 namespace clang {
22 
23 class ASTContext;
24 class Decl;
25 class DeclContext;
26 class DeclarationName;
27 class ExternalPreprocessorSource;
28 class NamedDecl;
29 class Preprocessor;
30 class Scope;
31 
32 /// IdentifierResolver - Keeps track of shadowed decls on enclosing
33 /// scopes. It manages the shadowing chains of declaration names and
34 /// implements efficient decl lookup based on a declaration name.
36 
37  /// IdDeclInfo - Keeps track of information about decls associated
38  /// to a particular declaration name. IdDeclInfos are lazily
39  /// constructed and assigned to a declaration name the first time a
40  /// decl with that declaration name is shadowed in some scope.
41  class IdDeclInfo {
42  public:
43  typedef SmallVector<NamedDecl*, 2> DeclsTy;
44 
45  inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
46  inline DeclsTy::iterator decls_end() { return Decls.end(); }
47 
48  void AddDecl(NamedDecl *D) { Decls.push_back(D); }
49 
50  /// RemoveDecl - Remove the decl from the scope chain.
51  /// The decl must already be part of the decl chain.
52  void RemoveDecl(NamedDecl *D);
53 
54  /// \brief Insert the given declaration at the given position in the list.
55  void InsertDecl(DeclsTy::iterator Pos, NamedDecl *D) {
56  Decls.insert(Pos, D);
57  }
58 
59  private:
60  DeclsTy Decls;
61  };
62 
63 public:
64 
65  /// iterator - Iterate over the decls of a specified declaration name.
66  /// It will walk or not the parent declaration contexts depending on how
67  /// it was instantiated.
68  class iterator {
69  public:
70  typedef NamedDecl * value_type;
71  typedef NamedDecl * reference;
72  typedef NamedDecl * pointer;
73  typedef std::input_iterator_tag iterator_category;
75 
76  /// Ptr - There are 2 forms that 'Ptr' represents:
77  /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
78  /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
79  /// same declaration context. (Ptr & 0x1 == 0x1)
81  typedef IdDeclInfo::DeclsTy::iterator BaseIter;
82 
83  /// A single NamedDecl. (Ptr & 0x1 == 0)
85  Ptr = reinterpret_cast<uintptr_t>(D);
86  assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
87  }
88  /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
89  /// contexts depending on 'LookInParentCtx'.
90  iterator(BaseIter I) {
91  Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
92  }
93 
94  bool isIterator() const { return (Ptr & 0x1); }
95 
96  BaseIter getIterator() const {
97  assert(isIterator() && "Ptr not an iterator!");
98  return reinterpret_cast<BaseIter>(Ptr & ~0x1);
99  }
100 
101  friend class IdentifierResolver;
102 
103  void incrementSlowCase();
104  public:
105  iterator() : Ptr(0) {}
106 
107  NamedDecl *operator*() const {
108  if (isIterator())
109  return *getIterator();
110  else
111  return reinterpret_cast<NamedDecl*>(Ptr);
112  }
113 
114  bool operator==(const iterator &RHS) const {
115  return Ptr == RHS.Ptr;
116  }
117  bool operator!=(const iterator &RHS) const {
118  return Ptr != RHS.Ptr;
119  }
120 
121  // Preincrement.
123  if (!isIterator()) // common case.
124  Ptr = 0;
125  else
126  incrementSlowCase();
127  return *this;
128  }
129  };
130 
131  /// begin - Returns an iterator for decls with the name 'Name'.
133 
134  /// end - Returns an iterator that has 'finished'.
136  return iterator();
137  }
138 
139  /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
140  /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
141  /// true if 'D' belongs to the given declaration context.
142  ///
143  /// \param AllowInlineNamespace If \c true, we are checking whether a prior
144  /// declaration is in scope in a declaration that requires a prior
145  /// declaration (because it is either explicitly qualified or is a
146  /// template instantiation or specialization). In this case, a
147  /// declaration is in scope if it's in the inline namespace set of the
148  /// context.
149  bool isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S = nullptr,
150  bool AllowInlineNamespace = false) const;
151 
152  /// AddDecl - Link the decl to its shadowed decl chain.
153  void AddDecl(NamedDecl *D);
154 
155  /// RemoveDecl - Unlink the decl from its shadowed decl chain.
156  /// The decl must already be part of the decl chain.
157  void RemoveDecl(NamedDecl *D);
158 
159  /// \brief Insert the given declaration after the given iterator
160  /// position.
161  void InsertDeclAfter(iterator Pos, NamedDecl *D);
162 
163  /// \brief Try to add the given declaration to the top level scope, if it
164  /// (or a redeclaration of it) hasn't already been added.
165  ///
166  /// \param D The externally-produced declaration to add.
167  ///
168  /// \param Name The name of the externally-produced declaration.
169  ///
170  /// \returns true if the declaration was added, false otherwise.
172 
173  explicit IdentifierResolver(Preprocessor &PP);
175 
176 private:
177  const LangOptions &LangOpt;
178  Preprocessor &PP;
179 
180  class IdDeclInfoMap;
181  IdDeclInfoMap *IdDeclInfos;
182 
183  void updatingIdentifier(IdentifierInfo &II);
184  void readingIdentifier(IdentifierInfo &II);
185 
186  /// FETokenInfo contains a Decl pointer if lower bit == 0.
187  static inline bool isDeclPtr(void *Ptr) {
188  return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
189  }
190 
191  /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
192  static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
193  assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
194  && "Ptr not a IdDeclInfo* !");
195  return reinterpret_cast<IdDeclInfo*>(
196  reinterpret_cast<uintptr_t>(Ptr) & ~0x1
197  );
198  }
199 };
200 
201 } // end namespace clang
202 
203 #endif
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:81
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:48
iterator end()
end - Returns an iterator that has &#39;finished&#39;.
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:39
IdDeclInfoMap - Associates IdDeclInfos with declaration names.
IdentifierResolver - Keeps track of shadowed decls on enclosing scopes.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
__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:1224
IdentifierResolver(Preprocessor &PP)
bool operator==(const iterator &RHS) const
DeclarationName - 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.
std::input_iterator_tag iterator_category
IdDeclInfo::DeclsTy::iterator BaseIter
NamedDecl - This represents a decl with a name.
Definition: Decl.h:213
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:98