IdentifierResolver.cpp 13 KB

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