https://gcc.gnu.org/g:839e42810abb365b1fdb5690620c9a5be86f1b65
commit r16-2984-g839e42810abb365b1fdb5690620c9a5be86f1b65 Author: Philip Herron <herron.phi...@googlemail.com> Date: Fri Jul 25 17:49:15 2025 +0100 gccrs: Refactor the ParamType to a BaseGeneric base-type In order to support const generics we need to abstract away some of the specific ParamType to a BaseGeneric type so we can easily reuse our existing substitution bits for const generics which will mean creating a TyTy::ConstType to wrap up the gcc tree but also represent a const inference and the const decl. gcc/rust/ChangeLog: * backend/rust-compile-intrinsic.cc (sizeof_handler): refactor types (op_with_overflow_inner): likewise (uninit_handler): likewise (move_val_init_handler): likewise * backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile): likewise * typecheck/rust-hir-trait-resolve.cc: likewise * typecheck/rust-hir-type-check-base.cc: likewise * typecheck/rust-hir-type-check-item.cc: likewise * typecheck/rust-tyty-bounds.cc (TypeBoundPredicate::is_equal): likewise * typecheck/rust-tyty-subst.cc (SubstitutionParamMapping::get_param_ty): likewise (SubstitutionArg::get_param_mapping): likewise (SubstitutionRef::prepare_higher_ranked_bounds): likewise (SubstitutionRef::monomorphize): likewise * typecheck/rust-tyty-subst.h (class BaseGeneric): new generic base * typecheck/rust-tyty.cc (VariantDef::clone): likewise (VariantDef::monomorphized_clone): refactor (ADTType::is_equal): likewise (FnType::is_equal): likewise (ParamType::ParamType): likewise * typecheck/rust-tyty.h (class ParamType): likewise (class BaseGeneric): new base class impl Signed-off-by: Philip Herron <herron.phi...@googlemail.com> Diff: --- gcc/rust/backend/rust-compile-intrinsic.cc | 16 ++++++------- gcc/rust/backend/rust-compile-resolve-path.cc | 2 +- gcc/rust/typecheck/rust-hir-trait-resolve.cc | 12 +++++----- gcc/rust/typecheck/rust-hir-type-check-base.cc | 13 ++++++++--- gcc/rust/typecheck/rust-hir-type-check-item.cc | 4 ++-- gcc/rust/typecheck/rust-tyty-bounds.cc | 6 ++--- gcc/rust/typecheck/rust-tyty-subst.cc | 15 ++++++------- gcc/rust/typecheck/rust-tyty-subst.h | 11 ++++----- gcc/rust/typecheck/rust-tyty.cc | 28 +++++++++++------------ gcc/rust/typecheck/rust-tyty.h | 31 +++++++++++++++++++++----- 10 files changed, 82 insertions(+), 56 deletions(-) diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc index 7627620c4713..450a86979208 100644 --- a/gcc/rust/backend/rust-compile-intrinsic.cc +++ b/gcc/rust/backend/rust-compile-intrinsic.cc @@ -437,8 +437,8 @@ sizeof_handler (Context *ctx, TyTy::FnType *fntype) // get the template parameter type tree fn size_of<T>(); rust_assert (fntype->get_num_substitutions () == 1); auto ¶m_mapping = fntype->get_substs ().at (0); - const TyTy::ParamType *param_tyty = param_mapping.get_param_ty (); - TyTy::BaseType *resolved_tyty = param_tyty->resolve (); + const auto param_tyty = param_mapping.get_param_ty (); + auto resolved_tyty = param_tyty->resolve (); tree template_parameter_type = TyTyResolveCompile::compile (ctx, resolved_tyty); @@ -643,8 +643,8 @@ op_with_overflow_inner (Context *ctx, TyTy::FnType *fntype, tree_code op) rust_assert (fntype->get_num_substitutions () == 1); auto ¶m_mapping = fntype->get_substs ().at (0); - const TyTy::ParamType *param_tyty = param_mapping.get_param_ty (); - TyTy::BaseType *resolved_tyty = param_tyty->resolve (); + const auto param_tyty = param_mapping.get_param_ty (); + auto resolved_tyty = param_tyty->resolve (); tree template_parameter_type = TyTyResolveCompile::compile (ctx, resolved_tyty); @@ -1079,8 +1079,8 @@ uninit_handler (Context *ctx, TyTy::FnType *fntype) // get the template parameter type tree fn uninit<T>(); rust_assert (fntype->get_num_substitutions () == 1); auto ¶m_mapping = fntype->get_substs ().at (0); - const TyTy::ParamType *param_tyty = param_mapping.get_param_ty (); - TyTy::BaseType *resolved_tyty = param_tyty->resolve (); + const auto param_tyty = param_mapping.get_param_ty (); + auto resolved_tyty = param_tyty->resolve (); tree template_parameter_type = TyTyResolveCompile::compile (ctx, resolved_tyty); @@ -1144,8 +1144,8 @@ move_val_init_handler (Context *ctx, TyTy::FnType *fntype) // get the template parameter type tree fn size_of<T>(); rust_assert (fntype->get_num_substitutions () == 1); auto ¶m_mapping = fntype->get_substs ().at (0); - const TyTy::ParamType *param_tyty = param_mapping.get_param_ty (); - TyTy::BaseType *resolved_tyty = param_tyty->resolve (); + auto param_tyty = param_mapping.get_param_ty (); + auto resolved_tyty = param_tyty->resolve (); tree template_parameter_type = TyTyResolveCompile::compile (ctx, resolved_tyty); diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index be2af64fc38a..f3b9dc28db21 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -330,7 +330,7 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup, auto fn = lookup->as<TyTy::FnType> (); rust_assert (fn->get_num_type_params () > 0); TyTy::SubstitutionParamMapping &self = fn->get_substs ().at (0); - TyTy::ParamType *receiver = self.get_param_ty (); + TyTy::BaseGeneric *receiver = self.get_param_ty (); TyTy::BaseType *r = receiver; if (!receiver->can_resolve ()) { diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index e2e9dbf83b03..0fd0147b45f3 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -584,8 +584,8 @@ AssociatedImplTrait::setup_associated_types ( } else { - TyTy::ParamType *param = p.get_param_ty (); - TyTy::BaseType *resolved = param->destructure (); + auto param = p.get_param_ty (); + auto resolved = param->destructure (); subst_args.push_back (TyTy::SubstitutionArg (&p, resolved)); param_mappings[param->get_symbol ()] = resolved->get_ref (); } @@ -613,8 +613,8 @@ AssociatedImplTrait::setup_associated_types ( if (i == 0) continue; - const TyTy::ParamType *p = arg.get_param_ty (); - TyTy::BaseType *r = p->resolve (); + const auto p = arg.get_param_ty (); + auto r = p->resolve (); if (!r->is_concrete ()) { r = SubstMapperInternal::Resolve (r, infer_arguments); @@ -630,8 +630,8 @@ AssociatedImplTrait::setup_associated_types ( if (i == 0) continue; - const TyTy::ParamType *p = arg.get_param_ty (); - TyTy::BaseType *r = p->resolve (); + const auto p = arg.get_param_ty (); + auto r = p->resolve (); if (!r->is_concrete ()) { r = SubstMapperInternal::Resolve (r, infer_arguments); diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc index acc331620ba5..cbb85bd4f4f3 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-base.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc @@ -23,6 +23,7 @@ #include "rust-hir-trait-resolve.h" #include "rust-type-util.h" #include "rust-attribute-values.h" +#include "rust-tyty.h" namespace Rust { namespace Resolver { @@ -486,6 +487,10 @@ TypeCheckBase::resolve_generic_params ( context->insert_type (generic_param->get_mappings (), specified_type); + + // TODO for const generics + // TyTy::SubstitutionParamMapping p (*generic_param, param_type); + // substitutions.push_back (p); } break; @@ -501,8 +506,7 @@ TypeCheckBase::resolve_generic_params ( *generic_param, false /*resolve_trait_bounds*/); context->insert_type (generic_param->get_mappings (), param_type); - auto ¶m = static_cast<HIR::TypeParam &> (*generic_param); - TyTy::SubstitutionParamMapping p (param, param_type); + TyTy::SubstitutionParamMapping p (*generic_param, param_type); substitutions.push_back (p); } break; @@ -517,7 +521,10 @@ TypeCheckBase::resolve_generic_params ( continue; auto &type_param = static_cast<HIR::TypeParam &> (generic); - auto pty = subst.get_param_ty (); + auto bpty = subst.get_param_ty (); + rust_assert (bpty->get_kind () == TyTy::TypeKind::PARAM); + auto pty = static_cast<TyTy::ParamType *> (bpty); + TypeResolveGenericParam::ApplyAnyTraitBounds (type_param, pty); } } diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc index e86525a68e55..f104a1a56e81 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc @@ -118,8 +118,8 @@ TypeCheckItem::ResolveImplBlockSelfWithInference ( } else { - TyTy::ParamType *param = p.get_param_ty (); - TyTy::BaseType *resolved = param->destructure (); + auto param = p.get_param_ty (); + auto resolved = param->destructure (); args.push_back (TyTy::SubstitutionArg (&p, resolved)); } } diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc b/gcc/rust/typecheck/rust-tyty-bounds.cc index c34d3628895a..3ae4effa6af4 100644 --- a/gcc/rust/typecheck/rust-tyty-bounds.cc +++ b/gcc/rust/typecheck/rust-tyty-bounds.cc @@ -721,7 +721,7 @@ TypeBoundPredicate::handle_substitions ( if (sub.get_param_ty () == nullptr) continue; - ParamType *p = sub.get_param_ty (); + auto p = sub.get_param_ty (); BaseType *r = p->resolve (); BaseType *s = Resolver::SubstMapperInternal::Resolve (r, subst_mappings); @@ -851,8 +851,8 @@ TypeBoundPredicate::is_equal (const TypeBoundPredicate &other) const const SubstitutionParamMapping &a = substitutions.at (i); const SubstitutionParamMapping &b = other.substitutions.at (i); - const ParamType *ap = a.get_param_ty (); - const ParamType *bp = b.get_param_ty (); + const auto ap = a.get_param_ty (); + const auto bp = b.get_param_ty (); const BaseType *apd = ap->destructure (); const BaseType *bpd = bp->destructure (); diff --git a/gcc/rust/typecheck/rust-tyty-subst.cc b/gcc/rust/typecheck/rust-tyty-subst.cc index 13aca42182f2..01899d83435f 100644 --- a/gcc/rust/typecheck/rust-tyty-subst.cc +++ b/gcc/rust/typecheck/rust-tyty-subst.cc @@ -54,13 +54,13 @@ SubstitutionParamMapping::clone () const static_cast<ParamType *> (param->clone ())); } -ParamType * +BaseGeneric * SubstitutionParamMapping::get_param_ty () { return param; } -const ParamType * +const BaseGeneric * SubstitutionParamMapping::get_param_ty () const { return param; @@ -229,7 +229,7 @@ SubstitutionArg::get_param_mapping () const return param; } -const ParamType * +const BaseGeneric * SubstitutionArg::get_param_ty () const { return original_param; @@ -334,11 +334,11 @@ SubstitutionArgumentMappings::is_error () const bool SubstitutionArgumentMappings::get_argument_for_symbol ( - const ParamType *param_to_find, SubstitutionArg *argument) const + const BaseGeneric *param_to_find, SubstitutionArg *argument) const { for (const auto &mapping : mappings) { - const ParamType *p = mapping.get_param_ty (); + const auto *p = mapping.get_param_ty (); if (p->get_symbol () == param_to_find->get_symbol ()) { *argument = mapping; @@ -951,7 +951,7 @@ SubstitutionRef::prepare_higher_ranked_bounds () { for (const auto &subst : get_substs ()) { - const TyTy::ParamType *pty = subst.get_param_ty (); + const auto pty = subst.get_param_ty (); for (const auto &bound : pty->get_specified_bounds ()) { const auto ref = bound.get (); @@ -965,8 +965,7 @@ SubstitutionRef::monomorphize () { for (const auto &subst : get_substs ()) { - const TyTy::ParamType *pty = subst.get_param_ty (); - + const auto pty = subst.get_param_ty (); if (!pty->can_resolve ()) continue; diff --git a/gcc/rust/typecheck/rust-tyty-subst.h b/gcc/rust/typecheck/rust-tyty-subst.h index f78242499eaf..0ac3ea73807d 100644 --- a/gcc/rust/typecheck/rust-tyty-subst.h +++ b/gcc/rust/typecheck/rust-tyty-subst.h @@ -31,6 +31,7 @@ namespace Rust { namespace TyTy { class ParamType; +class BaseGeneric; struct RegionConstraints { @@ -56,8 +57,8 @@ public: SubstitutionParamMapping clone () const; - ParamType *get_param_ty (); - const ParamType *get_param_ty () const; + BaseGeneric *get_param_ty (); + const BaseGeneric *get_param_ty () const; HIR::GenericParam &get_generic_param (); const HIR::GenericParam &get_generic_param () const; @@ -156,7 +157,7 @@ public: const SubstitutionParamMapping *get_param_mapping () const; - const ParamType *get_param_ty () const; + const BaseGeneric *get_param_ty () const; static SubstitutionArg error (); @@ -168,7 +169,7 @@ public: private: const SubstitutionParamMapping *param; - const ParamType *original_param; + const BaseGeneric *original_param; BaseType *argument; }; @@ -208,7 +209,7 @@ public: bool is_error () const; - bool get_argument_for_symbol (const ParamType *param_to_find, + bool get_argument_for_symbol (const BaseGeneric *param_to_find, SubstitutionArg *argument) const; /** Return type parameter index for symbol */ diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 4b4117bdb8dd..64207e9d7ea7 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -1690,7 +1690,7 @@ VariantDef::clone () const auto &&discriminant_opt = has_discriminant () ? tl::optional<std::unique_ptr<HIR::Expr>> ( - get_discriminant ().clone_expr ()) + get_discriminant ().clone_expr ()) : tl::nullopt; return new VariantDef (id, defid, identifier, ident, type, @@ -1706,7 +1706,7 @@ VariantDef::monomorphized_clone () const auto discriminant_opt = has_discriminant () ? tl::optional<std::unique_ptr<HIR::Expr>> ( - get_discriminant ().clone_expr ()) + get_discriminant ().clone_expr ()) : tl::nullopt; return new VariantDef (id, defid, identifier, ident, type, @@ -1818,8 +1818,8 @@ ADTType::is_equal (const BaseType &other) const const SubstitutionParamMapping &a = substitutions.at (i); const SubstitutionParamMapping &b = other2->substitutions.at (i); - const ParamType *aa = a.get_param_ty (); - const ParamType *bb = b.get_param_ty (); + const auto &aa = a.get_param_ty (); + const auto &bb = b.get_param_ty (); if (!aa->is_equal (*bb)) return false; } @@ -2145,8 +2145,8 @@ FnType::is_equal (const BaseType &other) const const SubstitutionParamMapping &a = get_substs ().at (i); const SubstitutionParamMapping &b = ofn.get_substs ().at (i); - const ParamType *pa = a.get_param_ty (); - const ParamType *pb = b.get_param_ty (); + const auto *pa = a.get_param_ty (); + const auto *pb = b.get_param_ty (); if (!pa->is_equal (*pb)) return false; } @@ -3409,10 +3409,10 @@ ParamType::ParamType (std::string symbol, location_t locus, HirId ref, HIR::GenericParam ¶m, std::vector<TypeBoundPredicate> specified_bounds, std::set<HirId> refs) - : BaseType (ref, ref, KIND, - {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), - locus}, - specified_bounds, refs), + : BaseGeneric (ref, ref, KIND, + {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), + locus}, + specified_bounds, refs), is_trait_self (false), symbol (symbol), param (param) {} @@ -3420,10 +3420,10 @@ ParamType::ParamType (bool is_trait_self, std::string symbol, location_t locus, HirId ref, HirId ty_ref, HIR::GenericParam ¶m, std::vector<TypeBoundPredicate> specified_bounds, std::set<HirId> refs) - : BaseType (ref, ty_ref, KIND, - {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), - locus}, - specified_bounds, refs), + : BaseGeneric (ref, ty_ref, KIND, + {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), + locus}, + specified_bounds, refs), is_trait_self (is_trait_self), symbol (symbol), param (param) {} diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 13e9184f29d7..381bbeb835d7 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -365,7 +365,26 @@ public: std::string get_name () const override final; }; -class ParamType : public BaseType +class BaseGeneric : public BaseType +{ +public: + virtual std::string get_symbol () const = 0; + + virtual HIR::GenericParam &get_generic_param () = 0; + + virtual bool can_resolve () const = 0; + + virtual BaseType *resolve () const = 0; + +protected: + BaseGeneric (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident, + std::vector<TypeBoundPredicate> specified_bounds, + std::set<HirId> refs = std::set<HirId> ()) + : BaseType (ref, ty_ref, kind, ident, specified_bounds, refs) + {} +}; + +class ParamType : public BaseGeneric { public: static constexpr auto KIND = TypeKind::PARAM; @@ -389,13 +408,13 @@ public: BaseType *clone () const final override; - std::string get_symbol () const; + std::string get_symbol () const override final; - HIR::GenericParam &get_generic_param (); + HIR::GenericParam &get_generic_param () override final; - bool can_resolve () const; + bool can_resolve () const override final; - BaseType *resolve () const; + BaseType *resolve () const override final; std::string get_name () const override final; @@ -523,7 +542,7 @@ public: TypeBoundPredicate (const TypeBoundPredicate &other); - virtual ~TypeBoundPredicate (){}; + virtual ~TypeBoundPredicate () {}; TypeBoundPredicate &operator= (const TypeBoundPredicate &other);