IdentifierResolver.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //===- IdentifierResolver.cpp - Lexical Scope Name lookup -----------------===//
  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. //
  10. // This file implements the IdentifierResolver class, which is used for lexical
  11. // scoped lookup, based on declaration names.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Sema/IdentifierResolver.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/DeclBase.h"
  17. #include "clang/AST/DeclarationName.h"
  18. #include "clang/Basic/IdentifierTable.h"
  19. #include "clang/Basic/LangOptions.h"
  20. #include "clang/Lex/ExternalPreprocessorSource.h"
  21. #include "clang/Lex/Preprocessor.h"
  22. #include "clang/Sema/Scope.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. #include <cassert>
  25. #include <cstdint>
  26. using namespace clang;
  27. //===----------------------------------------------------------------------===//
  28. // IdDeclInfoMap class
  29. //===----------------------------------------------------------------------===//
  30. /// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
  31. /// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
  32. /// individual IdDeclInfo to heap.
  33. class IdentifierResolver::IdDeclInfoMap {
  34. static const unsigned int POOL_SIZE = 512;
  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 *Next;
  40. IdDeclInfo Pool[POOL_SIZE];
  41. IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {}
  42. };
  43. IdDeclInfoPool *CurPool = nullptr;
  44. unsigned int CurIndex = POOL_SIZE;
  45. public:
  46. IdDeclInfoMap() = default;
  47. ~IdDeclInfoMap() {
  48. IdDeclInfoPool *Cur = CurPool;
  49. while (IdDeclInfoPool *P = Cur) {
  50. Cur = Cur->Next;
  51. delete P;
  52. }
  53. }
  54. /// Returns the IdDeclInfo associated to the DeclarationName.
  55. /// It creates a new IdDeclInfo if one was not created before for this id.
  56. IdDeclInfo &operator[](DeclarationName Name);
  57. };
  58. //===----------------------------------------------------------------------===//
  59. // IdDeclInfo Implementation
  60. //===----------------------------------------------------------------------===//
  61. /// RemoveDecl - Remove the decl from the scope chain.
  62. /// The decl must already be part of the decl chain.
  63. void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
  64. for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
  65. if (D == *(I-1)) {
  66. Decls.erase(I-1);
  67. return;
  68. }
  69. }
  70. llvm_unreachable("Didn't find this decl on its identifier's chain!");
  71. }
  72. //===----------------------------------------------------------------------===//
  73. // IdentifierResolver Implementation
  74. //===----------------------------------------------------------------------===//
  75. IdentifierResolver::IdentifierResolver(Preprocessor &PP)
  76. : LangOpt(PP.getLangOpts()), PP(PP), IdDeclInfos(new IdDeclInfoMap) {}
  77. IdentifierResolver::~IdentifierResolver() {
  78. delete IdDeclInfos;
  79. }
  80. /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
  81. /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
  82. /// true if 'D' belongs to the given declaration context.
  83. bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S,
  84. bool AllowInlineNamespace) const {
  85. Ctx = Ctx->getRedeclContext();
  86. if (Ctx->isFunctionOrMethod() || (S && S->isFunctionPrototypeScope())) {
  87. // Ignore the scopes associated within transparent declaration contexts.
  88. while (S->getEntity() && S->getEntity()->isTransparentContext())
  89. S = S->getParent();
  90. if (S->isDeclScope(D))
  91. return true;
  92. if (LangOpt.CPlusPlus) {
  93. // C++ 3.3.2p3:
  94. // The name declared in a catch exception-declaration is local to the
  95. // handler and shall not be redeclared in the outermost block of the
  96. // handler.
  97. // C++ 3.3.2p4:
  98. // Names declared in the for-init-statement, and in the condition of if,
  99. // while, for, and switch statements are local to the if, while, for, or
  100. // switch statement (including the controlled statement), and shall not be
  101. // redeclared in a subsequent condition of that statement nor in the
  102. // outermost block (or, for the if statement, any of the outermost blocks)
  103. // of the controlled statement.
  104. //
  105. assert(S->getParent() && "No TUScope?");
  106. if (S->getParent()->getFlags() & Scope::ControlScope) {
  107. S = S->getParent();
  108. if (S->isDeclScope(D))
  109. return true;
  110. }
  111. if (S->getFlags() & Scope::FnTryCatchScope)
  112. return S->getParent()->isDeclScope(D);
  113. }
  114. return false;
  115. }
  116. // FIXME: If D is a local extern declaration, this check doesn't make sense;
  117. // we should be checking its lexical context instead in that case, because
  118. // that is its scope.
  119. DeclContext *DCtx = D->getDeclContext()->getRedeclContext();
  120. return AllowInlineNamespace ? Ctx->InEnclosingNamespaceSetOf(DCtx)
  121. : Ctx->Equals(DCtx);
  122. }
  123. /// AddDecl - Link the decl to its shadowed decl chain.
  124. void IdentifierResolver::AddDecl(NamedDecl *D) {
  125. DeclarationName Name = D->getDeclName();
  126. if (IdentifierInfo *II = Name.getAsIdentifierInfo())
  127. updatingIdentifier(*II);
  128. auto *Ptr = Name.getFETokenInfo<void>();
  129. if (!Ptr) {
  130. Name.setFETokenInfo(D);
  131. return;
  132. }
  133. IdDeclInfo *IDI;
  134. if (isDeclPtr(Ptr)) {
  135. Name.setFETokenInfo(nullptr);
  136. IDI = &(*IdDeclInfos)[Name];
  137. auto *PrevD = static_cast<NamedDecl *>(Ptr);
  138. IDI->AddDecl(PrevD);
  139. } else
  140. IDI = toIdDeclInfo(Ptr);
  141. IDI->AddDecl(D);
  142. }
  143. void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) {
  144. DeclarationName Name = D->getDeclName();
  145. if (IdentifierInfo *II = Name.getAsIdentifierInfo())
  146. updatingIdentifier(*II);
  147. auto *Ptr = Name.getFETokenInfo<void>();
  148. if (!Ptr) {
  149. AddDecl(D);
  150. return;
  151. }
  152. if (isDeclPtr(Ptr)) {
  153. // We only have a single declaration: insert before or after it,
  154. // as appropriate.
  155. if (Pos == iterator()) {
  156. // Add the new declaration before the existing declaration.
  157. auto *PrevD = static_cast<NamedDecl *>(Ptr);
  158. RemoveDecl(PrevD);
  159. AddDecl(D);
  160. AddDecl(PrevD);
  161. } else {
  162. // Add new declaration after the existing declaration.
  163. AddDecl(D);
  164. }
  165. return;
  166. }
  167. // General case: insert the declaration at the appropriate point in the
  168. // list, which already has at least two elements.
  169. IdDeclInfo *IDI = toIdDeclInfo(Ptr);
  170. if (Pos.isIterator()) {
  171. IDI->InsertDecl(Pos.getIterator() + 1, D);
  172. } else
  173. IDI->InsertDecl(IDI->decls_begin(), D);
  174. }
  175. /// RemoveDecl - Unlink the decl from its shadowed decl chain.
  176. /// The decl must already be part of the decl chain.
  177. void IdentifierResolver::RemoveDecl(NamedDecl *D) {
  178. assert(D && "null param passed");
  179. DeclarationName Name = D->getDeclName();
  180. if (IdentifierInfo *II = Name.getAsIdentifierInfo())
  181. updatingIdentifier(*II);
  182. auto *Ptr = Name.getFETokenInfo<void>();
  183. assert(Ptr && "Didn't find this decl on its identifier's chain!");
  184. if (isDeclPtr(Ptr)) {
  185. assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
  186. Name.setFETokenInfo(nullptr);
  187. return;
  188. }
  189. return toIdDeclInfo(Ptr)->RemoveDecl(D);
  190. }
  191. /// begin - Returns an iterator for decls with name 'Name'.
  192. IdentifierResolver::iterator
  193. IdentifierResolver::begin(DeclarationName Name) {
  194. if (IdentifierInfo *II = Name.getAsIdentifierInfo())
  195. readingIdentifier(*II);
  196. auto *Ptr = Name.getFETokenInfo<void>();
  197. if (!Ptr) return end();
  198. if (isDeclPtr(Ptr))
  199. return iterator(static_cast<NamedDecl *>(Ptr));
  200. IdDeclInfo *IDI = toIdDeclInfo(Ptr);
  201. IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
  202. if (I != IDI->decls_begin())
  203. return iterator(I-1);
  204. // No decls found.
  205. return end();
  206. }
  207. namespace {
  208. enum DeclMatchKind {
  209. DMK_Different,
  210. DMK_Replace,
  211. DMK_Ignore
  212. };
  213. } // namespace
  214. /// \brief Compare two declarations to see whether they are different or,
  215. /// if they are the same, whether the new declaration should replace the
  216. /// existing declaration.
  217. static DeclMatchKind compareDeclarations(NamedDecl *Existing, NamedDecl *New) {
  218. // If the declarations are identical, ignore the new one.
  219. if (Existing == New)
  220. return DMK_Ignore;
  221. // If the declarations have different kinds, they're obviously different.
  222. if (Existing->getKind() != New->getKind())
  223. return DMK_Different;
  224. // If the declarations are redeclarations of each other, keep the newest one.
  225. if (Existing->getCanonicalDecl() == New->getCanonicalDecl()) {
  226. // If we're adding an imported declaration, don't replace another imported
  227. // declaration.
  228. if (Existing->isFromASTFile() && New->isFromASTFile())
  229. return DMK_Different;
  230. // If either of these is the most recent declaration, use it.
  231. Decl *MostRecent = Existing->getMostRecentDecl();
  232. if (Existing == MostRecent)
  233. return DMK_Ignore;
  234. if (New == MostRecent)
  235. return DMK_Replace;
  236. // If the existing declaration is somewhere in the previous declaration
  237. // chain of the new declaration, then prefer the new declaration.
  238. for (auto RD : New->redecls()) {
  239. if (RD == Existing)
  240. return DMK_Replace;
  241. if (RD->isCanonicalDecl())
  242. break;
  243. }
  244. return DMK_Ignore;
  245. }
  246. return DMK_Different;
  247. }
  248. bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name){
  249. if (IdentifierInfo *II = Name.getAsIdentifierInfo())
  250. readingIdentifier(*II);
  251. auto *Ptr = Name.getFETokenInfo<void>();
  252. if (!Ptr) {
  253. Name.setFETokenInfo(D);
  254. return true;
  255. }
  256. IdDeclInfo *IDI;
  257. if (isDeclPtr(Ptr)) {
  258. auto *PrevD = static_cast<NamedDecl *>(Ptr);
  259. switch (compareDeclarations(PrevD, D)) {
  260. case DMK_Different:
  261. break;
  262. case DMK_Ignore:
  263. return false;
  264. case DMK_Replace:
  265. Name.setFETokenInfo(D);
  266. return true;
  267. }
  268. Name.setFETokenInfo(nullptr);
  269. IDI = &(*IdDeclInfos)[Name];
  270. // If the existing declaration is not visible in translation unit scope,
  271. // then add the new top-level declaration first.
  272. if (!PrevD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
  273. IDI->AddDecl(D);
  274. IDI->AddDecl(PrevD);
  275. } else {
  276. IDI->AddDecl(PrevD);
  277. IDI->AddDecl(D);
  278. }
  279. return true;
  280. }
  281. IDI = toIdDeclInfo(Ptr);
  282. // See whether this declaration is identical to any existing declarations.
  283. // If not, find the right place to insert it.
  284. for (IdDeclInfo::DeclsTy::iterator I = IDI->decls_begin(),
  285. IEnd = IDI->decls_end();
  286. I != IEnd; ++I) {
  287. switch (compareDeclarations(*I, D)) {
  288. case DMK_Different:
  289. break;
  290. case DMK_Ignore:
  291. return false;
  292. case DMK_Replace:
  293. *I = D;
  294. return true;
  295. }
  296. if (!(*I)->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
  297. // We've found a declaration that is not visible from the translation
  298. // unit (it's in an inner scope). Insert our declaration here.
  299. IDI->InsertDecl(I, D);
  300. return true;
  301. }
  302. }
  303. // Add the declaration to the end.
  304. IDI->AddDecl(D);
  305. return true;
  306. }
  307. void IdentifierResolver::readingIdentifier(IdentifierInfo &II) {
  308. if (II.isOutOfDate())
  309. PP.getExternalSource()->updateOutOfDateIdentifier(II);
  310. }
  311. void IdentifierResolver::updatingIdentifier(IdentifierInfo &II) {
  312. if (II.isOutOfDate())
  313. PP.getExternalSource()->updateOutOfDateIdentifier(II);
  314. if (II.isFromAST())
  315. II.setFETokenInfoChangedSinceDeserialization();
  316. }
  317. //===----------------------------------------------------------------------===//
  318. // IdDeclInfoMap Implementation
  319. //===----------------------------------------------------------------------===//
  320. /// Returns the IdDeclInfo associated to the DeclarationName.
  321. /// It creates a new IdDeclInfo if one was not created before for this id.
  322. IdentifierResolver::IdDeclInfo &
  323. IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
  324. auto *Ptr = Name.getFETokenInfo<void>();
  325. if (Ptr) return *toIdDeclInfo(Ptr);
  326. if (CurIndex == POOL_SIZE) {
  327. CurPool = new IdDeclInfoPool(CurPool);
  328. CurIndex = 0;
  329. }
  330. IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
  331. Name.setFETokenInfo(reinterpret_cast<void*>(
  332. reinterpret_cast<uintptr_t>(IDI) | 0x1)
  333. );
  334. ++CurIndex;
  335. return *IDI;
  336. }
  337. void IdentifierResolver::iterator::incrementSlowCase() {
  338. NamedDecl *D = **this;
  339. auto *InfoPtr = D->getDeclName().getFETokenInfo<void>();
  340. assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
  341. IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
  342. BaseIter I = getIterator();
  343. if (I != Info->decls_begin())
  344. *this = iterator(I-1);
  345. else // No more decls.
  346. *this = iterator();
  347. }