Chris Lattner | a2f42b1 | 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 | 321f278 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 10 | // This file implements 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 | |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/IdentifierResolver.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
Argyrios Kyrtzidis | f99cb05 | 2008-09-09 21:57:58 +0000 | [diff] [blame] | 17 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 18 | #include "clang/Lex/ExternalPreprocessorSource.h" |
| 19 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Scope.h" |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // IdDeclInfoMap class |
| 26 | //===----------------------------------------------------------------------===// |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 27 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 28 | /// IdDeclInfoMap - Associates IdDeclInfos with declaration names. |
Argyrios Kyrtzidis | 321f278 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 29 | /// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 30 | /// individual IdDeclInfo to heap. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 31 | class IdentifierResolver::IdDeclInfoMap { |
John McCall | eeb1cb4 | 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 | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 45 | unsigned int CurIndex; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 47 | public: |
John McCall | eeb1cb4 | 2010-02-15 19:38:00 +0000 | [diff] [blame] | 48 | IdDeclInfoMap() : CurPool(0), CurIndex(POOL_SIZE) {} |
| 49 | |
| 50 | ~IdDeclInfoMap() { |
| 51 | IdDeclInfoPool *Cur = CurPool; |
| 52 | while (IdDeclInfoPool *P = Cur) { |
| 53 | Cur = Cur->Next; |
| 54 | delete P; |
| 55 | } |
| 56 | } |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 57 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 58 | /// Returns the IdDeclInfo associated to the DeclarationName. |
Chris Lattner | a2f42b1 | 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 | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 60 | IdDeclInfo &operator[](DeclarationName Name); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Argyrios Kyrtzidis | 7bc198f | 2008-04-14 00:09:21 +0000 | [diff] [blame] | 63 | |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 64 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 65 | // IdDeclInfo Implementation |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | |
Argyrios Kyrtzidis | 81bebb1 | 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 | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 78 | llvm_unreachable("Didn't find this decl on its identifier's chain!"); |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | bool |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 82 | IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl *Old, NamedDecl *New) { |
| 83 | for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) { |
| 84 | if (Old == *(I-1)) { |
| 85 | *(I - 1) = New; |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return false; |
| 91 | } |
| 92 | |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 93 | |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | // IdentifierResolver Implementation |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 98 | IdentifierResolver::IdentifierResolver(Preprocessor &PP) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 99 | : LangOpt(PP.getLangOpts()), PP(PP), |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 100 | IdDeclInfos(new IdDeclInfoMap) { |
Argyrios Kyrtzidis | eb7c388 | 2008-09-09 21:32:02 +0000 | [diff] [blame] | 101 | } |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 102 | |
Argyrios Kyrtzidis | 7bc198f | 2008-04-14 00:09:21 +0000 | [diff] [blame] | 103 | IdentifierResolver::~IdentifierResolver() { |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 104 | delete IdDeclInfos; |
Argyrios Kyrtzidis | 7bc198f | 2008-04-14 00:09:21 +0000 | [diff] [blame] | 105 | } |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 106 | |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 107 | /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true |
| 108 | /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns |
| 109 | /// true if 'D' belongs to the given declaration context. |
Nico Weber | 355a166 | 2012-12-17 03:51:09 +0000 | [diff] [blame] | 110 | bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S, |
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 111 | bool ExplicitInstantiationOrSpecialization) const { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 112 | Ctx = Ctx->getRedeclContext(); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 113 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 114 | if (Ctx->isFunctionOrMethod() || S->isFunctionPrototypeScope()) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 115 | // Ignore the scopes associated within transparent declaration contexts. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | while (S->getEntity() && |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 117 | ((DeclContext *)S->getEntity())->isTransparentContext()) |
| 118 | S = S->getParent(); |
| 119 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 120 | if (S->isDeclScope(D)) |
Argyrios Kyrtzidis | f99cb05 | 2008-09-09 21:57:58 +0000 | [diff] [blame] | 121 | return true; |
| 122 | if (LangOpt.CPlusPlus) { |
Sebastian Redl | a0fd865 | 2008-12-21 16:41:36 +0000 | [diff] [blame] | 123 | // C++ 3.3.2p3: |
| 124 | // The name declared in a catch exception-declaration is local to the |
| 125 | // handler and shall not be redeclared in the outermost block of the |
| 126 | // handler. |
Argyrios Kyrtzidis | f99cb05 | 2008-09-09 21:57:58 +0000 | [diff] [blame] | 127 | // C++ 3.3.2p4: |
| 128 | // Names declared in the for-init-statement, and in the condition of if, |
| 129 | // while, for, and switch statements are local to the if, while, for, or |
| 130 | // switch statement (including the controlled statement), and shall not be |
| 131 | // redeclared in a subsequent condition of that statement nor in the |
| 132 | // outermost block (or, for the if statement, any of the outermost blocks) |
| 133 | // of the controlled statement. |
| 134 | // |
| 135 | assert(S->getParent() && "No TUScope?"); |
David Blaikie | c4027c8 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 136 | if (S->getParent()->getFlags() & Scope::ControlScope) { |
David Blaikie | 3a9fefe | 2012-11-12 22:25:41 +0000 | [diff] [blame] | 137 | S = S->getParent(); |
| 138 | if (S->isDeclScope(D)) |
| 139 | return true; |
David Blaikie | c4027c8 | 2012-11-10 01:04:23 +0000 | [diff] [blame] | 140 | } |
David Blaikie | 3a9fefe | 2012-11-12 22:25:41 +0000 | [diff] [blame] | 141 | if (S->getFlags() & Scope::FnTryCatchScope) |
| 142 | return S->getParent()->isDeclScope(D); |
Argyrios Kyrtzidis | f99cb05 | 2008-09-09 21:57:58 +0000 | [diff] [blame] | 143 | } |
| 144 | return false; |
| 145 | } |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 146 | |
Douglas Gregor | cc20945 | 2011-03-07 16:54:27 +0000 | [diff] [blame] | 147 | DeclContext *DCtx = D->getDeclContext()->getRedeclContext(); |
| 148 | return ExplicitInstantiationOrSpecialization |
| 149 | ? Ctx->InEnclosingNamespaceSetOf(DCtx) |
| 150 | : Ctx->Equals(DCtx); |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Argyrios Kyrtzidis | 321f278 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 153 | /// AddDecl - Link the decl to its shadowed decl chain. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 154 | void IdentifierResolver::AddDecl(NamedDecl *D) { |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 155 | DeclarationName Name = D->getDeclName(); |
Sebastian Redl | 1450ef9 | 2010-07-30 17:25:10 +0000 | [diff] [blame] | 156 | if (IdentifierInfo *II = Name.getAsIdentifierInfo()) |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 157 | updatingIdentifier(*II); |
Sebastian Redl | 1450ef9 | 2010-07-30 17:25:10 +0000 | [diff] [blame] | 158 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 159 | void *Ptr = Name.getFETokenInfo<void>(); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 160 | |
| 161 | if (!Ptr) { |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 162 | Name.setFETokenInfo(D); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
| 166 | IdDeclInfo *IDI; |
| 167 | |
| 168 | if (isDeclPtr(Ptr)) { |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 169 | Name.setFETokenInfo(NULL); |
| 170 | IDI = &(*IdDeclInfos)[Name]; |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 171 | NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr); |
| 172 | IDI->AddDecl(PrevD); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 173 | } else |
| 174 | IDI = toIdDeclInfo(Ptr); |
| 175 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 176 | IDI->AddDecl(D); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Douglas Gregor | 250e7a7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 179 | void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) { |
| 180 | DeclarationName Name = D->getDeclName(); |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 181 | if (IdentifierInfo *II = Name.getAsIdentifierInfo()) |
| 182 | updatingIdentifier(*II); |
| 183 | |
Douglas Gregor | 250e7a7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 184 | void *Ptr = Name.getFETokenInfo<void>(); |
| 185 | |
Douglas Gregor | fa7b8ce | 2011-03-24 10:35:39 +0000 | [diff] [blame] | 186 | if (!Ptr) { |
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 187 | AddDecl(D); |
| 188 | return; |
| 189 | } |
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 190 | |
Douglas Gregor | fa7b8ce | 2011-03-24 10:35:39 +0000 | [diff] [blame] | 191 | if (isDeclPtr(Ptr)) { |
| 192 | // We only have a single declaration: insert before or after it, |
| 193 | // as appropriate. |
| 194 | if (Pos == iterator()) { |
| 195 | // Add the new declaration before the existing declaration. |
| 196 | NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr); |
| 197 | RemoveDecl(PrevD); |
| 198 | AddDecl(D); |
| 199 | AddDecl(PrevD); |
| 200 | } else { |
| 201 | // Add new declaration after the existing declaration. |
| 202 | AddDecl(D); |
| 203 | } |
| 204 | |
| 205 | return; |
| 206 | } |
| 207 | |
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 208 | // General case: insert the declaration at the appropriate point in the |
| 209 | // list, which already has at least two elements. |
| 210 | IdDeclInfo *IDI = toIdDeclInfo(Ptr); |
Douglas Gregor | 250e7a7 | 2011-03-16 16:39:03 +0000 | [diff] [blame] | 211 | if (Pos.isIterator()) { |
| 212 | IDI->InsertDecl(Pos.getIterator() + 1, D); |
| 213 | } else |
Douglas Gregor | 7cbc558 | 2011-03-14 21:19:51 +0000 | [diff] [blame] | 214 | IDI->InsertDecl(IDI->decls_begin(), D); |
| 215 | } |
| 216 | |
Argyrios Kyrtzidis | 321f278 | 2008-04-12 01:50:47 +0000 | [diff] [blame] | 217 | /// RemoveDecl - Unlink the decl from its shadowed decl chain. |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 218 | /// The decl must already be part of the decl chain. |
| 219 | void IdentifierResolver::RemoveDecl(NamedDecl *D) { |
| 220 | assert(D && "null param passed"); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 221 | DeclarationName Name = D->getDeclName(); |
Sebastian Redl | 1450ef9 | 2010-07-30 17:25:10 +0000 | [diff] [blame] | 222 | if (IdentifierInfo *II = Name.getAsIdentifierInfo()) |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 223 | updatingIdentifier(*II); |
Sebastian Redl | 1450ef9 | 2010-07-30 17:25:10 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 225 | void *Ptr = Name.getFETokenInfo<void>(); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 226 | |
| 227 | assert(Ptr && "Didn't find this decl on its identifier's chain!"); |
| 228 | |
| 229 | if (isDeclPtr(Ptr)) { |
| 230 | assert(D == Ptr && "Didn't find this decl on its identifier's chain!"); |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 231 | Name.setFETokenInfo(NULL); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 232 | return; |
| 233 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 235 | return toIdDeclInfo(Ptr)->RemoveDecl(D); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 238 | bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | assert(Old->getDeclName() == New->getDeclName() && |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 240 | "Cannot replace a decl with another decl of a different name"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 242 | DeclarationName Name = Old->getDeclName(); |
Sebastian Redl | 1450ef9 | 2010-07-30 17:25:10 +0000 | [diff] [blame] | 243 | if (IdentifierInfo *II = Name.getAsIdentifierInfo()) |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 244 | updatingIdentifier(*II); |
Sebastian Redl | 1450ef9 | 2010-07-30 17:25:10 +0000 | [diff] [blame] | 245 | |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 246 | void *Ptr = Name.getFETokenInfo<void>(); |
| 247 | |
| 248 | if (!Ptr) |
| 249 | return false; |
| 250 | |
| 251 | if (isDeclPtr(Ptr)) { |
| 252 | if (Ptr == Old) { |
| 253 | Name.setFETokenInfo(New); |
| 254 | return true; |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 262 | /// begin - Returns an iterator for decls with name 'Name'. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 263 | IdentifierResolver::iterator |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 264 | IdentifierResolver::begin(DeclarationName Name) { |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 265 | if (IdentifierInfo *II = Name.getAsIdentifierInfo()) |
| 266 | readingIdentifier(*II); |
| 267 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 268 | void *Ptr = Name.getFETokenInfo<void>(); |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 269 | if (!Ptr) return end(); |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 271 | if (isDeclPtr(Ptr)) |
| 272 | return iterator(static_cast<NamedDecl*>(Ptr)); |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 273 | |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 274 | IdDeclInfo *IDI = toIdDeclInfo(Ptr); |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 275 | |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 276 | IdDeclInfo::DeclsTy::iterator I = IDI->decls_end(); |
Argyrios Kyrtzidis | 90eb539 | 2008-07-17 17:49:50 +0000 | [diff] [blame] | 277 | if (I != IDI->decls_begin()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 278 | return iterator(I-1); |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 279 | // No decls found. |
| 280 | return end(); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 283 | namespace { |
| 284 | enum DeclMatchKind { |
| 285 | DMK_Different, |
| 286 | DMK_Replace, |
| 287 | DMK_Ignore |
| 288 | }; |
| 289 | } |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 291 | /// \brief Compare two declarations to see whether they are different or, |
| 292 | /// if they are the same, whether the new declaration should replace the |
| 293 | /// existing declaration. |
| 294 | static DeclMatchKind compareDeclarations(NamedDecl *Existing, NamedDecl *New) { |
| 295 | // If the declarations are identical, ignore the new one. |
| 296 | if (Existing == New) |
| 297 | return DMK_Ignore; |
| 298 | |
| 299 | // If the declarations have different kinds, they're obviously different. |
| 300 | if (Existing->getKind() != New->getKind()) |
| 301 | return DMK_Different; |
| 302 | |
| 303 | // If the declarations are redeclarations of each other, keep the newest one. |
| 304 | if (Existing->getCanonicalDecl() == New->getCanonicalDecl()) { |
Douglas Gregor | f4e955b | 2013-02-11 18:16:18 +0000 | [diff] [blame^] | 305 | // If either of these is the most recent declaration, use it. |
| 306 | Decl *MostRecent = Existing->getMostRecentDecl(); |
| 307 | if (Existing == MostRecent) |
| 308 | return DMK_Ignore; |
| 309 | |
| 310 | if (New == MostRecent) |
| 311 | return DMK_Replace; |
| 312 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 313 | // If the existing declaration is somewhere in the previous declaration |
| 314 | // chain of the new declaration, then prefer the new declaration. |
| 315 | for (Decl::redecl_iterator RD = New->redecls_begin(), |
| 316 | RDEnd = New->redecls_end(); |
| 317 | RD != RDEnd; ++RD) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 318 | if (*RD == Existing) |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 319 | return DMK_Replace; |
| 320 | |
| 321 | if (RD->isCanonicalDecl()) |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | return DMK_Ignore; |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 326 | } |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 327 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 328 | return DMK_Different; |
| 329 | } |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 330 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 331 | bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name){ |
| 332 | if (IdentifierInfo *II = Name.getAsIdentifierInfo()) |
Douglas Gregor | 5d5051f | 2012-01-24 15:24:38 +0000 | [diff] [blame] | 333 | readingIdentifier(*II); |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 334 | |
| 335 | void *Ptr = Name.getFETokenInfo<void>(); |
| 336 | |
| 337 | if (!Ptr) { |
| 338 | Name.setFETokenInfo(D); |
| 339 | return true; |
| 340 | } |
| 341 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 342 | IdDeclInfo *IDI; |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 343 | |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 344 | if (isDeclPtr(Ptr)) { |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 345 | NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr); |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 346 | |
| 347 | switch (compareDeclarations(PrevD, D)) { |
| 348 | case DMK_Different: |
| 349 | break; |
| 350 | |
| 351 | case DMK_Ignore: |
| 352 | return false; |
| 353 | |
| 354 | case DMK_Replace: |
| 355 | Name.setFETokenInfo(D); |
| 356 | return true; |
| 357 | } |
| 358 | |
| 359 | Name.setFETokenInfo(NULL); |
| 360 | IDI = &(*IdDeclInfos)[Name]; |
| 361 | |
| 362 | // If the existing declaration is not visible in translation unit scope, |
| 363 | // then add the new top-level declaration first. |
| 364 | if (!PrevD->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
| 365 | IDI->AddDecl(D); |
| 366 | IDI->AddDecl(PrevD); |
| 367 | } else { |
| 368 | IDI->AddDecl(PrevD); |
| 369 | IDI->AddDecl(D); |
| 370 | } |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | IDI = toIdDeclInfo(Ptr); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 375 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 376 | // See whether this declaration is identical to any existing declarations. |
| 377 | // If not, find the right place to insert it. |
| 378 | for (IdDeclInfo::DeclsTy::iterator I = IDI->decls_begin(), |
| 379 | IEnd = IDI->decls_end(); |
| 380 | I != IEnd; ++I) { |
| 381 | |
| 382 | switch (compareDeclarations(*I, D)) { |
| 383 | case DMK_Different: |
| 384 | break; |
| 385 | |
| 386 | case DMK_Ignore: |
| 387 | return false; |
| 388 | |
| 389 | case DMK_Replace: |
| 390 | *I = D; |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | if (!(*I)->getDeclContext()->getRedeclContext()->isTranslationUnit()) { |
| 395 | // We've found a declaration that is not visible from the translation |
| 396 | // unit (it's in an inner scope). Insert our declaration here. |
| 397 | IDI->InsertDecl(I, D); |
| 398 | return true; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // Add the declaration to the end. |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 403 | IDI->AddDecl(D); |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 404 | return true; |
| 405 | } |
| 406 | |
| 407 | void IdentifierResolver::readingIdentifier(IdentifierInfo &II) { |
| 408 | if (II.isOutOfDate()) |
| 409 | PP.getExternalSource()->updateOutOfDateIdentifier(II); |
| 410 | } |
| 411 | |
| 412 | void IdentifierResolver::updatingIdentifier(IdentifierInfo &II) { |
| 413 | if (II.isOutOfDate()) |
| 414 | PP.getExternalSource()->updateOutOfDateIdentifier(II); |
| 415 | |
| 416 | if (II.isFromAST()) |
| 417 | II.setChangedSinceDeserialization(); |
Douglas Gregor | 668c1a4 | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Argyrios Kyrtzidis | 81bebb1 | 2008-09-09 19:28:27 +0000 | [diff] [blame] | 420 | //===----------------------------------------------------------------------===// |
| 421 | // IdDeclInfoMap Implementation |
| 422 | //===----------------------------------------------------------------------===// |
| 423 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 424 | /// Returns the IdDeclInfo associated to the DeclarationName. |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 425 | /// It creates a new IdDeclInfo if one was not created before for this id. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 426 | IdentifierResolver::IdDeclInfo & |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 427 | IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) { |
| 428 | void *Ptr = Name.getFETokenInfo<void>(); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 429 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 430 | if (Ptr) return *toIdDeclInfo(Ptr); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 431 | |
John McCall | eeb1cb4 | 2010-02-15 19:38:00 +0000 | [diff] [blame] | 432 | if (CurIndex == POOL_SIZE) { |
| 433 | CurPool = new IdDeclInfoPool(CurPool); |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 434 | CurIndex = 0; |
| 435 | } |
John McCall | eeb1cb4 | 2010-02-15 19:38:00 +0000 | [diff] [blame] | 436 | IdDeclInfo *IDI = &CurPool->Pool[CurIndex]; |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 437 | Name.setFETokenInfo(reinterpret_cast<void*>( |
Chris Lattner | a2f42b1 | 2008-04-11 07:06:57 +0000 | [diff] [blame] | 438 | reinterpret_cast<uintptr_t>(IDI) | 0x1) |
| 439 | ); |
| 440 | ++CurIndex; |
| 441 | return *IDI; |
| 442 | } |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 443 | |
| 444 | void IdentifierResolver::iterator::incrementSlowCase() { |
| 445 | NamedDecl *D = **this; |
| 446 | void *InfoPtr = D->getDeclName().getFETokenInfo<void>(); |
| 447 | assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?"); |
| 448 | IdDeclInfo *Info = toIdDeclInfo(InfoPtr); |
| 449 | |
| 450 | BaseIter I = getIterator(); |
| 451 | if (I != Info->decls_begin()) |
| 452 | *this = iterator(I-1); |
| 453 | else // No more decls. |
| 454 | *this = iterator(); |
| 455 | } |