Skip to content

Commit

Permalink
Clean BiMap to use tl::optional for lookups
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
srj31 authored and CohenArthur committed Mar 5, 2024
1 parent dae69e8 commit 452345f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 23 deletions.
8 changes: 4 additions & 4 deletions gcc/rust/expand/rust-expand-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions gcc/rust/expand/rust-macro-builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,19 @@ 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 {
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>>
Expand Down
22 changes: 15 additions & 7 deletions gcc/rust/util/bi-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,29 @@
// 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))
{
for (auto &kv : map)
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;
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/util/rust-hir-map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions gcc/rust/util/rust-lang-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions gcc/rust/util/rust-token-converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 452345f

Please sign in to comment.