aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2024-04-25 16:34:44 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2025-03-17 16:35:24 +0100
commit520a61b1dc7fdc072c825a687749316ed698f926 (patch)
tree292833323c496cfde8ec1e7edde750d8fb799e0a /gcc
parent8c5be0dd1c460694a2c8ef78bdadc2c6aea01ad8 (diff)
downloadgcc-520a61b1dc7fdc072c825a687749316ed698f926.zip
gcc-520a61b1dc7fdc072c825a687749316ed698f926.tar.gz
gcc-520a61b1dc7fdc072c825a687749316ed698f926.tar.bz2
gccrs: Change crate name retrieval function return types
Change their return type to a const reference in order to avoid copies when possible. Also wrap this new return type into an optional. gcc/rust/ChangeLog: * checks/errors/borrowck/rust-borrow-checker.cc (BorrowChecker::go): Adapt the code to the new return types. * resolve/rust-ast-resolve.cc (NameResolution::go): Likewise. * util/rust-hir-map.cc (Mappings::get_crate_name): Change return type to const string reference optional. (Mappings::get_current_crate_name): Likewise. * util/rust-hir-map.h: Update function prototypes. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc6
-rw-r--r--gcc/rust/resolve/rust-ast-resolve.cc4
-rw-r--r--gcc/rust/util/rust-hir-map.cc16
-rw-r--r--gcc/rust/util/rust-hir-map.h4
4 files changed, 11 insertions, 19 deletions
diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
index da4738c..6eec021 100644
--- a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
@@ -65,10 +65,8 @@ BorrowChecker::go (HIR::Crate &crate)
{
mkdir_wrapped ("bir_dump");
auto &mappings = Analysis::Mappings::get ();
- bool ok = mappings.get_crate_name (crate.get_mappings ().get_crate_num (),
- crate_name);
- rust_assert (ok);
-
+ crate_name
+ = *mappings.get_crate_name (crate.get_mappings ().get_crate_num ());
mkdir_wrapped ("nll_facts_gccrs");
}
diff --git a/gcc/rust/resolve/rust-ast-resolve.cc b/gcc/rust/resolve/rust-ast-resolve.cc
index bb8bcc4..a467d1e 100644
--- a/gcc/rust/resolve/rust-ast-resolve.cc
+++ b/gcc/rust/resolve/rust-ast-resolve.cc
@@ -63,9 +63,7 @@ NameResolution::go (AST::Crate &crate)
{
// lookup current crate name
CrateNum cnum = mappings.get_current_crate ();
- std::string crate_name;
- bool ok = mappings.get_crate_name (cnum, crate_name);
- rust_assert (ok);
+ const auto &crate_name = mappings.get_crate_name (cnum).value ();
// setup the ribs
NodeId scope_node_id = crate.get_node_id ();
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 76642a1..cb4e95a 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -138,15 +138,14 @@ Mappings::get_current_crate () const
return currentCrateNum;
}
-bool
-Mappings::get_crate_name (CrateNum crate_num, std::string &name) const
+tl::optional<const std::string &>
+Mappings::get_crate_name (CrateNum crate_num) const
{
auto it = crate_names.find (crate_num);
if (it == crate_names.end ())
- return false;
+ return tl::nullopt;
- name.assign (it->second);
- return true;
+ return it->second;
}
void
@@ -155,13 +154,10 @@ Mappings::set_crate_name (CrateNum crate_num, const std::string &name)
crate_names[crate_num] = name;
}
-std::string
+const 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;
+ return get_crate_name (get_current_crate ()).value ();
}
tl::optional<CrateNum>
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 8181abe..4ac719f 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -80,9 +80,9 @@ 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;
+ tl::optional<const std::string &> get_crate_name (CrateNum crate_num) const;
void set_crate_name (CrateNum crate_num, const std::string &name);
- std::string get_current_crate_name () const;
+ const std::string &get_current_crate_name () const;
tl::optional<CrateNum>
lookup_crate_name (const std::string &crate_name) const;
tl::optional<NodeId> crate_num_to_nodeid (const CrateNum &crate_num) const;