aboutsummaryrefslogtreecommitdiff
path: root/lib/Sema/IdentifierResolver.h
blob: da1327be485a75bd2ef0ceb2d20c7e2e96e61117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//===- IdentifierResolver.h - Lexical Scope Name lookup ---------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the IdentifierResolver class, which is used for lexical
// scoped lookup, based on identifier.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H
#define LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H

#include "clang/Basic/IdentifierTable.h"
#include "clang/Parse/Scope.h"
#include "clang/AST/Decl.h"

namespace clang {

/// IdentifierResolver - Keeps track of shadowed decls on enclosing scopes.
/// It manages the shadowing chains of identifiers and implements efficent decl
/// lookup based on an identifier.
class IdentifierResolver {

  /// LookupContext - A wrapper for DeclContext. DeclContext is only part of
  /// ScopedDecls, LookupContext can be used with all decls (assumes
  /// translation unit context for non ScopedDecls).
  class LookupContext {
    DeclContext *Ctx;

    /// TUCtx - Provides a common value for translation unit context for all
    /// decls.
    /// FIXME: When (if ?) all decls can point to their translation unit context
    /// remove this hack.
    static inline DeclContext *TUCtx() {
      return reinterpret_cast<DeclContext*>(-1);
    }

    /// getContext - Returns translation unit context for non ScopedDecls and
    /// for EnumConstantDecls returns the parent context of their EnumDecl.
    static DeclContext *getContext(Decl *D) {
      DeclContext *Ctx;

      if (EnumConstantDecl *EnumD = dyn_cast<EnumConstantDecl>(D)) {
        Ctx = EnumD->getDeclContext()->getParent();
      } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D))
        Ctx = SD->getDeclContext();
      else
        return TUCtx();

      if (isa<TranslationUnitDecl>(Ctx))
        return TUCtx();

      return Ctx;
    }

  public:
    LookupContext(Decl *D) {
      Ctx = getContext(D);
    }
    LookupContext(DeclContext *DC) {
      if (!DC || isa<TranslationUnitDecl>(DC))
        Ctx = TUCtx();
      else
        Ctx = DC;
    }

    bool isTU() const {
      return (Ctx == TUCtx());
    }

    /// getParent - Returns the parent context. This should not be called for
    /// a translation unit context.
    LookupContext getParent() const {
      assert(!isTU() && "TU has no parent!");
      return LookupContext(Ctx->getParent());
    }

    /// isEqOrContainedBy - Returns true of the given context is the same or a
    /// parent of this one.
    bool isEqOrContainedBy(const LookupContext &PC) const {
      if (PC.isTU()) return true;

      for (LookupContext Next = *this; !Next.isTU();  Next = Next.getParent())
        if (Next.Ctx == PC.Ctx) return true;

      return false;
    }

