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