Skip to content

Commit 34db31f

Browse files
committed
Move import mapping resolution to in tree visit
Import mapping was relying on resolve_path which in turn relies on the cursor function. This means the mapping resolver should be called from the correct scope instead of being called from the crate scope. gcc/rust/ChangeLog: * resolve/rust-early-name-resolver-2.0.cc (Early::Early): Move the top level visitor from the function scope to attributes. (Early::go): Remove top level visitor creation and adapt calling code. Remove call to mapping resolution and import finalization. (Early::finalize_simple_import): Move the finalization from it's visitor. (Early::finalize_glob_import): Likewise. (Early::finalize_rebind_import): Likewise. (Early::visit): Add mapping resolution and finalization in UseDeclaration visitor function. * resolve/rust-finalize-imports-2.0.cc (finalize_simple_import): Move function. (finalize_glob_import): Likewise. (finalize_rebind_import): Likewise. (FinalizeImports::visit): Remove call to finalizers. * resolve/rust-early-name-resolver-2.0.h (class Early): Add top level attribute. * resolve/rust-finalize-imports-2.0.h: Add function prototypes. * resolve/rust-toplevel-name-resolver-2.0.h: Change getter return type to reference. Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
1 parent 9d88bcc commit 34db31f

File tree

5 files changed

+105
-90
lines changed

5 files changed

