From: Kushal Pal <kushalpal...@gmail.com> gcc/rust/ChangeLog:
* checks/errors/borrowck/rust-bir-place.h: Use strong types as index. Signed-off-by: Kushal Pal <kushalpal...@gmail.com> --- .../checks/errors/borrowck/rust-bir-place.h | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-place.h b/gcc/rust/checks/errors/borrowck/rust-bir-place.h index ae8bec2e273..148bd2a7f49 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-place.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-place.h @@ -215,19 +215,22 @@ public: { internal_vector.emplace_back (std::forward<Args> (args)...); } - std::vector<T> &get_vector () { return internal_vector; } + size_t size () const { return internal_vector.size (); } + + std::vector<T> &get_vector () { return internal_vector; } }; using Scopes = IndexVec<ScopeId, Scope>; using Loans = IndexVec<LoanId, Loan>; +using Places = IndexVec<PlaceId, Place>; /** Allocated places and keeps track of paths. */ class PlaceDB { private: // Possible optimizations: separate variables to speedup lookup. - std::vector<Place> places; + Places places; std::unordered_map<TyTy::BaseType *, PlaceId> constants_lookup; Scopes scopes; ScopeId current_scope = ROOT_SCOPE; @@ -245,11 +248,8 @@ public: scopes.emplace_back (); // Root scope. } - Place &operator[] (PlaceId id) { return places.at (id.value); } - const Place &operator[] (PlaceId id) const { return places.at (id.value); } - - decltype (places)::const_iterator begin () const { return places.begin (); } - decltype (places)::const_iterator end () const { return places.end (); } + Place &operator[] (PlaceId id) { return places.at (id); } + const Place &operator[] (PlaceId id) const { return places.at (id); } size_t size () const { return places.size (); } @@ -294,11 +294,11 @@ public: { places.emplace_back (std::forward<Place &&> (place)); PlaceId new_place = {places.size () - 1}; - Place &new_place_ref = places[new_place.value]; // Intentional shadowing. + Place &new_place_ref = places[new_place]; // Intentional shadowing. if (last_sibling == INVALID_PLACE) - places[new_place_ref.path.parent.value].path.first_child = new_place; + places[new_place_ref.path.parent].path.first_child = new_place; else - places[last_sibling.value].path.next_sibling = new_place; + places[last_sibling].path.next_sibling = new_place; if (new_place_ref.kind == Place::VARIABLE || new_place_ref.kind == Place::TEMPORARY) @@ -332,16 +332,16 @@ public: PlaceId current = INVALID_PLACE; if (parent.value < places.size ()) { - current = places[parent.value].path.first_child; + current = places[parent].path.first_child; while (current != INVALID_PLACE) { - if (places[current.value].kind == kind - && places[current.value].variable_or_field_index == id) + if (places[current].kind == kind + && places[current].variable_or_field_index == id) { - rust_assert (places[current.value].tyty->is_equal (*tyty)); + rust_assert (places[current].tyty->is_equal (*tyty)); return current; } - current = places[current.value].path.next_sibling; + current = places[current].path.next_sibling; } } return add_place ({kind, (uint32_t) id, @@ -370,8 +370,8 @@ public: while (current.value != places.size ()) { - if (places[current.value].kind == Place::VARIABLE - && places[current.value].variable_or_field_index == id) + if (places[current].kind == Place::VARIABLE + && places[current].variable_or_field_index == id) return current; ++current.value; } @@ -383,25 +383,23 @@ public: LoanId id = {loans.size ()}; loans.push_back (std::forward<Loan &&> (loan)); PlaceId borrowed_place = loans.get_vector ().rbegin ()->place; - places[loans.get_vector ().rbegin ()->place.value].borrowed_by.push_back ( - id); - if (places[borrowed_place.value].kind == Place::DEREF) + places[loans.get_vector ().rbegin ()->place].borrowed_by.push_back (id); + if (places[borrowed_place].kind == Place::DEREF) { - places[places[borrowed_place.value].path.parent.value] - .borrowed_by.push_back (id); + places[places[borrowed_place].path.parent].borrowed_by.push_back (id); } return id; } PlaceId get_var (PlaceId id) const { - if (places[id.value].is_var ()) + if (places[id].is_var ()) return id; - rust_assert (places[id.value].is_path ()); + rust_assert (places[id].is_path ()); PlaceId current = id; - while (!places[current.value].is_var ()) + while (!places[current].is_var ()) { - current = places[current.value].path.parent; + current = places[current].path.parent; } return current; } @@ -424,12 +422,12 @@ public: template <typename FN> void for_each_path_from_root (PlaceId var, FN fn) const { PlaceId current = var; - current = places[current.value].path.first_child; + current = places[current].path.first_child; while (current != INVALID_PLACE) { fn (current); for_each_path_from_root (current, fn); - current = places[current.value].path.next_sibling; + current = places[current].path.next_sibling; } } @@ -440,7 +438,7 @@ public: while (current != INVALID_PLACE) { fn (current); - current = places[current.value].path.parent; + current = places[current].path.parent; } } -- 2.45.2