Skip to content

Commit 60d6760

Browse files
committed
nr2.0: Separate out canonical path handling
This should improve our canonical path handling, without requiring further tweaks to ForeverStack. This may also help if, in the future, we have to move canonical path calculation to later compilation phases for proper handling of generics. gcc/rust/ChangeLog: * backend/rust-compile-base.cc (HIRCompileBase::compile_function): Since canonical paths returned from nr2.0 now include the crate name, avoid prepending the crate name again. * backend/rust-compile-implitem.cc (CompileTraitItem::visit): Use NameResolutionContext::to_canonical_path instead of ForeverStack::to_canonical_path. * backend/rust-compile-item.cc (CompileItem::visit): Likewise. * typecheck/rust-hir-type-check-enumitem.cc (TypeCheckEnumItem::visit): Likewise. * typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItem::visit): Likewise. * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Likewise. * typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): Likewise. * resolve/rust-default-resolver.cc (DefaultResolver::visit): Add Crate and EnumItem instance visitors, handle canonical path context scoping. * resolve/rust-default-resolver.h (DefaultResolver::visit): Add Crate and EnumItem instance visitors. * resolve/rust-early-name-resolver-2.0.cc (Early::go): Visit instances of Crate using the virtual member function visit. * resolve/rust-forever-stack.h (ForeverStack::to_canonical_path): Remove function declaration. * resolve/rust-forever-stack.hxx (ForeverStack::to_canonical_path): Remove function definition. * resolve/rust-late-name-resolver-2.0.cc (Late::go): Visit instances of Crate using the virtual member function visit. * resolve/rust-name-resolution-context.cc (CanonicalPathRecordRoot::as_path): New function definition. (CanonicalPathRecordNormal::as_path): Likewise. (CanonicalPathRecordLookup::as_path): Likewise. (CanonicalPathRecordImpl::as_path): Likewise. (CanonicalPathRecordTraitImpl::as_path): Likewise. (NameResolutionContext::NameResolutionContext): Initialize member variable canonical_ctx. * resolve/rust-name-resolution-context.h: Include "rust-item.h". (class NameResolutionContext): Forward declare class. (class CanonicalPathRecord): New class. (class CanonicalPathRecordWithParent): Likewise. (class CanonicalPathRecordRoot): Likewise. (class CanonicalPathRecordNormal): Likewise. (class CanonicalPathRecordLookup): Likewise. (class CanonicalPathRecordImpl): Likewise. (class CanonicalPathRecordTraitImpl): Likewise. (class CanonicalPathCtx): Likewise. (NameResolutionContext::canonical_ctx): New member variable. (NameResolutionContext::to_canonical_path): New member function. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::go): Visit instances of Crate with the virtual member function visit. (TopLevel::visit): Handle canonical path context scoping for external crates, use DefaultResolver::visit when visiting instances of StructStruct. * util/rust-canonical-path.h (CanonicalPath::new_seg): Take path parameter by-value, as a duplicate instance will be constructed regardless. gcc/testsuite/ChangeLog: * rust/compile/nr2/exclude: Remove canonical_paths1.rs.
1 parent d90770b commit 60d6760

18 files changed

+479
-152
lines changed

gcc/rust/backend/rust-compile-base.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,6 @@ HIRCompileBase::compile_function (
687687
}
688688
std::string asm_name = fn_name;
689689

690-
auto &mappings = Analysis::Mappings::get ();
691-
692-
if (flag_name_resolution_2_0)
693-
ir_symbol_name = mappings.get_current_crate_name () + "::" + ir_symbol_name;
694-
695690
unsigned int flags = 0;
696691
tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name,
697692
"" /* asm_name */, flags, locus);

gcc/rust/backend/rust-compile-implitem.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ CompileTraitItem::visit (HIR::TraitItemConst &constant)
3333
auto &nr_ctx
3434
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
3535

