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