| 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_TYTY_SUBST_H |
| 20 |
#define RUST_TYTY_SUBST_H |
| 21 |
|
| 22 |
#include "rust-system.h" |
| 23 |
#include "rust-location.h" |
| 24 |
#include "rust-hir-full-decls.h" |
| 25 |
#include "rust-tyty-bounds.h" |
| 26 |
#include "rust-tyty-region.h" |
| 27 |
#include "optional.h" |
| 28 |
|
| 29 |
namespace Rust { |
| 30 |
namespace TyTy { |
| 31 |
|
| 32 |
class ParamType; |
| 33 |
|
| 34 |
struct RegionConstraints |
| 35 |
{ |
| 36 |
/** 'a: 'b */ |
| 37 |
std::vector<std::pair<Region, Region>> region_region; |
| 38 |
/** T: 'a */ |
| 39 |
std::vector<std::pair<ParamType *, Region>> type_region; |
| 40 |
}; |
| 41 |
|
| 42 |
class BaseType; |
| 43 |
class SubstitutionArgumentMappings; |
| 44 |
class SubstitutionParamMapping |
| 45 |
{ |
| 46 |
public: |
| 47 |
SubstitutionParamMapping (HIR::TypeParam &generic, ParamType *param); |
| 48 |
|
| 49 |
SubstitutionParamMapping (const SubstitutionParamMapping &other); |
| 50 |
|
| 51 |
std::string as_string () const; |
| 52 |
|
| 53 |
bool fill_param_ty (SubstitutionArgumentMappings &subst_mappings, |
| 54 |
location_t locus); |
| 55 |
|
| 56 |
SubstitutionParamMapping clone () const; |
| 57 |
|
| 58 |
ParamType *get_param_ty (); |
| 59 |
|
| 60 |
const ParamType *get_param_ty () const; |
| 61 |
|
| 62 |
HIR::TypeParam &get_generic_param (); |
| 63 |
|
| 64 |
// this is used for the backend to override the HirId ref of the param to |
| 65 |
// what the concrete type is for the rest of the context |
| 66 |
void override_context (); |
| 67 |
|
| 68 |
bool needs_substitution () const; |
| 69 |
|
| 70 |
location_t get_param_locus () const; |
| 71 |
|
| 72 |
bool param_has_default_ty () const; |
| 73 |
|
| 74 |
BaseType *get_default_ty () const; |
| 75 |
|
| 76 |
bool need_substitution () const; |
| 77 |
|
| 78 |
private: |
| 79 |
HIR::TypeParam &generic; |
| 80 |
ParamType *param; |
| 81 |
}; |
| 82 |
|
| 83 |
/** |
| 84 |
* Represents the part of the parameter list that contains lifetime |
| 85 |
* parameters. |
| 86 |
* |
| 87 |
* ``` |
| 88 |
* Foo<'a, 'b, i32, 8> |
| 89 |
* ^^^^^^ |
| 90 |
* ``` |
| 91 |
* |
| 92 |
* It has fixed size based on the number of lifetime parameters and they are |
| 93 |
* indexed based on their order. |
| 94 |
* |
| 95 |
* All regions are initially set to unresolved. When type instantiation is |
| 96 |
* encountered, all explicitly mentioned lifetimes are resolved to bound |
| 97 |
* lifetimes. The remaining unresolved lifetimes are set to anonymous. During |
| 98 |
* BIR construction, all lifetimes are replaced with free region variables. |
| 99 |
* Inference of anonymous regions happens automatically using BIR subtyping |
| 100 |
* pass. |
| 101 |
*/ |
| 102 |
class RegionParamList |
| 103 |
{ |
| 104 |
std::vector<Region> regions; |
| 105 |
|
| 106 |
public: |
| 107 |
RegionParamList (size_t num_regions) : regions (num_regions) {} |
| 108 |
|
| 109 |
Region *begin () { return regions.data (); } |
| 110 |
Region *end () { return regions.data () + regions.size (); } |
| 111 |
Region &operator[] (size_t index) { return regions.at (index); } |
| 112 |
const Region &operator[] (size_t index) const { return regions.at (index); } |
| 113 |
WARN_UNUSED_RESULT const Region *begin () const { return regions.data (); } |
| 114 |
WARN_UNUSED_RESULT const Region *end () const |
| 115 |
{ |
| 116 |
return regions.data () + regions.size (); |
| 117 |
} |
| 118 |
size_t size () const { return regions.size (); } |
| 119 |
|
| 120 |
/** |
| 121 |
* Takes regions from the `subst` parameter and fills the rest with anonymous |
| 122 |
* regions. |
| 123 |
*/ |
| 124 |
static RegionParamList from_subst (size_t num_regions, |
| 125 |
std::vector<Region> subst) |
| 126 |
{ |
| 127 |
RegionParamList list (num_regions); |
| 128 |
for (size_t i = 0; i < MIN (num_regions, subst.size ()); i++) |
| 129 |
list.regions.at (i) = subst.at (i); |
| 130 |
for (size_t i = subst.size (); i < num_regions; i++) |
| 131 |
{ |
| 132 |
list.regions.at (i) = Region::make_anonymous (); |
| 133 |
} |
| 134 |
return list; |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
class SubstitutionArg |
| 139 |
{ |
| 140 |
public: |
| 141 |
SubstitutionArg (const SubstitutionParamMapping *param, BaseType *argument); |
| 142 |
|
| 143 |
// FIXME |
| 144 |
// the copy constructors need removed - they are unsafe see |
| 145 |
// TypeBoundPredicate |
| 146 |
SubstitutionArg (const SubstitutionArg &other); |
| 147 |
|
| 148 |
SubstitutionArg &operator= (const SubstitutionArg &other); |
| 149 |
|
| 150 |
BaseType *get_tyty (); |
| 151 |
|
| 152 |
const BaseType *get_tyty () const; |
| 153 |
|
| 154 |
const SubstitutionParamMapping *get_param_mapping () const; |
| 155 |
|
| 156 |
const ParamType *get_param_ty () const; |
| 157 |
|
| 158 |
static SubstitutionArg error (); |
| 159 |
|
| 160 |
bool is_error () const; |
| 161 |
|
| 162 |
bool is_conrete () const; |
| 163 |
|
| 164 |
std::string as_string () const; |
| 165 |
|
| 166 |
private: |
| 167 |
const SubstitutionParamMapping *param; |
| 168 |
const ParamType *original_param; |
| 169 |
BaseType *argument; |
| 170 |
}; |
| 171 |
|
| 172 |
typedef std::function<void (const ParamType &, const SubstitutionArg &)> |
| 173 |
ParamSubstCb; |
| 174 |
class SubstitutionArgumentMappings |
| 175 |
{ |
| 176 |
public: |
| 177 |
SubstitutionArgumentMappings (std::vector<SubstitutionArg> mappings, |
| 178 |
std::map<std::string, BaseType *> binding_args, |
| 179 |
RegionParamList regions, location_t locus, |
| 180 |
ParamSubstCb param_subst_cb = nullptr, |
| 181 |
bool trait_item_flag = false, |
| 182 |
bool error_flag = false); |
| 183 |
|
| 184 |
SubstitutionArgumentMappings (const SubstitutionArgumentMappings &other); |
| 185 |
SubstitutionArgumentMappings & |
| 186 |
operator= (const SubstitutionArgumentMappings &other); |
| 187 |
|
| 188 |
SubstitutionArgumentMappings (SubstitutionArgumentMappings &&other) = default; |
| 189 |
SubstitutionArgumentMappings &operator= (SubstitutionArgumentMappings &&other) |
| 190 |
= default; |
| 191 |
|
| 192 |
static SubstitutionArgumentMappings error (); |
| 193 |
|
| 194 |
/** Creates empty substitution argument mappings with unresolved regions */ |
| 195 |
static SubstitutionArgumentMappings empty (size_t num_regions = 0); |
| 196 |
|
| 197 |
static RegionParamList |
| 198 |
regions_from_nullable_args (SubstitutionArgumentMappings *args) |
| 199 |
{ |
| 200 |
if (args == nullptr) |
| 201 |
return RegionParamList (0); |
| 202 |
|
| 203 |
return args->get_regions (); |
| 204 |
} |
| 205 |
|
| 206 |
bool is_error () const; |
| 207 |
|
| 208 |
bool get_argument_for_symbol (const ParamType *param_to_find, |
| 209 |
SubstitutionArg *argument) const; |
| 210 |
|
| 211 |
/** Return type parameter index for symbol */ |
| 212 |
tl::optional<size_t> find_symbol (const ParamType ¶m_to_find) const; |
| 213 |
|
| 214 |
bool get_argument_at (size_t index, SubstitutionArg *argument); |
| 215 |
|
| 216 |
// is_concrete means if the used args is non error, ie: non empty this will |
| 217 |
// verify if actual real types have been put in place of are they still |
| 218 |
// ParamTy |
| 219 |
bool is_concrete () const; |
| 220 |
|
| 221 |
location_t get_locus () const; |
| 222 |
|
| 223 |
size_t size () const; |
| 224 |
|
| 225 |
bool is_empty () const; |
| 226 |
|
| 227 |
std::vector<SubstitutionArg> &get_mappings (); |
| 228 |
|
| 229 |
const std::vector<SubstitutionArg> &get_mappings () const; |
| 230 |
|
| 231 |
std::map<std::string, BaseType *> &get_binding_args (); |
| 232 |
|
| 233 |
const std::map<std::string, BaseType *> &get_binding_args () const; |
| 234 |
|
| 235 |
const RegionParamList &get_regions () const; |
| 236 |
RegionParamList &get_mut_regions (); |
| 237 |
|
| 238 |
std::string as_string () const; |
| 239 |
|
| 240 |
void on_param_subst (const ParamType &p, const SubstitutionArg &a) const; |
| 241 |
|
| 242 |
ParamSubstCb get_subst_cb () const; |
| 243 |
|
| 244 |
bool trait_item_mode () const; |
| 245 |
|
| 246 |
private: |
| 247 |
std::vector<SubstitutionArg> mappings; |
| 248 |
std::map<std::string, BaseType *> binding_args; |
| 249 |
RegionParamList regions; |
| 250 |
location_t locus; |
| 251 |
ParamSubstCb param_subst_cb; |
| 252 |
bool trait_item_flag; |
| 253 |
bool error_flag; |
| 254 |
}; |
| 255 |
|
| 256 |
class TypeBoundPredicateItem; |
| 257 |
class SubstitutionRef |
| 258 |
{ |
| 259 |
public: |
| 260 |
SubstitutionRef (std::vector<SubstitutionParamMapping> substitutions, |
| 261 |
SubstitutionArgumentMappings arguments, |
| 262 |
RegionConstraints region_constraints); |
| 263 |
|
| 264 |
bool has_substitutions () const; |
| 265 |
|
| 266 |
std::string subst_as_string () const; |
| 267 |
|
| 268 |
bool supports_associated_bindings () const; |
| 269 |
|
| 270 |
// this is overridden in TypeBoundPredicate |
| 271 |
// which support bindings we don't add them directly to the SubstitutionRef |
| 272 |
// base class because this class represents the fn<X: Foo, Y: Bar>. The only |
| 273 |
// construct which supports associated types |
| 274 |
virtual size_t get_num_associated_bindings () const; |
| 275 |
|
| 276 |
// this is overridden in TypeBoundPredicate |
| 277 |
virtual TypeBoundPredicateItem |
| 278 |
lookup_associated_type (const std::string &search); |
| 279 |
|
| 280 |
size_t get_num_substitutions () const; |
| 281 |
|
| 282 |
size_t get_num_lifetime_params () const; |
| 283 |
|
| 284 |
size_t get_num_type_params () const; |
| 285 |
|
| 286 |
std::vector<SubstitutionParamMapping> &get_substs (); |
| 287 |
|
| 288 |
const std::vector<SubstitutionParamMapping> &get_substs () const; |
| 289 |
|
| 290 |
std::vector<SubstitutionParamMapping> clone_substs () const; |
| 291 |
|
| 292 |
void override_context (); |
| 293 |
|
| 294 |
bool needs_substitution () const; |
| 295 |
|
| 296 |
bool was_substituted () const; |
| 297 |
|
| 298 |
SubstitutionArgumentMappings &get_substitution_arguments (); |
| 299 |
const SubstitutionArgumentMappings &get_substitution_arguments () const; |
| 300 |
|
| 301 |
// this is the count of type params that are not substituted fuly |
| 302 |
size_t num_required_substitutions () const; |
| 303 |
|
| 304 |
// this is the count of type params that need substituted taking into account |
| 305 |
// possible defaults |
| 306 |
size_t min_required_substitutions () const; |
| 307 |
|
| 308 |
// We are trying to subst <i32, f32> into Struct Foo<X,Y> {} |
| 309 |
// in the case of Foo<i32,f32>{...} |
| 310 |
// |
| 311 |
// the substitions we have here define X,Y but the arguments have no bindings |
| 312 |
// so its a matter of ordering |
| 313 |
SubstitutionArgumentMappings |
| 314 |
get_mappings_from_generic_args (HIR::GenericArgs &args, |
| 315 |
const std::vector<Region> ®ions); |
| 316 |
|
| 317 |
// Recursive substitutions |
| 318 |
// Foo <A,B> { a:A, b: B}; Bar <X,Y,Z>{a:X, b: Foo<Y,Z>} |
| 319 |
// |
| 320 |
// we have bindings for X Y Z and need to propagate the binding Y,Z into Foo |
| 321 |
// Which binds to A,B |
| 322 |
SubstitutionArgumentMappings |
| 323 |
adjust_mappings_for_this (SubstitutionArgumentMappings &mappings, |
| 324 |
bool trait_mode = false); |
| 325 |
|
| 326 |
// Are the mappings here actually bound to this type. For example imagine the |
| 327 |
// case: |
| 328 |
// |
| 329 |
// struct Foo<T>(T); |
| 330 |
// impl<T> Foo<T> { |
| 331 |
// fn test(self) { ... } |
| 332 |
// } |
| 333 |
// |
| 334 |
// In this case we have a generic ADT of Foo and an impl block of a generic T |
| 335 |
// on Foo for the Self type. When we it comes to path resolution we can have: |
| 336 |
// |
| 337 |
// Foo::<i32>::test() |
| 338 |
// |
| 339 |
// This means the first segment of Foo::<i32> returns the ADT Foo<i32> not the |
| 340 |
// Self ADT bound to the T from the impl block. This means when it comes to |
| 341 |
// the next segment of test which resolves to the function we need to check |
| 342 |
// wether the arguments in the struct definition of foo can be bound here |
| 343 |
// before substituting the previous segments type here. This functions acts as |
| 344 |
// a guard for the solve_mappings_from_receiver_for_self to handle the case |
| 345 |
// where arguments are not bound. This is important for this next case: |
| 346 |
// |
| 347 |
// struct Baz<A, B>(A, B); |
| 348 |
// impl Baz<i32, f32> { |
| 349 |
// fn test<X>(a: X) -> X { |
| 350 |
// a |
| 351 |
// } |
| 352 |
// } |
| 353 |
// |
| 354 |
// In this case Baz has been already substituted for the impl's Self to become |
| 355 |
// ADT<i32, f32> so that the function test only has 1 generic argument of X. |
| 356 |
// The path for this will be: |
| 357 |
// |
| 358 |
// Baz::test::<_>(123) |
| 359 |
// |
| 360 |
// So the first segment here will be Baz<_, _> to try and infer the arguments |
| 361 |
// which will be taken from the impl's Self type in this case since it is |
| 362 |
// already substituted and like the previous case the check to see if we need |
| 363 |
// to inherit the previous segments generic arguments takes place but the |
| 364 |
// generic arguments are not bound to this type as they have already been |
| 365 |
// substituted. |
| 366 |
// |
| 367 |
// Its important to remember from the first example the FnType actually looks |
| 368 |
// like: |
| 369 |
// |
| 370 |
// fn <T>test(self :Foo<T>(T)) |
| 371 |
// |
| 372 |
// As the generic parameters are "bound" to each of the items in the impl |
| 373 |
// block. So this check is about wether the arguments we have here can |
| 374 |
// actually be bound to this type. |
| 375 |
bool are_mappings_bound (SubstitutionArgumentMappings &mappings); |
| 376 |
|
| 377 |
// struct Foo<A, B>(A, B); |
| 378 |
// |
| 379 |
// impl<T> Foo<T, f32>; |
| 380 |
// -> fn test<X>(self, a: X) -> X |
| 381 |
// |
| 382 |
// We might invoke this via: |
| 383 |
// |
| 384 |
// a = Foo(123, 456f32); |
| 385 |
// b = a.test::<bool>(false); |
| 386 |
// |
| 387 |
// we need to figure out relevant generic arguemts for self to apply to the |
| 388 |
// fntype |
| 389 |
SubstitutionArgumentMappings solve_mappings_from_receiver_for_self ( |
| 390 |
SubstitutionArgumentMappings &mappings) const; |
| 391 |
|
| 392 |
// Given a type such as: |
| 393 |
// |
| 394 |
// fn<X,Y>(a:&X, b:Y) -> (...) |
| 395 |
// |
| 396 |
// This function will inject implicit inference variables for the type |
| 397 |
// parameters X and Y |
| 398 |
BaseType *infer_substitions (location_t locus); |
| 399 |
|
| 400 |
// this clears any possible projections from higher ranked trait bounds which |
| 401 |
// could be hanging around from a previous resolution |
| 402 |
void prepare_higher_ranked_bounds (); |
| 403 |
|
| 404 |
// FIXME |
| 405 |
// this is bad name for this, i think it should be something like |
| 406 |
// compute-higher-ranked-bounds |
| 407 |
bool monomorphize (); |
| 408 |
|
| 409 |
// TODO comment |
| 410 |
virtual BaseType *handle_substitions (SubstitutionArgumentMappings &mappings) |
| 411 |
= 0; |
| 412 |
|
| 413 |
WARN_UNUSED_RESULT const SubstitutionArgumentMappings & |
| 414 |
get_used_arguments () const; |
| 415 |
|
| 416 |
WARN_UNUSED_RESULT tl::optional<SubstitutionArg> get_arg_at (size_t i) const; |
| 417 |
|
| 418 |
const RegionConstraints &get_region_constraints () const; |
| 419 |
|
| 420 |
protected: |
| 421 |
std::vector<SubstitutionParamMapping> substitutions; |
| 422 |
SubstitutionArgumentMappings used_arguments; |
| 423 |
RegionConstraints region_constraints; |
| 424 |
}; |
| 425 |
|
| 426 |
} // namespace TyTy |
| 427 |
} // namespace Rust |
| 428 |
#endif // RUST_TYTY_SUBST_H |