Skip to content

Commit 3001bb6

Browse files
committed
resolver: Refactor Rib class in a source file
1 parent a936265 commit 3001bb6

File tree

4 files changed

+144
-132
lines changed

4 files changed

+144
-132
lines changed

gcc/rust/Make-lang.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ GRS_OBJS = \
8585
rust/rust-ast-lower.o \
8686
rust/rust-ast-lower-base.o \
8787
rust/rust-ast-lower-pattern.o \
88+
rust/rust-name-resolver.o \
8889
rust/rust-ast-resolve.o \
8990
rust/rust-ast-resolve-pattern.o \
9091
rust/rust-ast-resolve-expr.o \

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -306,33 +306,33 @@ HIRCompileBase::compile_locals_for_block (Context *ctx, Resolver::Rib &rib,
306306
tree fndecl)
307307
{
308308
std::vector<Bvariable *> locals;
309-
rib.iterate_decls ([&] (NodeId n, Location) mutable -> bool {
310-
Resolver::Definition d;
311-
bool ok = ctx->get_resolver ()->lookup_definition (n, &d);
312-
rust_assert (ok);
313-
314-
HIR::Stmt *decl = nullptr;
315-
ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl);
316-
rust_assert (ok);
317-
318-
// if its a function we extract this out side of this fn context
319-
// and it is not a local to this function
320-
bool is_item = ctx->get_mappings ()->lookup_hir_item (
321-
decl->get_mappings ().get_crate_num (),
322-
decl->get_mappings ().get_hirid ())
323-
!= nullptr;
324-
if (is_item)
325-
{
326-
HIR::Item *item = static_cast<HIR::Item *> (decl);
327-
CompileItem::compile (item, ctx);
328-
return true;
329-
}
330-
331-
Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx);
332-
locals.push_back (compiled);
309+
for (auto it : rib.get_declarations ())
310+
{
311+
auto node_id = it.first;
312+
313+
Resolver::Definition d;
314+
bool ok = ctx->get_resolver ()->lookup_definition (node_id, &d);
315+
rust_assert (ok);
316+
317+
HIR::Stmt *decl = nullptr;
318+
ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl);
319+
rust_assert (ok);
320+
321+
// if its a function we extract this out side of this fn context
322+
// and it is not a local to this function
323+
bool is_item = ctx->get_mappings ()->lookup_hir_item (
324+
decl->get_mappings ().get_crate_num (),
325+
decl->get_mappings ().get_hirid ())
326+
!= nullptr;
327+
if (is_item)
328+
{
329+
HIR::Item *item = static_cast<HIR::Item *> (decl);
330+
CompileItem::compile (item, ctx);
331+
}
333332

334-
return true;
335-
});
333+
Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx);
334+
locals.push_back (compiled);
335+
};
336336

337337
return locals;
338338
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (C) 2020-2022 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+
21+
namespace Rust {
22+
namespace Resolver {
23+
24+
Rib::Rib (CrateNum crateNum, NodeId node_id)
25+
: crate_num (crateNum), node_id (node_id),
26+
mappings (Analysis::Mappings::get ())
27+
{}
28+
29+
void
30+
Rib::insert_name (
31+
const CanonicalPath &path, NodeId id, Location locus, bool shadow,
32+
std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb)
33+
{
34+
auto it = path_mappings.find (path);
35+
bool path_already_exists = it != path_mappings.end ();
36+
if (path_already_exists && !shadow)
37+
{
38+
const auto &decl = decls_within_rib.find (it->second);
39+
if (decl != decls_within_rib.end ())
40+
dup_cb (path, it->second, decl->second);
41+
else
42+
dup_cb (path, it->second, locus);
43+
44+
return;
45+
}
46+
47+
path_mappings[path] = id;
48+
reverse_path_mappings.insert (std::pair<NodeId, CanonicalPath> (id, path));
49+
decls_within_rib.insert (std::pair<NodeId, Location> (id, locus));
50+
references[id] = {};
51+
}
52+
53+
bool
54+
Rib::lookup_name (const CanonicalPath &ident, NodeId *id)
55+
{
56+
auto it = path_mappings.find (ident);
57+
if (it == path_mappings.end ())
58+
return false;
59+
60+
*id = it->second;
61+
return true;
62+
}
63+
64+
void
65+
Rib::clear_name (const CanonicalPath &ident, NodeId id)
66+
{
67+
auto ii = path_mappings.find (ident);
68+
if (ii != path_mappings.end ())
69+
path_mappings.erase (ii);
70+
71+
auto ij = reverse_path_mappings.find (id);
72+
if (ij != reverse_path_mappings.end ())
73+
reverse_path_mappings.erase (ij);
74+
75+
auto ik = decls_within_rib.find (id);
76+
if (ik != decls_within_rib.end ())
77+
decls_within_rib.erase (ik);
78+
}
79+
80+
void
81+
Rib::append_reference_for_def (NodeId def, NodeId ref)
82+
{
83+
references[def].insert (ref);
84+
}
85+
86+
bool
87+
Rib::have_references_for_node (NodeId def) const
88+
{
89+
auto it = references.find (def);
90+
if (it == references.end ())
91+
return false;
92+
93+
return !it->second.empty ();
94+
}
95+
96+
bool
97+
Rib::decl_was_declared_here (NodeId def) const
98+
{
99+
for (auto &it : decls_within_rib)
100+
{
101+
if (it.first == def)
102+
return true;
103+
}
104+
return false;
105+
}
106+
107+
} // namespace Resolver
108+
} // namespace Rust

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

