Skip to content

Commit 23db789

Browse files
committed
resolver: Move Scope methods into source file
1 parent 8466c9b commit 23db789

File tree

2 files changed

+88
-58
lines changed

2 files changed

+88
-58
lines changed

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
_R.push_back (builtin_type); \
3535
tyctx->insert_builtin (_TY->get_ref (), builtin_type->get_node_id (), \
3636
_TY); \
37-
} while (0)
37+
} \
38+
while (0)
3839

3940
namespace Rust {
4041
namespace Resolver {
@@ -122,6 +123,83 @@ Rib::decl_was_declared_here (NodeId def) const
122123
return false;
123124
}
124125

126+
Scope::Scope (CrateNum crate_num) : crate_num (crate_num) {}
127+
128+
void
129+
Scope::insert (
130+
const CanonicalPath &ident, NodeId id, Location locus, bool shadow,
131+
std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb)
132+
{
133+
peek ()->insert_name (ident, id, locus, shadow, dup_cb);
134+
}
135+
136+
void
137+
Scope::insert (const CanonicalPath &ident, NodeId id, Location locus)
138+
{
139+
peek ()->insert_name (ident, id, locus, true,
140+
[] (const CanonicalPath &, NodeId, Location) -> void {
141+
});
142+
}
143+
144+
bool
145+
Scope::lookup (const CanonicalPath &ident, NodeId *id)
146+
{
147+
NodeId lookup = UNKNOWN_NODEID;
148+
iterate ([&] (Rib *r) mutable -> bool {
149+
if (r->lookup_name (ident, &lookup))
150+
return false;
151+
return true;
152+
});
153+
154+
*id = lookup;
155+
return lookup != UNKNOWN_NODEID;
156+
}
157+
158+
void
159+
Scope::iterate (std::function<bool (Rib *)> cb)
160+
{
161+
for (auto it = stack.rbegin (); it != stack.rend (); ++it)
162+
{
163+
if (!cb (*it))
164+
return;
165+
}
166+
}
167+
168+
Rib *
169+
Scope::peek ()
170+
{
171+
return stack.back ();
172+
}
173+
174+
void
175+
Scope::push (NodeId id)
176+
{
177+
stack.push_back (new Rib (get_crate_num (), id));
178+
}
179+
180+
Rib *
181+
Scope::pop ()
182+
{
183+
Rib *r = peek ();
184+
stack.pop_back ();
185+
return r;
186+
}
187+
188+
void
189+
Scope::append_reference_for_def (NodeId refId, NodeId defId)
190+
{
191+
bool ok = false;
192+
iterate ([&] (Rib *r) mutable -> bool {
193+
if (r->decl_was_declared_here (defId))
194+
{
195+
ok = true;
196+
r->append_reference_for_def (defId, refId);
197+
}
198+
return true;
199+
});
200+
rust_assert (ok);
201+
}
202+
125203
Resolver::Resolver ()
126204
: mappings (Analysis::Mappings::get ()), tyctx (TypeCheckContext::get ()),
127205
name_scope (Scope (mappings->get_current_crate ())),

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

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -63,73 +63,25 @@ class Rib
6363
class Scope
6464
{
6565
public:
66-
Scope (CrateNum crate_num) : crate_num (crate_num) {}
67-
68-
~Scope () {}
66+
Scope (CrateNum crate_num);
6967

7068
void
7169
insert (const CanonicalPath &ident, NodeId id, Location locus, bool shadow,
72-
std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb)
73-
{
74-
peek ()->insert_name (ident, id, locus, shadow, dup_cb);
75-
}
76-
77-
void insert (const CanonicalPath &ident, NodeId id, Location locus)
78-
{
79-
peek ()->insert_name (ident, id, locus, true,
80-
[] (const CanonicalPath &, NodeId, Location) -> void {
81-
});
82-
}
83-
84-
bool lookup (const CanonicalPath &ident, NodeId *id)
85-
{
86-
NodeId lookup = UNKNOWN_NODEID;
87-
iterate ([&] (Rib *r) mutable -> bool {
88-
if (r->lookup_name (ident, &lookup))
89-
return false;
90-
return true;
91-
});
92-
93-
*id = lookup;
94-
return lookup != UNKNOWN_NODEID;
95-
}
70+
std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb);
9671

97-
void iterate (std::function<bool (Rib *)> cb)
98-
{
99-
for (auto it = stack.rbegin (); it != stack.rend (); ++it)
100-
{
101-
if (!cb (*it))
102-
return;
103-
}
104-
}
72+
void insert (const CanonicalPath &ident, NodeId id, Location locus);
73+
bool lookup (const CanonicalPath &ident, NodeId *id);
10574

106-
Rib *peek () { return stack.back (); }
75+
void iterate (std::function<bool (Rib *)> cb);
10776

108-
void push (NodeId id) { stack.push_back (new Rib (get_crate_num (), id)); }
77+
Rib *peek ();
78+
void push (NodeId id);
79+
Rib *pop ();
10980

110-
Rib *pop ()
111-
{
112-
Rib *r = peek ();
113-
stack.pop_back ();
114-
return r;
115-
}
81+
void append_reference_for_def (NodeId refId, NodeId defId);
11682

11783
CrateNum get_crate_num () const { return crate_num; }
11884

119-
void append_reference_for_def (NodeId refId, NodeId defId)
120-
{
121-
bool ok = false;
122-
iterate ([&] (Rib *r) mutable -> bool {
123-
if (r->decl_was_declared_here (defId))
124-
{
125-
ok = true;
126-
r->append_reference_for_def (defId, refId);
127-
}
128-
return true;
129-
});
130-
rust_assert (ok);
131-
}
132-
13385
private:
13486
CrateNum crate_num;
13587
std::vector<Rib *> stack;

0 commit comments

Comments
 (0)