36-
canonical_path = nr_ctx.values.to_canonical_path (
37-
constant.get_mappings ().get_nodeid ());
36+
canonical_path
37+
= nr_ctx.to_canonical_path (constant.get_mappings ().get_nodeid ());
3838
}
3939
else
4040
{
@@ -103,7 +103,7 @@ CompileTraitItem::visit (HIR::TraitItemFunc &func)
103103
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
104104

105105
canonical_path
106-
= nr_ctx.values.to_canonical_path (func.get_mappings ().get_nodeid ());
106+
= nr_ctx.to_canonical_path (func.get_mappings ().get_nodeid ());
107107
}
108108
else
109109
{

gcc/rust/backend/rust-compile-item.cc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ CompileItem::visit (HIR::StaticItem &var)
5858
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
5959

6060
canonical_path
61-
= nr_ctx.values.to_canonical_path (var.get_mappings ().get_nodeid ());
61+
= nr_ctx.to_canonical_path (var.get_mappings ().get_nodeid ());
6262
}
6363
else
6464
{
@@ -124,8 +124,7 @@ CompileItem::visit (HIR::ConstantItem &constant)
124124
auto &nr_ctx
125125
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
126126

127-
canonical_path
128-
= nr_ctx.values.to_canonical_path (mappings.get_nodeid ()).value ();
127+
canonical_path = nr_ctx.to_canonical_path (mappings.get_nodeid ());
129128
}
130129
else
131130
{
@@ -218,10 +217,8 @@ CompileItem::visit (HIR::Function &function)
218217
auto &nr_ctx
219218
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
220219

221-
auto path = nr_ctx.values.to_canonical_path (
222-
function.get_mappings ().get_nodeid ());
223-
224-
canonical_path = path.value ();
220+
canonical_path
221+
= nr_ctx.to_canonical_path (function.get_mappings ().get_nodeid ());
225222
}
226223
else
227224
{

gcc/rust/resolve/rust-default-resolver.cc

Lines changed: 141 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424
namespace Rust {
2525
namespace Resolver2_0 {
2626

27+
void
28+
DefaultResolver::visit (AST::Crate &crate)
29+
{
30+
auto inner_fn = [this, &crate] () { AST::DefaultASTVisitor::visit (crate); };
31+
32+
auto &mappings = Analysis::Mappings::get ();
33+
34+
auto crate_num = mappings.lookup_crate_num (crate.get_node_id ());
35+
rust_assert (crate_num.has_value ());
36+
auto crate_name = mappings.get_crate_name (*crate_num);
37+
rust_assert (crate_name.has_value ());
38+
39+
ctx.canonical_ctx.scope (crate.get_node_id (), *crate_name, inner_fn);
40+
}
41+
2742
void
2843
DefaultResolver::visit (AST::BlockExpr &expr)
2944
{
@@ -38,19 +53,32 @@ DefaultResolver::visit (AST::BlockExpr &expr)
3853
void
3954
DefaultResolver::visit (AST::Module &module)
4055
{
41-
auto item_fn = [this, &module] () { AST::DefaultASTVisitor::visit (module); };
56+
auto item_fn_1
57+
= [this, &module] () { AST::DefaultASTVisitor::visit (module); };
4258

43-
ctx.scoped (Rib::Kind::Module, module.get_node_id (), item_fn,
59+
auto item_fn_2 = [this, &module, &item_fn_1] () {
60+
ctx.canonical_ctx.scope (module.get_node_id (), module.get_name (),
61+
std::move (item_fn_1));
62+
};
63+
64+
ctx.scoped (Rib::Kind::Module, module.get_node_id (), item_fn_2,
4465
module.get_name ());
4566
}
4667

4768
void
4869
DefaultResolver::visit (AST::Function &function)
4970
{
50-
auto def_fn
71+
auto def_fn_1
5172
= [this, &function] () { AST::DefaultASTVisitor::visit (function); };
5273

53-
ctx.scoped (Rib::Kind::Function, function.get_node_id (), def_fn);
74+
auto def_fn_2 = [this, &function, &def_fn_1] () {
75+
ctx.canonical_ctx.scope (function.get_node_id (),
76+
function.get_function_name (),
77+
std::move (def_fn_1));
78+
};
79+
80+
ctx.scoped (Rib::Kind::Function, function.get_node_id (), def_fn_2,
81+
function.get_function_name ());
5482
}
5583

5684
void
@@ -63,9 +91,15 @@ DefaultResolver::visit (AST::ForLoopExpr &expr)
6391
void
6492
DefaultResolver::visit (AST::Trait &trait)
6593
{
66-
auto inner_fn = [this, &trait] () { AST::DefaultASTVisitor::visit (trait); };
94+
auto inner_fn_1
95+
= [this, &trait] () { AST::DefaultASTVisitor::visit (trait); };
96+
97+
auto inner_fn_2 = [this, &trait, &inner_fn_1] () {
98+
ctx.canonical_ctx.scope (trait.get_node_id (), trait.get_identifier (),
99+
std::move (inner_fn_1));
100+
};
67101

68-
ctx.scoped (Rib::Kind::TraitOrImpl, trait.get_node_id (), inner_fn,
102+
ctx.scoped (Rib::Kind::TraitOrImpl, trait.get_node_id (), inner_fn_2,
69103
trait.get_identifier () /* FIXME: Is that valid?*/);
70104
}
71105

@@ -76,23 +110,27 @@ DefaultResolver::visit (AST::InherentImpl &impl)
76110
visit (impl.get_visibility ());
77111
visit_inner_attrs (impl);
78112

79-
auto inner_fn_inner = [this, &impl] () {
113+
auto inner_fn_1 = [this, &impl] () {
80114
for (auto &item : impl.get_impl_items ())
81115
visit (item);
82116
};
83117

84-
auto inner_fn_outer = [this, &impl, &inner_fn_inner] () {
118+
auto inner_fn_2 = [this, &impl, &inner_fn_1] () {
85119
maybe_insert_big_self (impl);
86120
for (auto &generic : impl.get_generic_params ())
87121
visit (generic);
88122
if (impl.has_where_clause ())
89123
visit (impl.get_where_clause ());
90124
visit (impl.get_type ());
91125

92-
ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn_inner);
126+
ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn_1);
93127
};
94128

95-
ctx.scoped (Rib::Kind::Generics, impl.get_node_id (), inner_fn_outer);
129+
auto inner_fn_3 = [this, &impl, &inner_fn_2] () {
130+
ctx.canonical_ctx.scope_impl (impl, std::move (inner_fn_2));
131+
};
132+
133+
ctx.scoped (Rib::Kind::Generics, impl.get_node_id (), inner_fn_3);
96134
}
97135

98136
void
@@ -102,12 +140,12 @@ DefaultResolver::visit (AST::TraitImpl &impl)
102140
visit (impl.get_visibility ());
103141
visit_inner_attrs (impl);
104142

105-
auto inner_fn_inner = [this, &impl] () {
143+
auto inner_fn_1 = [this, &impl] () {
106144
for (auto &item : impl.get_impl_items ())
107145
visit (item);
108146
};
109147

110-
auto inner_fn_outer = [this, &impl, &inner_fn_inner] () {
148+
auto inner_fn_2 = [this, &impl, &inner_fn_1] () {
111149
maybe_insert_big_self (impl);
112150
for (auto &generic : impl.get_generic_params ())
113151
visit (generic);
@@ -116,55 +154,120 @@ DefaultResolver::visit (AST::TraitImpl &impl)
116154
visit (impl.get_type ());
117155
visit (impl.get_trait_path ());
118156

119-
ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn_inner);
157+
ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn_1);
158+
};
159+
160+
auto inner_fn_3 = [this, &impl, &inner_fn_2] () {
161+
ctx.canonical_ctx.scope_impl (impl, std::move (inner_fn_2));
120162
};
121163

122-
ctx.scoped (Rib::Kind::Generics, impl.get_node_id (), inner_fn_outer);
164+
ctx.scoped (Rib::Kind::Generics, impl.get_node_id (), inner_fn_3);
123165
}
124166

125167
void
126168
DefaultResolver::visit (AST::StructStruct &type)
127169
{
128-
auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
170+
auto inner_fn_1 = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
171+
172+
auto inner_fn_2 = [this, &type, &inner_fn_1] () {
173+
ctx.canonical_ctx.scope (type.get_node_id (), type.get_struct_name (),
174+
std::move (inner_fn_1));
175+
};
129176

130177
ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
131-
inner_fn, type.get_struct_name ());
178+
inner_fn_2, type.get_struct_name ());
132179
}
133180

134181
void
135182
DefaultResolver::visit (AST::TupleStruct &type)
136183
{
137-
auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
184+
auto inner_fn_1 = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
185+
186+
auto inner_fn_2 = [this, &type, &inner_fn_1] () {
187+
ctx.canonical_ctx.scope (type.get_node_id (), type.get_struct_name (),
188+
std::move (inner_fn_1));
189+
};
138190

139191
ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
140-
inner_fn, type.get_struct_name ());
192+
inner_fn_2, type.get_struct_name ());
193+
}
194+
195+
void
196+
DefaultResolver::visit (AST::EnumItem &item)
197+
{
198+
auto inner_fn = [this, &item] () { AST::DefaultASTVisitor::visit (item); };
199+
200+
ctx.canonical_ctx.scope (item.get_node_id (), item.get_identifier (),
201+
inner_fn);
202+
}
203+
204+
void
205+
DefaultResolver::visit (AST::EnumItemTuple &item)
206+
{
207+
auto inner_fn = [this, &item] () { AST::DefaultASTVisitor::visit (item); };
208+
209+
ctx.canonical_ctx.scope (item.get_node_id (), item.get_identifier (),
210+
inner_fn);
211+
}
212+
213+
void
214+
DefaultResolver::visit (AST::EnumItemStruct &item)
215+
{
216+
auto inner_fn = [this, &item] () { AST::DefaultASTVisitor::visit (item); };
217+
218+
ctx.canonical_ctx.scope (item.get_node_id (), item.get_identifier (),
219+
inner_fn);
220+
}
221+
222+
void
223+
DefaultResolver::visit (AST::EnumItemDiscriminant &item)
224+
{
225+
auto inner_fn = [this, &item] () { AST::DefaultASTVisitor::visit (item); };
226+
227+
ctx.canonical_ctx.scope (item.get_node_id (), item.get_identifier (),
228+
inner_fn);
141229
}
142230

