Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 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 | // |
Argyrios Kyrtzidis | 321f278 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 10 | // This file defines the IdentifierResolver class, which is used for lexical |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 11 | // scoped lookup, based on declaration names. |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H |
| 16 | #define LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H |
| 17 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 18 | #include "clang/Basic/IdentifierTable.h" |
| 19 | #include "clang/Parse/Scope.h" |
| 20 | #include "clang/AST/Decl.h" |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclarationName.h" |
Argyrios Kyrtzidis | 32a5ba0 | 2008-06-24 23:08:34 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 23 | |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 24 | namespace clang { |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 25 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 26 | /// IdentifierResolver - Keeps track of shadowed decls on enclosing |
| 27 | /// scopes. It manages the shadowing chains of declaration names and |
| 28 | /// implements efficent decl lookup based on a declaration name. |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 29 | class IdentifierResolver { |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 30 | |
| 31 | /// LookupContext - A wrapper for DeclContext. DeclContext is only part of |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 32 | /// Decls, LookupContext can be used with all decls (assumes |
| 33 | /// translation unit context for non Decls). |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 34 | class LookupContext { |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 35 | const DeclContext *Ctx; |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 36 | |
| 37 | /// TUCtx - Provides a common value for translation unit context for all |
| 38 | /// decls. |
| 39 | /// FIXME: When (if ?) all decls can point to their translation unit context |
| 40 | /// remove this hack. |
| 41 | static inline DeclContext *TUCtx() { |
| 42 | return reinterpret_cast<DeclContext*>(-1); |
| 43 | } |
| 44 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 45 | /// getContext - Returns translation unit context for non Decls and |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 46 | /// for EnumConstantDecls returns the parent context of their EnumDecl. |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 47 | static DeclContext *getContext(Decl *D); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 48 | |
| 49 | public: |
| 50 | LookupContext(Decl *D) { |
| 51 | Ctx = getContext(D); |
| 52 | } |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 53 | LookupContext(const DeclContext *DC) { |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 54 | if (!DC || isa<TranslationUnitDecl>(DC)) |
| 55 | Ctx = TUCtx(); |
| 56 | else |
| 57 | Ctx = DC; |
| 58 | } |
| 59 | |
| 60 | bool isTU() const { |
| 61 | return (Ctx == TUCtx()); |
| 62 | } |
| 63 | |
| 64 | /// getParent - Returns the parent context. This should not be called for |
| 65 | /// a translation unit context. |
| 66 | LookupContext getParent() const { |
| 67 | assert(!isTU() && "TU has no parent!"); |
| 68 | return LookupContext(Ctx->getParent()); |
| 69 | } |
| 70 | |
| 71 | /// isEqOrContainedBy - Returns true of the given context is the same or a |
| 72 | /// parent of this one. |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 73 | bool isEqOrContainedBy(const LookupContext &PC) const; |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 74 | |
| 75 | bool operator==(const LookupContext &RHS) const { |
| 76 | return Ctx == RHS.Ctx; |
| 77 | } |
| 78 | bool operator!=(const LookupContext &RHS) const { |
| 79 | return Ctx != RHS.Ctx; |
| 80 | } |
| 81 | }; |
| 82 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 83 | /// IdDeclInfo - Keeps track of information about decls associated |
| 84 | /// to a particular declaration name. IdDeclInfos are lazily |
| 85 | /// constructed and assigned to a declaration name the first time a |
| 86 | /// decl with that declaration name is shadowed in some scope. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 87 | class IdDeclInfo { |
| 88 | public: |
| 89 | typedef llvm::SmallVector<NamedDecl*, 2> DeclsTy; |
| 90 | |
| 91 | inline DeclsTy::iterator decls_begin() { return Decls.begin(); } |
| 92 | inline DeclsTy::iterator decls_end() { return Decls.end(); } |
| 93 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 94 | void AddDecl(NamedDecl *D) { Decls.push_back(D); } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 95 | |
| 96 | /// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl. |
| 97 | /// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must |
| 98 | /// be already added to the scope chain and must be in the same context as |
| 99 | /// the decl that we want to add. |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 100 | void AddShadowed(NamedDecl *D, NamedDecl *Shadow); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 101 | |
| 102 | /// RemoveDecl - Remove the decl from the scope chain. |
| 103 | /// The decl must already be part of the decl chain. |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 104 | void RemoveDecl(NamedDecl *D); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 105 | |
| 106 | private: |
| 107 | DeclsTy Decls; |
| 108 | }; |
| 109 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 110 | public: |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 111 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 112 | /// iterator - Iterate over the decls of a specified declaration name. |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 113 | /// It will walk or not the parent declaration contexts depending on how |
| 114 | /// it was instantiated. |
| 115 | class iterator { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 116 | public: |
| 117 | typedef NamedDecl * value_type; |
| 118 | typedef NamedDecl * reference; |
| 119 | typedef NamedDecl * pointer; |
| 120 | typedef std::input_iterator_tag iterator_category; |
| 121 | typedef std::ptrdiff_t difference_type; |
| 122 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 123 | /// Ptr - There are 3 forms that 'Ptr' represents: |
| 124 | /// 1) A single NamedDecl. (Ptr & 0x1 == 0) |
| 125 | /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the |
| 126 | /// same declaration context. (Ptr & 0x3 == 0x1) |
| 127 | /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent |
| 128 | /// declaration contexts too. (Ptr & 0x3 == 0x3) |
| 129 | uintptr_t Ptr; |
| 130 | typedef IdDeclInfo::DeclsTy::iterator BaseIter; |
| 131 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 132 | /// A single NamedDecl. (Ptr & 0x1 == 0) |
| 133 | iterator(NamedDecl *D) { |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 134 | Ptr = reinterpret_cast<uintptr_t>(D); |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 135 | assert((Ptr & 0x1) == 0 && "Invalid Ptr!"); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 136 | } |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 137 | /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration |
| 138 | /// contexts depending on 'LookInParentCtx'. |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame^] | 139 | iterator(BaseIter I) { |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 140 | Ptr = reinterpret_cast<uintptr_t>(I) | 0x1; |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | bool isIterator() const { return (Ptr & 0x1); } |
| 144 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 145 | BaseIter getIterator() const { |
| 146 | assert(isIterator() && "Ptr not an iterator!"); |
| 147 | return reinterpret_cast<BaseIter>(Ptr & ~0x3); |
| 148 | } |
| 149 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 150 | friend class IdentifierResolver; |
| 151 | public: |
Argyrios Kyrtzidis | b53c784 | 2008-08-01 10:20:48 +0000 | [diff] [blame] | 152 | iterator() : Ptr(0) {} |
| 153 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 154 | NamedDecl *operator*() const { |
| 155 | if (isIterator()) |
| 156 | return *getIterator(); |
| 157 | else |
| 158 | return reinterpret_cast<NamedDecl*>(Ptr); |
| 159 | } |
| 160 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 161 | bool operator==(const iterator &RHS) const { |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 162 | return Ptr == RHS.Ptr; |
| 163 | } |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 164 | bool operator!=(const iterator &RHS) const { |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 165 | return Ptr != RHS.Ptr; |
| 166 | } |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 167 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 168 | // Preincrement. |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 169 | iterator& operator++() { |
| 170 | if (!isIterator()) // common case. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 171 | Ptr = 0; |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame^] | 172 | else { |
| 173 | NamedDecl *D = **this; |
| 174 | void *InfoPtr = D->getDeclName().getFETokenInfo<void>(); |
| 175 | assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?"); |
| 176 | IdDeclInfo *Info = toIdDeclInfo(InfoPtr); |
| 177 | |
| 178 | BaseIter I = getIterator(); |
| 179 | if (I != Info->decls_begin()) |
| 180 | *this = iterator(I-1); |
| 181 | else // No more decls. |
| 182 | *this = iterator(); |
| 183 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 184 | return *this; |
| 185 | } |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 186 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 187 | uintptr_t getAsOpaqueValue() const { return Ptr; } |
| 188 | |
| 189 | static iterator getFromOpaqueValue(uintptr_t P) { |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame^] | 190 | iterator Result; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 191 | Result.Ptr = P; |
| 192 | return Result; |
| 193 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame^] | 196 | /// begin - Returns an iterator for decls with the name 'Name'. |
| 197 | static iterator begin(DeclarationName Name); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 198 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 199 | /// end - Returns an iterator that has 'finished'. |
| 200 | static iterator end() { |
| 201 | return iterator(); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Argyrios Kyrtzidis | e29f0a4 | 2008-05-14 10:49:47 +0000 | [diff] [blame] | 204 | /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true |
| 205 | /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns |
| 206 | /// true if 'D' belongs to the given declaration context. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 207 | bool isDeclInScope(Decl *D, DeclContext *Ctx, ASTContext &Context, |
| 208 | Scope *S = 0) const; |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 209 | |
Argyrios Kyrtzidis | 321f278 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 210 | /// AddDecl - Link the decl to its shadowed decl chain. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 211 | void AddDecl(NamedDecl *D); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 212 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 213 | /// AddShadowedDecl - Link the decl to its shadowed decl chain putting it |
Argyrios Kyrtzidis | 3d0d83a | 2008-05-15 17:26:35 +0000 | [diff] [blame] | 214 | /// after the decl that the iterator points to, thus the 'Shadow' decl will be |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 215 | /// encountered before the 'D' decl. |
| 216 | void AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 217 | |
Argyrios Kyrtzidis | 321f278 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 218 | /// RemoveDecl - Unlink the decl from its shadowed decl chain. |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 219 | /// The decl must already be part of the decl chain. |
| 220 | void RemoveDecl(NamedDecl *D); |
| 221 | |
Argyrios Kyrtzidis | eb7c388 | 2008-09-09 21:32:02 +0000 | [diff] [blame] | 222 | explicit IdentifierResolver(const LangOptions &LangOpt); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 223 | ~IdentifierResolver(); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 224 | |
| 225 | private: |
Argyrios Kyrtzidis | eb7c388 | 2008-09-09 21:32:02 +0000 | [diff] [blame] | 226 | const LangOptions &LangOpt; |
| 227 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 228 | class IdDeclInfoMap; |
| 229 | IdDeclInfoMap *IdDeclInfos; |
| 230 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 231 | /// FETokenInfo contains a Decl pointer if lower bit == 0. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 232 | static inline bool isDeclPtr(void *Ptr) { |
| 233 | return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0; |
| 234 | } |
| 235 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 236 | /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 237 | static inline IdDeclInfo *toIdDeclInfo(void *Ptr) { |
| 238 | assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1 |
| 239 | && "Ptr not a IdDeclInfo* !"); |
| 240 | return reinterpret_cast<IdDeclInfo*>( |
| 241 | reinterpret_cast<uintptr_t>(Ptr) & ~0x1 |
| 242 | ); |
| 243 | } |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | } // end namespace clang |
| 247 | |
| 248 | #endif |