clang 20.0.0git
IdentifierResolver.h
Go to the documentation of this file.
1//===- IdentifierResolver.h - Lexical Scope Name lookup ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the IdentifierResolver class, which is used for lexical
10// scoped lookup, based on declaration names.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_SEMA_IDENTIFIERRESOLVER_H
15#define LLVM_CLANG_SEMA_IDENTIFIERRESOLVER_H
16
17#include "clang/Basic/LLVM.h"
18#include "llvm/ADT/SmallVector.h"
19#include <cassert>
20#include <cstddef>
21#include <cstdint>
22#include <iterator>
23
24namespace clang {
25
26class Decl;
27class DeclarationName;
28class DeclContext;
29class IdentifierInfo;
30class LangOptions;
31class NamedDecl;
32class Preprocessor;
33class Scope;
34
35/// IdentifierResolver - Keeps track of shadowed decls on enclosing
36/// scopes. It manages the shadowing chains of declaration names and
37/// implements efficient decl lookup based on a declaration name.
39 /// IdDeclInfo - Keeps track of information about decls associated
40 /// to a particular declaration name. IdDeclInfos are lazily
41 /// constructed and assigned to a declaration name the first time a
42 /// decl with that declaration name is shadowed in some scope.
43 class IdDeclInfo {
44 public:
45 using DeclsTy = SmallVector<NamedDecl *, 2>;
46
47 DeclsTy::iterator decls_begin() { return Decls.begin(); }
48 DeclsTy::iterator decls_end() { return Decls.end(); }
49
50 void AddDecl(NamedDecl *D) { Decls.push_back(D); }
51
52 /// RemoveDecl - Remove the decl from the scope chain.
53 /// The decl must already be part of the decl chain.
54 void RemoveDecl(NamedDecl *D);
55
56 /// Insert the given declaration at the given position in the list.
57 void InsertDecl(DeclsTy::iterator Pos, NamedDecl *D) {
58 Decls.insert(Pos, D);
59 }
60
61 private:
62 DeclsTy Decls;
63 };
64
65public:
66 /// iterator - Iterate over the decls of a specified declaration name.
67 /// It will walk or not the parent declaration contexts depending on how
68 /// it was instantiated.
69 class iterator {
70 public:
71 friend class IdentifierResolver;
72
75 using pointer = NamedDecl *;
76 using iterator_category = std::input_iterator_tag;
77 using difference_type = std::ptrdiff_t;
78
79 /// Ptr - There are 2 forms that 'Ptr' represents:
80 /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
81 /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
82 /// same declaration context. (Ptr & 0x1 == 0x1)
84 using BaseIter = IdDeclInfo::DeclsTy::iterator;
85
86 /// A single NamedDecl. (Ptr & 0x1 == 0)
88 Ptr = reinterpret_cast<uintptr_t>(D);
89 assert((Ptr & 0x1) == 0 && "Invalid Ptr!");