Lines changed: 9 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -32,120 +32,23 @@ class Rib
3232
public:
3333
// Rust uses local_def_ids assigned by def_collector on the AST
3434
// lets use NodeId instead
35-
Rib (CrateNum crateNum, NodeId node_id)
36-
: crate_num (crateNum), node_id (node_id),
37-
mappings (Analysis::Mappings::get ())
38-
{}
39-
40-
~Rib () {}
35+
Rib (CrateNum crateNum, NodeId node_id);
4136

4237
// this takes the relative paths of items within a compilation unit for lookup
4338
void insert_name (
4439
const CanonicalPath &path, NodeId id, Location locus, bool shadow,
45-
std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb)
46-
{
47-
auto it = path_mappings.find (path);
48-
bool path_already_exists = it != path_mappings.end ();
49-
if (path_already_exists && !shadow)
50-
{
51-
const auto &decl = decls_within_rib.find (it->second);
52-
if (decl != decls_within_rib.end ())
53-
dup_cb (path, it->second, decl->second);
54-
else
55-
dup_cb (path, it->second, locus);
56-
57-
return;
58-
}
59-
60-
path_mappings[path] = id;
61-
reverse_path_mappings.insert (std::pair<NodeId, CanonicalPath> (id, path));
62-
decls_within_rib.insert (std::pair<NodeId, Location> (id, locus));
63-
references[id] = {};
64-
}
65-
66-
bool lookup_name (const CanonicalPath &ident, NodeId *id)
67-
{
68-
auto it = path_mappings.find (ident);
69-
if (it == path_mappings.end ())
70-
return false;
71-
72-
*id = it->second;
73-
return true;
74-
}
40+
std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb);
7541

76-
bool lookup_canonical_path (const NodeId &id, CanonicalPath *ident)
77-
{
78-
auto it = reverse_path_mappings.find (id);
79-
if (it == reverse_path_mappings.end ())
80-
return false;
81-
82-
*ident = it->second;
83-
return true;
84-
}
85-
86-
void clear_name (const CanonicalPath &ident, NodeId id)
87-
{
88-
auto ii = path_mappings.find (ident);
89-
if (ii != path_mappings.end ())
90-
path_mappings.erase (ii);
91-
92-
auto ij = reverse_path_mappings.find (id);
93-
if (ij != reverse_path_mappings.end ())
94-
reverse_path_mappings.erase (ij);
95-
96-
auto ik = decls_within_rib.find (id);
97-
if (ik != decls_within_rib.end ())
98-
decls_within_rib.erase (ik);
99-
}
42+
bool lookup_canonical_path (const NodeId &id, CanonicalPath *ident);
43+
bool lookup_name (const CanonicalPath &ident, NodeId *id);
44+
void clear_name (const CanonicalPath &ident, NodeId id);
45+
void append_reference_for_def (NodeId def, NodeId ref);
46+
bool have_references_for_node (NodeId def) const;
47+
bool decl_was_declared_here (NodeId def) const;
10048

10149
CrateNum get_crate_num () const { return crate_num; }
10250
NodeId get_node_id () const { return node_id; }
103-
104-
void iterate_decls (std::function<bool (NodeId, Location)> cb)
105-
{
106-
for (auto it : decls_within_rib)
107-
{
108-
if (!cb (it.first, it.second))
109-
return;
110-
}
111-
}
112-
113-
void iterate_references_for_def (NodeId def, std::function<bool (NodeId)> cb)
114-
{
115-
auto it = references.find (def);
116-
if (it == references.end ())
117-
return;
118-
119-
for (auto ref : it->second)
120-
{
121-
if (!cb (ref))
122-
return;
123-
}
124-
}
125-
126-
void append_reference_for_def (NodeId def, NodeId ref)
127-
{
128-
references[def].insert (ref);
129-
}
130-
131-
bool have_references_for_node (NodeId def) const
132-
{
133-
auto it = references.find (def);
134-
if (it == references.end ())
135-
return false;
136-
137-
return !it->second.empty ();
138-
}
139-
140-
bool decl_was_declared_here (NodeId def) const
141-
{
142-
for (auto &it : decls_within_rib)
143-
{
144-
if (it.first == def)
145-
return true;
146-
}
147-
return false;
148-
}
51+
std::map<NodeId, Location> &get_declarations () { return decls_within_rib; }
14952

15053
private:
15154
CrateNum crate_num;

0 commit comments

Comments
 (0)