Debian Codegraph @

gcc/rust/resolve/rust-early-name-resolver-2.0.h

raw
1 // Copyright (C) 2020-2025 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_EARLY_NAME_RESOLVER_2_0_H
20 #define RUST_EARLY_NAME_RESOLVER_2_0_H
21
22 #include "optional.h"
23 #include "rust-ast.h"
24 #include "rust-ast-visitor.h"
25 #include "rust-name-resolution-context.h"
26 #include "rust-default-resolver.h"
27 #include "rust-rib.h"
28 #include "rust-toplevel-name-resolver-2.0.h"
29
30 namespace Rust {
31 namespace Resolver2_0 {
32
33 class Early : public DefaultResolver
34 {
35 using DefaultResolver::visit;
36
37 TopLevel toplevel;
38 bool dirty;
39
40 public:
41 Early (NameResolutionContext &ctx);
42
43 bool is_dirty () { return dirty; }
44
45 void go (AST::Crate &crate);
46
47 const std::vector<Error> &get_macro_resolve_errors () const
48 {
49 return macro_resolve_errors;
50 }
51
52 // we need to handle definitions for textual scoping
53 void visit (AST::MacroRulesDefinition &) override;
54
55 // as well as lexical scopes
56 void visit (AST::BlockExpr &) override;
57 void visit (AST::Module &) override;
58
59 void visit (AST::MacroInvocation &) override;
60
61 void visit (AST::Function &) override;
62 void visit (AST::StructStruct &) override;
63 void visit (AST::UseDeclaration &) override;
64
65 struct ImportData
66 {
67 enum class Kind
68 {
69 Simple,
70 Glob,
71 Rebind
72 } kind;
73
74 static ImportData
75 Simple (std::vector<std::pair<Rib::Definition, Namespace>> &&definitions)
76 {
77 return ImportData (Kind::Simple, std::move (definitions));
78 }
79
80 static ImportData
81 Rebind (std::vector<std::pair<Rib::Definition, Namespace>> &&definitions)
82 {
83 return ImportData (Kind::Rebind, std::move (definitions));
84 }
85
86 static ImportData Glob (Rib::Definition module)
87 {
88 return ImportData (Kind::Glob, module);
89 }
90
91 Rib::Definition module () const
92 {
93 rust_assert (kind == Kind::Glob);
94 return glob_module;
95 }
96
97 std::vector<std::pair<Rib::Definition, Namespace>> definitions () const
98 {
99 rust_assert (kind != Kind::Glob);
100 return std::move (resolved_definitions);
101 }
102
103 private:
104 ImportData (
105 Kind kind,
106 std::vector<std::pair<Rib::Definition, Namespace>> &&definitions)
107 : kind (kind), resolved_definitions (std::move (definitions))
108 {}
109
110 ImportData (Kind kind, Rib::Definition module)
111 : kind (kind), glob_module (module)
112 {}
113
114 // TODO: Should this be a union?
115
116 // For Simple and Rebind
117 std::vector<std::pair<Rib::Definition, Namespace>> resolved_definitions;
118
119 // For Glob
120 Rib::Definition glob_module;
121 };
122
123 struct ImportPair
124 {
125 TopLevel::ImportKind import_kind;
126 ImportData data;
127
128 explicit ImportPair (TopLevel::ImportKind &&kind, ImportData &&data)
129 : import_kind (std::move (kind)), data (std::move (data))
130 {}
131 };
132
133 class ImportMappings
134 {
135 public:
136 std::vector<ImportPair> &new_or_access (NodeId path_id)
137 {
138 // We insert an empty vector, unless an element was already present for
139 // `use_dec_id` - which is returned in the tuple's first member
140 auto iter = mappings.insert ({{path_id}, {}});
141
142 // We then get that tuple's first member, which will be an iterator to the
143 // existing vec<pair<ImportKind, ImportData>> OR an iterator to our newly
144 // created empty vector (plus its key since this is a hashmap iterator).
145 // we then access the second member of the pair to get access to the
146 // vector directly.
147 return iter.first->second;
148 }
149
150 void insert (NodeId path_id, std::vector<ImportPair> &&pairs)
151 {
152 mappings.insert ({{path_id}, std::move (pairs)});
153 }
154
155 // Same as `insert`, but with just one node
156 void insert (NodeId path_id, ImportPair &&pair)
157 {
158 mappings.insert ({{path_id}, {pair}});
159 }
160
161 std::vector<ImportPair> &get (NodeId use_id) { return mappings[use_id]; }
162
163 private:
164 // Each path can import in multiple namespaces, hence the mapping from one
165 // path to a vector of import pairs
166 std::unordered_map<NodeId, std::vector<ImportPair>> mappings;
167 };
168
169 private:
170 void visit_attributes (std::vector<AST::Attribute> &attrs);
171
172 /**
173 * Insert a resolved macro invocation into the mappings once, meaning that we
174 * can call this function each time the early name resolution pass is underway
175 * and it will not trigger assertions for already resolved invocations.
176 */
177 // TODO: Rename
178 void insert_once (AST::MacroInvocation &invocation, NodeId resolved);
179 // TODO: Rename
180 void insert_once (AST::MacroRulesDefinition &definition);
181
182 /**
183 * Macros can either be resolved through textual scoping or regular path
184 * scoping - which this class represents. Textual scoping works similarly to a
185 * "simple" name resolution algorith, with the addition of "shadowing". Each
186 * time a new lexical scope is entered, we push a new map onto the stack, in
187 * which newly defined macros are added. The latest defined macro is the one
188 * that takes precedence. When resolving a macro invocation to its definition,
189 * we walk up the stack and look for a definition in each of the map until we
190 * find one. Otherwise, the macro invocation is unresolved, and goes through
191 * regular path resolution.
192 */
193 class TextualScope
194 {
195 public:
196 void push ();
197 void pop ();
198
199 void insert (std::string name, NodeId id);
200 tl::optional<NodeId> get (const std::string &name);
201
202 private:
203 std::vector<std::unordered_map<std::string, NodeId>> scopes;
204 };
205
206 // Mappings between an import and the definition it imports
207 ImportMappings import_mappings;
208
209 // FIXME: Documentation
210 // Call this on all the paths of a UseDec - so each flattened path in a
211 // UseTreeList for example
212 // FIXME: Should that return `found`?
213 bool resolve_simple_import (NodeId use_dec_id, TopLevel::ImportKind &&import);
214 bool resolve_glob_import (NodeId use_dec_id, TopLevel::ImportKind &&import);
215 bool resolve_rebind_import (NodeId use_dec_id, TopLevel::ImportKind &&import);
216
217 template <typename P>
218 std::vector<std::pair<Rib::Definition, Namespace>>
219 resolve_path_in_all_ns (const P &path)
220 {
221 const auto &segments = path.get_segments ();
222 std::vector<std::pair<Rib::Definition, Namespace>> resolved;
223
224 // Pair a definition with the namespace it was found in
225 auto pair_with_ns = [&] (Namespace ns) {
226 return [&, ns] (Rib::Definition def) {
227 auto pair = std::make_pair (def, ns);
228 return resolved.emplace_back (std::move (pair));
229 };
230 };
231
232 ctx.resolve_path (segments, Namespace::Values)
233 .map (pair_with_ns (Namespace::Values));
234 ctx.resolve_path (segments, Namespace::Types)
235 .map (pair_with_ns (Namespace::Types));
236 ctx.resolve_path (segments, Namespace::Macros)
237 .map (pair_with_ns (Namespace::Macros));
238
239 return resolved;
240 }
241
242 // Handle an import, resolving it to its definition and adding it to the list
243 // of import mappings
244 void build_import_mapping (
245 std::pair<NodeId, std::vector<TopLevel::ImportKind>> &&use_import);
246
247 TextualScope textual_scope;
248 std::vector<Error> macro_resolve_errors;
249
250 void collect_error (Error e) { macro_resolve_errors.push_back (e); }
251
252 void finalize_simple_import (const Early::ImportPair &mapping);
253
254 void finalize_glob_import (NameResolutionContext &ctx,
255 const Early::ImportPair &mapping);
256
257 void finalize_rebind_import (const Early::ImportPair &mapping);
258 };
259
260 } // namespace Resolver2_0
261 } // namespace Rust
262
263 #endif // ! RUST_EARLY_NAME_RESOLVER_2_0_H