From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 6473A3858CDA; Tue, 7 Feb 2023 17:55:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6473A3858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675792515; bh=n1eu0xxgiePv+NI2sVbWMb03a4TwN9x2N/8ghTHpR9k=; h=From:To:Subject:Date:From; b=Y/2JEnPGklrjjO+eaKGTBHcFarhEztwr8IM5zwQGcWM3gNVgyl7SFxHEhKRnpGEvI UrTWGSg2Tdm1YS+eYx2ibZx68k3DxVzA8QqMq2EfoZN0pFZRPswF0P1cOkYDNI55AT GXuv1Ua4zuFkzvymF0oG2gU3sXA2Zo5sSX54Qxzs= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] gccrs: Refactor all substitution mapper code implementation into its own CC file X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: fba98d61c34e349e0e4327a23b10732f4f4083fd X-Git-Newrev: 1cd74211cca9f5696c15d9f8b6ef4c2a2be7ea3a Message-Id: <20230207175515.6473A3858CDA@sourceware.org> Date: Tue, 7 Feb 2023 17:55:15 +0000 (GMT) List-Id: https://gcc.gnu.org/g:1cd74211cca9f5696c15d9f8b6ef4c2a2be7ea3a commit 1cd74211cca9f5696c15d9f8b6ef4c2a2be7ea3a Author: Philip Herron Date: Sat Jan 14 23:36:47 2023 +0000 gccrs: Refactor all substitution mapper code implementation into its own CC file Signed-off-by: Philip Herron gcc/rust/ChangeLog: * typecheck/rust-substitution-mapper.cc (SubstMapper::SubstMapper): refactor (SubstMapper::Resolve): likewise (SubstMapper::InferSubst): likewise (SubstMapper::have_generic_args): likewise (SubstMapper::visit): likewise (SubstMapperInternal::visit): likewise (SubstMapperFromExisting::SubstMapperFromExisting): likewise (SubstMapperFromExisting::Resolve): likewise (SubstMapperFromExisting::visit): likewise (GetUsedSubstArgs::GetUsedSubstArgs): likewise (GetUsedSubstArgs::From): likewise (GetUsedSubstArgs::visit): likewise * typecheck/rust-substitution-mapper.h: refactor * typecheck/rust-tyty-subst.cc (SubstitutionParamMapping::get_generic_param): likewise Diff: --- gcc/rust/typecheck/rust-substitution-mapper.cc | 343 +++++++++++++++++++++++++ gcc/rust/typecheck/rust-substitution-mapper.h | 286 ++++----------------- gcc/rust/typecheck/rust-tyty-subst.cc | 2 +- 3 files changed, 387 insertions(+), 244 deletions(-) diff --git a/gcc/rust/typecheck/rust-substitution-mapper.cc b/gcc/rust/typecheck/rust-substitution-mapper.cc index f80368a0339..9ccb80bf2a5 100644 --- a/gcc/rust/typecheck/rust-substitution-mapper.cc +++ b/gcc/rust/typecheck/rust-substitution-mapper.cc @@ -22,6 +22,116 @@ namespace Rust { namespace Resolver { +SubstMapper::SubstMapper (HirId ref, HIR::GenericArgs *generics, Location locus) + : resolved (new TyTy::ErrorType (ref)), generics (generics), locus (locus) +{} + +TyTy::BaseType * +SubstMapper::Resolve (TyTy::BaseType *base, Location locus, + HIR::GenericArgs *generics) +{ + SubstMapper mapper (base->get_ref (), generics, locus); + base->accept_vis (mapper); + rust_assert (mapper.resolved != nullptr); + return mapper.resolved; +} + +TyTy::BaseType * +SubstMapper::InferSubst (TyTy::BaseType *base, Location locus) +{ + return SubstMapper::Resolve (base, locus, nullptr); +} + +bool +SubstMapper::have_generic_args () const +{ + return generics != nullptr; +} + +void +SubstMapper::visit (TyTy::FnType &type) +{ + TyTy::FnType *concrete = nullptr; + if (!have_generic_args ()) + { + TyTy::BaseType *substs = type.infer_substitions (locus); + rust_assert (substs->get_kind () == TyTy::TypeKind::FNDEF); + concrete = static_cast (substs); + } + else + { + TyTy::SubstitutionArgumentMappings mappings + = type.get_mappings_from_generic_args (*generics); + if (mappings.is_error ()) + return; + + concrete = type.handle_substitions (mappings); + } + + if (concrete != nullptr) + resolved = concrete; +} + +void +SubstMapper::visit (TyTy::ADTType &type) +{ + TyTy::ADTType *concrete = nullptr; + if (!have_generic_args ()) + { + TyTy::BaseType *substs = type.infer_substitions (locus); + rust_assert (substs->get_kind () == TyTy::TypeKind::ADT); + concrete = static_cast (substs); + } + else + { + TyTy::SubstitutionArgumentMappings mappings + = type.get_mappings_from_generic_args (*generics); + if (mappings.is_error ()) + return; + + concrete = type.handle_substitions (mappings); + } + + if (concrete != nullptr) + resolved = concrete; +} + +void +SubstMapper::visit (TyTy::PlaceholderType &type) +{ + rust_assert (type.can_resolve ()); + resolved = SubstMapper::Resolve (type.resolve (), locus, generics); +} + +void +SubstMapper::visit (TyTy::ProjectionType &type) +{ + TyTy::ProjectionType *concrete = nullptr; + if (!have_generic_args ()) + { + TyTy::BaseType *substs = type.infer_substitions (locus); + rust_assert (substs->get_kind () == TyTy::TypeKind::PROJECTION); + concrete = static_cast (substs); + } + else + { + TyTy::SubstitutionArgumentMappings mappings + = type.get_mappings_from_generic_args (*generics); + if (mappings.is_error ()) + return; + + concrete = type.handle_substitions (mappings); + } + + if (concrete != nullptr) + resolved = concrete; +} + +SubstMapperInternal::SubstMapperInternal ( + HirId ref, TyTy::SubstitutionArgumentMappings &mappings) + : resolved (new TyTy::ErrorType (ref)), mappings (mappings) +{} + TyTy::BaseType * SubstMapperInternal::Resolve (TyTy::BaseType *base, TyTy::SubstitutionArgumentMappings &mappings) @@ -73,5 +183,238 @@ SubstMapperInternal::mappings_are_bound ( return false; } +void +SubstMapperInternal::visit (TyTy::FnType &type) +{ + TyTy::SubstitutionArgumentMappings adjusted + = type.adjust_mappings_for_this (mappings); + if (adjusted.is_error ()) + return; + + TyTy::BaseType *concrete = type.handle_substitions (adjusted); + if (concrete != nullptr) + resolved = concrete; +} + +void +SubstMapperInternal::visit (TyTy::ADTType &type) +{ + TyTy::SubstitutionArgumentMappings adjusted + = type.adjust_mappings_for_this (mappings); + if (adjusted.is_error ()) + return; + + TyTy::BaseType *concrete = type.handle_substitions (adjusted); + if (concrete != nullptr) + resolved = concrete; +} + +// these don't support generic arguments but might contain a type param +void +SubstMapperInternal::visit (TyTy::TupleType &type) +{ + resolved = type.handle_substitions (mappings); +} + +void +SubstMapperInternal::visit (TyTy::ReferenceType &type) +{ + resolved = type.handle_substitions (mappings); +} + +void +SubstMapperInternal::visit (TyTy::PointerType &type) +{ + resolved = type.handle_substitions (mappings); +} + +void +SubstMapperInternal::visit (TyTy::ParamType &type) +{ + resolved = type.handle_substitions (mappings); +} + +void +SubstMapperInternal::visit (TyTy::PlaceholderType &type) +{ + rust_assert (type.can_resolve ()); + if (mappings.trait_item_mode ()) + { + resolved = type.resolve (); + } + else + { + resolved = SubstMapperInternal::Resolve (type.resolve (), mappings); + } +} + +void +SubstMapperInternal::visit (TyTy::ProjectionType &type) +{ + resolved = type.handle_substitions (mappings); +} + +void +SubstMapperInternal::visit (TyTy::ClosureType &type) +{ + resolved = type.handle_substitions (mappings); +} + +void +SubstMapperInternal::visit (TyTy::ArrayType &type) +{ + resolved = type.handle_substitions (mappings); +} + +void +SubstMapperInternal::visit (TyTy::SliceType &type) +{ + resolved = type.handle_substitions (mappings); +} + +// nothing to do for these +void +SubstMapperInternal::visit (TyTy::InferType &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::FnPtr &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::BoolType &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::IntType &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::UintType &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::FloatType &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::USizeType &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::ISizeType &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::ErrorType &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::CharType &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::StrType &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::NeverType &type) +{ + resolved = type.clone (); +} +void +SubstMapperInternal::visit (TyTy::DynamicObjectType &type) +{ + resolved = type.clone (); +} + +// SubstMapperFromExisting + +SubstMapperFromExisting::SubstMapperFromExisting (TyTy::BaseType *concrete, + TyTy::BaseType *receiver) + : concrete (concrete), receiver (receiver), resolved (nullptr) +{} + +TyTy::BaseType * +SubstMapperFromExisting::Resolve (TyTy::BaseType *concrete, + TyTy::BaseType *receiver) +{ + rust_assert (concrete->get_kind () == receiver->get_kind ()); + + SubstMapperFromExisting mapper (concrete, receiver); + concrete->accept_vis (mapper); + return mapper.resolved; +} + +void +SubstMapperFromExisting::visit (TyTy::FnType &type) +{ + rust_assert (type.was_substituted ()); + + TyTy::FnType *to_sub = static_cast (receiver); + resolved = to_sub->handle_substitions (type.get_substitution_arguments ()); +} + +void +SubstMapperFromExisting::visit (TyTy::ADTType &type) +{ + rust_assert (type.was_substituted ()); + + TyTy::ADTType *to_sub = static_cast (receiver); + resolved = to_sub->handle_substitions (type.get_substitution_arguments ()); +} + +void +SubstMapperFromExisting::visit (TyTy::ClosureType &type) +{ + rust_assert (type.was_substituted ()); + + TyTy::ClosureType *to_sub = static_cast (receiver); + resolved = to_sub->handle_substitions (type.get_substitution_arguments ()); +} + +// GetUsedSubstArgs + +GetUsedSubstArgs::GetUsedSubstArgs () + : args (TyTy::SubstitutionArgumentMappings::error ()) +{} + +TyTy::SubstitutionArgumentMappings +GetUsedSubstArgs::From (const TyTy::BaseType *from) +{ + GetUsedSubstArgs mapper; + from->accept_vis (mapper); + return mapper.args; +} + +void +GetUsedSubstArgs::visit (const TyTy::FnType &type) +{ + args = type.get_substitution_arguments (); +} + +void +GetUsedSubstArgs::visit (const TyTy::ADTType &type) +{ + args = type.get_substitution_arguments (); +} + +void +GetUsedSubstArgs::visit (const TyTy::ClosureType &type) +{ + args = type.get_substitution_arguments (); +} + } // namespace Resolver } // namespace Rust diff --git a/gcc/rust/typecheck/rust-substitution-mapper.h b/gcc/rust/typecheck/rust-substitution-mapper.h index e326bbd94d9..047034ef0e2 100644 --- a/gcc/rust/typecheck/rust-substitution-mapper.h +++ b/gcc/rust/typecheck/rust-substitution-mapper.h @@ -29,95 +29,16 @@ class SubstMapper : public TyTy::TyVisitor { public: static TyTy::BaseType *Resolve (TyTy::BaseType *base, Location locus, - HIR::GenericArgs *generics = nullptr) - { - SubstMapper mapper (base->get_ref (), generics, locus); - base->accept_vis (mapper); - rust_assert (mapper.resolved != nullptr); - return mapper.resolved; - } + HIR::GenericArgs *generics = nullptr); - static TyTy::BaseType *InferSubst (TyTy::BaseType *base, Location locus) - { - return SubstMapper::Resolve (base, locus, nullptr); - } + static TyTy::BaseType *InferSubst (TyTy::BaseType *base, Location locus); - bool have_generic_args () const { return generics != nullptr; } + bool have_generic_args () const; - void visit (TyTy::FnType &type) override - { - TyTy::FnType *concrete = nullptr; - if (!have_generic_args ()) - { - TyTy::BaseType *substs = type.infer_substitions (locus); - rust_assert (substs->get_kind () == TyTy::TypeKind::FNDEF); - concrete = static_cast (substs); - } - else - { - TyTy::SubstitutionArgumentMappings mappings - = type.get_mappings_from_generic_args (*generics); - if (mappings.is_error ()) - return; - - concrete = type.handle_substitions (mappings); - } - - if (concrete != nullptr) - resolved = concrete; - } - - void visit (TyTy::ADTType &type) override - { - TyTy::ADTType *concrete = nullptr; - if (!have_generic_args ()) - { - TyTy::BaseType *substs = type.infer_substitions (locus); - rust_assert (substs->get_kind () == TyTy::TypeKind::ADT); - concrete = static_cast (substs); - } - else - { - TyTy::SubstitutionArgumentMappings mappings - = type.get_mappings_from_generic_args (*generics); - if (mappings.is_error ()) - return; - - concrete = type.handle_substitions (mappings); - } - - if (concrete != nullptr) - resolved = concrete; - } - - void visit (TyTy::PlaceholderType &type) override - { - rust_assert (type.can_resolve ()); - resolved = SubstMapper::Resolve (type.resolve (), locus, generics); - } - - void visit (TyTy::ProjectionType &type) override - { - TyTy::ProjectionType *concrete = nullptr; - if (!have_generic_args ()) - { - TyTy::BaseType *substs = type.infer_substitions (locus); - rust_assert (substs->get_kind () == TyTy::TypeKind::PROJECTION); - concrete = static_cast (substs); - } - else - { - TyTy::SubstitutionArgumentMappings mappings - = type.get_mappings_from_generic_args (*generics); - if (mappings.is_error ()) - return; - - concrete = type.handle_substitions (mappings); - } - - if (concrete != nullptr) - resolved = concrete; - } + void visit (TyTy::FnType &type) override; + void visit (TyTy::ADTType &type) override; + void visit (TyTy::PlaceholderType &type) override; + void visit (TyTy::ProjectionType &type) override; // nothing to do for these void visit (TyTy::InferType &) override { gcc_unreachable (); } @@ -142,9 +63,7 @@ public: void visit (TyTy::ClosureType &) override { gcc_unreachable (); } private: - SubstMapper (HirId ref, HIR::GenericArgs *generics, Location locus) - : resolved (new TyTy::ErrorType (ref)), generics (generics), locus (locus) - {} + SubstMapper (HirId ref, HIR::GenericArgs *generics, Location locus); TyTy::BaseType *resolved; HIR::GenericArgs *generics; @@ -160,106 +79,33 @@ public: static bool mappings_are_bound (TyTy::BaseType *ty, TyTy::SubstitutionArgumentMappings &mappings); - void visit (TyTy::FnType &type) override - { - TyTy::SubstitutionArgumentMappings adjusted - = type.adjust_mappings_for_this (mappings); - if (adjusted.is_error ()) - return; - - TyTy::BaseType *concrete = type.handle_substitions (adjusted); - if (concrete != nullptr) - resolved = concrete; - } - - void visit (TyTy::ADTType &type) override - { - TyTy::SubstitutionArgumentMappings adjusted - = type.adjust_mappings_for_this (mappings); - if (adjusted.is_error ()) - return; - - TyTy::BaseType *concrete = type.handle_substitions (adjusted); - if (concrete != nullptr) - resolved = concrete; - } - - // these don't support generic arguments but might contain a type param - void visit (TyTy::TupleType &type) override - { - resolved = type.handle_substitions (mappings); - } - - void visit (TyTy::ReferenceType &type) override - { - resolved = type.handle_substitions (mappings); - } - - void visit (TyTy::PointerType &type) override - { - resolved = type.handle_substitions (mappings); - } - - void visit (TyTy::ParamType &type) override - { - resolved = type.handle_substitions (mappings); - } - - void visit (TyTy::PlaceholderType &type) override - { - rust_assert (type.can_resolve ()); - if (mappings.trait_item_mode ()) - { - resolved = type.resolve (); - } - else - { - resolved = SubstMapperInternal::Resolve (type.resolve (), mappings); - } - } - - void visit (TyTy::ProjectionType &type) override - { - resolved = type.handle_substitions (mappings); - } - - void visit (TyTy::ClosureType &type) override - { - resolved = type.handle_substitions (mappings); - } - - void visit (TyTy::ArrayType &type) override - { - resolved = type.handle_substitions (mappings); - } - - void visit (TyTy::SliceType &type) override - { - resolved = type.handle_substitions (mappings); - } - - // nothing to do for these - void visit (TyTy::InferType &type) override { resolved = type.clone (); } - void visit (TyTy::FnPtr &type) override { resolved = type.clone (); } - void visit (TyTy::BoolType &type) override { resolved = type.clone (); } - void visit (TyTy::IntType &type) override { resolved = type.clone (); } - void visit (TyTy::UintType &type) override { resolved = type.clone (); } - void visit (TyTy::FloatType &type) override { resolved = type.clone (); } - void visit (TyTy::USizeType &type) override { resolved = type.clone (); } - void visit (TyTy::ISizeType &type) override { resolved = type.clone (); } - void visit (TyTy::ErrorType &type) override { resolved = type.clone (); } - void visit (TyTy::CharType &type) override { resolved = type.clone (); } - void visit (TyTy::StrType &type) override { resolved = type.clone (); } - void visit (TyTy::NeverType &type) override { resolved = type.clone (); } - void visit (TyTy::DynamicObjectType &type) override - { - resolved = type.clone (); - } + void visit (TyTy::FnType &type) override; + void visit (TyTy::ADTType &type) override; + void visit (TyTy::TupleType &type) override; + void visit (TyTy::ReferenceType &type) override; + void visit (TyTy::PointerType &type) override; + void visit (TyTy::ParamType &type) override; + void visit (TyTy::PlaceholderType &type) override; + void visit (TyTy::ProjectionType &type) override; + void visit (TyTy::ClosureType &type) override; + void visit (TyTy::ArrayType &type) override; + void visit (TyTy::SliceType &type) override; + void visit (TyTy::InferType &type) override; + void visit (TyTy::FnPtr &type) override; + void visit (TyTy::BoolType &type) override; + void visit (TyTy::IntType &type) override; + void visit (TyTy::UintType &type) override; + void visit (TyTy::FloatType &type) override; + void visit (TyTy::USizeType &type) override; + void visit (TyTy::ISizeType &type) override; + void visit (TyTy::ErrorType &type) override; + void visit (TyTy::CharType &type) override; + void visit (TyTy::StrType &type) override; + void visit (TyTy::NeverType &type) override; + void visit (TyTy::DynamicObjectType &type) override; private: - SubstMapperInternal (HirId ref, TyTy::SubstitutionArgumentMappings &mappings) - : resolved (new TyTy::ErrorType (ref)), mappings (mappings) - {} + SubstMapperInternal (HirId ref, TyTy::SubstitutionArgumentMappings &mappings); TyTy::BaseType *resolved; TyTy::SubstitutionArgumentMappings &mappings; @@ -269,38 +115,11 @@ class SubstMapperFromExisting : public TyTy::TyVisitor { public: static TyTy::BaseType *Resolve (TyTy::BaseType *concrete, - TyTy::BaseType *receiver) - { - rust_assert (concrete->get_kind () == receiver->get_kind ()); - - SubstMapperFromExisting mapper (concrete, receiver); - concrete->accept_vis (mapper); - return mapper.resolved; - } + TyTy::BaseType *receiver); - void visit (TyTy::FnType &type) override - { - rust_assert (type.was_substituted ()); - - TyTy::FnType *to_sub = static_cast (receiver); - resolved = to_sub->handle_substitions (type.get_substitution_arguments ()); - } - - void visit (TyTy::ADTType &type) override - { - rust_assert (type.was_substituted ()); - - TyTy::ADTType *to_sub = static_cast (receiver); - resolved = to_sub->handle_substitions (type.get_substitution_arguments ()); - } - - void visit (TyTy::ClosureType &type) override - { - rust_assert (type.was_substituted ()); - - TyTy::ClosureType *to_sub = static_cast (receiver); - resolved = to_sub->handle_substitions (type.get_substitution_arguments ()); - } + void visit (TyTy::FnType &type) override; + void visit (TyTy::ADTType &type) override; + void visit (TyTy::ClosureType &type) override; void visit (TyTy::InferType &) override { gcc_unreachable (); } void visit (TyTy::TupleType &) override { gcc_unreachable (); } @@ -325,40 +144,21 @@ public: void visit (TyTy::DynamicObjectType &) override { gcc_unreachable (); } private: - SubstMapperFromExisting (TyTy::BaseType *concrete, TyTy::BaseType *receiver) - : concrete (concrete), receiver (receiver), resolved (nullptr) - {} + SubstMapperFromExisting (TyTy::BaseType *concrete, TyTy::BaseType *receiver); TyTy::BaseType *concrete; TyTy::BaseType *receiver; - TyTy::BaseType *resolved; }; class GetUsedSubstArgs : public TyTy::TyConstVisitor { public: - static TyTy::SubstitutionArgumentMappings From (const TyTy::BaseType *from) - { - GetUsedSubstArgs mapper; - from->accept_vis (mapper); - return mapper.args; - } - - void visit (const TyTy::FnType &type) override - { - args = type.get_substitution_arguments (); - } - - void visit (const TyTy::ADTType &type) override - { - args = type.get_substitution_arguments (); - } + static TyTy::SubstitutionArgumentMappings From (const TyTy::BaseType *from); - void visit (const TyTy::ClosureType &type) override - { - args = type.get_substitution_arguments (); - } + void visit (const TyTy::FnType &type) override; + void visit (const TyTy::ADTType &type) override; + void visit (const TyTy::ClosureType &type) override; void visit (const TyTy::InferType &) override {} void visit (const TyTy::TupleType &) override {} @@ -383,7 +183,7 @@ public: void visit (const TyTy::DynamicObjectType &) override {} private: - GetUsedSubstArgs () : args (TyTy::SubstitutionArgumentMappings::error ()) {} + GetUsedSubstArgs (); TyTy::SubstitutionArgumentMappings args; }; diff --git a/gcc/rust/typecheck/rust-tyty-subst.cc b/gcc/rust/typecheck/rust-tyty-subst.cc index 64001459b4d..aceed29ff03 100644 --- a/gcc/rust/typecheck/rust-tyty-subst.cc +++ b/gcc/rust/typecheck/rust-tyty-subst.cc @@ -68,7 +68,7 @@ const HIR::TypeParam & SubstitutionParamMapping::get_generic_param () { return generic; -}; +} bool SubstitutionParamMapping::needs_substitution () const