blob: bff47519a743db734ccc465d5baf5ea771e3a192 [file] [log] [blame]
Chris Lattnera2f42b12008-04-11 07:06:57 +00001//===- 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 Kyrtzidis321f2782008-04-12 01:50:47 +000010// This file implements the IdentifierResolver class, which is used for lexical
Douglas Gregor2def4832008-11-17 20:34:05 +000011// scoped lookup, based on declaration names.
Chris Lattnera2f42b12008-04-11 07:06:57 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "IdentifierResolver.h"
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +000016#include "clang/Basic/LangOptions.h"
Chris Lattnera2f42b12008-04-11 07:06:57 +000017#include <list>
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000018#include <vector>
Chris Lattnera2f42b12008-04-11 07:06:57 +000019
20using namespace clang;
21
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000022//===----------------------------------------------------------------------===//
23// IdDeclInfoMap class
24//===----------------------------------------------------------------------===//
Chris Lattnera2f42b12008-04-11 07:06:57 +000025
Douglas Gregor2def4832008-11-17 20:34:05 +000026/// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000027/// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
Chris Lattnera2f42b12008-04-11 07:06:57 +000028/// individual IdDeclInfo to heap.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000029class IdentifierResolver::IdDeclInfoMap {
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000030 static const unsigned int VECTOR_SIZE = 512;
31 // Holds vectors of IdDeclInfos that serve as 'pools'.
32 // New vectors are added when the current one is full.
33 std::list< std::vector<IdDeclInfo> > IDIVecs;
Chris Lattnera2f42b12008-04-11 07:06:57 +000034 unsigned int CurIndex;
Mike Stump1eb44332009-09-09 15:08:12 +000035
Chris Lattnera2f42b12008-04-11 07:06:57 +000036public:
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000037 IdDeclInfoMap() : CurIndex(VECTOR_SIZE) {}
Chris Lattnera2f42b12008-04-11 07:06:57 +000038
Douglas Gregor2def4832008-11-17 20:34:05 +000039 /// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +000040 /// It creates a new IdDeclInfo if one was not created before for this id.
Douglas Gregor2def4832008-11-17 20:34:05 +000041 IdDeclInfo &operator[](DeclarationName Name);
Chris Lattnera2f42b12008-04-11 07:06:57 +000042};
43
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000044
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000045//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000046// IdDeclInfo Implementation
47//===----------------------------------------------------------------------===//
48
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000049/// RemoveDecl - Remove the decl from the scope chain.
50/// The decl must already be part of the decl chain.
51void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
52 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
53 if (D == *(I-1)) {
54 Decls.erase(I-1);
55 return;
56 }
57 }
58
59 assert(0 && "Didn't find this decl on its identifier's chain!");
60}
61
Mike Stump1eb44332009-09-09 15:08:12 +000062bool
Douglas Gregor63935192009-03-02 00:19:53 +000063IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
64 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
65 if (Old == *(I-1)) {
66 *(I - 1) = New;
67 return true;
68 }
69 }
70
71 return false;
72}
73
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000074
75//===----------------------------------------------------------------------===//
76// IdentifierResolver Implementation
77//===----------------------------------------------------------------------===//
78
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +000079IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
80 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
81}
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000082IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000083 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000084}
Chris Lattnera2f42b12008-04-11 07:06:57 +000085
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000086/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
87/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
88/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +000089bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
Douglas Gregor44b43212008-12-11 16:49:14 +000090 ASTContext &Context, Scope *S) const {
Douglas Gregorce356072009-01-06 23:51:29 +000091 Ctx = Ctx->getLookupContext();
Douglas Gregor074149e2009-01-05 19:45:36 +000092
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +000093 if (Ctx->isFunctionOrMethod()) {
Douglas Gregor074149e2009-01-05 19:45:36 +000094 // Ignore the scopes associated within transparent declaration contexts.
Mike Stump1eb44332009-09-09 15:08:12 +000095 while (S->getEntity() &&
Douglas Gregor074149e2009-01-05 19:45:36 +000096 ((DeclContext *)S->getEntity())->isTransparentContext())
97 S = S->getParent();
98
Chris Lattnerb28317a2009-03-28 19:18:32 +000099 if (S->isDeclScope(Action::DeclPtrTy::make(D)))
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000100 return true;
101 if (LangOpt.CPlusPlus) {
Sebastian Redla0fd8652008-12-21 16:41:36 +0000102 // C++ 3.3.2p3:
103 // The name declared in a catch exception-declaration is local to the
104 // handler and shall not be redeclared in the outermost block of the
105 // handler.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000106 // C++ 3.3.2p4:
107 // Names declared in the for-init-statement, and in the condition of if,
108 // while, for, and switch statements are local to the if, while, for, or
109 // switch statement (including the controlled statement), and shall not be
110 // redeclared in a subsequent condition of that statement nor in the
111 // outermost block (or, for the if statement, any of the outermost blocks)
112 // of the controlled statement.
113 //
114 assert(S->getParent() && "No TUScope?");
115 if (S->getParent()->getFlags() & Scope::ControlScope)
Chris Lattnerb28317a2009-03-28 19:18:32 +0000116 return S->getParent()->isDeclScope(Action::DeclPtrTy::make(D));
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000117 }
118 return false;
119 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000120
Douglas Gregor61481da2009-09-01 17:22:34 +0000121 return D->getDeclContext()->getLookupContext()->Equals(Ctx);
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000122}
123
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000124/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000125void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000126 DeclarationName Name = D->getDeclName();
127 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000128
129 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000130 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000131 return;
132 }
133
134 IdDeclInfo *IDI;
135
136 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000137 Name.setFETokenInfo(NULL);
138 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000139 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
140 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000141 } else
142 IDI = toIdDeclInfo(Ptr);
143
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000144 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000145}
146
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000147/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000148/// The decl must already be part of the decl chain.
149void IdentifierResolver::RemoveDecl(NamedDecl *D) {
150 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000151 DeclarationName Name = D->getDeclName();
152 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000153
154 assert(Ptr && "Didn't find this decl on its identifier's chain!");
155
156 if (isDeclPtr(Ptr)) {
157 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000158 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000159 return;
160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000162 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000163}
164
Douglas Gregor63935192009-03-02 00:19:53 +0000165bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
Mike Stump1eb44332009-09-09 15:08:12 +0000166 assert(Old->getDeclName() == New->getDeclName() &&
Douglas Gregor63935192009-03-02 00:19:53 +0000167 "Cannot replace a decl with another decl of a different name");
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Douglas Gregor63935192009-03-02 00:19:53 +0000169 DeclarationName Name = Old->getDeclName();
170 void *Ptr = Name.getFETokenInfo<void>();
171
172 if (!Ptr)
173 return false;
174
175 if (isDeclPtr(Ptr)) {
176 if (Ptr == Old) {
177 Name.setFETokenInfo(New);
178 return true;
179 }
180 return false;
181 }
182
Mike Stump1eb44332009-09-09 15:08:12 +0000183 return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New);
Douglas Gregor63935192009-03-02 00:19:53 +0000184}
185
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000186/// begin - Returns an iterator for decls with name 'Name'.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000187IdentifierResolver::iterator
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000188IdentifierResolver::begin(DeclarationName Name) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000189 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000190 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000191
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000192 if (isDeclPtr(Ptr))
193 return iterator(static_cast<NamedDecl*>(Ptr));
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000194
Chris Lattnera2f42b12008-04-11 07:06:57 +0000195 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000196
Douglas Gregor074149e2009-01-05 19:45:36 +0000197 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000198 if (I != IDI->decls_begin())
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000199 return iterator(I-1);
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000200 // No decls found.
201 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000202}
203
Mike Stump1eb44332009-09-09 15:08:12 +0000204void IdentifierResolver::AddDeclToIdentifierChain(IdentifierInfo *II,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000205 NamedDecl *D) {
206 void *Ptr = II->getFETokenInfo<void>();
207
208 if (!Ptr) {
209 II->setFETokenInfo(D);
210 return;
211 }
212
213 IdDeclInfo *IDI;
214
215 if (isDeclPtr(Ptr)) {
216 II->setFETokenInfo(NULL);
217 IDI = &(*IdDeclInfos)[II];
218 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
219 IDI->AddDecl(PrevD);
220 } else
221 IDI = toIdDeclInfo(Ptr);
222
223 IDI->AddDecl(D);
224}
225
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000226//===----------------------------------------------------------------------===//
227// IdDeclInfoMap Implementation
228//===----------------------------------------------------------------------===//
229
Douglas Gregor2def4832008-11-17 20:34:05 +0000230/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000231/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000232IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000233IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
234 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000235
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000236 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000237
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000238 if (CurIndex == VECTOR_SIZE) {
239 // Add a IdDeclInfo vector 'pool'
Argyrios Kyrtzidis72e62b02008-04-12 12:38:58 +0000240 IDIVecs.push_back(std::vector<IdDeclInfo>());
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000241 // Fill the vector
242 IDIVecs.back().resize(VECTOR_SIZE);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000243 CurIndex = 0;
244 }
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000245 IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000246 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000247 reinterpret_cast<uintptr_t>(IDI) | 0x1)
248 );
249 ++CurIndex;
250 return *IDI;
251}