Debian Codegraph @

gcc/rust/resolve/rust-toplevel-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_TOPLEVEL_NAME_RESOLVER_2_0_H
20 #define RUST_TOPLEVEL_NAME_RESOLVER_2_0_H
21
22 #include "optional.h"
23 #include "rust-ast-visitor.h"
24 #include "rust-ast.h"
25 #include "rust-item.h"
26 #include "rust-name-resolution-context.h"
27 #include "rust-default-resolver.h"
28
29 namespace Rust {
30 namespace Resolver2_0 {
31
32 /**
33 * The `TopLevel` visitor takes care of collecting all the definitions in a
34 * crate, and inserting them into the proper namespaces. These definitions can
35 * then be accessed by subsequent resolvers, such as `Early` or `Late`.
36 */
37 class TopLevel : public DefaultResolver
38 {
39 using DefaultResolver::visit;
40
41 public:
42 TopLevel (NameResolutionContext &resolver);
43
44 void go (AST::Crate &crate);
45
46 bool is_dirty () { return dirty; }
47
48 // Each import will be transformed into an instance of `ImportKind`, a class
49 // representing some of the data we need to resolve in the
50 // `EarlyNameResolver`. Basically, for each `UseTree` that we see in
51 // `TopLevel`, create one of these. `TopLevel` should build a list of these
52 // `ImportKind`s, which `Early` can then resolve to their proper definitions.
53 // Then, a final pass will insert the definitions into the `ForeverStack` -
54 // `FinalizeImports`.
55 //
56 // Using this struct should be very simple - each path within a `UseTree`
57 // becomes one `ImportKind`. The complex case is glob imports, in which case
58 // one glob import will become one `ImportKind` which will later become
59 // multiple definitions thanks to the `GlobbingVisitor`.
60 struct ImportKind
61 {
62 enum class Kind
63 {
64 Glob,
65 Simple,
66 Rebind,
67 } kind;
68
69 static ImportKind Glob (AST::SimplePath &&to_resolve, Rib &values_rib,
70 Rib &types_rib, Rib &macros_rib)
71 {
72 return ImportKind (Kind::Glob, std::move (to_resolve), values_rib,
73 types_rib, macros_rib);
74 }
75
76 static ImportKind Simple (AST::SimplePath &&to_resolve, Rib &values_rib,
77 Rib &types_rib, Rib &macros_rib)
78 {
79 return ImportKind (Kind::Simple, std::move (to_resolve), values_rib,
80 types_rib, macros_rib);
81 }
82
83 static ImportKind Rebind (AST::SimplePath &&to_resolve,
84 AST::UseTreeRebind &&rebind, Rib &values_rib,
85 Rib &types_rib, Rib &macros_rib)
86 {
87 return ImportKind (Kind::Rebind, std::move (to_resolve), values_rib,
88 types_rib, macros_rib, std::move (rebind));
89 }
90
91 // The path for `Early` to resolve.
92 AST::SimplePath to_resolve;
93
94 // The path to rebind an import to - only present if kind is Kind::Rebind
95 tl::optional<AST::UseTreeRebind> rebind;
96
97 Rib &values_rib;
98 Rib &types_rib;
99 Rib &macros_rib;
100
101 private:
102 ImportKind (Kind kind, AST::SimplePath &&to_resolve, Rib &values_rib,
103 Rib &types_rib, Rib &macros_rib,
104 tl::optional<AST::UseTreeRebind> &&rebind = tl::nullopt)
105 : kind (kind), to_resolve (std::move (to_resolve)),
106 rebind (std::move (rebind)), values_rib (values_rib),
107 types_rib (types_rib), macros_rib (macros_rib)
108 {}
109 };
110
111 std::unordered_map<NodeId, std::vector<ImportKind>> &get_imports_to_resolve ()
112 {
113 return imports_to_resolve;
114 }
115
116 void check_multiple_insertion_error (
117 tl::expected<NodeId, DuplicateNameError> result,
118 const Identifier &identifier, const location_t &locus,
119 const NodeId node_id);
120
121 /**
122 * Insert a new definition or error out if a definition with the same name
123 * was already present in the same namespace in the same scope.
124 *
125 * @param identifier The identifier of the definition to add.
126 * @param node A reference to the node, so we can get its `NodeId` and
127 * location.
128 * @param ns The namespace in which to add the definition.
129 */
130 template <typename T>
131 void insert_or_error_out (const Identifier &identifier, const T &node,
132 Namespace ns);
133 void insert_or_error_out (const Identifier &identifier,
134 const location_t &locus, const NodeId &id,
135 Namespace ns);
136
137 template <typename T>
138 void insert_enum_variant_or_error_out (const Identifier &identifier,
139 const T &node);
140
141 void insert_enum_variant_or_error_out (const Identifier &identifier,
142 const location_t &locus,
143 const NodeId node_id);
144
145 private:
146 // If a new export has been defined whilst visiting the visitor is considered
147 // dirty
148 bool dirty;
149
150 // FIXME: Do we move these to our mappings?
151 std::unordered_map<NodeId, location_t> node_locations;
152
153 // Store node forwarding for use declaration, the link between a
154 // definition and its new local name.
155 std::unordered_map<NodeId, NodeId> node_forwarding;
156
157 // One of the outputs of the `TopLevel` visitor - the list of imports that
158 // `Early` should take care of resolving
159 std::unordered_map<NodeId, std::vector<ImportKind>> imports_to_resolve;
160
161 void visit (AST::Module &module) override;
162 void visit (AST::Trait &trait) override;
163 void visit (AST::InherentImpl &impl) override;
164 void visit (AST::TraitImpl &impl) override;
165 void visit (AST::TraitItemType &trait_item) override;
166 void visit (AST::MacroRulesDefinition &macro) override;
167 void visit (AST::Function &function) override;
168 void visit (AST::StaticItem &static_item) override;
169 void visit (AST::ExternalStaticItem &static_item) override;
170 void visit (AST::StructStruct &struct_item) override;
171 void visit (AST::TupleStruct &tuple_struct) override;
172 void visit (AST::EnumItem &variant) override;
173 void visit (AST::EnumItemTuple &variant) override;
174 void visit (AST::EnumItemStruct &variant) override;
175 void visit (AST::EnumItemDiscriminant &variant) override;
176 void visit (AST::Enum &enum_item) override;
177 void visit (AST::Union &union_item) override;
178 void visit (AST::ConstantItem &const_item) override;
179 void visit (AST::TypeAlias &type_item) override;
180 void visit (AST::ExternCrate &crate) override;
181 void visit (AST::TypeParam &type_param) override;
182 void visit (AST::ConstGenericParam &const_param) override;
183
184 void visit (AST::UseDeclaration &use) override;
185 };
186
187 } // namespace Resolver2_0
188 } // namespace Rust
189
190 // For storing Imports as keys in maps
191 namespace std {
192 template <> struct less<Rust::Resolver2_0::TopLevel::ImportKind>
193 {
194 bool operator() (const Rust::Resolver2_0::TopLevel::ImportKind &lhs,
195 const Rust::Resolver2_0::TopLevel::ImportKind &rhs) const
196 {
197 return lhs.to_resolve.as_string () < rhs.to_resolve.as_string ()
198 && lhs.kind < rhs.kind;
199 }
200 };
201 } // namespace std
202
203 #endif // !RUST_TOPLEVEL_NAME_RESOLVER_2_0_H