143231
void
144232
DefaultResolver::visit (AST::Enum &type)
145233
{
146-
auto variant_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
234+
auto inner_fn_1 = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
235+
236+
auto inner_fn_2 = [this, &type, &inner_fn_1] () {
237+
ctx.canonical_ctx.scope (type.get_node_id (), type.get_identifier (),
238+
std::move (inner_fn_1));
239+
};
147240

148241
ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
149-
variant_fn, type.get_identifier ());
242+
inner_fn_2, type.get_identifier ());
150243
}
151244

152245
void
153246
DefaultResolver::visit (AST::Union &type)
154247
{
155-
auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
248+
auto inner_fn_1 = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
249+
250+
auto inner_fn_2 = [this, &type, &inner_fn_1] () {
251+
ctx.canonical_ctx.scope (type.get_node_id (), type.get_identifier (),
252+
std::move (inner_fn_1));
253+
};
156254

157255
ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
158-
inner_fn, type.get_identifier ());
256+
inner_fn_2, type.get_identifier ());
159257
}
160258

161259
void
162260
DefaultResolver::visit (AST::TypeAlias &type)
163261
{
164-
auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
262+
auto inner_fn_1 = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
263+
264+
auto inner_fn_2 = [this, &type, &inner_fn_1] () {
265+
ctx.canonical_ctx.scope (type.get_node_id (), type.get_new_type_name (),
266+
std::move (inner_fn_1));
267+
};
165268

166269
ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
167-
inner_fn, type.get_new_type_name ());
270+
inner_fn_2, type.get_new_type_name ());
168271
}
169272