    bool operator==(const LookupContext &RHS) const {
      return Ctx == RHS.Ctx;
    }
    bool operator!=(const LookupContext &RHS) const {
      return Ctx != RHS.Ctx;
    }
  };

  /// IdDeclInfo - Keeps track of information about decls associated to a
  /// particular identifier. IdDeclInfos are lazily constructed and assigned
  /// to an identifier the first time a decl with that identifier is shadowed
  /// in some scope.
  class IdDeclInfo {
  public:
    typedef llvm::SmallVector<NamedDecl*, 2> DeclsTy;

    inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
    inline DeclsTy::iterator decls_end() { return Decls.end(); }

    /// FindContext - Returns an iterator pointing just after the decl that is
    /// in the given context or in a parent of it. The search is in reverse
    /// order, from end to begin.
    DeclsTy::iterator FindContext(const LookupContext &Ctx) {
      return FindContext(Ctx, Decls.end());
    }

    /// FindContext - Returns an iterator pointing just after the decl that is
    /// in the given context or in a parent of it. The search is in reverse
    /// order, from end to begin.
    DeclsTy::iterator FindContext(const LookupContext &Ctx,
                                  const DeclsTy::iterator &Start) {
      for (DeclsTy::iterator I = Start; I != Decls.begin(); --I) {
        if (Ctx.isEqOrContainedBy(LookupContext(*(I-1))))
          return I;
      }

      return Decls.begin();
    }

    /// iterator - Iterate over the decls by walking their parent contexts too.
    class iterator {
    public:
      typedef DeclsTy::iterator BaseIter;

      iterator(const BaseIter &DeclIt) : DI(DeclIt) {}
      const BaseIter &getBase() { return DI; }

      NamedDecl *&operator*() const {
        return *(DI-1);
      }
      
      bool operator==(const iterator &RHS) const {
        return DI == RHS.DI;
      }
      bool operator!=(const iterator &RHS) const {
        return DI != RHS.DI;
      }
      
      // Preincrement.
      iterator& operator++() {
        NamedDecl *D = **this;
        void *Ptr = D->getIdentifier()->getFETokenInfo<void>();
        assert(!isDeclPtr(Ptr) && "Decl with wrong id ?");
        DI = toIdDeclInfo(Ptr)->FindContext(LookupContext(D), DI-1);
        return *this;
      }

    private:
      BaseIter DI;
    };

    /// ctx_iterator - Iterator over the decls of a specific context only.
    class ctx_iterator {
    public:
      typedef DeclsTy::iterator BaseIter;

      ctx_iterator(const BaseIter &DeclIt) : DI(DeclIt) {}
      const BaseIter &getBase() { return DI; }

      NamedDecl *&operator*() const {
        return *(DI-1);
      }
      
      bool operator==(const ctx_iterator &RHS) const {
        return DI == RHS.DI;
      }
      bool operator!=(const ctx_iterator &RHS) const {
        return DI != RHS.DI;
      }
      
      // Preincrement.
      ctx_iterator& operator++() {
        NamedDecl *D = **this;
        void *Ptr = D->getIdentifier()->getFETokenInfo<void>();
        assert(!isDeclPtr(Ptr) && "Decl with wrong id ?");
        IdDeclInfo *Info = toIdDeclInfo(Ptr);
        
        --DI;
        if (DI != Info->Decls.begin() &&
            LookupContext(D) != LookupContext(**this))
          DI = Info->Decls.begin();
        return *this;
      }

    private:
      BaseIter DI;
    };

    void AddDecl(NamedDecl *D) {
      Decls.insert(FindContext(LookupContext(D)), D);
    }

    /// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
    /// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
    /// be already added to the scope chain and must be in the same context as
    /// the decl that we want to add.
    void AddShadowed(NamedDecl *D, NamedDecl *Shadow) {
      assert(LookupContext(D) == LookupContext(Shadow) &&
             "Decl and Shadow not in same context!");

      for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
        if (Shadow == *(I-1)) {
          Decls.insert(I-1, D);
          return;
        }
      }

      assert(0 && "Shadow wasn't in scope chain!");
    }

    /// RemoveDecl - Remove the decl from the scope chain.
    /// The decl must already be part of the decl chain.
    void RemoveDecl(NamedDecl *D) {
      for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
        if (D == *(I-1)) {
          Decls.erase(I-1);
          return;
        }
      }

      assert(0 && "Didn't find this decl on its identifier's chain!");
    }

  private:
    DeclsTy Decls;
  };

  /// SwizzledIterator - Can be instantiated either with a single NamedDecl*
  /// (the common case where only one decl is associated with an identifier) or
  /// with an 'Iter' iterator, when there are more than one decls to lookup.
  template<typename Iter>
  class SwizzledIterator {
    uintptr_t Ptr;

    SwizzledIterator() : Ptr(0) {}
    SwizzledIterator(NamedDecl *D) {
      Ptr = reinterpret_cast<uintptr_t>(D);
    }
    SwizzledIterator(Iter I) {
      Ptr = reinterpret_cast<uintptr_t>(I.getBase()) | 0x1;
    }

    bool isIterator() const { return (Ptr & 0x1); }

    Iter getIterator() const {
      assert(isIterator() && "Ptr not an iterator.");
      return reinterpret_cast<typename