diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-04-22 16:39:34 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-04-25 12:34:13 +0200 |
commit | 23db789ecb574e702ca3f3aea6b7d6f9ddbe99cd (patch) | |
tree | 96d44ef2943bba66eb7a57f6cf782d7b102399e4 /gcc | |
parent | 8466c9b1626a71641078ac2f15003aafea92961d (diff) | |
download | gcc-23db789ecb574e702ca3f3aea6b7d6f9ddbe99cd.zip gcc-23db789ecb574e702ca3f3aea6b7d6f9ddbe99cd.tar.gz gcc-23db789ecb574e702ca3f3aea6b7d6f9ddbe99cd.tar.bz2 |
resolver: Move Scope methods into source file
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/resolve/rust-name-resolver.cc | 80 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-name-resolver.h | 66 |
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; |