Skip to content

Commit

Permalink
Used IndexVec for Places
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* checks/errors/borrowck/rust-bir-place.h: Use strong types as
	  index.

Signed-off-by: Kushal Pal <[email protected]>
  • Loading branch information
braw-lee committed Aug 20, 2024
1 parent 8292118 commit 30c9497
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions gcc/rust/checks/errors/borrowck/rust-bir-place.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,22 @@ template <typename I, typename T> class IndexVec
{
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<Scope, ScopeId>;
using Loans = IndexVec<Loan, LoanId>;
using Places = IndexVec<Place, PlaceId>;

/** 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;
Expand All @@ -244,11 +247,8 @@ class PlaceDB
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 (); }

Expand Down Expand Up @@ -293,11 +293,11 @@ class PlaceDB
{
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)
Expand Down Expand Up @@ -331,16 +331,16 @@ class PlaceDB
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,
Expand Down Expand Up @@ -369,8 +369,8 @@ class PlaceDB

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;
}
Expand All @@ -382,25 +382,23 @@ class PlaceDB
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;
}
Expand All @@ -423,12 +421,12 @@ class PlaceDB
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;
}
}

Expand All @@ -439,7 +437,7 @@ class PlaceDB
while (current != INVALID_PLACE)
{
fn (current);
current = places[current.value].path.parent;
current = places[current].path.parent;
}
}

Expand Down

0 comments on commit 30c9497

Please sign in to comment.