| 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 |
#include "rust-substitution-mapper.h" |
| 20 |
#include "rust-hir-type-check.h" |
| 21 |
|
| 22 |
namespace Rust { |
| 23 |
namespace Resolver { |
| 24 |
|
| 25 |
SubstMapper::SubstMapper (HirId ref, HIR::GenericArgs *generics, |
| 26 |
const std::vector<TyTy::Region> ®ions, |
| 27 |
location_t locus) |
| 28 |
: resolved (new TyTy::ErrorType (ref)), generics (generics), |
| 29 |
regions (regions), locus (locus) |
| 30 |
{} |
| 31 |
|
| 32 |
TyTy::BaseType * |
| 33 |
SubstMapper::Resolve (TyTy::BaseType *base, location_t locus, |
| 34 |
HIR::GenericArgs *generics, |
| 35 |
const std::vector<TyTy::Region> ®ions) |
| 36 |
{ |
| 37 |
if (!valid_type (base)) |
| 38 |
{ |
| 39 |
rich_location r (line_table, locus); |
| 40 |
r.add_fixit_remove (generics->get_locus ()); |
| 41 |
rust_error_at (r, ErrorCode::E0109, |
| 42 |
"generic arguments are not allowed for this type"); |
| 43 |
return base; |
| 44 |
} |
| 45 |
|
| 46 |
SubstMapper mapper (base->get_ref (), generics, regions, locus); |
| 47 |
base->accept_vis (mapper); |
| 48 |
rust_assert (mapper.resolved != nullptr); |
| 49 |
return mapper.resolved; |
| 50 |
} |
| 51 |
|
| 52 |
TyTy::BaseType * |
| 53 |
SubstMapper::InferSubst (TyTy::BaseType *base, location_t locus) |
| 54 |
{ |
| 55 |
return SubstMapper::Resolve (base, locus, nullptr, {}); |
| 56 |
} |
| 57 |
|
| 58 |
bool |
| 59 |
SubstMapper::valid_type (TyTy::BaseType *base) |
| 60 |
{ |
| 61 |
bool is_fn = base->is<TyTy::FnType> (); |
| 62 |
bool is_adt = base->is<TyTy::ADTType> (); |
| 63 |
bool is_placeholder = base->is<TyTy::PlaceholderType> (); |
| 64 |
bool is_projection = base->is<TyTy::ProjectionType> (); |
| 65 |
|
| 66 |
return is_fn || is_adt || is_placeholder || is_projection; |
| 67 |
} |
| 68 |
|
| 69 |
bool |
| 70 |
SubstMapper::have_generic_args () const |
| 71 |
{ |
| 72 |
return generics != nullptr; |
| 73 |
} |
| 74 |
|
| 75 |
void |
| 76 |
SubstMapper::visit (TyTy::FnType &type) |
| 77 |
{ |
| 78 |
TyTy::FnType *concrete = nullptr; |
| 79 |
if (!have_generic_args ()) |
| 80 |
{ |
| 81 |
TyTy::BaseType *substs = type.infer_substitions (locus); |
| 82 |
rust_assert (substs->get_kind () == TyTy::TypeKind::FNDEF); |
| 83 |
concrete = static_cast<TyTy::FnType *> (substs); |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
TyTy::SubstitutionArgumentMappings mappings |
| 88 |
= type.get_mappings_from_generic_args (*generics, regions); |
| 89 |
if (mappings.is_error ()) |
| 90 |
return; |
| 91 |
|
| 92 |
concrete = type.handle_substitions (mappings); |
| 93 |
} |
| 94 |
|
| 95 |
if (concrete != nullptr) |
| 96 |
resolved = concrete; |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
SubstMapper::visit (TyTy::ADTType &type) |
| 101 |
{ |
| 102 |
TyTy::ADTType *concrete = nullptr; |
| 103 |
if (!have_generic_args ()) |
| 104 |
{ |
| 105 |
TyTy::BaseType *substs = type.infer_substitions (locus); |
| 106 |
rust_assert (substs->get_kind () == TyTy::TypeKind::ADT); |
| 107 |
concrete = static_cast<TyTy::ADTType *> (substs); |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
TyTy::SubstitutionArgumentMappings mappings |
| 112 |
= type.get_mappings_from_generic_args (*generics, regions); |
| 113 |
if (mappings.is_error ()) |
| 114 |
return; |
| 115 |
|
| 116 |
concrete = type.handle_substitions (mappings); |
| 117 |
} |
| 118 |
|
| 119 |
if (concrete != nullptr) |
| 120 |
resolved = concrete; |
| 121 |
} |
| 122 |
|
| 123 |
void |
| 124 |
SubstMapper::visit (TyTy::PlaceholderType &type) |
| 125 |
{ |
| 126 |
if (!type.can_resolve ()) |
| 127 |
{ |
| 128 |
resolved = &type; |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
resolved = SubstMapper::Resolve (type.resolve (), locus, generics, regions); |
| 133 |
} |
| 134 |
|
| 135 |
void |
| 136 |
SubstMapper::visit (TyTy::ProjectionType &type) |
| 137 |
{ |
| 138 |
TyTy::ProjectionType *concrete = nullptr; |
| 139 |
if (!have_generic_args ()) |
| 140 |
{ |
| 141 |
TyTy::BaseType *substs = type.infer_substitions (locus); |
| 142 |
rust_assert (substs->get_kind () == TyTy::TypeKind::PROJECTION); |
| 143 |
concrete = static_cast<TyTy::ProjectionType *> (substs); |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
TyTy::SubstitutionArgumentMappings mappings |
| 148 |
= type.get_mappings_from_generic_args (*generics, regions); |
| 149 |
if (mappings.is_error ()) |
| 150 |
return; |
| 151 |
|
| 152 |
concrete = type.handle_substitions (mappings); |
| 153 |
} |
| 154 |
|
| 155 |
if (concrete != nullptr) |
| 156 |
resolved = concrete; |
| 157 |
} |
| 158 |
|
| 159 |
SubstMapperInternal::SubstMapperInternal ( |
| 160 |
HirId ref, TyTy::SubstitutionArgumentMappings &mappings) |
| 161 |
: resolved (new TyTy::ErrorType (ref)), mappings (mappings) |
| 162 |
{} |
| 163 |
|
| 164 |
TyTy::BaseType * |
| 165 |
SubstMapperInternal::Resolve (TyTy::BaseType *base, |
| 166 |
TyTy::SubstitutionArgumentMappings &mappings) |
| 167 |
{ |
| 168 |
auto context = TypeCheckContext::get (); |
| 169 |
|
| 170 |
SubstMapperInternal mapper (base->get_ref (), mappings); |
| 171 |
base->accept_vis (mapper); |
| 172 |
rust_assert (mapper.resolved != nullptr); |
| 173 |
|
| 174 |
// insert these new implict types into the context |
| 175 |
TyTy::BaseType *unused = nullptr; |
| 176 |
bool is_ty_available |
| 177 |
= context->lookup_type (mapper.resolved->get_ty_ref (), &unused); |
| 178 |
if (!is_ty_available) |
| 179 |
{ |
| 180 |
context->insert_type ( |
| 181 |
Analysis::NodeMapping (0, 0, mapper.resolved->get_ty_ref (), 0), |
| 182 |
mapper.resolved); |
| 183 |
} |
| 184 |
bool is_ref_available |
| 185 |
= context->lookup_type (mapper.resolved->get_ref (), &unused); |
| 186 |
if (!is_ref_available) |
| 187 |
{ |
| 188 |
context->insert_type (Analysis::NodeMapping (0, 0, |
| 189 |
mapper.resolved->get_ref (), |
| 190 |
0), |
| 191 |
mapper.resolved); |
| 192 |
} |
| 193 |
|
| 194 |
return mapper.resolved; |
| 195 |
} |
| 196 |
|
| 197 |
bool |
| 198 |
SubstMapperInternal::mappings_are_bound ( |
| 199 |
TyTy::BaseType *tyseg, TyTy::SubstitutionArgumentMappings &mappings) |
| 200 |
{ |
| 201 |
if (tyseg->get_kind () == TyTy::TypeKind::ADT) |
| 202 |
{ |
| 203 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (tyseg); |
| 204 |
return adt->are_mappings_bound (mappings); |
| 205 |
} |
| 206 |
else if (tyseg->get_kind () == TyTy::TypeKind::FNDEF) |
| 207 |
{ |
| 208 |
TyTy::FnType *fn = static_cast<TyTy::FnType *> (tyseg); |
| 209 |
return fn->are_mappings_bound (mappings); |
| 210 |
} |
| 211 |
|
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
void |
| 216 |
SubstMapperInternal::visit (TyTy::FnType &type) |
| 217 |
{ |
| 218 |
TyTy::SubstitutionArgumentMappings adjusted |
| 219 |
= type.adjust_mappings_for_this (mappings); |
| 220 |
if (adjusted.is_error () && !mappings.trait_item_mode ()) |
| 221 |
return; |
| 222 |
if (adjusted.is_error () && mappings.trait_item_mode ()) |
| 223 |
adjusted = mappings; |
| 224 |
|
| 225 |
TyTy::BaseType *concrete = type.handle_substitions (adjusted); |
| 226 |
if (concrete != nullptr) |
| 227 |
resolved = concrete; |
| 228 |
} |
| 229 |
|
| 230 |
void |
| 231 |
SubstMapperInternal::visit (TyTy::ADTType &type) |
| 232 |
{ |
| 233 |
TyTy::SubstitutionArgumentMappings adjusted |
| 234 |
= type.adjust_mappings_for_this (mappings); |
| 235 |
if (adjusted.is_error () && !mappings.trait_item_mode ()) |
| 236 |
return; |
| 237 |
if (adjusted.is_error () && mappings.trait_item_mode ()) |
| 238 |
adjusted = mappings; |
| 239 |
|
| 240 |
TyTy::BaseType *concrete = type.handle_substitions (adjusted); |
| 241 |
if (concrete != nullptr) |
| 242 |
resolved = concrete; |
| 243 |
} |
| 244 |
|
| 245 |
// these don't support generic arguments but might contain a type param |
| 246 |
void |
| 247 |
SubstMapperInternal::visit (TyTy::TupleType &type) |
| 248 |
{ |
| 249 |
resolved = type.handle_substitions (mappings); |
| 250 |
} |
| 251 |
|
| 252 |
void |
| 253 |
SubstMapperInternal::visit (TyTy::ReferenceType &type) |
| 254 |
{ |
| 255 |
resolved = type.handle_substitions (mappings); |
| 256 |
} |
| 257 |
|
| 258 |
void |
| 259 |
SubstMapperInternal::visit (TyTy::PointerType &type) |
| 260 |
{ |
| 261 |
resolved = type.handle_substitions (mappings); |
| 262 |
} |
| 263 |
|
| 264 |
void |
| 265 |
SubstMapperInternal::visit (TyTy::ParamType &type) |
| 266 |
{ |
| 267 |
resolved = type.handle_substitions (mappings); |
| 268 |
} |
| 269 |
|
| 270 |
void |
| 271 |
SubstMapperInternal::visit (TyTy::PlaceholderType &type) |
| 272 |
{ |
| 273 |
rust_assert (type.can_resolve ()); |
| 274 |
if (mappings.trait_item_mode ()) |
| 275 |
{ |
| 276 |
resolved = type.resolve (); |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
resolved = SubstMapperInternal::Resolve (type.resolve (), mappings); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void |
| 285 |
SubstMapperInternal::visit (TyTy::ProjectionType &type) |
| 286 |
{ |
| 287 |
resolved = type.handle_substitions (mappings); |
| 288 |
} |
| 289 |
|
| 290 |
void |
| 291 |
SubstMapperInternal::visit (TyTy::ClosureType &type) |
| 292 |
{ |
| 293 |
resolved = type.handle_substitions (mappings); |
| 294 |
} |
| 295 |
|
| 296 |
void |
| 297 |
SubstMapperInternal::visit (TyTy::ArrayType &type) |
| 298 |
{ |
| 299 |
resolved = type.handle_substitions (mappings); |
| 300 |
} |
| 301 |
|
| 302 |
void |
| 303 |
SubstMapperInternal::visit (TyTy::SliceType &type) |
| 304 |
{ |
| 305 |
resolved = type.handle_substitions (mappings); |
| 306 |
} |
| 307 |
|
| 308 |
// nothing to do for these |
| 309 |
void |
| 310 |
SubstMapperInternal::visit (TyTy::InferType &type) |
| 311 |
{ |
| 312 |
resolved = type.clone (); |
| 313 |
} |
| 314 |
void |
| 315 |
SubstMapperInternal::visit (TyTy::FnPtr &type) |
| 316 |
{ |
| 317 |
resolved = type.clone (); |
| 318 |
} |
| 319 |
void |
| 320 |
SubstMapperInternal::visit (TyTy::BoolType &type) |
| 321 |
{ |
| 322 |
resolved = type.clone (); |
| 323 |
} |
| 324 |
void |
| 325 |
SubstMapperInternal::visit (TyTy::IntType &type) |
| 326 |
{ |
| 327 |
resolved = type.clone (); |
| 328 |
} |
| 329 |
void |
| 330 |
SubstMapperInternal::visit (TyTy::UintType &type) |
| 331 |
{ |
| 332 |
resolved = type.clone (); |
| 333 |
} |
| 334 |
void |
| 335 |
SubstMapperInternal::visit (TyTy::FloatType &type) |
| 336 |
{ |
| 337 |
resolved = type.clone (); |
| 338 |
} |
| 339 |
void |
| 340 |
SubstMapperInternal::visit (TyTy::USizeType &type) |
| 341 |
{ |
| 342 |
resolved = type.clone (); |
| 343 |
} |
| 344 |
void |
| 345 |
SubstMapperInternal::visit (TyTy::ISizeType &type) |
| 346 |
{ |
| 347 |
resolved = type.clone (); |
| 348 |
} |
| 349 |
void |
| 350 |
SubstMapperInternal::visit (TyTy::ErrorType &type) |
| 351 |
{ |
| 352 |
resolved = type.clone (); |
| 353 |
} |
| 354 |
void |
| 355 |
SubstMapperInternal::visit (TyTy::CharType &type) |
| 356 |
{ |
| 357 |
resolved = type.clone (); |
| 358 |
} |
| 359 |
void |
| 360 |
SubstMapperInternal::visit (TyTy::StrType &type) |
| 361 |
{ |
| 362 |
resolved = type.clone (); |
| 363 |
} |
| 364 |
void |
| 365 |
SubstMapperInternal::visit (TyTy::NeverType &type) |
| 366 |
{ |
| 367 |
resolved = type.clone (); |
| 368 |
} |
| 369 |
void |
| 370 |
SubstMapperInternal::visit (TyTy::DynamicObjectType &type) |
| 371 |
{ |
| 372 |
resolved = type.clone (); |
| 373 |
} |
| 374 |
void |
| 375 |
SubstMapperInternal::visit (TyTy::OpaqueType &type) |
| 376 |
{ |
| 377 |
resolved = type.handle_substitions (mappings); |
| 378 |
} |
| 379 |
|
| 380 |
// SubstMapperFromExisting |
| 381 |
|
| 382 |
SubstMapperFromExisting::SubstMapperFromExisting (TyTy::BaseType *concrete, |
| 383 |
TyTy::BaseType *receiver) |
| 384 |
: concrete (concrete), receiver (receiver), resolved (nullptr) |
| 385 |
{} |
| 386 |
|
| 387 |
TyTy::BaseType * |
| 388 |
SubstMapperFromExisting::Resolve (TyTy::BaseType *concrete, |
| 389 |
TyTy::BaseType *receiver) |
| 390 |
{ |
| 391 |
rust_assert (concrete->get_kind () == receiver->get_kind ()); |
| 392 |
|
| 393 |
SubstMapperFromExisting mapper (concrete, receiver); |
| 394 |
concrete->accept_vis (mapper); |
| 395 |
return mapper.resolved; |
| 396 |
} |
| 397 |
|
| 398 |
void |
| 399 |
SubstMapperFromExisting::visit (TyTy::FnType &type) |
| 400 |
{ |
| 401 |
rust_assert (type.was_substituted ()); |
| 402 |
|
| 403 |
TyTy::FnType *to_sub = static_cast<TyTy::FnType *> (receiver); |
| 404 |
resolved = to_sub->handle_substitions (type.get_substitution_arguments ()); |
| 405 |
} |
| 406 |
|
| 407 |
void |
| 408 |
SubstMapperFromExisting::visit (TyTy::ADTType &type) |
| 409 |
{ |
| 410 |
rust_assert (type.was_substituted ()); |
| 411 |
|
| 412 |
TyTy::ADTType *to_sub = static_cast<TyTy::ADTType *> (receiver); |
| 413 |
resolved = to_sub->handle_substitions (type.get_substitution_arguments ()); |
| 414 |
} |
| 415 |
|
| 416 |
void |
| 417 |
SubstMapperFromExisting::visit (TyTy::ClosureType &type) |
| 418 |
{ |
| 419 |
rust_assert (type.was_substituted ()); |
| 420 |
|
| 421 |
TyTy::ClosureType *to_sub = static_cast<TyTy::ClosureType *> (receiver); |
| 422 |
resolved = to_sub->handle_substitions (type.get_substitution_arguments ()); |
| 423 |
} |
| 424 |
|
| 425 |
// GetUsedSubstArgs |
| 426 |
|
| 427 |
GetUsedSubstArgs::GetUsedSubstArgs () |
| 428 |
: args (TyTy::SubstitutionArgumentMappings::error ()) |
| 429 |
{} |
| 430 |
|
| 431 |
TyTy::SubstitutionArgumentMappings |
| 432 |
GetUsedSubstArgs::From (const TyTy::BaseType *from) |
| 433 |
{ |
| 434 |
GetUsedSubstArgs mapper; |
| 435 |
from->accept_vis (mapper); |
| 436 |
return mapper.args; |
| 437 |
} |
| 438 |
|
| 439 |
void |
| 440 |
GetUsedSubstArgs::visit (const TyTy::FnType &type) |
| 441 |
{ |
| 442 |
args = type.get_substitution_arguments (); |
| 443 |
} |
| 444 |
|
| 445 |
void |
| 446 |
GetUsedSubstArgs::visit (const TyTy::ADTType &type) |
| 447 |
{ |
| 448 |
args = type.get_substitution_arguments (); |
| 449 |
} |
| 450 |
|
| 451 |
void |
| 452 |
GetUsedSubstArgs::visit (const TyTy::ClosureType &type) |
| 453 |
{ |
| 454 |
args = type.get_substitution_arguments (); |
| 455 |
} |
| 456 |
|
| 457 |
} // namespace Resolver |
| 458 |
} // namespace Rust |