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 : #include "rust-name-resolver.h"
20 : #include "rust-ast-full.h"
21 :
22 : namespace Rust {
23 : namespace Resolver {
24 :
25 0 : Rib::Rib (CrateNum crateNum, NodeId node_id)
26 0 : : crate_num (crateNum), node_id (node_id)
27 0 : {}
28 :
29 : void
30 0 : Rib::insert_name (
31 : const CanonicalPath &path, NodeId id, location_t locus, bool shadow,
32 : ItemType type,
33 : std::function<void (const CanonicalPath &, NodeId, location_t)> dup_cb)
34 : {
35 0 : auto it = path_mappings.find (path);
36 0 : bool path_already_exists = it != path_mappings.end ();
37 0 : if (path_already_exists && !shadow)
38 : {
39 0 : const auto &decl = decls_within_rib.find (it->second);
40 0 : if (decl != decls_within_rib.end ())
41 0 : dup_cb (path, it->second, decl->second);
42 : else
43 0 : dup_cb (path, it->second, locus);
44 :
45 : return;
46 : }
47 :
48 0 : path_mappings[path] = id;
49 0 : reverse_path_mappings.insert ({id, path});
50 0 : decls_within_rib.insert ({id, locus});
51 0 : references[id] = {};
52 0 : decl_type_mappings.insert ({id, type});
53 : }
54 :
55 : bool
56 0 : Rib::lookup_name (const CanonicalPath &ident, NodeId *id)
57 : {
58 0 : auto it = path_mappings.find (ident);
59 0 : if (it == path_mappings.end ())
60 : return false;
61 :
62 0 : *id = it->second;
63 0 : return true;
64 : }
65 :
66 : void
67 0 : Rib::clear_name (const CanonicalPath &ident, NodeId id)
68 : {
69 0 : auto ii = path_mappings.find (ident);
70 0 : if (ii != path_mappings.end ())
71 0 : path_mappings.erase (ii);
72 :
73 0 : auto ij = reverse_path_mappings.find (id);
74 0 : if (ij != reverse_path_mappings.end ())
75 0 : reverse_path_mappings.erase (ij);
76 :
77 0 : auto ik = decls_within_rib.find (id);
78 0 : if (ik != decls_within_rib.end ())
79 0 : decls_within_rib.erase (ik);
80 0 : }
81 :
82 : void
83 0 : Rib::append_reference_for_def (NodeId def, NodeId ref)
84 : {
85 0 : references[def].insert (ref);
86 0 : }
87 :
88 : bool
89 0 : Rib::have_references_for_node (NodeId def) const
90 : {
91 0 : auto it = references.find (def);
92 0 : if (it == references.end ())
93 : return false;
94 :
95 0 : return !it->second.empty ();
96 : }
97 :
98 : bool
99 0 : Rib::decl_was_declared_here (NodeId def) const
100 : {
101 0 : for (auto &it : decls_within_rib)
102 : {
103 0 : if (it.first == def)
104 0 : return true;
105 : }
106 : return false;
107 : }
108 :
109 : bool
110 0 : Rib::lookup_decl_type (NodeId def, ItemType *type) const
111 : {
112 0 : auto it = decl_type_mappings.find (def);
113 0 : if (it == decl_type_mappings.end ())
114 : return false;
115 :
116 0 : *type = it->second;
117 0 : return true;
118 : }
119 :
120 : void
121 0 : Rib::debug () const
122 : {
123 0 : fprintf (stderr, "%s\n", debug_str ().c_str ());
124 0 : }
125 :
126 : std::string
127 0 : Rib::debug_str () const
128 : {
129 0 : std::string buffer;
130 0 : for (const auto &it : path_mappings)
131 : {
132 0 : buffer += it.first.get () + "=" + std::to_string (it.second);
133 0 : buffer += ",";
134 : }
135 0 : return "{" + buffer + "}";
136 0 : }
137 :
138 5050 : Resolver::Resolver () {}
139 :
140 : Resolver *
141 13942 : Resolver::get ()
142 : {
143 13942 : static Resolver *instance;
144 13942 : if (instance == nullptr)
145 5050 : instance = new Resolver ();
146 :
147 13942 : return instance;
148 : }
149 :
150 : void
151 0 : Resolver::insert_resolved_name (NodeId refId, NodeId defId)
152 : {
153 0 : rust_unreachable ();
154 : }
155 :
156 : bool
157 0 : Resolver::lookup_resolved_name (NodeId refId, NodeId *defId)
158 : {
159 0 : rust_unreachable ();
160 : }
161 :
162 : void
163 0 : Resolver::insert_resolved_type (NodeId refId, NodeId defId)
164 : {
165 0 : rust_unreachable ();
166 : }
167 :
168 : bool
169 0 : Resolver::lookup_resolved_type (NodeId refId, NodeId *defId)
170 : {
171 0 : rust_unreachable ();
172 : }
173 :
174 : void
175 0 : Resolver::insert_resolved_misc (NodeId refId, NodeId defId)
176 : {
177 0 : rust_unreachable ();
178 : }
179 :
180 : bool
181 0 : Resolver::lookup_resolved_misc (NodeId refId, NodeId *defId)
182 : {
183 0 : rust_unreachable ();
184 : }
185 :
186 : } // namespace Resolver
187 : } // namespace Rust
|