aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/util/rust-hir-map.cc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/rust/util/rust-hir-map.cc')
-rw-r--r--gcc/rust/util/rust-hir-map.cc118
1 files changed, 108 insertions, 10 deletions
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 03b3343..eaa640c 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -148,6 +148,16 @@ Mappings::get_crate_name (CrateNum crate_num) const
return it->second;
}
+tl::optional<CrateNum>
+Mappings::lookup_crate_num (NodeId node_id) const
+{
+ auto it = crate_node_to_crate_num.find (node_id);
+ if (it == crate_node_to_crate_num.end ())
+ return tl::nullopt;
+
+ return it->second;
+}
+
void
Mappings::set_crate_name (CrateNum crate_num, const std::string &name)
{
@@ -184,13 +194,7 @@ Mappings::crate_num_to_nodeid (const CrateNum &crate_num) const
bool
Mappings::node_is_crate (NodeId node_id) const
{
- for (const auto &it : ast_crate_mappings)
- {
- NodeId crate_node_id = it.second->get_node_id ();
- if (crate_node_id == node_id)
- return true;
- }
- return false;
+ return lookup_crate_num (node_id).has_value ();
}
NodeId
@@ -262,6 +266,7 @@ Mappings::insert_ast_crate (std::unique_ptr<AST::Crate> &&crate,
rust_assert (it == ast_crate_mappings.end ());
// store it
+ crate_node_to_crate_num.insert ({crate->get_node_id (), crate_num});
ast_crate_mappings.insert ({crate_num, crate.release ()});
// return the reference to it
@@ -459,7 +464,7 @@ Mappings::insert_hir_impl_block (HIR::ImplBlock *item)
auto id = item->get_mappings ().get_hirid ();
rust_assert (!lookup_hir_impl_block (id));
- HirId impl_type_id = item->get_type ()->get_mappings ().get_hirid ();
+ HirId impl_type_id = item->get_type ().get_mappings ().get_hirid ();
hirImplBlockMappings[id] = item;
hirImplBlockTypeMappings[impl_type_id] = item;
insert_node_to_hir (item->get_mappings ().get_nodeid (), id);
@@ -874,7 +879,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
auto it = macroMappings.find (macro->get_node_id ());
rust_assert (it == macroMappings.end ());
- macroMappings[macro->get_node_id ()] = macro;
+ macroMappings[macro->get_node_id ()] = {macro, currentCrateNum};
}
tl::optional<AST::MacroRulesDefinition *>
@@ -884,7 +889,17 @@ Mappings::lookup_macro_def (NodeId id)
if (it == macroMappings.end ())
return tl::nullopt;
- return it->second;
+ return it->second.first;
+}
+
+tl::optional<CrateNum>
+Mappings::lookup_macro_def_crate (NodeId id)
+{
+ auto it = macroMappings.find (id);
+ if (it == macroMappings.end ())
+ return tl::nullopt;
+
+ return it->second.second;
}
void
@@ -1241,6 +1256,9 @@ Mappings::lookup_builtin_marker ()
return builtinMarker;
}
+// FIXME: Before merging: Should we remove the `locus` parameter here? since
+// lang items are looked up mostly for code generation, it doesn't make sense to
+// error out on the locus of the node trying to access an inexistant lang item
DefId
Mappings::get_lang_item (LangItem::Kind item_type, location_t locus)
{
@@ -1258,5 +1276,85 @@ Mappings::lookup_trait_item_lang_item (LangItem::Kind item, location_t locus)
return lookup_trait_item_defid (trait_item_id);
}
+void
+Mappings::insert_lang_item (LangItem::Kind item_type, DefId id)
+{
+ auto it = lang_item_mappings.find (item_type);
+ rust_assert (it == lang_item_mappings.end ());
+
+ lang_item_mappings[item_type] = id;
+}
+
+tl::optional<DefId &>
+Mappings::lookup_lang_item (LangItem::Kind item_type)
+{
+ auto it = lang_item_mappings.find (item_type);
+ if (it == lang_item_mappings.end ())
+ return tl::nullopt;
+
+ return it->second;
+}
+
+void
+Mappings::insert_lang_item_node (LangItem::Kind item_type, NodeId node_id)
+{
+ auto it = lang_item_nodes.find (item_type);
+ rust_assert (it == lang_item_nodes.end ());
+
+ lang_item_nodes.insert ({item_type, node_id});
+}
+
+tl::optional<NodeId &>
+Mappings::lookup_lang_item_node (LangItem::Kind item_type)
+{
+ auto it = lang_item_nodes.find (item_type);
+ if (it == lang_item_nodes.end ())
+ return tl::nullopt;
+
+ return it->second;
+}
+
+NodeId
+Mappings::get_lang_item_node (LangItem::Kind item_type)
+{
+ if (auto lookup = lookup_lang_item_node (item_type))
+ return *lookup;
+
+ rust_fatal_error (UNKNOWN_LOCATION, "undeclared lang item: %qs",
+ LangItem::PrettyString (item_type).c_str ());
+}
+
+void
+Mappings::insert_auto_trait (HIR::Trait *trait)
+{
+ auto_traits.emplace_back (trait);
+}
+
+std::vector<HIR::Trait *> &
+Mappings::get_auto_traits ()
+{
+ return auto_traits;
+}
+
+void
+Mappings::add_capture (NodeId closure, NodeId definition)
+{
+ auto cap = captures.find (closure);
+ if (cap == captures.end ())
+ captures[closure] = {definition};
+ else
+ cap->second.push_back (definition);
+}
+
+tl::optional<std::vector<NodeId>>
+Mappings::lookup_captures (NodeId closure)
+{
+ auto cap = captures.find (closure);
+ if (cap == captures.end ())
+ return tl::nullopt;
+ else
+ return cap->second;
+}
+
} // namespace Analysis
} // namespace Rust