blob: 59bd834073f3dae46e6649f2e857c83f6dd856e7 [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
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000044 /// RemoveDecl - Remove the decl from the scope chain.
45 /// The decl must already be part of the decl chain.
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000046 void RemoveDecl(NamedDecl *D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000047
Douglas Gregor63935192009-03-02 00:19:53 +000048 /// Replaces the Old declaration with the New declaration. If the
49 /// replacement is successful, returns true. If the old
50 /// declaration was not found, returns false.
51 bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
52
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000053 private:
54 DeclsTy Decls;
55 };
56
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000057public:
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000058
Douglas Gregor2def4832008-11-17 20:34:05 +000059 /// iterator - Iterate over the decls of a specified declaration name.
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000060 /// It will walk or not the parent declaration contexts depending on how
61 /// it was instantiated.
62 class iterator {
Douglas Gregor6ed40e32008-12-23 21:05:05 +000063 public:
64 typedef NamedDecl * value_type;
65 typedef NamedDecl * reference;
66 typedef NamedDecl * pointer;
67 typedef std::input_iterator_tag iterator_category;
68 typedef std::ptrdiff_t difference_type;
69
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000070 /// Ptr - There are 3 forms that 'Ptr' represents:
71 /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
72 /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
73 /// same declaration context. (Ptr & 0x3 == 0x1)
74 /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
75 /// declaration contexts too. (Ptr & 0x3 == 0x3)
76 uintptr_t Ptr;
77 typedef IdDeclInfo::DeclsTy::iterator BaseIter;
78
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000079 /// A single NamedDecl. (Ptr & 0x1 == 0)
80 iterator(NamedDecl *D) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000081 Ptr = reinterpret_cast<uintptr_t>(D);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000082 assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000083 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000084 /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
85 /// contexts depending on 'LookInParentCtx'.
Douglas Gregor4c921ae2009-01-30 01:04:22 +000086 iterator(BaseIter I) {
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000087 Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000088 }
89
90 bool isIterator() const { return (Ptr & 0x1); }
91
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000092 BaseIter getIterator() const {
93 assert(isIterator() && "Ptr not an iterator!");
94 return reinterpret_cast<BaseIter>(Ptr & ~0x3);
95 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000097 friend class IdentifierResolver;
98 public:
Argyrios Kyrtzidisb53c7842008-08-01 10:20:48 +000099 iterator() : Ptr(0) {}
100
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000101 NamedDecl *operator*() const {
102 if (isIterator())
103 return *getIterator();
104 else
105 return reinterpret_cast<NamedDecl*>(Ptr);
106 }
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000108 bool operator==(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000109 return Ptr == RHS.Ptr;
110 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000111 bool operator!=(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000112 return Ptr != RHS.Ptr;
113 }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000115 // Preincrement.
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000116 iterator& operator++() {
117 if (!isIterator()) // common case.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000118 Ptr = 0;
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000119 else {
120 NamedDecl *D = **this;
121 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
122 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
123 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000125 BaseIter I = getIterator();
126 if (I != Info->decls_begin())
127 *this = iterator(I-1);
128 else // No more decls.
129 *this = iterator();
130 }
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000131 return *this;
132 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000133
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000134 uintptr_t getAsOpaqueValue() const { return Ptr; }
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000136 static iterator getFromOpaqueValue(uintptr_t P) {
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000137 iterator Result;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000138 Result.Ptr = P;
139 return Result;
140 }
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000141 };
142
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000143 /// begin - Returns an iterator for decls with the name 'Name'.
144 static iterator begin(DeclarationName Name);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000145
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000146 /// end - Returns an iterator that has 'finished'.
147 static iterator end() {
148 return iterator();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000149 }
150
Argyrios Kyrtzidise29f0a42008-05-14 10:49:47 +0000151 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
152 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
153 /// true if 'D' belongs to the given declaration context.
Douglas Gregor44b43212008-12-11 16:49:14 +0000154 bool isDeclInScope(Decl *D, DeclContext *Ctx, ASTContext &Context,
155 Scope *S = 0) const;
Chris Lattnera2f42b12008-04-11 07:06:57 +0000156
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000157 /// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000158 void AddDecl(NamedDecl *D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000159
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000160 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000161 /// The decl must already be part of the decl chain.
162 void RemoveDecl(NamedDecl *D);
163
Douglas Gregor63935192009-03-02 00:19:53 +0000164 /// Replace the decl Old with the new declaration New on its
165 /// identifier chain. Returns true if the old declaration was found
166 /// (and, therefore, replaced).
167 bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
168
Douglas Gregor668c1a42009-04-21 22:25:48 +0000169 /// \brief Link the declaration into the chain of declarations for
170 /// the given identifier.
171 ///
172 /// This is a lower-level routine used by the PCH reader to link a
173 /// declaration into a specific IdentifierInfo before the
174 /// declaration actually has a name.
175 void AddDeclToIdentifierChain(IdentifierInfo *II, NamedDecl *D);
176
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000177 explicit IdentifierResolver(const LangOptions &LangOpt);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000178 ~IdentifierResolver();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000179
180private:
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000181 const LangOptions &LangOpt;
182
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000183 class IdDeclInfoMap;
184 IdDeclInfoMap *IdDeclInfos;
185
Douglas Gregor2def4832008-11-17 20:34:05 +0000186 /// FETokenInfo contains a Decl pointer if lower bit == 0.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000187 static inline bool isDeclPtr(void *Ptr) {
188 return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
189 }
190
Douglas Gregor2def4832008-11-17 20:34:05 +0000191 /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000192 static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
193 assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
194 && "Ptr not a IdDeclInfo* !");
195 return reinterpret_cast<IdDeclInfo*>(
196 reinterpret_cast<uintptr_t>(Ptr) & ~0x1
197 );
198 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000199};
200
201} // end namespace clang
202
203#endif