170273
void
@@ -199,21 +302,31 @@ DefaultResolver::visit (AST::ConstantItem &item)
199302
{
200303
if (item.has_expr ())
201304
{
202-
auto expr_vis
305+
auto expr_vis_1
203306
= [this, &item] () { AST::DefaultASTVisitor::visit (item); };
204307

308+
auto expr_vis_2 = [this, &item, &expr_vis_1] () {
309+
ctx.canonical_ctx.scope (item.get_node_id (), item.get_identifier (),
310+
std::move (expr_vis_1));
311+
};
312+
205313
// FIXME: Why do we need a Rib here?
206-
ctx.scoped (Rib::Kind::ConstantItem, item.get_node_id (), expr_vis);
314+
ctx.scoped (Rib::Kind::ConstantItem, item.get_node_id (), expr_vis_2);
207315
}
208316
}
209317

210318
void
211319
DefaultResolver::visit (AST::StaticItem &item)
212320
{
213-
auto expr_vis = [this, &item] () { AST::DefaultASTVisitor::visit (item); };
321+
auto expr_vis_1 = [this, &item] () { AST::DefaultASTVisitor::visit (item); };
322+
323+
auto expr_vis_2 = [this, &item, &expr_vis_1] () {
324+
ctx.canonical_ctx.scope (item.get_node_id (), item.get_identifier (),
325+
std::move (expr_vis_1));
326+
};
214327

215328
// FIXME: Why do we need a Rib here?
216-
ctx.scoped (Rib::Kind::ConstantItem, item.get_node_id (), expr_vis);
329+
ctx.scoped (Rib::Kind::ConstantItem, item.get_node_id (), expr_vis_2);
217330
}
218331

