aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/resolve/rust-name-resolver.cc80
-rw-r--r--gcc/rust/resolve/rust-name-resolver.h66
2 files changed, 88 insertions, 58 deletions
diff --git a/gcc/rust/resolve/rust-name-resolver.cc b/gcc/rust/resolve/rust-name-resolver.cc
index 10e7297..fc1f361 100644
--- a/gcc/rust/resolve/rust-name-resolver.cc
+++ b/gcc/rust/resolve/rust-name-resolver.cc
@@ -34,7 +34,8 @@
_R.push_back (builtin_type); \
tyctx->insert_builtin (_TY->get_ref (), builtin_type->get_node_id (), \
_TY); \
- } while (0)
+ } \
+ while (0)
namespace Rust {
namespace Resolver {
@@ -122,6 +123,83 @@ Rib::decl_was_declared_here (NodeId def) const
return false;
}
+Scope::Scope (CrateNum crate_num) : crate_num (crate_num) {}
+
+void
+Scope::insert (
+ const CanonicalPath &ident, NodeId id, Location locus, bool shadow,
+ std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb)
+{
+ peek ()->insert_name (ident, id, locus, shadow, dup_cb);
+}
+
+void
+Scope::insert (const CanonicalPath &ident, NodeId id, Location locus)
+{
+ peek ()->insert_name (ident, id, locus, true,
+ [] (const CanonicalPath &, NodeId, Location) -> void {
+ });
+}
+
+bool
+Scope::lookup (const CanonicalPath &ident, NodeId *id)
+{
+ NodeId lookup = UNKNOWN_NODEID;
+ iterate ([&] (Rib *r) mutable -> bool {
+ if (r->lookup_name (ident, &lookup))
+ return false;
+ return true;
+ });
+
+ *id = lookup;
+ return lookup != UNKNOWN_NODEID;
+}
+
+void
+Scope::iterate (std::function<bool (Rib *)> cb)
+{
+ for (auto it = stack.rbegin (); it != stack.rend (); ++it)
+ {
+ if (!cb (*it))
+ return;
+ }
+}
+
+Rib *
+Scope::peek ()
+{
+ return stack.back ();
+}
+
+void
+Scope::push (NodeId id)
+{
+ stack.push_back (new Rib (get_crate_num (), id));
+}
+
+Rib *
+Scope::pop ()
+{
+ Rib *r = peek ();
+ stack.pop_back ();
+ return r;
+}
+
+void
+Scope::append_reference_for_def (NodeId refId, NodeId defId)
+{
+ bool ok = false;
+ iterate ([&] (Rib *r) mutable -> bool {
+ if (r->decl_was_declared_here (defId))
+ {
+ ok = true;
+ r->append_reference_for_def (defId, refId);
+ }
+ return true;
+ });
+ rust_assert (ok);
+}
+
Resolver::Resolver ()
: mappings (Analysis::Mappings::get ()), tyctx (TypeCheckContext::get ()),
name_scope (Scope (mappings->get_current_crate ())),
diff --git a/gcc/rust/resolve/rust-name-resolver.h b/gcc/rust/resolve/rust-name-resolver.h
index e46d455..ab7cb55 100644
--- a/gcc/rust/resolve/rust-name-resolver.h
+++ b/gcc/rust/resolve/rust-name-resolver.h
@@ -63,73 +63,25 @@ private:
class Scope
{
public:
- Scope (CrateNum crate_num) : crate_num (crate_num) {}
-
- ~Scope () {}
+ Scope (CrateNum crate_num);
void
insert (const CanonicalPath &ident, NodeId id, Location locus, bool shadow,
- std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb)
- {
- peek ()->insert_name (ident, id, locus, shadow, dup_cb);
- }
-
- void insert (const CanonicalPath &ident, NodeId id, Location locus)
- {
- peek ()->insert_name (ident, id, locus, true,
- [] (const CanonicalPath &, NodeId, Location) -> void {
- });
- }
-
- bool lookup (const CanonicalPath &ident, NodeId *id)
- {
- NodeId lookup = UNKNOWN_NODEID;
- iterate ([&] (Rib *r) mutable -> bool {
- if (r->lookup_name (ident, &lookup))
- return false;
- return true;
- });
-
- *id = lookup;
- return lookup != UNKNOWN_NODEID;
- }
+ std::function<void (const CanonicalPath &, NodeId, Location)> dup_cb);
- void iterate (std::function<bool (Rib *)> cb)
- {
- for (auto it = stack.rbegin (); it != stack.rend (); ++it)
- {
- if (!cb (*it))
- return;
- }
- }
+ void insert (const CanonicalPath &ident, NodeId id, Location locus);
+ bool lookup (const CanonicalPath &ident, NodeId *id);
- Rib *peek () { return stack.back (); }
+ void iterate (std::function<bool (Rib *)> cb);
- void push (NodeId id) { stack.push_back (new Rib (get_crate_num (), id)); }
+ Rib *peek ();
+ void push (NodeId id);
+ Rib *pop ();
- Rib *pop ()
- {
- Rib *r = peek ();
- stack.pop_back ();
- return r;
- }
+ void append_reference_for_def (NodeId refId, NodeId defId);
CrateNum get_crate_num () const { return crate_num; }
- void append_reference_for_def (NodeId refId, NodeId defId)
- {
- bool ok = false;
- iterate ([&] (Rib *r) mutable -> bool {
- if (r->decl_was_declared_here (defId))
- {
- ok = true;
- r->append_reference_for_def (defId, refId);
- }
- return true;
- });
- rust_assert (ok);
- }
-
private:
CrateNum crate_num;
std::vector<Rib *> stack;