blob: 6efedb4895e74f946d7b7917b47f3986778dbed6 [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;
35
36public:
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/// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
50/// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
51/// be already added to the scope chain and must be in the same context as
52/// the decl that we want to add.
53void IdentifierResolver::IdDeclInfo::AddShadowed(NamedDecl *D,
54 NamedDecl *Shadow) {
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000055 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
56 if (Shadow == *(I-1)) {
57 Decls.insert(I-1, D);
58 return;
59 }
60 }
61
62 assert(0 && "Shadow wasn't in scope chain!");
63}
64
65/// RemoveDecl - Remove the decl from the scope chain.
66/// The decl must already be part of the decl chain.
67void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
68 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
69 if (D == *(I-1)) {
70 Decls.erase(I-1);
71 return;
72 }
73 }
74
75 assert(0 && "Didn't find this decl on its identifier's chain!");
76}
77
78
79//===----------------------------------------------------------------------===//
80// IdentifierResolver Implementation
81//===----------------------------------------------------------------------===//
82
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +000083IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
84 : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
85}
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000086IdentifierResolver::~IdentifierResolver() {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000087 delete IdDeclInfos;
Argyrios Kyrtzidis7bc198f2008-04-14 00:09:21 +000088}
Chris Lattnera2f42b12008-04-11 07:06:57 +000089
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000090/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
91/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
92/// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +000093bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
Douglas Gregor44b43212008-12-11 16:49:14 +000094 ASTContext &Context, Scope *S) const {
Douglas Gregorce356072009-01-06 23:51:29 +000095 Ctx = Ctx->getLookupContext();
Douglas Gregor074149e2009-01-05 19:45:36 +000096
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +000097 if (Ctx->isFunctionOrMethod()) {
Douglas Gregor074149e2009-01-05 19:45:36 +000098 // Ignore the scopes associated within transparent declaration contexts.
99 while (S->getEntity() &&
100 ((DeclContext *)S->getEntity())->isTransparentContext())
101 S = S->getParent();
102
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000103 if (S->isDeclScope(D))
104 return true;
105 if (LangOpt.CPlusPlus) {
Sebastian Redla0fd8652008-12-21 16:41:36 +0000106 // C++ 3.3.2p3:
107 // The name declared in a catch exception-declaration is local to the
108 // handler and shall not be redeclared in the outermost block of the
109 // handler.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000110 // C++ 3.3.2p4:
111 // Names declared in the for-init-statement, and in the condition of if,
112 // while, for, and switch statements are local to the if, while, for, or
113 // switch statement (including the controlled statement), and shall not be
114 // redeclared in a subsequent condition of that statement nor in the
115 // outermost block (or, for the if statement, any of the outermost blocks)
116 // of the controlled statement.
117 //
118 assert(S->getParent() && "No TUScope?");
119 if (S->getParent()->getFlags() & Scope::ControlScope)
120 return S->getParent()->isDeclScope(D);
121 }
122 return false;
123 }
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000124
Argyrios Kyrtzidis7fd46da2009-02-17 20:21:51 +0000125 return D->getDeclContext()->getLookupContext() == Ctx->getPrimaryContext();
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000126}
127
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000128/// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000129void IdentifierResolver::AddDecl(NamedDecl *D) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000130 DeclarationName Name = D->getDeclName();
131 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000132
133 if (!Ptr) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000134 Name.setFETokenInfo(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000135 return;
136 }
137
138 IdDeclInfo *IDI;
139
140 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000141 Name.setFETokenInfo(NULL);
142 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000143 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
144 IDI->AddDecl(PrevD);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000145 } else
146 IDI = toIdDeclInfo(Ptr);
147
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000148 IDI->AddDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000149}
150
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000151/// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000152/// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000153/// encountered before the 'D' decl.
154void IdentifierResolver::AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000155 assert(D->getDeclName() == Shadow->getDeclName() && "Different ids!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000156
Douglas Gregor2def4832008-11-17 20:34:05 +0000157 DeclarationName Name = D->getDeclName();
158 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000159 assert(Ptr && "No decl from Ptr ?");
Chris Lattnera2f42b12008-04-11 07:06:57 +0000160
161 IdDeclInfo *IDI;
162
163 if (isDeclPtr(Ptr)) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000164 Name.setFETokenInfo(NULL);
165 IDI = &(*IdDeclInfos)[Name];
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000166 NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
167 assert(PrevD == Shadow && "Invalid shadow decl ?");
168 IDI->AddDecl(D);
169 IDI->AddDecl(PrevD);
170 return;
171 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000172
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000173 IDI = toIdDeclInfo(Ptr);
174 IDI->AddShadowed(D, Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000175}
176
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000177/// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000178/// The decl must already be part of the decl chain.
179void IdentifierResolver::RemoveDecl(NamedDecl *D) {
180 assert(D && "null param passed");
Douglas Gregor2def4832008-11-17 20:34:05 +0000181 DeclarationName Name = D->getDeclName();
182 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000183
184 assert(Ptr && "Didn't find this decl on its identifier's chain!");
185
186 if (isDeclPtr(Ptr)) {
187 assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
Douglas Gregor2def4832008-11-17 20:34:05 +0000188 Name.setFETokenInfo(NULL);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000189 return;
190 }
191
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000192 return toIdDeclInfo(Ptr)->RemoveDecl(D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000193}
194
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000195/// begin - Returns an iterator for decls with name 'Name'.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000196IdentifierResolver::iterator
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000197IdentifierResolver::begin(DeclarationName Name) {
Douglas Gregor2def4832008-11-17 20:34:05 +0000198 void *Ptr = Name.getFETokenInfo<void>();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000199 if (!Ptr) return end();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000200
Chris Lattnera2f42b12008-04-11 07:06:57 +0000201 if (isDeclPtr(Ptr)) {
202 NamedDecl *D = static_cast<NamedDecl*>(Ptr);
Douglas Gregor074149e2009-01-05 19:45:36 +0000203 return iterator(D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000204 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000205
Chris Lattnera2f42b12008-04-11 07:06:57 +0000206 IdDeclInfo *IDI = toIdDeclInfo(Ptr);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000207
Douglas Gregor074149e2009-01-05 19:45:36 +0000208 IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000209 if (I != IDI->decls_begin())
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000210 return iterator(I-1);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000211 else // No decls found.
212 return end();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000213}
214
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000215//===----------------------------------------------------------------------===//
216// IdDeclInfoMap Implementation
217//===----------------------------------------------------------------------===//
218
Douglas Gregor2def4832008-11-17 20:34:05 +0000219/// Returns the IdDeclInfo associated to the DeclarationName.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000220/// It creates a new IdDeclInfo if one was not created before for this id.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000221IdentifierResolver::IdDeclInfo &
Douglas Gregor2def4832008-11-17 20:34:05 +0000222IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
223 void *Ptr = Name.getFETokenInfo<void>();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000224
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000225 if (Ptr) return *toIdDeclInfo(Ptr);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000226
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000227 if (CurIndex == VECTOR_SIZE) {
228 // Add a IdDeclInfo vector 'pool'
Argyrios Kyrtzidis72e62b02008-04-12 12:38:58 +0000229 IDIVecs.push_back(std::vector<IdDeclInfo>());
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000230 // Fill the vector
231 IDIVecs.back().resize(VECTOR_SIZE);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000232 CurIndex = 0;
233 }
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000234 IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
Douglas Gregor2def4832008-11-17 20:34:05 +0000235 Name.setFETokenInfo(reinterpret_cast<void*>(
Chris Lattnera2f42b12008-04-11 07:06:57 +0000236 reinterpret_cast<uintptr_t>(IDI) | 0x1)
237 );
238 ++CurIndex;
239 return *IDI;
240}