Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #ifndef RUST_NAME_RESOLVER_H
20 : #define RUST_NAME_RESOLVER_H
21 :
22 : #include "rust-system.h"
23 : #include "rust-canonical-path.h"
24 : #include "rust-hir-map.h"
25 : #include "rust-hir-type-check.h"
26 :
27 : namespace Rust {
28 : namespace Resolver {
29 :
30 : class Rib
31 : {
32 : public:
33 : enum ItemType
34 : {
35 : Var,
36 : Param,
37 : Function,
38 : Type,
39 : Module,
40 : Static,
41 : Const,
42 : Trait,
43 : Impl,
44 : TraitImpl,
45 : ExternCrate,
46 : MacroDecl,
47 : Label,
48 : Unknown
49 : };
50 :
51 : // FIXME
52 : // Rust uses local_def_ids assigned by def_collector on the AST. Consider
53 : // moving to a local-def-id
54 : Rib (CrateNum crateNum, NodeId node_id);
55 :
56 : // this takes the relative paths of items within a compilation unit for lookup
57 : void insert_name (
58 : const CanonicalPath &path, NodeId id, location_t locus, bool shadow,
59 : ItemType type,
60 : std::function<void (const CanonicalPath &, NodeId, location_t)> dup_cb);
61 :
62 : bool lookup_canonical_path (const NodeId &id, CanonicalPath *ident);
63 : bool lookup_name (const CanonicalPath &ident, NodeId *id);
64 : void clear_name (const CanonicalPath &ident, NodeId id);
65 : void append_reference_for_def (NodeId def, NodeId ref);
66 : bool have_references_for_node (NodeId def) const;
67 : bool decl_was_declared_here (NodeId def) const;
68 : bool lookup_decl_type (NodeId def, ItemType *type) const;
69 : void debug () const;
70 : std::string debug_str () const;
71 :
72 : CrateNum get_crate_num () const { return crate_num; }
73 : NodeId get_node_id () const { return node_id; }
74 : std::map<NodeId, location_t> &get_declarations () { return decls_within_rib; }
75 :
76 : private:
77 : CrateNum crate_num;
78 : NodeId node_id;
79 : std::map<CanonicalPath, NodeId> path_mappings;
80 : std::map<NodeId, CanonicalPath> reverse_path_mappings;
81 : std::map<NodeId, location_t> decls_within_rib;
82 : std::map<NodeId, std::set<NodeId>> references;
83 : std::map<NodeId, ItemType> decl_type_mappings;
84 : };
85 :
86 : class Scope
87 : {
88 : public:
89 : Scope (CrateNum crate_num);
90 :
91 : void insert (
92 : const CanonicalPath &ident, NodeId id, location_t locus, bool shadow,
93 : Rib::ItemType type,
94 : std::function<void (const CanonicalPath &, NodeId, location_t)> dup_cb);
95 :
96 : void insert (const CanonicalPath &ident, NodeId id, location_t locus,
97 : Rib::ItemType type = Rib::ItemType::Unknown);
98 : bool lookup (const CanonicalPath &ident, NodeId *id);
99 : bool lookup_decl_type (NodeId id, Rib::ItemType *type);
100 : bool lookup_rib_for_decl (NodeId id, const Rib **rib);
101 :
102 : void iterate (std::function<bool (Rib *)> cb);
103 : void iterate (std::function<bool (const Rib *)> cb) const;
104 :
105 : Rib *peek ();
106 : void push (NodeId id);
107 : Rib *pop ();
108 :
109 : bool decl_was_declared_here (NodeId def) const;
110 : void append_reference_for_def (NodeId refId, NodeId defId);
111 :
112 0 : CrateNum get_crate_num () const { return crate_num; }
113 :
114 : const std::vector<Rib *> &get_context () const { return stack; };
115 :
116 : private:
117 : CrateNum crate_num;
118 : std::vector<Rib *> stack;
119 : };
120 :
121 : class Resolver
122 : {
123 : public:
124 : static Resolver *get ();
125 : ~Resolver () {}
126 :
127 : void insert_resolved_name (NodeId refId, NodeId defId);
128 : bool lookup_resolved_name (NodeId refId, NodeId *defId);
129 :
130 : void insert_resolved_type (NodeId refId, NodeId defId);
131 : bool lookup_resolved_type (NodeId refId, NodeId *defId);
132 :
133 : void insert_resolved_misc (NodeId refId, NodeId defId);
134 : bool lookup_resolved_misc (NodeId refId, NodeId *defId);
135 :
136 : private:
137 : Resolver ();
138 : };
139 :
140 : } // namespace Resolver
141 : } // namespace Rust
142 :
143 : #endif // RUST_NAME_RESOLVER_H
|