aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorKushal Pal <kushalpal109@gmail.com>2024-08-07 05:19:28 +0000
committerP-E-P <32375388+P-E-P@users.noreply.github.com>2024-09-09 08:33:02 +0000
commitd479eb583efdb83525e2ca2a3f113dfc17fc458f (patch)
tree9600aeb9747ba00bc4671d4930ea493e6732d795 /gcc
parent5819537947cd6ddb1e62b7f895601b1555389c3f (diff)
downloadgcc-d479eb583efdb83525e2ca2a3f113dfc17fc458f.zip
gcc-d479eb583efdb83525e2ca2a3f113dfc17fc458f.tar.gz
gcc-d479eb583efdb83525e2ca2a3f113dfc17fc458f.tar.bz2
Strong type FreeRegion
gcc/rust/ChangeLog: * checks/errors/borrowck/rust-bir-builder-internal.h: Use STATIC_FREE_REGION, use value of FreeRegion for origin. * checks/errors/borrowck/rust-bir-builder.h: Use free region value. * checks/errors/borrowck/rust-bir-dump.cc (Dump::visit_scope): Likewise. * checks/errors/borrowck/rust-bir-fact-collector.h (points): Likewise. * checks/errors/borrowck/rust-bir-free-region.h (struct FreeRegion): Make FreeRegion a struct. * checks/errors/borrowck/rust-bir-place.h: Use FreeRegion instead of Origin in PlaceDB. * typecheck/rust-tyty-variance-analysis.cc (FieldVisitorCtx::add_constraints_from_region): Use value of FreeRegion for origin. (FieldVisitorCtx::add_constrints_from_param): Likewise. (Term::make_transform): Likewise. Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h11
-rw-r--r--gcc/rust/checks/errors/borrowck/rust-bir-builder.h6
-rw-r--r--gcc/rust/checks/errors/borrowck/rust-bir-dump.cc2
-rw-r--r--gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h36
-rw-r--r--gcc/rust/checks/errors/borrowck/rust-bir-free-region.h30
-rw-r--r--gcc/rust/checks/errors/borrowck/rust-bir-place.h14
-rw-r--r--gcc/rust/typecheck/rust-tyty-variance-analysis.cc6
7 files changed, 68 insertions, 37 deletions
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h
index 38a0449..ac93714 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h
@@ -215,7 +215,7 @@ protected:
}
else if (region.is_static ())
{
- free_regions.push_back (0);
+ free_regions.push_back (STATIC_FREE_REGION);
}
else if (region.is_anonymous ())
{
@@ -314,9 +314,10 @@ protected: // Helpers to add BIR statements
{
auto mutability = ty->as<const TyTy::ReferenceType> ()->mutability ();
auto loan = ctx.place_db.add_loan ({mutability, place_id, location});
- push_tmp_assignment (new BorrowExpr (place_id, loan,
- ctx.place_db.get_next_free_region ()),
- ty, location);
+ push_tmp_assignment (
+ new BorrowExpr (place_id, loan,
+ ctx.place_db.get_next_free_region ().value),
+ ty, location);
return translated;
}
@@ -609,7 +610,7 @@ protected:
{ty->as<const TyTy::ReferenceType> ()->mutability (), place_id,
location});
return_expr (new BorrowExpr (place_id, loan,
- ctx.place_db.get_next_free_region ()),
+ ctx.place_db.get_next_free_region ().value),
ty, location);
return translated;
}
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder.h
index 3a92872..9509e00 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-builder.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder.h
@@ -94,8 +94,8 @@ private:
ctx.fn_free_regions[bound.second.get_index ()]);
auto last_bound = universal_region_bounds.back ();
- rust_debug ("\t\t %lu: %lu", (unsigned long) last_bound.first,
- (unsigned long) last_bound.second);
+ rust_debug ("\t\t %lu: %lu", (unsigned long) last_bound.first.value,
+ (unsigned long) last_bound.second.value);
}
// TODO: handle type_region constraints
@@ -178,7 +178,7 @@ private:
if (generic_param->get_kind ()
== HIR::GenericParam::GenericKind::LIFETIME)
{
- result[regions[region_index++]]
+ result[regions[region_index++].value]
= static_cast<HIR::LifetimeParam *> (generic_param.get ());
}
}
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
index f9478a9..8d6b2bb 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
@@ -380,7 +380,7 @@ Dump::visit_scope (ScopeId id, size_t depth)
print_comma_separated (stream,
func.place_db[local].regions.get_regions (),
[this] (FreeRegion region_id) {
- stream << "'?" << region_id;
+ stream << "'?" << region_id.value;
});
stream << "]\n";
}
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h b/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h
index 466e643..6ca5404 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h
@@ -107,14 +107,15 @@ protected: // Main collection entry points (for different categories).
for (auto &region : universal_regions)
{
- facts.universal_region.emplace_back (region);
- facts.placeholder.emplace_back (region, next_loan++);
- facts.known_placeholder_subset.emplace_back (0, region);
+ facts.universal_region.emplace_back (region.value);
+ facts.placeholder.emplace_back (region.value, next_loan++);
+ facts.known_placeholder_subset.emplace_back (0, region.value);
}
// Copy already collected subset facts, that are universally valid.
for (auto &bound : universal_region_bounds)
- facts.known_placeholder_subset.emplace_back (bound.first, bound.second);
+ facts.known_placeholder_subset.emplace_back (bound.first.value,
+ bound.second.value);
}
void visit_places (const std::vector<PlaceId> &args)
@@ -131,7 +132,7 @@ protected: // Main collection entry points (for different categories).
facts.path_is_var.emplace_back (place_id.value, place_id.value);
for (auto &region : place.regions)
facts.use_of_var_derefs_origin.emplace_back (place_id.value,
- region);
+ region.value);
// TODO: drop_of_var_derefs_origin
break;
@@ -178,10 +179,11 @@ protected: // Main collection entry points (for different categories).
rust_debug ("\tSanitize deref of %s", base.tyty->as_string ().c_str ());
- std::vector<Polonius::Origin> regions;
+ std::vector<FreeRegion> regions;
regions.insert (regions.end (), base.regions.begin () + 1,
base.regions.end ());
- FreeRegions r (std::move (regions));
+ FreeRegions r;
+ r.set_from (std::move (regions));
push_subset_all (place.tyty, r, place.regions);
}
void sanizite_field (PlaceId place_id)
@@ -199,7 +201,8 @@ protected: // Main collection entry points (for different categories).
.query_field_regions (base.tyty->as<TyTy::ADTType> (), 0,
place.variable_or_field_index,
base.regions); // FIXME
- FreeRegions f (std::move (r));
+ FreeRegions f;
+ f.set_from (std::move (r));
push_subset_all (place.tyty, f, place.regions);
}
@@ -326,14 +329,14 @@ protected: // Main collection entry points (for different categories).
issue_loan (expr.get_origin (), expr.get_loan ());
}
- push_subset (main_loan_place.regions[0], expr.get_origin ());
+ push_subset (main_loan_place.regions[0], {expr.get_origin ()});
}
else
{
issue_loan (expr.get_origin (), expr.get_loan ());
}
- auto loan_regions = base_place.regions.prepend (expr.get_origin ());
+ auto loan_regions = base_place.regions.prepend ({expr.get_origin ()});
push_subset (ref_place.tyty, loan_regions, ref_place.regions);
}
@@ -632,19 +635,20 @@ protected: // Generic BIR operations.
protected: // Subset helpers.
void push_subset (FreeRegion lhs, FreeRegion rhs)
{
- rust_debug ("\t\tpush_subset: '?%lu: '?%lu", (unsigned long) lhs,
- (unsigned long) rhs);
+ rust_debug ("\t\tpush_subset: '?%lu: '?%lu", (unsigned long) lhs.value,
+ (unsigned long) rhs.value);
- facts.subset_base.emplace_back (lhs, rhs, get_current_point_mid ());
+ facts.subset_base.emplace_back (lhs.value, rhs.value,
+ get_current_point_mid ());
}
void push_subset_all (FreeRegion lhs, FreeRegion rhs)
{
- rust_debug ("\t\tpush_subset_all: '?%lu: '?%lu", (unsigned long) lhs,
- (unsigned long) rhs);
+ rust_debug ("\t\tpush_subset_all: '?%lu: '?%lu", (unsigned long) lhs.value,
+ (unsigned long) rhs.value);
for (auto point : cfg_points_all)
- facts.subset_base.emplace_back (lhs, rhs, point);
+ facts.subset_base.emplace_back (lhs.value, rhs.value, point);
}
void push_subset (Variance variance, FreeRegion lhs, FreeRegion rhs)
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-free-region.h b/gcc/rust/checks/errors/borrowck/rust-bir-free-region.h
index b8d73c9..95d4e11 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-free-region.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-free-region.h
@@ -24,7 +24,19 @@
namespace Rust {
-using FreeRegion = size_t;
+struct FreeRegion
+{
+ size_t value;
+ // some overloads for comparision
+ bool operator== (const FreeRegion &rhs) const { return value == rhs.value; }
+ bool operator!= (const FreeRegion &rhs) const { return !(operator== (rhs)); }
+ bool operator< (const FreeRegion &rhs) const { return value < rhs.value; }
+ bool operator> (const FreeRegion &rhs) const { return value > rhs.value; }
+ bool operator<= (const FreeRegion &rhs) const { return !(operator> (rhs)); }
+ bool operator>= (const FreeRegion &rhs) const { return !(operator< (rhs)); }
+};
+
+static constexpr FreeRegion STATIC_FREE_REGION = {0};
class FreeRegions
{
@@ -46,6 +58,14 @@ public:
this->regions.push_back ({region});
}
}
+ void set_from (std::vector<FreeRegion> &&regions)
+ {
+ this->regions.clear ();
+ for (auto &region : regions)
+ {
+ this->regions.push_back (region);
+ }
+ }
WARN_UNUSED_RESULT FreeRegions prepend (FreeRegion region) const
{
@@ -55,13 +75,14 @@ public:
}
FreeRegions (std::vector<FreeRegion> &&regions) : regions (regions) {}
+ FreeRegions () {}
WARN_UNUSED_RESULT std::string to_string () const
{
std::stringstream result;
for (auto &region : regions)
{
- result << region;
+ result << region.value;
result << ", ";
}
// Remove the last ", " from the string.
@@ -83,7 +104,8 @@ public:
WARN_UNUSED_RESULT FreeRegion get_next_free_region () const
{
- return next_free_region++;
+ ++next_free_region.value;
+ return {next_free_region.value - 1};
}
FreeRegions bind_regions (std::vector<TyTy::Region> regions,
@@ -95,7 +117,7 @@ public:
if (region.is_early_bound ())
free_regions.push_back (parent_free_regions[region.get_index ()]);
else if (region.is_static ())
- free_regions.push_back (0);
+ free_regions.push_back (STATIC_FREE_REGION);
else if (region.is_anonymous ())
free_regions.push_back (get_next_free_region ());
else if (region.is_named ())
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-place.h b/gcc/rust/checks/errors/borrowck/rust-bir-place.h
index 3b0fad2..589aaf0 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-place.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-place.h
@@ -187,7 +187,7 @@ private:
std::vector<Loan> loans;
- Polonius::Origin next_free_region = 1;
+ FreeRegion next_free_region = {1};
public:
PlaceDB ()
@@ -216,7 +216,11 @@ public:
const Scope &get_scope (ScopeId id) const { return scopes[id]; }
- FreeRegion get_next_free_region () { return next_free_region++; }
+ FreeRegion get_next_free_region ()
+ {
+ ++next_free_region.value;
+ return {next_free_region.value - 1};
+ }
FreeRegion peek_next_free_region () const { return next_free_region; }
@@ -256,8 +260,8 @@ public:
->get_variance_analysis_ctx ()
.query_type_variances (new_place_ref.tyty);
std::vector<Polonius::Origin> regions;
- for (size_t i = 0; i < variances.size (); i++)
- regions.push_back (next_free_region++);
+ for (size_t i = 0; i < variances.size (); ++i)
+ regions.push_back (next_free_region.value++);
new_place_ref.regions.set_from (std::move (regions));
@@ -352,7 +356,7 @@ public:
void set_next_free_region (Polonius::Origin next_free_region)
{
- this->next_free_region = next_free_region;
+ this->next_free_region.value = next_free_region;
}
PlaceId lookup_or_add_variable (NodeId id, TyTy::BaseType *tyty)
diff --git a/gcc/rust/typecheck/rust-tyty-variance-analysis.cc b/gcc/rust/typecheck/rust-tyty-variance-analysis.cc
index d7116f5..15c16db 100644
--- a/gcc/rust/typecheck/rust-tyty-variance-analysis.cc
+++ b/gcc/rust/typecheck/rust-tyty-variance-analysis.cc
@@ -570,7 +570,7 @@ FieldVisitorCtx::add_constraints_from_region (const Region &region,
{
if (region.is_early_bound ())
{
- regions.push_back (parent_regions[region.get_index ()]);
+ regions.push_back (parent_regions[region.get_index ()].value);
}
else if (region.is_late_bound ())
{
@@ -585,7 +585,7 @@ FieldVisitorCtx::add_constrints_from_param (ParamType &param, Variance variance)
for (size_t i = type_param_ranges[param_i];
i < type_param_ranges[param_i + 1]; i++)
{
- regions.push_back (parent_regions[i]);
+ regions.push_back (parent_regions[i].value);
}
}
@@ -621,4 +621,4 @@ Term::make_transform (Term lhs, Term rhs)
} // namespace VarianceAnalysis
} // namespace TyTy
-} // namespace Rust \ No newline at end of file
+} // namespace Rust