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