+105
-90
lines changed

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

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
namespace Rust {
2828
namespace Resolver2_0 {
2929

30-
Early::Early (NameResolutionContext &ctx) : DefaultResolver (ctx), dirty (false)
30+
Early::Early (NameResolutionContext &ctx)
31+
: DefaultResolver (ctx), toplevel (TopLevel (ctx)), dirty (false)
3132
{}
3233

3334
void
@@ -52,16 +53,10 @@ void
5253
Early::go (AST::Crate &crate)
5354
{
5455
// First we go through TopLevel resolution to get all our declared items
55-
auto toplevel = TopLevel (ctx);
5656
toplevel.go (crate);
5757

5858
// We start with resolving the list of imports that `TopLevel` has built for
5959
// us
60-
for (auto &&import : toplevel.get_imports_to_resolve ())
61-
build_import_mapping (std::move (import));
62-
63-
// Once this is done, we finalize their resolution
64-
FinalizeImports (std::move (import_mappings), toplevel, ctx).go (crate);
6560

6661
dirty = toplevel.is_dirty ();
6762
// We now proceed with resolving macros, which can be nested in almost any
@@ -375,5 +370,94 @@ Early::visit (AST::StructStruct &s)
375370
DefaultResolver::visit (s);
376371
}
377372

373+
void
374+
Early::finalize_simple_import (const Early::ImportPair &mapping)
375+
{
376+
// FIXME: We probably need to store namespace information
377+
378+
auto locus = mapping.import_kind.to_resolve.get_locus ();
379+
auto data = mapping.data;
380+
auto identifier
381+
= mapping.import_kind.to_resolve.get_final_segment ().get_segment_name ();
382+
383+
for (auto &&definition : data.definitions ())
384+
toplevel
385+
.insert_or_error_out (
386+
identifier, locus, definition.first.get_node_id (), definition.second /* TODO: This isn't clear - it would be better if it was called .ns or something */);
387+
}
388+
389+
void
390+
Early::finalize_glob_import (NameResolutionContext &ctx,
391+
const Early::ImportPair &mapping)
392+
{
393+
auto module = Analysis::Mappings::get ().lookup_ast_module (
394+
mapping.data.module ().get_node_id ());
395+
rust_assert (module);
396+
397+
GlobbingVisitor glob_visitor (ctx);
398+
glob_visitor.go (module.value ());
399+
}
400+
401+
void
402+
Early::finalize_rebind_import (const Early::ImportPair &mapping)
403+
{
404+
// We can fetch the value here as `resolve_rebind` will only be called on
405+
// imports of the right kind
406+
auto &path = mapping.import_kind.to_resolve;
407+
auto &rebind = mapping.import_kind.rebind.value ();
408+
auto data = mapping.data;
409+
410+
location_t locus = UNKNOWN_LOCATION;
411+
std::string declared_name;
412+
413+
// FIXME: This needs to be done in `FinalizeImports`
414+
switch (rebind.get_new_bind_type ())
415+
{
416+
case AST::UseTreeRebind::NewBindType::IDENTIFIER:
417+
declared_name = rebind.get_identifier ().as_string ();
418+
locus = rebind.get_identifier ().get_locus ();
419+
break;
420+
case AST::UseTreeRebind::NewBindType::NONE:
421+
declared_name = path.get_final_segment ().as_string ();
422+
locus = path.get_final_segment ().get_locus ();
423+
break;
424+
case AST::UseTreeRebind::NewBindType::WILDCARD:
425+
rust_unreachable ();
426+
break;
427+
}
428+
429+
for (auto &&definition : data.definitions ())
430+
toplevel.insert_or_error_out (
431+
declared_name, locus, definition.first.get_node_id (), definition.second /* TODO: This isn't clear - it would be better if it was called .ns or something */);
432+
}
433+
434+
void
435+
Early::visit (AST::UseDeclaration &decl)
436+
{
437+
auto &imports = toplevel.get_imports_to_resolve ();
438+
auto current_import = imports.find (decl.get_node_id ());
439+
if (current_import != imports.end ())
440+
{
441+
build_import_mapping (*current_import);
442+
}
443+
444+
// Once this is done, we finalize their resolution
445+
for (const auto &mapping : import_mappings.get (decl.get_node_id ()))
446+
switch (mapping.import_kind.kind)
447+
{
448+
case TopLevel::ImportKind::Kind::Glob:
449+
finalize_glob_import (ctx, mapping);
450+
break;
451+
case TopLevel::ImportKind::Kind::Simple:
452+
finalize_simple_import (mapping);
453+
break;
454+
case TopLevel::ImportKind::Kind::Rebind:
455+
finalize_rebind_import (mapping);
456+
break;
457+
}
458+
459+
DefaultResolver::visit (decl);
460+
}
461+
378462
} // namespace Resolver2_0
379463
} // namespace Rust

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Early : public DefaultResolver
3434
{
3535
using DefaultResolver::visit;
3636

37+
TopLevel toplevel;
3738
bool dirty;
3839

3940
public:
@@ -59,6 +60,7 @@ class Early : public DefaultResolver
5960

6061
void visit (AST::Function &) override;
6162
void visit (AST::StructStruct &) override;
63+
void visit (AST::UseDeclaration &) override;
6264

6365
struct ImportData
6466
{
@@ -246,6 +248,13 @@ class Early : public DefaultResolver
246248
std::vector<Error> macro_resolve_errors;
247249

248250
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);
249258
};
250259

251260
} // namespace Resolver2_0

gcc/rust/resolve/rust-finalize-imports-2.0.cc

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -125,67 +125,6 @@ GlobbingVisitor::visit (AST::UseDeclaration &use)
125125
// Handle cycles ?
126126
}
127127

