@@ -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));
@@ -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>>
@@ -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;
@@ -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",
@@ -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
@@ -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)
{