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