diff mbox series

[037/125] gccrs: Clean BiMap to use tl::optional for lookups

Message ID 20240801145809.366388-39-arthur.cohen@embecosm.com
State New
Headers show
Series [001/125] Rust: Make 'tree'-level 'MAIN_NAME_P' work | expand

Commit Message

Arthur Cohen Aug. 1, 2024, 2:56 p.m. UTC
From: Sourabh Jaiswal <sourabhrj31@gmail.com>

gcc/rust/Changelog:

	* expand/rust-expand-visitor.cc
	(ExpandVisitor::expand_inner_items): Adjust to use has_value ()
	(ExpandVisitor::expand_inner_stmts): Likewise
	* expand/rust-macro-builtins.cc (builtin_macro_from_string): Likewise
	(make_macro_path_str): Likewise
	* util/rust-hir-map.cc (Mappings::insert_macro_def): Likewise
	* util/rust-lang-item.cc (LangItem::Parse): Adjust to return tl::optional
	(LangItem::toString) Likewise
	* util/rust-token-converter.cc (handle_suffix): Adjust to use value.or ()
	(from_literal) Likewise
	* util/bi-map.h (BiMap::lookup): Adjust to use tl::optional for
	lookups

Signed-off-by: Sourabh Jaiswal <sourabhrj31@gmail.com>
---
 gcc/rust/expand/rust-expand-visitor.cc |  8 ++++----
 gcc/rust/expand/rust-macro-builtins.cc |  8 ++++----
 gcc/rust/util/bi-map.h                 | 22 +++++++++++++++-------
 gcc/rust/util/rust-hir-map.cc          |  2 +-
 gcc/rust/util/rust-lang-item.cc        |  6 ++----
 gcc/rust/util/rust-token-converter.cc  |  5 ++---
 6 files changed, 28 insertions(+), 23 deletions(-)
diff mbox series

Patch

diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc
index 6ca63115195..bd49fd91092 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -186,11 +186,11 @@  ExpandVisitor::expand_inner_items (
 		    {
 		      auto maybe_builtin = MacroBuiltin::builtins.lookup (
 			to_derive.get ().as_string ());
-		      if (MacroBuiltin::builtins.is_iter_ok (maybe_builtin))
+		      if (maybe_builtin.has_value ())
 			{
 			  auto new_item
 			    = builtin_derive_item (*item, current,
-						   maybe_builtin->second);
+						   maybe_builtin.value ());
 			  // this inserts the derive *before* the item - is it a
 			  // problem?
 			  it = items.insert (it, std::move (new_item));
@@ -272,11 +272,11 @@  ExpandVisitor::expand_inner_stmts (AST::BlockExpr &expr)
 		    {
 		      auto maybe_builtin = MacroBuiltin::builtins.lookup (
 			to_derive.get ().as_string ());
-		      if (MacroBuiltin::builtins.is_iter_ok (maybe_builtin))
+		      if (maybe_builtin.has_value ())
 			{
 			  auto new_item
 			    = builtin_derive_item (item, current,
-						   maybe_builtin->second);
+						   maybe_builtin.value ());
 			  // this inserts the derive *before* the item - is it a
 			  // problem?
 			  it = stmts.insert (it, std::move (new_item));
diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc
index e4ca0d8ba3b..8cf32051c7a 100644
--- a/gcc/rust/expand/rust-macro-builtins.cc
+++ b/gcc/rust/expand/rust-macro-builtins.cc
@@ -142,9 +142,9 @@  tl::optional<BuiltinMacro>
 builtin_macro_from_string (const std::string &identifier)
 {
   auto macro = MacroBuiltin::builtins.lookup (identifier);
-  rust_assert (MacroBuiltin::builtins.is_iter_ok (macro));
+  rust_assert (macro.has_value ());
 
-  return macro->second;
+  return macro;
 }
 
 namespace {
@@ -152,9 +152,9 @@  std::string
 make_macro_path_str (BuiltinMacro kind)
 {
   auto str = MacroBuiltin::builtins.lookup (kind);
-  rust_assert (MacroBuiltin::builtins.is_iter_ok (str));
+  rust_assert (str.has_value ());
 
-  return str->second;
+  return str.value ();
 }
 
 static std::vector<std::unique_ptr<AST::MacroInvocation>>
diff --git a/gcc/rust/util/bi-map.h b/gcc/rust/util/bi-map.h
index ff26c833811..bc4f5800fb1 100644
--- a/gcc/rust/util/bi-map.h
+++ b/gcc/rust/util/bi-map.h
@@ -24,9 +24,6 @@ 
 // very simple bi-directional hashmap
 template <typename K, typename V> class BiMap
 {
-  using v_iter = typename std::unordered_map<K, V>::const_iterator;
-  using k_iter = typename std::unordered_map<V, K>::const_iterator;
-
 public:
   BiMap (std::unordered_map<K, V> &&original) : map (std::move (original))
   {
@@ -34,11 +31,22 @@  public:
       rmap.insert ({kv.second, kv.first});
   }
 
-  const v_iter lookup (const K &key) const { return map.find (key); }
-  const k_iter lookup (const V &key) const { return rmap.find (key); }
+  const tl::optional<const V &> lookup (const K &key) const
+  {
+    auto itr = map.find (key);
+    if (itr == map.end ())
+      return tl::nullopt;
+
+    return itr->second;
+  }
+  const tl::optional<const K &> lookup (const V &key) const
+  {
+    auto itr = rmap.find (key);
+    if (itr == rmap.end ())
+      return tl::nullopt;
 
-  bool is_iter_ok (const v_iter &iter) const { return iter != map.end (); }
-  bool is_iter_ok (const k_iter &iter) const { return iter != rmap.end (); }
+    return itr->second;
+  }
 
 private:
   std::unordered_map<K, V> map;
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 1f7cb4d2c48..c62c11189ae 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -880,7 +880,7 @@  Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
     {
       auto builtin
 	= MacroBuiltin::builtins.lookup (macro->get_rule_name ().as_string ());
-      if (!MacroBuiltin::builtins.is_iter_ok (builtin))
+      if (!builtin.has_value ())
 	{
 	  rust_error_at (macro->get_locus (),
 			 "cannot find a built-in macro with name %qs",
diff --git a/gcc/rust/util/rust-lang-item.cc b/gcc/rust/util/rust-lang-item.cc
index 38df90f29a1..0d8a98077d1 100644
--- a/gcc/rust/util/rust-lang-item.cc
+++ b/gcc/rust/util/rust-lang-item.cc
@@ -97,17 +97,15 @@  tl::optional<LangItem::Kind>
 LangItem::Parse (const std::string &item)
 {
   auto lang_item = LangItem::lang_items.lookup (item);
-  if (!LangItem::lang_items.is_iter_ok (lang_item))
-    return tl::nullopt;
 
-  return lang_item->second;
+  return lang_item;
 }
 
 std::string
 LangItem::ToString (LangItem::Kind type)
 {
   auto str = LangItem::lang_items.lookup (type);
-  return str->second;
+  return str.value ();
 }
 
 LangItem::Kind
diff --git a/gcc/rust/util/rust-token-converter.cc b/gcc/rust/util/rust-token-converter.cc
index 41f810542de..220e891247f 100644
--- a/gcc/rust/util/rust-token-converter.cc
+++ b/gcc/rust/util/rust-token-converter.cc
@@ -67,7 +67,7 @@  handle_suffix (const const_TokenPtr &token, ProcMacro::LitKind kind)
 {
   auto str = token->as_string ();
   auto lookup = suffixes.lookup (token->get_type_hint ());
-  auto suffix = suffixes.is_iter_ok (lookup) ? lookup->second : "";
+  auto suffix = lookup.value_or ("");
   return ProcMacro::Literal::make_literal (kind, convert (token->get_locus ()),
 					   str, suffix);
 }
@@ -296,8 +296,7 @@  from_literal (const ProcMacro::Literal &literal,
 {
   auto lookup = suffixes.lookup (literal.suffix.to_string ());
   auto loc = convert (literal.span);
-  auto suffix
-    = suffixes.is_iter_ok (lookup) ? lookup->second : CORETYPE_UNKNOWN;
+  auto suffix = lookup.value_or (CORETYPE_UNKNOWN);
   // FIXME: Add spans instead of empty locations
   switch (literal.kind.tag)
     {