128-
void
129-
finalize_simple_import (TopLevel &toplevel, const Early::ImportPair &mapping)
130-
{
131-
// FIXME: We probably need to store namespace information
132-
133-
auto locus = mapping.import_kind.to_resolve.get_locus ();
134-
auto data = mapping.data;
135-
auto identifier
136-
= mapping.import_kind.to_resolve.get_final_segment ().get_segment_name ();
137-
138-
for (auto &&definition : data.definitions ())
139-
toplevel
140-
.insert_or_error_out (
141-
identifier, locus, definition.first.get_node_id (), definition.second /* TODO: This isn't clear - it would be better if it was called .ns or something */);
142-
}
143-
144-
void
145-
finalize_glob_import (NameResolutionContext &ctx,
146-
const Early::ImportPair &mapping)
147-
{
148-
auto module = Analysis::Mappings::get ().lookup_ast_module (
149-
mapping.data.module ().get_node_id ());
150-
rust_assert (module);
151-
152-
GlobbingVisitor glob_visitor (ctx);
153-
glob_visitor.go (module.value ());
154-
}
155-
156-
void
157-
finalize_rebind_import (TopLevel &toplevel, const Early::ImportPair &mapping)
158-
{
159-
// We can fetch the value here as `resolve_rebind` will only be called on
160-
// imports of the right kind
161-
auto &path = mapping.import_kind.to_resolve;
162-
auto &rebind = mapping.import_kind.rebind.value ();
163-
auto data = mapping.data;
164-
165-
location_t locus = UNKNOWN_LOCATION;
166-
std::string declared_name;
167-
168-
// FIXME: This needs to be done in `FinalizeImports`
169-
switch (rebind.get_new_bind_type ())
170-
{
171-
case AST::UseTreeRebind::NewBindType::IDENTIFIER:
172-
declared_name = rebind.get_identifier ().as_string ();
173-
locus = rebind.get_identifier ().get_locus ();
174-
break;
175-
case AST::UseTreeRebind::NewBindType::NONE:
176-
declared_name = path.get_final_segment ().as_string ();
177-
locus = path.get_final_segment ().get_locus ();
178-
break;
179-
case AST::UseTreeRebind::NewBindType::WILDCARD:
180-
rust_unreachable ();
181-
break;
182-
}
183-
184-
for (auto &&definition : data.definitions ())
185-
toplevel.insert_or_error_out (
186-
declared_name, locus, definition.first.get_node_id (), definition.second /* TODO: This isn't clear - it would be better if it was called .ns or something */);
187-
}
188-
189128
FinalizeImports::FinalizeImports (Early::ImportMappings &&data,
190129
TopLevel &toplevel,
191130
NameResolutionContext &ctx)
@@ -202,23 +141,7 @@ FinalizeImports::go (AST::Crate &crate)
202141

203142
void
204143
FinalizeImports::visit (AST::UseDeclaration &use)
205-
{
206-
auto import_mappings = data.get (use.get_node_id ());
207-
208-
for (const auto &mapping : import_mappings)
209-
switch (mapping.import_kind.kind)
210-
{
211-
case TopLevel::ImportKind::Kind::Glob:
212-
finalize_glob_import (ctx, mapping);
213-
break;
214-
case TopLevel::ImportKind::Kind::Simple:
215-
finalize_simple_import (toplevel, mapping);
216-
break;
217-
case TopLevel::ImportKind::Kind::Rebind:
218-
finalize_rebind_import (toplevel, mapping);
219-
break;
220-
}
221-
}
144+
{}
222145

223146
} // namespace Resolver2_0
224147
} // namespace Rust

gcc/rust/resolve/rust-finalize-imports-2.0.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ class FinalizeImports : DefaultResolver
9696

9797
void go (AST::Crate &crate);
9898

99+
void visit (AST::UseDeclaration &) override;
100+
99101
private:
100102
using AST::DefaultASTVisitor::visit;
101103

102-
void visit (AST::UseDeclaration &) override;
103-
104104
Early::ImportMappings data;
105105
TopLevel &toplevel;
106106
NameResolutionContext &ctx;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,9 @@ class TopLevel : public DefaultResolver
108108
{}
109109
};
110110

111-
std::unordered_map<NodeId, std::vector<ImportKind>> &&
112-
get_imports_to_resolve ()
111+
std::unordered_map<NodeId, std::vector<ImportKind>> &get_imports_to_resolve ()
113112
{
114-
return std::move (imports_to_resolve);
113+
return imports_to_resolve;
115114
}
116115

117116
void check_multiple_insertion_error (

0 commit comments

Comments
 (0)