blob: 333f4d70986a0db0bb0c4eb908c1eec87b663d52 [file] [log] [blame]
Eugene Zelenko9fbf6412018-02-21 01:45:26 +00001//===- IdentifierResolver.cpp - Lexical Scope Name lookup -----------------===//
Chris Lattner9950c802008-04-11 07:06:57 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Lattner9950c802008-04-11 07:06:57 +00006//
7//===----------------------------------------------------------------------===//
8//
Argyrios Kyrtzidisdfd52222008-04-12 01:50:47 +00009// This file implements the IdentifierResolver class, which is used for lexical
Douglas Gregorae2fbad2008-11-17 20:34:05 +000010// scoped lookup, based on declaration names.
Chris Lattner9950c802008-04-11 07:06:57 +000011//
12//===----------------------------------------------------------------------===//
13
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000014#include "clang/Sema/IdentifierResolver.h"
John McCallde6836a2010-08-24 07:21:54 +000015#include "clang/AST/Decl.h"
Eugene Zelenko9fbf6412018-02-21 01:45:26 +000016#include "clang/AST/DeclBase.h"
17#include "clang/AST/DeclarationName.h"
18#include "clang/Basic/IdentifierTable.h"
Argyrios Kyrtzidis2bdac732008-09-09 21:57:58 +000019#include "clang/Basic/LangOptions.h"
Douglas Gregor935bc7a22011-10-27 09:33:13 +000020#include "clang/Lex/ExternalPreprocessorSource.h"
21#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
Eugene Zelenko9fbf6412018-02-21 01:45:26 +000023#include "llvm/Support/ErrorHandling.h"
24#include <cassert>
25#include <cstdint>
Chris Lattner9950c802008-04-11 07:06:57 +000026
27using namespace clang;
28
Argyrios Kyrtzidis4f11d782008-09-09 19:28:27 +000029//===----------------------------------------------------------------------===//
30// IdDeclInfoMap class
31//===----------------------------------------------------------------------===//
Chris Lattner9950c802008-04-11 07:06:57 +000032
Douglas Gregorae2fbad2008-11-17 20:34:05 +000033/// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
Argyrios Kyrtzidisdfd52222008-04-12 01:50:47 +000034/// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
Chris Lattner9950c802008-04-11 07:06:57 +000035/// individual IdDeclInfo to heap.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +000036class IdentifierResolver::IdDeclInfoMap {
John McCall5f5cbf52010-02-15 19:38:00 +000037 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 McCall5f5cbf52010-02-15 19:38:00 +000043 IdDeclInfoPool *Next;
44 IdDeclInfo Pool[POOL_SIZE];
Eugene Zelenko9fbf6412018-02-21 01:45:26 +000045
46 IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {}
John McCall5f5cbf52010-02-15 19:38:00 +000047 };
Fangrui Song6907ce22018-07-30 19:24:48 +000048
Eugene Zelenko9fbf6412018-02-21 01:45:26 +000049 IdDeclInfoPool *CurPool = nullptr;
50 unsigned int CurIndex = POOL_SIZE;
Mike Stump11289f42009-09-09 15:08:12 +000051
Chris Lattner9950c802008-04-11 07:06:57 +000052public:
Eugene Zelenko9fbf6412018-02-21 01:45:26 +000053 IdDeclInfoMap() = default;
John McCall5f5cbf52010-02-15 19:38:00 +000054
55 ~IdDeclInfoMap() {
56 IdDeclInfoPool *Cur = CurPool;
57 while (IdDeclInfoPool *P = Cur) {
58 Cur = Cur->Next;
59 delete P;
60 }
61 }
Chris Lattner9950c802008-04-11 07:06:57 +000062
Douglas Gregorae2fbad2008-11-17 20:34:05 +000063 /// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattner9950c802008-04-11 07:06:57 +000064 /// It creates a new IdDeclInfo if one was not created before for this id.
Douglas Gregorae2fbad2008-11-17 20:34:05 +000065 IdDeclInfo &operator[](DeclarationName Name);
Chris Lattner9950c802008-04-11 07:06:57 +000066};
67
Argyrios Kyrtzidis4f11d782008-09-09 19:28:27 +000068//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis4f11d782008-09-09 19:28:27 +000069// IdDeclInfo Implementation
70//===----------------------------------------------------------------------===//
71
Argyrios Kyrtzidis4f11d782008-09-09 19:28:27 +000072/// RemoveDecl - Remove the decl from the scope chain.
73/// The decl must already be part of the decl chain.
74void 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 Blaikie83d382b2011-09-23 05:06:16 +000082 llvm_unreachable("Didn't find this decl on its identifier's chain!");
Argyrios Kyrtzidis4f11d782008-09-09 19:28:27 +000083}
84
Argyrios Kyrtzidis4f11d782008-09-09 19:28:27 +000085//===----------------------------------------------------------------------===//
86// IdentifierResolver Implementation
87//===----------------------------------------------------------------------===//
88
Douglas Gregor935bc7a22011-10-27 09:33:13 +000089IdentifierResolver::IdentifierResolver(Preprocessor &PP)
Eugene Zelenko9fbf6412018-02-21 01:45:26 +000090 : LangOpt(PP.getLangOpts()), PP(PP), IdDeclInfos(new IdDeclInfoMap) {}
Douglas Gregor935bc7a22011-10-27 09:33:13 +000091
Argyrios Kyrtzidis25f54c72008-04-14 00:09:21 +000092IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +000093 delete IdDeclInfos;
Argyrios Kyrtzidis25f54c72008-04-14 00:09:21 +000094}
Chris Lattner9950c802008-04-11 07:06:57 +000095
Argyrios Kyrtzidis4f11d782008-09-09 19:28:27 +000096/// 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 Weber555d1aa2012-12-17 03:51:09 +000099bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S,
Richard Smith72bcaec2013-12-05 04:30:04 +0000100 bool AllowInlineNamespace) const {
Sebastian Redl50c68252010-08-31 00:36:30 +0000101 Ctx = Ctx->getRedeclContext();
Douglas Gregor07665a62009-01-05 19:45:36 +0000102
Richard Smith9e2341d2015-03-23 03:25:59 +0000103 if (Ctx->isFunctionOrMethod() || (S && S->isFunctionPrototypeScope())) {
Douglas Gregor07665a62009-01-05 19:45:36 +0000104 // Ignore the scopes associated within transparent declaration contexts.
Ted Kremenekc37877d2013-10-08 17:08:03 +0000105 while (S->getEntity() && S->getEntity()->isTransparentContext())
Douglas Gregor07665a62009-01-05 19:45:36 +0000106 S = S->getParent();
107
John McCall48871652010-08-21 09:40:31 +0000108 if (S->isDeclScope(D))
Argyrios Kyrtzidis2bdac732008-09-09 21:57:58 +0000109 return true;
110 if (LangOpt.CPlusPlus) {
Sebastian Redlb219c902008-12-21 16:41:36 +0000111 // 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 Kyrtzidis2bdac732008-09-09 21:57:58 +0000115 // 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 Blaikie1c9c9042012-11-10 01:04:23 +0000124 if (S->getParent()->getFlags() & Scope::ControlScope) {
David Blaikie1d617802012-11-12 22:25:41 +0000125 S = S->getParent();
126 if (S->isDeclScope(D))
127 return true;
David Blaikie1c9c9042012-11-10 01:04:23 +0000128 }
David Blaikie1d617802012-11-12 22:25:41 +0000129 if (S->getFlags() & Scope::FnTryCatchScope)
130 return S->getParent()->isDeclScope(D);
Argyrios Kyrtzidis2bdac732008-09-09 21:57:58 +0000131 }
132 return false;
133 }
Argyrios Kyrtzidis4f11d782008-09-09 19:28:27 +0000134
Richard Smith5971e8c2014-08-27 22:31:34 +0000135 // 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 Gregordb446112011-03-07 16:54:27 +0000138 DeclContext *DCtx = D->getDeclContext()->getRedeclContext();
Richard Smith72bcaec2013-12-05 04:30:04 +0000139 return AllowInlineNamespace ? Ctx->InEnclosingNamespaceSetOf(DCtx)
140 : Ctx->Equals(DCtx);
Argyrios Kyrtzidis4f11d782008-09-09 19:28:27 +0000141}
142
Argyrios Kyrtzidisdfd52222008-04-12 01:50:47 +0000143/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +0000144void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000145 DeclarationName Name = D->getDeclName();
Sebastian Redl671eee92010-07-30 17:25:10 +0000146 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000147 updatingIdentifier(*II);
Sebastian Redl671eee92010-07-30 17:25:10 +0000148
Bruno Ricci366ba732018-09-21 12:53:22 +0000149 void *Ptr = Name.getFETokenInfo();
Chris Lattner9950c802008-04-11 07:06:57 +0000150
151 if (!Ptr) {
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000152 Name.setFETokenInfo(D);
Chris Lattner9950c802008-04-11 07:06:57 +0000153 return;
154 }
155
156 IdDeclInfo *IDI;
157
158 if (isDeclPtr(Ptr)) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000159 Name.setFETokenInfo(nullptr);
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000160 IDI = &(*IdDeclInfos)[Name];
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000161 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +0000162 IDI->AddDecl(PrevD);
Chris Lattner9950c802008-04-11 07:06:57 +0000163 } else
164 IDI = toIdDeclInfo(Ptr);
165
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +0000166 IDI->AddDecl(D);
Chris Lattner9950c802008-04-11 07:06:57 +0000167}
168
Douglas Gregor46c04e72011-03-16 16:39:03 +0000169void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) {
170 DeclarationName Name = D->getDeclName();
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000171 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
172 updatingIdentifier(*II);
Fangrui Song6907ce22018-07-30 19:24:48 +0000173
Bruno Ricci366ba732018-09-21 12:53:22 +0000174 void *Ptr = Name.getFETokenInfo();
Fangrui Song6907ce22018-07-30 19:24:48 +0000175
Douglas Gregor33f352c2011-03-24 10:35:39 +0000176 if (!Ptr) {
Douglas Gregor88764cf2011-03-14 21:19:51 +0000177 AddDecl(D);
178 return;
179 }
Douglas Gregor88764cf2011-03-14 21:19:51 +0000180
Douglas Gregor33f352c2011-03-24 10:35:39 +0000181 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 Zelenko1e95bc02018-04-05 22:15:42 +0000186 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
Douglas Gregor33f352c2011-03-24 10:35:39 +0000187 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 Song6907ce22018-07-30 19:24:48 +0000198 // General case: insert the declaration at the appropriate point in the
Douglas Gregor88764cf2011-03-14 21:19:51 +0000199 // list, which already has at least two elements.
200 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Douglas Gregor46c04e72011-03-16 16:39:03 +0000201 if (Pos.isIterator()) {
202 IDI->InsertDecl(Pos.getIterator() + 1, D);
203 } else
Douglas Gregor88764cf2011-03-14 21:19:51 +0000204 IDI->InsertDecl(IDI->decls_begin(), D);
205}
206
Argyrios Kyrtzidisdfd52222008-04-12 01:50:47 +0000207/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattner9950c802008-04-11 07:06:57 +0000208/// The decl must already be part of the decl chain.
209void IdentifierResolver::RemoveDecl(NamedDecl *D) {
210 assert(D && "null param passed");
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000211 DeclarationName Name = D->getDeclName();
Sebastian Redl671eee92010-07-30 17:25:10 +0000212 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000213 updatingIdentifier(*II);
Sebastian Redl671eee92010-07-30 17:25:10 +0000214
Bruno Ricci366ba732018-09-21 12:53:22 +0000215 void *Ptr = Name.getFETokenInfo();
Chris Lattner9950c802008-04-11 07:06:57 +0000216
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 Topperc3ec1492014-05-26 06:22:03 +0000221 Name.setFETokenInfo(nullptr);
Chris Lattner9950c802008-04-11 07:06:57 +0000222 return;
223 }
Mike Stump11289f42009-09-09 15:08:12 +0000224
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +0000225 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattner9950c802008-04-11 07:06:57 +0000226}
227
Douglas Gregored8f2882009-01-30 01:04:22 +0000228/// begin - Returns an iterator for decls with name 'Name'.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +0000229IdentifierResolver::iterator
Douglas Gregored8f2882009-01-30 01:04:22 +0000230IdentifierResolver::begin(DeclarationName Name) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000231 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
232 readingIdentifier(*II);
Fangrui Song6907ce22018-07-30 19:24:48 +0000233
Bruno Ricci366ba732018-09-21 12:53:22 +0000234 void *Ptr = Name.getFETokenInfo();
Argyrios Kyrtzidisef34aed2008-07-17 17:49:50 +0000235 if (!Ptr) return end();
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +0000236
Chris Lattner9631e182009-03-04 06:34:08 +0000237 if (isDeclPtr(Ptr))
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000238 return iterator(static_cast<NamedDecl*>(Ptr));
Argyrios Kyrtzidisef34aed2008-07-17 17:49:50 +0000239
Chris Lattner9950c802008-04-11 07:06:57 +0000240 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidisef34aed2008-07-17 17:49:50 +0000241
Douglas Gregor07665a62009-01-05 19:45:36 +0000242 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidisef34aed2008-07-17 17:49:50 +0000243 if (I != IDI->decls_begin())
Douglas Gregored8f2882009-01-30 01:04:22 +0000244 return iterator(I-1);
Chris Lattner9631e182009-03-04 06:34:08 +0000245 // No decls found.
246 return end();
Chris Lattner9950c802008-04-11 07:06:57 +0000247}
248
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000249namespace {
Eugene Zelenko9fbf6412018-02-21 01:45:26 +0000250
251enum DeclMatchKind {
252 DMK_Different,
253 DMK_Replace,
254 DMK_Ignore
255};
256
257} // namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000258
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000259/// Compare two declarations to see whether they are different or,
Fangrui Song6907ce22018-07-30 19:24:48 +0000260/// if they are the same, whether the new declaration should replace the
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000261/// existing declaration.
262static 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 Smithfe620d22015-03-05 23:24:12 +0000273 // 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 Gregordcf25082013-02-11 18:16:18 +0000278 // 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 Gregor935bc7a22011-10-27 09:33:13 +0000286 // If the existing declaration is somewhere in the previous declaration
287 // chain of the new declaration, then prefer the new declaration.
Aaron Ballman86c93902014-03-06 23:45:36 +0000288 for (auto RD : New->redecls()) {
289 if (RD == Existing)
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000290 return DMK_Replace;
Fangrui Song6907ce22018-07-30 19:24:48 +0000291
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000292 if (RD->isCanonicalDecl())
293 break;
294 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000295
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000296 return DMK_Ignore;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000297 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000298
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000299 return DMK_Different;
300}
Douglas Gregora868bbd2009-04-21 22:25:48 +0000301
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000302bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name){
303 if (IdentifierInfo *II = Name.getAsIdentifierInfo())
Douglas Gregor247afcc2012-01-24 15:24:38 +0000304 readingIdentifier(*II);
Fangrui Song6907ce22018-07-30 19:24:48 +0000305
Bruno Ricci366ba732018-09-21 12:53:22 +0000306 void *Ptr = Name.getFETokenInfo();
Fangrui Song6907ce22018-07-30 19:24:48 +0000307
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000308 if (!Ptr) {
309 Name.setFETokenInfo(D);
310 return true;
311 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000312
Douglas Gregora868bbd2009-04-21 22:25:48 +0000313 IdDeclInfo *IDI;
Fangrui Song6907ce22018-07-30 19:24:48 +0000314
Douglas Gregora868bbd2009-04-21 22:25:48 +0000315 if (isDeclPtr(Ptr)) {
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000316 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
Fangrui Song6907ce22018-07-30 19:24:48 +0000317
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000318 switch (compareDeclarations(PrevD, D)) {
319 case DMK_Different:
320 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000321
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000322 case DMK_Ignore:
323 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000324
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000325 case DMK_Replace:
326 Name.setFETokenInfo(D);
327 return true;
328 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000329
330 Name.setFETokenInfo(nullptr);
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000331 IDI = &(*IdDeclInfos)[Name];
Fangrui Song6907ce22018-07-30 19:24:48 +0000332
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000333 // 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 Song6907ce22018-07-30 19:24:48 +0000343 }
344
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000345 IDI = toIdDeclInfo(Ptr);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000346
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000347 // See whether this declaration is identical to any existing declarations.
348 // If not, find the right place to insert it.
Fangrui Song6907ce22018-07-30 19:24:48 +0000349 for (IdDeclInfo::DeclsTy::iterator I = IDI->decls_begin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000350 IEnd = IDI->decls_end();
351 I != IEnd; ++I) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000352
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000353 switch (compareDeclarations(*I, D)) {
354 case DMK_Different:
355 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000356
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000357 case DMK_Ignore:
358 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000359
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000360 case DMK_Replace:
361 *I = D;
362 return true;
363 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000364
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000365 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 Song6907ce22018-07-30 19:24:48 +0000372
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000373 // Add the declaration to the end.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000374 IDI->AddDecl(D);
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000375 return true;
376}
377
378void IdentifierResolver::readingIdentifier(IdentifierInfo &II) {
379 if (II.isOutOfDate())
Fangrui Song6907ce22018-07-30 19:24:48 +0000380 PP.getExternalSource()->updateOutOfDateIdentifier(II);
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000381}
382
383void IdentifierResolver::updatingIdentifier(IdentifierInfo &II) {
384 if (II.isOutOfDate())
385 PP.getExternalSource()->updateOutOfDateIdentifier(II);
Fangrui Song6907ce22018-07-30 19:24:48 +0000386
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000387 if (II.isFromAST())
Richard Smithd79514e2016-02-05 19:03:40 +0000388 II.setFETokenInfoChangedSinceDeserialization();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000389}
390
Argyrios Kyrtzidis4f11d782008-09-09 19:28:27 +0000391//===----------------------------------------------------------------------===//
392// IdDeclInfoMap Implementation
393//===----------------------------------------------------------------------===//
394
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000395/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattner9950c802008-04-11 07:06:57 +0000396/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +0000397IdentifierResolver::IdDeclInfo &
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000398IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
Bruno Ricci366ba732018-09-21 12:53:22 +0000399 void *Ptr = Name.getFETokenInfo();
Chris Lattner9950c802008-04-11 07:06:57 +0000400
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +0000401 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattner9950c802008-04-11 07:06:57 +0000402
John McCall5f5cbf52010-02-15 19:38:00 +0000403 if (CurIndex == POOL_SIZE) {
404 CurPool = new IdDeclInfoPool(CurPool);
Chris Lattner9950c802008-04-11 07:06:57 +0000405 CurIndex = 0;
406 }
John McCall5f5cbf52010-02-15 19:38:00 +0000407 IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
Douglas Gregorae2fbad2008-11-17 20:34:05 +0000408 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattner9950c802008-04-11 07:06:57 +0000409 reinterpret_cast<uintptr_t>(IDI) | 0x1)
410 );
411 ++CurIndex;
412 return *IDI;
413}
John McCallde6836a2010-08-24 07:21:54 +0000414
415void IdentifierResolver::iterator::incrementSlowCase() {
416 NamedDecl *D = **this;
Bruno Ricci366ba732018-09-21 12:53:22 +0000417 void *InfoPtr = D->getDeclName().getFETokenInfo();
John McCallde6836a2010-08-24 07:21:54 +0000418 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}