gccrs: Refactor all substitution mapper code implementation into its own CC file
authorPhilip Herron <herron.philip@googlemail.com>
Sat, 14 Jan 2023 23:36:47 +0000 (23:36 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Thu, 6 Apr 2023 08:47:20 +0000 (10:47 +0200)
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
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

gcc/rust/typecheck/rust-substitution-mapper.cc
gcc/rust/typecheck/rust-substitution-mapper.h
gcc/rust/typecheck/rust-tyty-subst.cc

index e1d9888..dc93857 100644 (file)
 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<TyTy::FnType *> (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<TyTy::ADTType *> (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<TyTy::ProjectionType *> (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<TyTy::FnType *> (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<TyTy::ADTType *> (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<TyTy::ClosureType *> (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
index 995d9c8..43b80b3 100644 (file)
@@ -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<TyTy::FnType *> (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<TyTy::ADTType *> (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<TyTy::ProjectionType *> (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<TyTy::FnType *> (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<TyTy::ADTType *> (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<TyTy::ClosureType *> (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;
 };
index 6400145..aceed29 100644 (file)
@@ -68,7 +68,7 @@ const HIR::TypeParam &
 SubstitutionParamMapping::get_generic_param ()
 {
   return generic;
-};
+}
 
 bool
 SubstitutionParamMapping::needs_substitution () const