blob: 067900eac1cdfd17bef147d4e6bb65b9003a8166 [file] [log] [blame]
Chris Lattnera2f42b12008-04-11 07:06:57 +00001//===- IdentifierResolver.h - 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 defines 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#ifndef LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H
16#define LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H
17
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000018#include "clang/Basic/IdentifierTable.h"
19#include "clang/Parse/Scope.h"
20#include "clang/AST/Decl.h"
Douglas Gregor2def4832008-11-17 20:34:05 +000021#include "clang/AST/DeclarationName.h"
Argyrios Kyrtzidis32a5ba02008-06-24 23:08:34 +000022#include "clang/AST/DeclCXX.h"
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000023
Chris Lattnera2f42b12008-04-11 07:06:57 +000024namespace clang {
Chris Lattnera2f42b12008-04-11 07:06:57 +000025
Douglas Gregor2def4832008-11-17 20:34:05 +000026/// IdentifierResolver - Keeps track of shadowed decls on enclosing
27/// scopes. It manages the shadowing chains of declaration names and
28/// implements efficent decl lookup based on a declaration name.
Chris Lattnera2f42b12008-04-11 07:06:57 +000029class IdentifierResolver {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000030
Douglas Gregor2def4832008-11-17 20:34:05 +000031 /// IdDeclInfo - Keeps track of information about decls associated
32 /// to a particular declaration name. IdDeclInfos are lazily
33 /// constructed and assigned to a declaration name the first time a
34 /// decl with that declaration name is shadowed in some scope.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000035 class IdDeclInfo {
36 public:
37 typedef llvm::SmallVector<NamedDecl*, 2> DeclsTy;
38
39 inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
40 inline DeclsTy::iterator decls_end() { return Decls.end(); }
41
Douglas Gregor074149e2009-01-05 19:45:36 +000042 void AddDecl(NamedDecl *D) { Decls.push_back(D); }
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000043
44 /// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
45 /// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
46 /// be already added to the scope chain and must be in the same context as
47 /// the decl that we want to add.
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000048 void AddShadowed(NamedDecl *D, NamedDecl *Shadow);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000049
50 /// RemoveDecl - Remove the decl from the scope chain.
51 /// The decl must already be part of the decl chain.
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000052 void RemoveDecl(NamedDecl *D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000053
54 private:
55 DeclsTy Decls;
56 };
57
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000058public:
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000059
Douglas Gregor2def4832008-11-17 20:34:05 +000060 /// iterator - Iterate over the decls of a specified declaration name.
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000061 /// It will walk or not the parent declaration contexts depending on how
62 /// it was instantiated.
63 class iterator {
Douglas Gregor6ed40e32008-12-23 21:05:05 +000064 public:
65 typedef NamedDecl * value_type;
66 typedef NamedDecl * reference;
67 typedef NamedDecl * pointer;
68 typedef std::input_iterator_tag iterator_category;
69 typedef std::ptrdiff_t difference_type;
70
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000071 /// Ptr - There are 3 forms that 'Ptr' represents:
72 /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
73 /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
74 /// same declaration context. (Ptr & 0x3 == 0x1)
75 /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
76 /// declaration contexts too. (Ptr & 0x3 == 0x3)
77 uintptr_t Ptr;
78 typedef IdDeclInfo::DeclsTy::iterator BaseIter;
79
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000080 /// A single NamedDecl. (Ptr & 0x1 == 0)
81 iterator(NamedDecl *D) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000082 Ptr = reinterpret_cast<uintptr_t>(D);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000083 assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000084 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000085 /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
86 /// contexts depending on 'LookInParentCtx'.
Douglas Gregor4c921ae2009-01-30 01:04:22 +000087 iterator(BaseIter I) {
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000088 Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000089 }
90
91 bool isIterator() const { return (Ptr & 0x1); }
92
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000093 BaseIter getIterator() const {
94 assert(isIterator() && "Ptr not an iterator!");
95 return reinterpret_cast<BaseIter>(Ptr & ~0x3);
96 }
97
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000098 friend class IdentifierResolver;
99 public:
Argyrios Kyrtzidisb53c7842008-08-01 10:20:48 +0000100 iterator() : Ptr(0) {}
101
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000102 NamedDecl *operator*() const {
103 if (isIterator())
104 return *getIterator();
105 else
106 return reinterpret_cast<NamedDecl*>(Ptr);
107 }
108
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000109 bool operator==(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000110 return Ptr == RHS.Ptr;
111 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000112 bool operator!=(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000113 return Ptr != RHS.Ptr;
114 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000115
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000116 // Preincrement.
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000117 iterator& operator++() {
118 if (!isIterator()) // common case.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000119 Ptr = 0;
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000120 else {
121 NamedDecl *D = **this;
122 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
123 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
124 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
125
126 BaseIter I = getIterator();
127 if (I != Info->decls_begin())
128 *this = iterator(I-1);
129 else // No more decls.
130 *this = iterator();
131 }
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000132 return *this;
133 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000134
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000135 uintptr_t getAsOpaqueValue() const { return Ptr; }
136
137 static iterator getFromOpaqueValue(uintptr_t P) {
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000138 iterator Result;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000139 Result.Ptr = P;
140 return Result;
141 }
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000142 };
143
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000144 /// begin - Returns an iterator for decls with the name 'Name'.
145 static iterator begin(DeclarationName Name);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000146
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000147 /// end - Returns an iterator that has 'finished'.
148 static iterator end() {
149 return iterator();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000150 }
151
Argyrios Kyrtzidise29f0a42008-05-14 10:49:47 +0000152 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
153 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
154 /// true if 'D' belongs to the given declaration context.
Douglas Gregor44b43212008-12-11 16:49:14 +0000155 bool isDeclInScope(Decl *D, DeclContext *Ctx, ASTContext &Context,
156 Scope *S = 0) const;
Chris Lattnera2f42b12008-04-11 07:06:57 +0000157
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000158 /// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000159 void AddDecl(NamedDecl *D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000160
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000161 /// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000162 /// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000163 /// encountered before the 'D' decl.
164 void AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000165
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000166 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000167 /// The decl must already be part of the decl chain.
168 void RemoveDecl(NamedDecl *D);
169
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000170 explicit IdentifierResolver(const LangOptions &LangOpt);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000171 ~IdentifierResolver();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000172
173private:
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000174 const LangOptions &LangOpt;
175
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000176 class IdDeclInfoMap;
177 IdDeclInfoMap *IdDeclInfos;
178
Douglas Gregor2def4832008-11-17 20:34:05 +0000179 /// FETokenInfo contains a Decl pointer if lower bit == 0.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000180 static inline bool isDeclPtr(void *Ptr) {
181 return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
182 }
183
Douglas Gregor2def4832008-11-17 20:34:05 +0000184 /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000185 static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
186 assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
187 && "Ptr not a IdDeclInfo* !");
188 return reinterpret_cast<IdDeclInfo*>(
189 reinterpret_cast<uintptr_t>(Ptr) & ~0x1
190 );
191 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000192};
193
194} // end namespace clang
195
196#endif