diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc index 6ca63115195b..bd49fd910929 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 e4ca0d8ba3ba..8cf32051c7aa 100644 --- a/gcc/rust/expand/rust-macro-builtins.cc +++ b/gcc/rust/expand/rust-macro-builtins.cc @@ -142,9 +142,9 @@ tl::optional 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> diff --git a/gcc/rust/util/bi-map.h b/gcc/rust/util/bi-map.h index ff26c833811c..bc4f5800fb1d 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 class BiMap { - using v_iter = typename std::unordered_map::const_iterator; - using k_iter = typename std::unordered_map::const_iterator; - public: BiMap (std::unordered_map &&original) : map (std::move (original)) { @@ -34,11 +31,22 @@ template class BiMap 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 lookup (const K &key) const + { + auto itr = map.find (key); + if (itr == map.end ()) + return tl::nullopt; + + return itr->second; + } + const tl::optional 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 map; diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 1f7cb4d2c48e..c62c11189aed 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 38df90f29a13..0d8a98077d1e 100644 --- a/gcc/rust/util/rust-lang-item.cc +++ b/gcc/rust/util/rust-lang-item.cc @@ -97,17 +97,15 @@ tl::optional 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 41f810542de5..220e891247f9 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) {