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 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 31 | /// IdDeclInfo - Keeps track of information about decls associated |
| 32 | /// to a particular declaration name. IdDeclInfos are lazily |
| 33 | /// constructed and assigned to a declaration name the first time a |
| 34 | /// decl with that declaration name is shadowed in some scope. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 35 | class IdDeclInfo { |
| 36 | public: |
| 37 | typedef llvm::SmallVector<NamedDecl*, 2> DeclsTy; |
| 38 | |
| 39 | inline DeclsTy::iterator decls_begin() { return Decls.begin(); } |
| 40 | inline DeclsTy::iterator decls_end() { return Decls.end(); } |
| 41 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 42 | void AddDecl(NamedDecl *D) { Decls.push_back(D); } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 43 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 44 | /// RemoveDecl - Remove the decl from the scope chain. |
| 45 | /// The decl must already be part of the decl chain. |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 46 | void RemoveDecl(NamedDecl *D); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 48 | /// Replaces the Old declaration with the New declaration. If the |
| 49 | /// replacement is successful, returns true. If the old |
| 50 | /// declaration was not found, returns false. |
| 51 | bool ReplaceDecl(NamedDecl *Old, NamedDecl *New); |
| 52 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 53 | private: |
| 54 | DeclsTy Decls; |
| 55 | }; |
| 56 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 57 | public: |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 58 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 59 | /// iterator - Iterate over the decls of a specified declaration name. |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 60 | /// It will walk or not the parent declaration contexts depending on how |
| 61 | /// it was instantiated. |
| 62 | class iterator { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 63 | public: |
| 64 | typedef NamedDecl * value_type; |
| 65 | typedef NamedDecl * reference; |
| 66 | typedef NamedDecl * pointer; |
| 67 | typedef std::input_iterator_tag iterator_category; |
| 68 | typedef std::ptrdiff_t difference_type; |
| 69 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 70 | /// Ptr - There are 3 forms that 'Ptr' represents: |
| 71 | /// 1) A single NamedDecl. (Ptr & 0x1 == 0) |
| 72 | /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the |
| 73 | /// same declaration context. (Ptr & 0x3 == 0x1) |
| 74 | /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent |
| 75 | /// declaration contexts too. (Ptr & 0x3 == 0x3) |
| 76 | uintptr_t Ptr; |
| 77 | typedef IdDeclInfo::DeclsTy::iterator BaseIter; |
| 78 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 79 | /// A single NamedDecl. (Ptr & 0x1 == 0) |
| 80 | iterator(NamedDecl *D) { |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 81 | Ptr = reinterpret_cast<uintptr_t>(D); |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 82 | assert((Ptr & 0x1) == 0 && "Invalid Ptr!"); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 83 | } |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 84 | /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration |
| 85 | /// contexts depending on 'LookInParentCtx'. |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 86 | iterator(BaseIter I) { |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 87 | Ptr = reinterpret_cast<uintptr_t>(I) | 0x1; |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool isIterator() const { return (Ptr & 0x1); } |
| 91 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 92 | BaseIter getIterator() const { |
| 93 | assert(isIterator() && "Ptr not an iterator!"); |
| 94 | return reinterpret_cast<BaseIter>(Ptr & ~0x3); |
| 95 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 97 | friend class IdentifierResolver; |
| 98 | public: |
Argyrios Kyrtzidis | b53c784 | 2008-08-01 10:20:48 +0000 | [diff] [blame] | 99 | iterator() : Ptr(0) {} |
| 100 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 101 | NamedDecl *operator*() const { |
| 102 | if (isIterator()) |
| 103 | return *getIterator(); |
| 104 | else |
| 105 | return reinterpret_cast<NamedDecl*>(Ptr); |
| 106 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 108 | bool operator==(const iterator &RHS) const { |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 109 | return Ptr == RHS.Ptr; |
| 110 | } |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 111 | bool operator!=(const iterator &RHS) const { |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 112 | return Ptr != RHS.Ptr; |
| 113 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 115 | // Preincrement. |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 116 | iterator& operator++() { |
| 117 | if (!isIterator()) // common case. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 118 | Ptr = 0; |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 119 | else { |
| 120 | NamedDecl *D = **this; |
| 121 | void *InfoPtr = D->getDeclName().getFETokenInfo<void>(); |
| 122 | assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?"); |
| 123 | IdDeclInfo *Info = toIdDeclInfo(InfoPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 125 | BaseIter I = getIterator(); |
| 126 | if (I != Info->decls_begin()) |
| 127 | *this = iterator(I-1); |
| 128 | else // No more decls. |
| 129 | *this = iterator(); |
| 130 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 131 | return *this; |
| 132 | } |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 133 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 134 | uintptr_t getAsOpaqueValue() const { return Ptr; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 136 | static iterator getFromOpaqueValue(uintptr_t P) { |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 137 | iterator Result; |
Douglas Gregor | eb11cd0 | 2009-01-14 22:20:51 +0000 | [diff] [blame] | 138 | Result.Ptr = P; |
| 139 | return Result; |
| 140 | } |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 143 | /// begin - Returns an iterator for decls with the name 'Name'. |
| 144 | static iterator begin(DeclarationName Name); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 145 | |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 146 | /// end - Returns an iterator that has 'finished'. |
| 147 | static iterator end() { |
| 148 | return iterator(); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Argyrios Kyrtzidis | e29f0a4 | 2008-05-14 10:49:47 +0000 | [diff] [blame] | 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. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 154 | bool isDeclInScope(Decl *D, DeclContext *Ctx, ASTContext &Context, |
| 155 | Scope *S = 0) const; |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 156 | |
Argyrios Kyrtzidis | 321f278 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 157 | /// AddDecl - Link the decl to its shadowed decl chain. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 158 | void AddDecl(NamedDecl *D); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 159 | |
Argyrios Kyrtzidis | 321f278 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 160 | /// RemoveDecl - Unlink the decl from its shadowed decl chain. |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 161 | /// The decl must already be part of the decl chain. |
| 162 | void RemoveDecl(NamedDecl *D); |
| 163 | |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 164 | /// Replace the decl Old with the new declaration New on its |
| 165 | /// identifier chain. Returns true if the old declaration was found |
| 166 | /// (and, therefore, replaced). |
| 167 | bool ReplaceDecl(NamedDecl *Old, NamedDecl *New); |
| 168 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 169 | /// \brief Link the declaration into the chain of declarations for |
| 170 | /// the given identifier. |
| 171 | /// |
| 172 | /// This is a lower-level routine used by the PCH reader to link a |
| 173 | /// declaration into a specific IdentifierInfo before the |
| 174 | /// declaration actually has a name. |
| 175 | void AddDeclToIdentifierChain(IdentifierInfo *II, NamedDecl *D); |
| 176 | |
Argyrios Kyrtzidis | eb7c388 | 2008-09-09 21:32:02 +0000 | [diff] [blame] | 177 | explicit IdentifierResolver(const LangOptions &LangOpt); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 178 | ~IdentifierResolver(); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 179 | |
| 180 | private: |
Argyrios Kyrtzidis | eb7c388 | 2008-09-09 21:32:02 +0000 | [diff] [blame] | 181 | const LangOptions &LangOpt; |
| 182 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 183 | class IdDeclInfoMap; |
| 184 | IdDeclInfoMap *IdDeclInfos; |
| 185 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 186 | /// FETokenInfo contains a Decl pointer if lower bit == 0. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 187 | static inline bool isDeclPtr(void *Ptr) { |
| 188 | return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0; |
| 189 | } |
| 190 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 191 | /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | } // end namespace clang |
| 202 | |
| 203 | #endif |