| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 1 | //===- IdentifierResolver.cpp - 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 | dfd5222 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 10 | // This file implements the IdentifierResolver class, which is used for lexical | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 11 | // scoped lookup, based on declaration names. | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
| Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/IdentifierResolver.h" | 
| John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" | 
| Argyrios Kyrtzidis | 2bdac73 | 2008-09-09 21:57:58 +0000 | [diff] [blame] | 17 | #include "clang/Basic/LangOptions.h" | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 18 | #include "clang/Lex/ExternalPreprocessorSource.h" | 
|  | 19 | #include "clang/Lex/Preprocessor.h" | 
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Scope.h" | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 21 |  | 
|  | 22 | using namespace clang; | 
|  | 23 |  | 
| Argyrios Kyrtzidis | 4f11d78 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// | 
|  | 25 | // IdDeclInfoMap class | 
|  | 26 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 27 |  | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 28 | /// IdDeclInfoMap - Associates IdDeclInfos with declaration names. | 
| Argyrios Kyrtzidis | dfd5222 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 29 | /// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 30 | /// individual IdDeclInfo to heap. | 
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 31 | class IdentifierResolver::IdDeclInfoMap { | 
| John McCall | 5f5cbf5 | 2010-02-15 19:38:00 +0000 | [diff] [blame] | 32 | static const unsigned int POOL_SIZE = 512; | 
|  | 33 |  | 
|  | 34 | /// We use our own linked-list implementation because it is sadly | 
|  | 35 | /// impossible to add something to a pre-C++0x STL container without | 
|  | 36 | /// a completely unnecessary copy. | 
|  | 37 | struct IdDeclInfoPool { | 
|  | 38 | IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {} | 
|  | 39 |  | 
|  | 40 | IdDeclInfoPool *Next; | 
|  | 41 | IdDeclInfo Pool[POOL_SIZE]; | 
|  | 42 | }; | 
|  | 43 |  | 
|  | 44 | IdDeclInfoPool *CurPool; | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 45 | unsigned int CurIndex; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 |  | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 47 | public: | 
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 48 | IdDeclInfoMap() : CurPool(nullptr), CurIndex(POOL_SIZE) {} | 
| John McCall | 5f5cbf5 | 2010-02-15 19:38:00 +0000 | [diff] [blame] | 49 |  | 
|  | 50 | ~IdDeclInfoMap() { | 
|  | 51 | IdDeclInfoPool *Cur = CurPool; | 
|  | 52 | while (IdDeclInfoPool *P = Cur) { | 
|  | 53 | Cur = Cur->Next; | 
|  | 54 | delete P; | 
|  | 55 | } | 
|  | 56 | } | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 57 |  | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 58 | /// Returns the IdDeclInfo associated to the DeclarationName. | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 59 | /// It creates a new IdDeclInfo if one was not created before for this id. | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 60 | IdDeclInfo &operator[](DeclarationName Name); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 61 | }; | 
|  | 62 |  | 
| Argyrios Kyrtzidis | 25f54c7 | 2008-04-14 00:09:21 +0000 | [diff] [blame] | 63 |  | 
| Argyrios Kyrtzidis | 4f11d78 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 64 | //===----------------------------------------------------------------------===// | 
| Argyrios Kyrtzidis | 4f11d78 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 65 | // IdDeclInfo Implementation | 
|  | 66 | //===----------------------------------------------------------------------===// | 
|  | 67 |  | 
| Argyrios Kyrtzidis | 4f11d78 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 68 | /// RemoveDecl - Remove the decl from the scope chain. | 
|  | 69 | /// The decl must already be part of the decl chain. | 
|  | 70 | void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) { | 
|  | 71 | for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) { | 
|  | 72 | if (D == *(I-1)) { | 
|  | 73 | Decls.erase(I-1); | 
|  | 74 | return; | 
|  | 75 | } | 
|  | 76 | } | 
|  | 77 |  | 
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 78 | llvm_unreachable("Didn't find this decl on its identifier's chain!"); | 
| Argyrios Kyrtzidis | 4f11d78 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
| Argyrios Kyrtzidis | 4f11d78 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 81 | //===----------------------------------------------------------------------===// | 
|  | 82 | // IdentifierResolver Implementation | 
|  | 83 | //===----------------------------------------------------------------------===// | 
|  | 84 |  | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 85 | IdentifierResolver::IdentifierResolver(Preprocessor &PP) | 
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 86 | : LangOpt(PP.getLangOpts()), PP(PP), | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 87 | IdDeclInfos(new IdDeclInfoMap) { | 
| Argyrios Kyrtzidis | f4b92e6 | 2008-09-09 21:32:02 +0000 | [diff] [blame] | 88 | } | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 89 |  | 
| Argyrios Kyrtzidis | 25f54c7 | 2008-04-14 00:09:21 +0000 | [diff] [blame] | 90 | IdentifierResolver::~IdentifierResolver() { | 
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 91 | delete IdDeclInfos; | 
| Argyrios Kyrtzidis | 25f54c7 | 2008-04-14 00:09:21 +0000 | [diff] [blame] | 92 | } | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 93 |  | 
| Argyrios Kyrtzidis | 4f11d78 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 94 | /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true | 
|  | 95 | /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns | 
|  | 96 | /// true if 'D' belongs to the given declaration context. | 
| Nico Weber | 555d1aa | 2012-12-17 03:51:09 +0000 | [diff] [blame] | 97 | bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S, | 
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 98 | bool AllowInlineNamespace) const { | 
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 99 | Ctx = Ctx->getRedeclContext(); | 
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 100 |  | 
| Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 101 | if (Ctx->isFunctionOrMethod() || (S && S->isFunctionPrototypeScope())) { | 
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 102 | // Ignore the scopes associated within transparent declaration contexts. | 
| Ted Kremenek | c37877d | 2013-10-08 17:08:03 +0000 | [diff] [blame] | 103 | while (S->getEntity() && S->getEntity()->isTransparentContext()) | 
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 104 | S = S->getParent(); | 
|  | 105 |  | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 106 | if (S->isDeclScope(D)) | 
| Argyrios Kyrtzidis | 2bdac73 | 2008-09-09 21:57:58 +0000 | [diff] [blame] | 107 | return true; | 
|  | 108 | if (LangOpt.CPlusPlus) { | 
| Sebastian Redl | b219c90 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 109 | // C++ 3.3.2p3: | 
|  | 110 | // The name declared in a catch exception-declaration is local to the | 
|  | 111 | // handler and shall not be redeclared in the outermost block of the | 
|  | 112 | // handler. | 
| Argyrios Kyrtzidis | 2bdac73 | 2008-09-09 21:57:58 +0000 | [diff] [blame] | 113 | // C++ 3.3.2p4: | 
|  | 114 | // Names declared in the for-init-statement, and in the condition of if, | 
|  | 115 | // while, for, and switch statements are local to the if, while, for, or | 
|  | 116 | // switch statement (including the controlled statement), and shall not be | 
|  | 117 | // redeclared in a subsequent condition of that statement nor in the | 
|  | 118 | // outermost block (or, for the if statement, any of the outermost blocks) | 
|  | 119 | // of the controlled statement. | 
|  | 120 | // | 
|  | 121 | assert(S->getParent() && "No TUScope?"); | 
| David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 122 | if (S->getParent()->getFlags() & Scope::ControlScope) { | 
| David Blaikie | 1d61780 | 2012-11-12 22:25:41 +0000 | [diff] [blame] | 123 | S = S->getParent(); | 
|  | 124 | if (S->isDeclScope(D)) | 
|  | 125 | return true; | 
| David Blaikie | 1c9c904 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 126 | } | 
| David Blaikie | 1d61780 | 2012-11-12 22:25:41 +0000 | [diff] [blame] | 127 | if (S->getFlags() & Scope::FnTryCatchScope) | 
|  | 128 | return S->getParent()->isDeclScope(D); | 
| Argyrios Kyrtzidis | 2bdac73 | 2008-09-09 21:57:58 +0000 | [diff] [blame] | 129 | } | 
|  | 130 | return false; | 
|  | 131 | } | 
| Argyrios Kyrtzidis | 4f11d78 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 132 |  | 
| Richard Smith | 5971e8c | 2014-08-27 22:31:34 +0000 | [diff] [blame] | 133 | // FIXME: If D is a local extern declaration, this check doesn't make sense; | 
|  | 134 | // we should be checking its lexical context instead in that case, because | 
|  | 135 | // that is its scope. | 
| Douglas Gregor | db44611 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 136 | DeclContext *DCtx = D->getDeclContext()->getRedeclContext(); | 
| Richard Smith | 72bcaec | 2013-12-05 04:30:04 +0000 | [diff] [blame] | 137 | return AllowInlineNamespace ? Ctx->InEnclosingNamespaceSetOf(DCtx) | 
|  | 138 | : Ctx->Equals(DCtx); | 
| Argyrios Kyrtzidis | 4f11d78 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
| Argyrios Kyrtzidis | dfd5222 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 141 | /// AddDecl - Link the decl to its shadowed decl chain. | 
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 142 | void IdentifierResolver::AddDecl(NamedDecl *D) { | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 143 | DeclarationName Name = D->getDeclName(); | 
| Sebastian Redl | 671eee9 | 2010-07-30 17:25:10 +0000 | [diff] [blame] | 144 | if (IdentifierInfo *II = Name.getAsIdentifierInfo()) | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 145 | updatingIdentifier(*II); | 
| Sebastian Redl | 671eee9 | 2010-07-30 17:25:10 +0000 | [diff] [blame] | 146 |  | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 147 | void *Ptr = Name.getFETokenInfo<void>(); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 148 |  | 
|  | 149 | if (!Ptr) { | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 150 | Name.setFETokenInfo(D); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 151 | return; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | IdDeclInfo *IDI; | 
|  | 155 |  | 
|  | 156 | if (isDeclPtr(Ptr)) { | 
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 157 | Name.setFETokenInfo(nullptr); | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 158 | IDI = &(*IdDeclInfos)[Name]; | 
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 159 | NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr); | 
|  | 160 | IDI->AddDecl(PrevD); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 161 | } else | 
|  | 162 | IDI = toIdDeclInfo(Ptr); | 
|  | 163 |  | 
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 164 | IDI->AddDecl(D); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
| Douglas Gregor | 46c04e7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 167 | void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) { | 
|  | 168 | DeclarationName Name = D->getDeclName(); | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 169 | if (IdentifierInfo *II = Name.getAsIdentifierInfo()) | 
|  | 170 | updatingIdentifier(*II); | 
|  | 171 |  | 
| Douglas Gregor | 46c04e7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 172 | void *Ptr = Name.getFETokenInfo<void>(); | 
|  | 173 |  | 
| Douglas Gregor | 33f352c | 2011-03-24 10:35:39 +0000 | [diff] [blame] | 174 | if (!Ptr) { | 
| Douglas Gregor | 88764cf | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 175 | AddDecl(D); | 
|  | 176 | return; | 
|  | 177 | } | 
| Douglas Gregor | 88764cf | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 178 |  | 
| Douglas Gregor | 33f352c | 2011-03-24 10:35:39 +0000 | [diff] [blame] | 179 | if (isDeclPtr(Ptr)) { | 
|  | 180 | // We only have a single declaration: insert before or after it, | 
|  | 181 | // as appropriate. | 
|  | 182 | if (Pos == iterator()) { | 
|  | 183 | // Add the new declaration before the existing declaration. | 
|  | 184 | NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr); | 
|  | 185 | RemoveDecl(PrevD); | 
|  | 186 | AddDecl(D); | 
|  | 187 | AddDecl(PrevD); | 
|  | 188 | } else { | 
|  | 189 | // Add new declaration after the existing declaration. | 
|  | 190 | AddDecl(D); | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | return; | 
|  | 194 | } | 
|  | 195 |  | 
| Douglas Gregor | 88764cf | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 196 | // General case: insert the declaration at the appropriate point in the | 
|  | 197 | // list, which already has at least two elements. | 
|  | 198 | IdDeclInfo *IDI = toIdDeclInfo(Ptr); | 
| Douglas Gregor | 46c04e7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 199 | if (Pos.isIterator()) { | 
|  | 200 | IDI->InsertDecl(Pos.getIterator() + 1, D); | 
|  | 201 | } else | 
| Douglas Gregor | 88764cf | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 202 | IDI->InsertDecl(IDI->decls_begin(), D); | 
|  | 203 | } | 
|  | 204 |  | 
| Argyrios Kyrtzidis | dfd5222 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 205 | /// RemoveDecl - Unlink the decl from its shadowed decl chain. | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 206 | /// The decl must already be part of the decl chain. | 
|  | 207 | void IdentifierResolver::RemoveDecl(NamedDecl *D) { | 
|  | 208 | assert(D && "null param passed"); | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 209 | DeclarationName Name = D->getDeclName(); | 
| Sebastian Redl | 671eee9 | 2010-07-30 17:25:10 +0000 | [diff] [blame] | 210 | if (IdentifierInfo *II = Name.getAsIdentifierInfo()) | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 211 | updatingIdentifier(*II); | 
| Sebastian Redl | 671eee9 | 2010-07-30 17:25:10 +0000 | [diff] [blame] | 212 |  | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 213 | void *Ptr = Name.getFETokenInfo<void>(); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 214 |  | 
|  | 215 | assert(Ptr && "Didn't find this decl on its identifier's chain!"); | 
|  | 216 |  | 
|  | 217 | if (isDeclPtr(Ptr)) { | 
|  | 218 | assert(D == Ptr && "Didn't find this decl on its identifier's chain!"); | 
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 219 | Name.setFETokenInfo(nullptr); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 220 | return; | 
|  | 221 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 |  | 
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 223 | return toIdDeclInfo(Ptr)->RemoveDecl(D); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
| Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 226 | /// begin - Returns an iterator for decls with name 'Name'. | 
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 227 | IdentifierResolver::iterator | 
| Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 228 | IdentifierResolver::begin(DeclarationName Name) { | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 229 | if (IdentifierInfo *II = Name.getAsIdentifierInfo()) | 
|  | 230 | readingIdentifier(*II); | 
|  | 231 |  | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 232 | void *Ptr = Name.getFETokenInfo<void>(); | 
| Argyrios Kyrtzidis | ef34aed | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 233 | if (!Ptr) return end(); | 
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 234 |  | 
| Chris Lattner | 9631e18 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 235 | if (isDeclPtr(Ptr)) | 
|  | 236 | return iterator(static_cast<NamedDecl*>(Ptr)); | 
| Argyrios Kyrtzidis | ef34aed | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 237 |  | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 238 | IdDeclInfo *IDI = toIdDeclInfo(Ptr); | 
| Argyrios Kyrtzidis | ef34aed | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 239 |  | 
| Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 240 | IdDeclInfo::DeclsTy::iterator I = IDI->decls_end(); | 
| Argyrios Kyrtzidis | ef34aed | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 241 | if (I != IDI->decls_begin()) | 
| Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 242 | return iterator(I-1); | 
| Chris Lattner | 9631e18 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 243 | // No decls found. | 
|  | 244 | return end(); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 245 | } | 
|  | 246 |  | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 247 | namespace { | 
|  | 248 | enum DeclMatchKind { | 
|  | 249 | DMK_Different, | 
|  | 250 | DMK_Replace, | 
|  | 251 | DMK_Ignore | 
|  | 252 | }; | 
|  | 253 | } | 
| Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 254 |  | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 255 | /// \brief Compare two declarations to see whether they are different or, | 
|  | 256 | /// if they are the same, whether the new declaration should replace the | 
|  | 257 | /// existing declaration. | 
|  | 258 | static DeclMatchKind compareDeclarations(NamedDecl *Existing, NamedDecl *New) { | 
|  | 259 | // If the declarations are identical, ignore the new one. | 
|  | 260 | if (Existing == New) | 
|  | 261 | return DMK_Ignore; | 
|  | 262 |  | 
|  | 263 | // If the declarations have different kinds, they're obviously different. | 
|  | 264 | if (Existing->getKind() != New->getKind()) | 
|  | 265 | return DMK_Different; | 
|  | 266 |  | 
|  | 267 | // If the declarations are redeclarations of each other, keep the newest one. | 
|  | 268 | if (Existing->getCanonicalDecl() == New->getCanonicalDecl()) { | 
| Richard Smith | fe620d2 | 2015-03-05 23:24:12 +0000 | [diff] [blame] | 269 | // If we're adding an imported declaration, don't replace another imported | 
|  | 270 | // declaration. | 
|  | 271 | if (Existing->isFromASTFile() && New->isFromASTFile()) | 
|  | 272 | return DMK_Different; | 
|  | 273 |  | 
| Douglas Gregor | dcf2508 | 2013-02-11 18:16:18 +0000 | [diff] [blame] | 274 | // If either of these is the most recent declaration, use it. | 
|  | 275 | Decl *MostRecent = Existing->getMostRecentDecl(); | 
|  | 276 | if (Existing == MostRecent) | 
|  | 277 | return DMK_Ignore; | 
|  | 278 |  | 
|  | 279 | if (New == MostRecent) | 
|  | 280 | return DMK_Replace; | 
|  | 281 |  | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 282 | // If the existing declaration is somewhere in the previous declaration | 
|  | 283 | // chain of the new declaration, then prefer the new declaration. | 
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 284 | for (auto RD : New->redecls()) { | 
|  | 285 | if (RD == Existing) | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 286 | return DMK_Replace; | 
|  | 287 |  | 
|  | 288 | if (RD->isCanonicalDecl()) | 
|  | 289 | break; | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | return DMK_Ignore; | 
| Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 293 | } | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 294 |  | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 295 | return DMK_Different; | 
|  | 296 | } | 
| Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 297 |  | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 298 | bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name){ | 
|  | 299 | if (IdentifierInfo *II = Name.getAsIdentifierInfo()) | 
| Douglas Gregor | 247afcc | 2012-01-24 15:24:38 +0000 | [diff] [blame] | 300 | readingIdentifier(*II); | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 301 |  | 
|  | 302 | void *Ptr = Name.getFETokenInfo<void>(); | 
|  | 303 |  | 
|  | 304 | if (!Ptr) { | 
|  | 305 | Name.setFETokenInfo(D); | 
|  | 306 | return true; | 
|  | 307 | } | 
|  | 308 |  | 
| Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 309 | IdDeclInfo *IDI; | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 310 |  | 
| Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 311 | if (isDeclPtr(Ptr)) { | 
| Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 312 | NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr); | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 313 |  | 
|  | 314 | switch (compareDeclarations(PrevD, D)) { | 
|  | 315 | case DMK_Different: | 
|  | 316 | break; | 
|  | 317 |  | 
|  | 318 | case DMK_Ignore: | 
|  | 319 | return false; | 
|  | 320 |  | 
|  | 321 | case DMK_Replace: | 
|  | 322 | Name.setFETokenInfo(D); | 
|  | 323 | return true; | 
|  | 324 | } | 
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 325 |  | 
|  | 326 | Name.setFETokenInfo(nullptr); | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 327 | IDI = &(*IdDeclInfos)[Name]; | 
|  | 328 |  | 
|  | 329 | // If the existing declaration is not visible in translation unit scope, | 
|  | 330 | // then add the new top-level declaration first. | 
|  | 331 | if (!PrevD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { | 
|  | 332 | IDI->AddDecl(D); | 
|  | 333 | IDI->AddDecl(PrevD); | 
|  | 334 | } else { | 
|  | 335 | IDI->AddDecl(PrevD); | 
|  | 336 | IDI->AddDecl(D); | 
|  | 337 | } | 
|  | 338 | return true; | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | IDI = toIdDeclInfo(Ptr); | 
| Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 342 |  | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 343 | // See whether this declaration is identical to any existing declarations. | 
|  | 344 | // If not, find the right place to insert it. | 
|  | 345 | for (IdDeclInfo::DeclsTy::iterator I = IDI->decls_begin(), | 
|  | 346 | IEnd = IDI->decls_end(); | 
|  | 347 | I != IEnd; ++I) { | 
|  | 348 |  | 
|  | 349 | switch (compareDeclarations(*I, D)) { | 
|  | 350 | case DMK_Different: | 
|  | 351 | break; | 
|  | 352 |  | 
|  | 353 | case DMK_Ignore: | 
|  | 354 | return false; | 
|  | 355 |  | 
|  | 356 | case DMK_Replace: | 
|  | 357 | *I = D; | 
|  | 358 | return true; | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 | if (!(*I)->getDeclContext()->getRedeclContext()->isTranslationUnit()) { | 
|  | 362 | // We've found a declaration that is not visible from the translation | 
|  | 363 | // unit (it's in an inner scope). Insert our declaration here. | 
|  | 364 | IDI->InsertDecl(I, D); | 
|  | 365 | return true; | 
|  | 366 | } | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | // Add the declaration to the end. | 
| Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 370 | IDI->AddDecl(D); | 
| Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 371 | return true; | 
|  | 372 | } | 
|  | 373 |  | 
|  | 374 | void IdentifierResolver::readingIdentifier(IdentifierInfo &II) { | 
|  | 375 | if (II.isOutOfDate()) | 
|  | 376 | PP.getExternalSource()->updateOutOfDateIdentifier(II); | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | void IdentifierResolver::updatingIdentifier(IdentifierInfo &II) { | 
|  | 380 | if (II.isOutOfDate()) | 
|  | 381 | PP.getExternalSource()->updateOutOfDateIdentifier(II); | 
|  | 382 |  | 
|  | 383 | if (II.isFromAST()) | 
|  | 384 | II.setChangedSinceDeserialization(); | 
| Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 385 | } | 
|  | 386 |  | 
| Argyrios Kyrtzidis | 4f11d78 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 387 | //===----------------------------------------------------------------------===// | 
|  | 388 | // IdDeclInfoMap Implementation | 
|  | 389 | //===----------------------------------------------------------------------===// | 
|  | 390 |  | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 391 | /// Returns the IdDeclInfo associated to the DeclarationName. | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 392 | /// It creates a new IdDeclInfo if one was not created before for this id. | 
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 393 | IdentifierResolver::IdDeclInfo & | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 394 | IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) { | 
|  | 395 | void *Ptr = Name.getFETokenInfo<void>(); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 396 |  | 
| Argyrios Kyrtzidis | fa8e15b | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 397 | if (Ptr) return *toIdDeclInfo(Ptr); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 398 |  | 
| John McCall | 5f5cbf5 | 2010-02-15 19:38:00 +0000 | [diff] [blame] | 399 | if (CurIndex == POOL_SIZE) { | 
|  | 400 | CurPool = new IdDeclInfoPool(CurPool); | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 401 | CurIndex = 0; | 
|  | 402 | } | 
| John McCall | 5f5cbf5 | 2010-02-15 19:38:00 +0000 | [diff] [blame] | 403 | IdDeclInfo *IDI = &CurPool->Pool[CurIndex]; | 
| Douglas Gregor | ae2fbad | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 404 | Name.setFETokenInfo(reinterpret_cast<void*>( | 
| Chris Lattner | 9950c80 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 405 | reinterpret_cast<uintptr_t>(IDI) | 0x1) | 
|  | 406 | ); | 
|  | 407 | ++CurIndex; | 
|  | 408 | return *IDI; | 
|  | 409 | } | 
| John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 410 |  | 
|  | 411 | void IdentifierResolver::iterator::incrementSlowCase() { | 
|  | 412 | NamedDecl *D = **this; | 
|  | 413 | void *InfoPtr = D->getDeclName().getFETokenInfo<void>(); | 
|  | 414 | assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?"); | 
|  | 415 | IdDeclInfo *Info = toIdDeclInfo(InfoPtr); | 
|  | 416 |  | 
|  | 417 | BaseIter I = getIterator(); | 
|  | 418 | if (I != Info->decls_begin()) | 
|  | 419 | *this = iterator(I-1); | 
|  | 420 | else // No more decls. | 
|  | 421 | *this = iterator(); | 
|  | 422 | } |