blob: 65f3256c2138f43b33d4c728bee6d4843f37d6fe [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
Douglas Gregor63935192009-03-02 00:19:53 +000054 /// Replaces the Old declaration with the New declaration. If the
55 /// replacement is successful, returns true. If the old
56 /// declaration was not found, returns false.
57 bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
58
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000059 private:
60 DeclsTy Decls;
61 };
62
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000063public:
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000064
Douglas Gregor2def4832008-11-17 20:34:05 +000065 /// iterator - Iterate over the decls of a specified declaration name.
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000066 /// It will walk or not the parent declaration contexts depending on how
67 /// it was instantiated.
68 class iterator {
Douglas Gregor6ed40e32008-12-23 21:05:05 +000069 public:
70 typedef NamedDecl * value_type;
71 typedef NamedDecl * reference;
72 typedef NamedDecl * pointer;
73 typedef std::input_iterator_tag iterator_category;
74 typedef std::ptrdiff_t difference_type;
75
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000076 /// Ptr - There are 3 forms that 'Ptr' represents:
77 /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
78 /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
79 /// same declaration context. (Ptr & 0x3 == 0x1)
80 /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
81 /// declaration contexts too. (Ptr & 0x3 == 0x3)
82 uintptr_t Ptr;
83 typedef IdDeclInfo::DeclsTy::iterator BaseIter;
84
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000085 /// A single NamedDecl. (Ptr & 0x1 == 0)
86 iterator(NamedDecl *D) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000087 Ptr = reinterpret_cast<uintptr_t>(D);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000088 assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000089 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000090 /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
91 /// contexts depending on 'LookInParentCtx'.
Douglas Gregor4c921ae2009-01-30 01:04:22 +000092 iterator(BaseIter I) {
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000093 Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000094 }
95
96 bool isIterator() const { return (Ptr & 0x1); }
97
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +000098 BaseIter getIterator() const {
99 assert(isIterator() && "Ptr not an iterator!");
100 return reinterpret_cast<BaseIter>(Ptr & ~0x3);
101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000103 friend class IdentifierResolver;
104 public:
Argyrios Kyrtzidisb53c7842008-08-01 10:20:48 +0000105 iterator() : Ptr(0) {}
106
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000107 NamedDecl *operator*() const {
108 if (isIterator())
109 return *getIterator();
110 else
111 return reinterpret_cast<NamedDecl*>(Ptr);
112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000114 bool operator==(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000115 return Ptr == RHS.Ptr;
116 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000117 bool operator!=(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000118 return Ptr != RHS.Ptr;
119 }
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000121 // Preincrement.
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000122 iterator& operator++() {
123 if (!isIterator()) // common case.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000124 Ptr = 0;
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000125 else {
126 NamedDecl *D = **this;
127 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
128 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
129 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000131 BaseIter I = getIterator();
132 if (I != Info->decls_begin())
133 *this = iterator(I-1);
134 else // No more decls.
135 *this = iterator();
136 }
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000137 return *this;
138 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000139
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000140 uintptr_t getAsOpaqueValue() const { return Ptr; }
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000142 static iterator getFromOpaqueValue(uintptr_t P) {
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000143 iterator Result;
Douglas Gregoreb11cd02009-01-14 22:20:51 +0000144 Result.Ptr = P;
145 return Result;
146 }
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000147 };
148
Douglas Gregor4c921ae2009-01-30 01:04:22 +0000149 /// begin - Returns an iterator for decls with the name 'Name'.
150 static iterator begin(DeclarationName Name);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000151
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000152 /// end - Returns an iterator that has 'finished'.
153 static iterator end() {
154 return iterator();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000155 }
156
Argyrios Kyrtzidise29f0a42008-05-14 10:49:47 +0000157 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
158 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
159 /// true if 'D' belongs to the given declaration context.
Douglas Gregor44b43212008-12-11 16:49:14 +0000160 bool isDeclInScope(Decl *D, DeclContext *Ctx, ASTContext &Context,
161 Scope *S = 0) const;
Chris Lattnera2f42b12008-04-11 07:06:57 +0000162
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000163 /// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000164 void AddDecl(NamedDecl *D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000165
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000166 /// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000167 /// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000168 /// encountered before the 'D' decl.
169 void AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000170
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000171 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000172 /// The decl must already be part of the decl chain.
173 void RemoveDecl(NamedDecl *D);
174
Douglas Gregor63935192009-03-02 00:19:53 +0000175 /// Replace the decl Old with the new declaration New on its
176 /// identifier chain. Returns true if the old declaration was found
177 /// (and, therefore, replaced).
178 bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
179
Douglas Gregor668c1a42009-04-21 22:25:48 +0000180 /// \brief Link the declaration into the chain of declarations for
181 /// the given identifier.
182 ///
183 /// This is a lower-level routine used by the PCH reader to link a
184 /// declaration into a specific IdentifierInfo before the
185 /// declaration actually has a name.
186 void AddDeclToIdentifierChain(IdentifierInfo *II, NamedDecl *D);
187
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000188 explicit IdentifierResolver(const LangOptions &LangOpt);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000189 ~IdentifierResolver();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000190
191private:
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000192 const LangOptions &LangOpt;
193
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000194 class IdDeclInfoMap;
195 IdDeclInfoMap *IdDeclInfos;
196
Douglas Gregor2def4832008-11-17 20:34:05 +0000197 /// FETokenInfo contains a Decl pointer if lower bit == 0.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000198 static inline bool isDeclPtr(void *Ptr) {
199 return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
200 }
201
Douglas Gregor2def4832008-11-17 20:34:05 +0000202 /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000203 static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
204 assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
205 && "Ptr not a IdDeclInfo* !");
206 return reinterpret_cast<IdDeclInfo*>(
207 reinterpret_cast<uintptr_t>(Ptr) & ~0x1
208 );
209 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000210};
211
212} // end namespace clang
213
214#endif