public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Add new interface to mappings class crate_num_to_nodeid and node_is_crate Date: Fri, 15 Jul 2022 11:37:45 +0000 (GMT) [thread overview] Message-ID: <20220715113745.D9C2A385702E@sourceware.org> (raw) https://gcc.gnu.org/g:e3f135ef0beb51b77cae7c6af40c5a1c4dfe7722 commit e3f135ef0beb51b77cae7c6af40c5a1c4dfe7722 Author: Philip Herron <philip.herron@embecosm.com> Date: Fri Jul 8 15:08:08 2022 +0100 Add new interface to mappings class crate_num_to_nodeid and node_is_crate In order to load and resolve extern crates we need to support detection of node_id is a crate in order to support paths outside of the current crate. Diff: --- gcc/rust/resolve/rust-ast-resolve-path.cc | 3 +- gcc/rust/resolve/rust-ast-resolve-type.cc | 3 +- gcc/rust/util/rust-hir-map.cc | 64 +++++++++++++++++++++++++++++++ gcc/rust/util/rust-hir-map.h | 31 ++++----------- 4 files changed, 75 insertions(+), 26 deletions(-) diff --git a/gcc/rust/resolve/rust-ast-resolve-path.cc b/gcc/rust/resolve/rust-ast-resolve-path.cc index a03bfb435b5..27f32aa37ff 100644 --- a/gcc/rust/resolve/rust-ast-resolve-path.cc +++ b/gcc/rust/resolve/rust-ast-resolve-path.cc @@ -199,7 +199,8 @@ ResolvePath::resolve_path (AST::PathInExpression *expr) bool did_resolve_segment = resolved_node_id != UNKNOWN_NODEID; if (did_resolve_segment) { - if (mappings->node_is_module (resolved_node_id)) + if (mappings->node_is_module (resolved_node_id) + || mappings->node_is_crate (resolved_node_id)) { module_scope_id = resolved_node_id; } diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc b/gcc/rust/resolve/rust-ast-resolve-type.cc index 8e85889ffd1..04183a4bc61 100644 --- a/gcc/rust/resolve/rust-ast-resolve-type.cc +++ b/gcc/rust/resolve/rust-ast-resolve-type.cc @@ -220,7 +220,8 @@ ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id) bool did_resolve_segment = resolved_node_id != UNKNOWN_NODEID; if (did_resolve_segment) { - if (mappings->node_is_module (resolved_node_id)) + if (mappings->node_is_module (resolved_node_id) + || mappings->node_is_crate (resolved_node_id)) { module_scope_id = resolved_node_id; } diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 353b7cb16ed..7d2371702c5 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -130,6 +130,70 @@ Mappings::get_current_crate () const return currentCrateNum; } +bool +Mappings::get_crate_name (CrateNum crate_num, std::string &name) const +{ + auto it = crate_names.find (crate_num); + if (it == crate_names.end ()) + return false; + + name.assign (it->second); + return true; +} + +void +Mappings::set_crate_name (CrateNum crate_num, const std::string &name) +{ + crate_names[crate_num] = name; +} + +std::string +Mappings::get_current_crate_name () const +{ + std::string name; + bool ok = get_crate_name (get_current_crate (), name); + rust_assert (ok); + return name; +} + +bool +Mappings::lookup_crate_name (const std::string &crate_name, + CrateNum &resolved_crate_num) const +{ + for (const auto &it : crate_names) + { + if (it.second.compare (crate_name) == 0) + { + resolved_crate_num = it.first; + return true; + } + } + return false; +} + +bool +Mappings::crate_num_to_nodeid (const CrateNum &crate_num, NodeId &node_id) const +{ + auto it = ast_crate_mappings.find (crate_num); + if (it == ast_crate_mappings.end ()) + return false; + + node_id = it->second->get_node_id (); + return true; +} + +bool +Mappings::node_is_crate (NodeId node_id) const +{ + for (const auto &it : ast_crate_mappings) + { + NodeId crate_node_id = it.second->get_node_id (); + if (crate_node_id == node_id) + return true; + } + return false; +} + NodeId Mappings::get_next_node_id () { diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 804242e99bb..c8cebefcd04 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -77,30 +77,13 @@ public: CrateNum get_next_crate_num (const std::string &name); void set_current_crate (CrateNum crateNum); CrateNum get_current_crate () const; - - bool get_crate_name (CrateNum crate_num, std::string &name) const - { - auto it = crate_names.find (crate_num); - if (it == crate_names.end ()) - return false; - - name.assign (it->second); - return true; - } - - // set crate name mid-compilation - void set_crate_name (CrateNum crate_num, const std::string &name) - { - crate_names[crate_num] = name; - } - - std::string get_current_crate_name () const - { - std::string name; - bool ok = get_crate_name (get_current_crate (), name); - rust_assert (ok); - return name; - } + bool get_crate_name (CrateNum crate_num, std::string &name) const; + void set_crate_name (CrateNum crate_num, const std::string &name); + std::string get_current_crate_name () const; + bool lookup_crate_name (const std::string &crate_name, + CrateNum &resolved_crate_num) const; + bool crate_num_to_nodeid (const CrateNum &crate_num, NodeId &node_id) const; + bool node_is_crate (NodeId node_id) const; NodeId get_next_node_id (); HirId get_next_hir_id () { return get_next_hir_id (get_current_crate ()); }
reply other threads:[~2022-07-15 11:37 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220715113745.D9C2A385702E@sourceware.org \ --to=tschwinge@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).