219332
void

gcc/rust/resolve/rust-default-resolver.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class DefaultResolver : public AST::DefaultASTVisitor
3939

4040
virtual ~DefaultResolver () {}
4141

42+
void visit (AST::Crate &) override;
4243
// First, our lexical scope expressions - these visit their sub nodes, always
4344
// these nodes create new scopes and ribs - they are often used to declare new
4445
// variables, such as a for loop's iterator, or a function's arguments
@@ -57,6 +58,10 @@ class DefaultResolver : public AST::DefaultASTVisitor
5758
// type dec nodes, which visit their fields or variants by default
5859
void visit (AST::StructStruct &) override;
5960
void visit (AST::TupleStruct &) override;
61+
void visit (AST::EnumItem &) override;
62+
void visit (AST::EnumItemTuple &) override;
63+
void visit (AST::EnumItemStruct &) override;
64+
void visit (AST::EnumItemDiscriminant &) override;
6065
void visit (AST::Enum &) override;
6166
void visit (AST::Union &) override;
6267
void visit (AST::TypeAlias &) override;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ Early::go (AST::Crate &crate)
6262
// We now proceed with resolving macros, which can be nested in almost any
6363
// items
6464
textual_scope.push ();
65-
for (auto &item : crate.items)
66-
item->accept_vis (*this);
65+
66+
visit (crate);
67+
6768
textual_scope.pop ();
6869
}
6970

gcc/rust/resolve/rust-forever-stack.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,6 @@ template <Namespace N> class ForeverStack
676676
std::function<void (const S &, NodeId)> insert_segment_resolution,
677677
std::vector<Error> &collect_errors);
678678

679-
// FIXME: Documentation
680-
tl::optional<Resolver::CanonicalPath> to_canonical_path (NodeId id) const;
681-
682679
// FIXME: Documentation
683680
tl::optional<Rib &> to_rib (NodeId rib_id);
684681
tl::optional<const Rib &> to_rib (NodeId rib_id) const;

0 commit comments

Comments
 (0)