| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 5 |
// GCC is free software; you can redistribute it and/or modify it under |
| 6 |
// the terms of the GNU General Public License as published by the Free |
| 7 |
// Software Foundation; either version 3, or (at your option) any later |
| 8 |
// version. |
| 9 |
|
| 10 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
// for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with GCC; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/> |
| 18 |
|
| 19 |
#ifndef RUST_BIR_FREE_REGION_H |
| 20 |
#define RUST_BIR_FREE_REGION_H |
| 21 |
|
| 22 |
#include "rust-diagnostics.h" |
| 23 |
#include "polonius/rust-polonius-ffi.h" |
| 24 |
|
| 25 |
namespace Rust { |
| 26 |
|
| 27 |
struct FreeRegion |
| 28 |
{ |
| 29 |
size_t value; |
| 30 |
// some overloads for comparision |
| 31 |
bool operator== (const FreeRegion &rhs) const { return value == rhs.value; } |
| 32 |
bool operator!= (const FreeRegion &rhs) const { return !(operator== (rhs)); } |
| 33 |
bool operator< (const FreeRegion &rhs) const { return value < rhs.value; } |
| 34 |
bool operator> (const FreeRegion &rhs) const { return value > rhs.value; } |
| 35 |
bool operator<= (const FreeRegion &rhs) const { return !(operator> (rhs)); } |
| 36 |
bool operator>= (const FreeRegion &rhs) const { return !(operator< (rhs)); } |
| 37 |
}; |
| 38 |
|
| 39 |
static constexpr FreeRegion STATIC_FREE_REGION = {0}; |
| 40 |
|
| 41 |
class FreeRegions |
| 42 |
{ |
| 43 |
std::vector<FreeRegion> regions; |
| 44 |
|
| 45 |
public: |
| 46 |
WARN_UNUSED_RESULT bool has_regions () const { return !regions.empty (); } |
| 47 |
decltype (regions)::const_iterator begin () const { return regions.begin (); } |
| 48 |
decltype (regions)::const_iterator end () const { return regions.end (); } |
| 49 |
size_t size () const { return regions.size (); } |
| 50 |
FreeRegion &operator[] (size_t i) { return regions.at (i); } |
| 51 |
const FreeRegion &operator[] (size_t i) const { return regions.at (i); } |
| 52 |
const std::vector<FreeRegion> &get_regions () const { return regions; } |
| 53 |
|
| 54 |
WARN_UNUSED_RESULT FreeRegions prepend (FreeRegion region) const |
| 55 |
{ |
| 56 |
std::vector<FreeRegion> new_regions = {region}; |
| 57 |
new_regions.insert (new_regions.end (), regions.begin (), regions.end ()); |
| 58 |
return FreeRegions (std::move (new_regions)); |
| 59 |
} |
| 60 |
|
| 61 |
void push_back (FreeRegion region) { regions.push_back (region); } |
| 62 |
|
| 63 |
FreeRegions () {} |
| 64 |
FreeRegions (std::vector<FreeRegion> &®ions) : regions (regions) {} |
| 65 |
|
| 66 |
WARN_UNUSED_RESULT std::string to_string () const |
| 67 |
{ |
| 68 |
std::stringstream result; |
| 69 |
for (auto ®ion : regions) |
| 70 |
{ |
| 71 |
result << region.value; |
| 72 |
result << ", "; |
| 73 |
} |
| 74 |
// Remove the last ", " from the string. |
| 75 |
if (result.tellg () > 2) |
| 76 |
result.seekp (-2, std::ios_base::cur); |
| 77 |
|
| 78 |
return result.str (); |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
class RegionBinder |
| 83 |
{ |
| 84 |
FreeRegion &next_free_region; |
| 85 |
|
| 86 |
public: |
| 87 |
explicit RegionBinder (FreeRegion &next_free_region) |
| 88 |
: next_free_region (next_free_region) |
| 89 |
{} |
| 90 |
|
| 91 |
WARN_UNUSED_RESULT FreeRegion get_next_free_region () const |
| 92 |
{ |
| 93 |
++next_free_region.value; |
| 94 |
return {next_free_region.value - 1}; |
| 95 |
} |
| 96 |
|
| 97 |
FreeRegions bind_regions (std::vector<TyTy::Region> regions, |
| 98 |
FreeRegions parent_free_regions) |
| 99 |
{ |
| 100 |
FreeRegions free_regions; |
| 101 |
for (auto ®ion : regions) |
| 102 |
{ |
| 103 |
if (region.is_early_bound ()) |
| 104 |
free_regions.push_back (parent_free_regions[region.get_index ()]); |
| 105 |
else if (region.is_static ()) |
| 106 |
free_regions.push_back (STATIC_FREE_REGION); |
| 107 |
else if (region.is_anonymous ()) |
| 108 |
free_regions.push_back (get_next_free_region ()); |
| 109 |
else if (region.is_named ()) |
| 110 |
rust_unreachable (); // FIXME |
| 111 |
else |
| 112 |
{ |
| 113 |
rust_sorry_at (UNKNOWN_LOCATION, "Unimplemented"); |
| 114 |
rust_unreachable (); |
| 115 |
} |
| 116 |
} |
| 117 |
return free_regions; |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
} // namespace Rust |
| 122 |
|
| 123 |
#endif // RUST_BIR_FREE_REGION_H |