aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-04-10 15:26:31 +0100
committerPhilip Herron <philip.herron@embecosm.com>2021-04-12 22:39:45 +0100
commit826ce3c92d69b2f2ff9d11ad39cd0843fcc21a05 (patch)
tree5e428fb5db30734fc1e453c60317e6e27aef1554
parent3fa740f4ff0475809edd45de9fb03b12e1f2a389 (diff)
downloadgcc-826ce3c92d69b2f2ff9d11ad39cd0843fcc21a05.zip
gcc-826ce3c92d69b2f2ff9d11ad39cd0843fcc21a05.tar.gz
gcc-826ce3c92d69b2f2ff9d11ad39cd0843fcc21a05.tar.bz2
Add impl mappings to inherent impl items
When lowering impl blocks to HIR we keep track of all the inherent impl items for lookup later but we also need to be able to reverse lookup impl items back to their respective impl block.
-rw-r--r--gcc/rust/hir/rust-ast-lower-item.h9
-rw-r--r--gcc/rust/hir/tree/rust-hir-path.h6
-rw-r--r--gcc/rust/util/rust-hir-map.cc19
-rw-r--r--gcc/rust/util/rust-hir-map.h27
4 files changed, 50 insertions, 11 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h
index edbc498..18ead3c 100644
--- a/gcc/rust/hir/rust-ast-lower-item.h
+++ b/gcc/rust/hir/rust-ast-lower-item.h
@@ -360,12 +360,14 @@ public:
mappings->get_next_localdef_id (crate_num));
std::vector<std::unique_ptr<HIR::InherentImplItem> > impl_items;
+ std::vector<HirId> impl_item_ids;
for (auto &impl_item : impl_block.get_impl_items ())
{
HIR::InherentImplItem *lowered
= ASTLowerImplItem::translate (impl_item.get (),
mapping.get_hirid ());
impl_items.push_back (std::unique_ptr<HIR::InherentImplItem> (lowered));
+ impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
}
translated
@@ -381,6 +383,13 @@ public:
translated);
mappings->insert_location (crate_num, mapping.get_hirid (),
impl_block.get_locus ());
+
+ for (auto &impl_item_id : impl_item_ids)
+ {
+ mappings->insert_impl_item_mapping (impl_item_id,
+ static_cast<HIR::InherentImpl *> (
+ translated));
+ }
}
private:
diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h
index 8dbde9c..d6f9fc0 100644
--- a/gcc/rust/hir/tree/rust-hir-path.h
+++ b/gcc/rust/hir/tree/rust-hir-path.h
@@ -255,6 +255,12 @@ public:
}
}
+ size_t get_num_segments () const { return segments.size (); }
+
+ std::vector<PathExprSegment> &get_segments () { return segments; }
+
+ PathExprSegment &get_root_seg () { return segments.at (0); }
+
PathExprSegment get_final_segment () const { return segments.back (); }
};
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 891618e..0dcbd4f 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -524,5 +524,24 @@ Mappings::resolve_nodeid_to_stmt (CrateNum crate, NodeId id, HIR::Stmt **stmt)
return resolved_stmt != nullptr;
}
+void
+Mappings::iterate_impl_items (
+ std::function<bool (HirId, HIR::InherentImplItem *, HIR::InherentImpl *)> cb)
+{
+ for (auto it = hirImplItemMappings.begin (); it != hirImplItemMappings.end ();
+ it++)
+ {
+ for (auto iy = it->second.begin (); iy != it->second.end (); iy++)
+ {
+ auto id = iy->first;
+ auto impl_item = iy->second.second;
+ auto impl = lookup_associated_impl (
+ impl_item->get_impl_mappings ().get_hirid ());
+ if (!cb (id, impl_item, impl))
+ return;
+ }
+ }
+}
+
} // namespace Analysis
} // namespace Rust
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 2e8a629..9777ee3 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -158,20 +158,24 @@ public:
return hirNodesWithinCrate[crate];
}
- void
- iterate_impl_items (std::function<bool (HirId, HIR::InherentImplItem *)> cb)
+ void insert_impl_item_mapping (HirId impl_item_id, HIR::InherentImpl *impl)
{
- for (auto it = hirImplItemMappings.begin ();
- it != hirImplItemMappings.end (); it++)
- {
- for (auto iy = it->second.begin (); iy != it->second.end (); iy++)
- {
- if (!cb (iy->first, iy->second.second))
- return;
- }
- }
+ rust_assert (hirImplItemsToImplMappings.find (impl_item_id)
+ == hirImplItemsToImplMappings.end ());
+ hirImplItemsToImplMappings[impl_item_id] = impl;
}
+ HIR::InherentImpl *lookup_associated_impl (HirId impl_item_id)
+ {
+ auto lookup = hirImplItemsToImplMappings.find (impl_item_id);
+ rust_assert (lookup != hirImplItemsToImplMappings.end ());
+ return lookup->second;
+ }
+
+ void iterate_impl_items (
+ std::function<bool (HirId, HIR::InherentImplItem *, HIR::InherentImpl *)>
+ cb);
+
private:
Mappings ();
@@ -198,6 +202,7 @@ private:
std::map<HirId, std::pair<HirId, HIR::InherentImplItem *> > >
hirImplItemMappings;
std::map<CrateNum, std::map<HirId, HIR::SelfParam *> > hirSelfParamMappings;
+ std::map<HirId, HIR::InherentImpl *> hirImplItemsToImplMappings;
// location info
std::map<CrateNum, std::map<NodeId, Location> > locations;