public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] gccrs: Refactor TyVar and TypeBoundPredicates
@ 2023-02-07 17:55 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-02-07 17:55 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:1fbf184bb9221f91a8c921ab868d8e1e5d2b56c3

commit 1fbf184bb9221f91a8c921ab868d8e1e5d2b56c3
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Thu Jan 12 18:00:52 2023 +0000

    gccrs: Refactor TyVar and TypeBoundPredicates
    
    This extract these helpers into seperate files
    
    Signed-off-by: Philip Herron <herron.philip@googlemail.com>
    
    gcc/rust/ChangeLog:
    
            * Make-lang.in: update makefile
            * typecheck/rust-tyty.cc (TyVar::TyVar): move to new file
            (TyVar::get_tyty): likewise
            (TyVar::get_implicit_infer_var): likewise
            (TyVar::subst_covariant_var): likewise
            (TyVar::clone): likewise
            (TyVar::monomorphized_clone): likewise
            (TyWithLocation::TyWithLocation): likewise
            * typecheck/rust-tyty.h (class BaseType): cleanup
            (class TypeBoundPredicate): move to its own file
            (class TypeBoundPredicateItem): likewise
            (class TypeBoundsMappings): likewise
            (class TyVar): likewise
            (class TyWithLocation): likewise
            * typecheck/rust-tyty-bounds.h: New file.
            * typecheck/rust-tyty-util.cc: New file.
            * typecheck/rust-tyty-util.h: New file.

Diff:
---
 gcc/rust/Make-lang.in                 |   1 +
 gcc/rust/typecheck/rust-tyty-bounds.h |  88 ++++++++++++++++++++++++++
 gcc/rust/typecheck/rust-tyty-util.cc  | 116 ++++++++++++++++++++++++++++++++++
 gcc/rust/typecheck/rust-tyty-util.h   |  69 ++++++++++++++++++++
 gcc/rust/typecheck/rust-tyty.cc       |  90 --------------------------
 gcc/rust/typecheck/rust-tyty.h        |  90 +-------------------------
 6 files changed, 276 insertions(+), 178 deletions(-)

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index 1aebf791dbb..986b8783539 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -115,6 +115,7 @@ GRS_OBJS = \
     rust/rust-pub-restricted-visitor.o \
     rust/rust-privacy-reporter.o \
     rust/rust-tyty.o \
+    rust/rust-tyty-util.o \
     rust/rust-tyty-call.o \
     rust/rust-tyctx.o \
     rust/rust-tyty-bounds.o \
diff --git a/gcc/rust/typecheck/rust-tyty-bounds.h b/gcc/rust/typecheck/rust-tyty-bounds.h
new file mode 100644
index 00000000000..44839bd0b40
--- /dev/null
+++ b/gcc/rust/typecheck/rust-tyty-bounds.h
@@ -0,0 +1,88 @@
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#ifndef RUST_TYTY_BOUNDS_H
+#define RUST_TYTY_BOUNDS_H
+
+#include "rust-location.h"
+
+namespace Rust {
+
+namespace Resolver {
+class TraitReference;
+class TraitItemReference;
+class AssociatedImplTrait;
+} // namespace Resolver
+
+namespace TyTy {
+
+class BaseType;
+class TypeBoundPredicate;
+class TypeBoundPredicateItem
+{
+public:
+  TypeBoundPredicateItem (const TypeBoundPredicate *parent,
+			  const Resolver::TraitItemReference *trait_item_ref);
+
+  static TypeBoundPredicateItem error ();
+
+  bool is_error () const;
+
+  BaseType *get_tyty_for_receiver (const TyTy::BaseType *receiver);
+
+  const Resolver::TraitItemReference *get_raw_item () const;
+
+  bool needs_implementation () const;
+
+  const TypeBoundPredicate *get_parent () const;
+
+  Location get_locus () const;
+
+private:
+  const TypeBoundPredicate *parent;
+  const Resolver::TraitItemReference *trait_item_ref;
+};
+
+class TypeBoundsMappings
+{
+protected:
+  TypeBoundsMappings (std::vector<TypeBoundPredicate> specified_bounds);
+
+public:
+  std::vector<TypeBoundPredicate> &get_specified_bounds ();
+
+  const std::vector<TypeBoundPredicate> &get_specified_bounds () const;
+
+  size_t num_specified_bounds () const;
+
+  std::string raw_bounds_as_string () const;
+
+  std::string bounds_as_string () const;
+
+  std::string raw_bounds_as_name () const;
+
+protected:
+  void add_bound (TypeBoundPredicate predicate);
+
+  std::vector<TypeBoundPredicate> specified_bounds;
+};
+
+} // namespace TyTy
+} // namespace Rust
+
+#endif // RUST_TYTY_BOUNDS_H
diff --git a/gcc/rust/typecheck/rust-tyty-util.cc b/gcc/rust/typecheck/rust-tyty-util.cc
new file mode 100644
index 00000000000..5037f68cb3f
--- /dev/null
+++ b/gcc/rust/typecheck/rust-tyty-util.cc
@@ -0,0 +1,116 @@
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include "rust-hir-type-check.h"
+#include "rust-tyty.h"
+
+namespace Rust {
+namespace TyTy {
+
+TyVar::TyVar (HirId ref) : ref (ref)
+{
+  // ensure this reference is defined within the context
+  auto context = Resolver::TypeCheckContext::get ();
+  BaseType *lookup = nullptr;
+  bool ok = context->lookup_type (ref, &lookup);
+  rust_assert (ok);
+}
+
+BaseType *
+TyVar::get_tyty () const
+{
+  auto context = Resolver::TypeCheckContext::get ();
+  BaseType *lookup = nullptr;
+  bool ok = context->lookup_type (ref, &lookup);
+  rust_assert (ok);
+  return lookup;
+}
+
+TyVar
+TyVar::get_implicit_infer_var (Location locus)
+{
+  auto mappings = Analysis::Mappings::get ();
+  auto context = Resolver::TypeCheckContext::get ();
+
+  InferType *infer = new InferType (mappings->get_next_hir_id (),
+				    InferType::InferTypeKind::GENERAL, locus);
+  context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
+					       UNKNOWN_NODEID,
+					       infer->get_ref (),
+					       UNKNOWN_LOCAL_DEFID),
+			infer);
+  mappings->insert_location (infer->get_ref (), locus);
+
+  return TyVar (infer->get_ref ());
+}
+
+TyVar
+TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst)
+{
+  if (orig->get_kind () != TyTy::TypeKind::PARAM)
+    return TyVar (subst->get_ty_ref ());
+  else if (subst->get_kind () == TyTy::TypeKind::PARAM)
+    {
+      TyTy::ParamType *p = static_cast<TyTy::ParamType *> (subst);
+      if (p->resolve ()->get_kind () == TyTy::TypeKind::PARAM)
+	{
+	  return TyVar (subst->get_ty_ref ());
+	}
+    }
+
+  return TyVar (subst->get_ref ());
+}
+
+TyVar
+TyVar::clone () const
+{
+  TyTy::BaseType *c = get_tyty ()->clone ();
+  return TyVar (c->get_ref ());
+}
+
+TyVar
+TyVar::monomorphized_clone () const
+{
+  auto mappings = Analysis::Mappings::get ();
+  auto context = Resolver::TypeCheckContext::get ();
+
+  // this needs a new hirid
+  TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
+  c->set_ref (mappings->get_next_hir_id ());
+
+  // insert it
+  context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
+					       UNKNOWN_NODEID, c->get_ref (),
+					       UNKNOWN_LOCAL_DEFID),
+			c);
+
+  return TyVar (c->get_ref ());
+}
+
+TyWithLocation::TyWithLocation (BaseType *ty, Location locus)
+  : ty (ty), locus (locus)
+{}
+
+TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty)
+{
+  auto mappings = Analysis::Mappings::get ();
+  locus = mappings->lookup_location (ty->get_ref ());
+}
+
+} // namespace TyTy
+} // namespace Rust
diff --git a/gcc/rust/typecheck/rust-tyty-util.h b/gcc/rust/typecheck/rust-tyty-util.h
new file mode 100644
index 00000000000..eccbb4423d5
--- /dev/null
+++ b/gcc/rust/typecheck/rust-tyty-util.h
@@ -0,0 +1,69 @@
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#ifndef RUST_TYTY_UTIL_H
+#define RUST_TYTY_UTIL_H
+
+#include "rust-hir-map.h"
+
+namespace Rust {
+namespace TyTy {
+
+class BaseType;
+
+// this is a placeholder for types that can change like inference variables
+class TyVar
+{
+public:
+  explicit TyVar (HirId ref);
+
+  HirId get_ref () const { return ref; }
+
+  BaseType *get_tyty () const;
+
+  TyVar clone () const;
+
+  TyVar monomorphized_clone () const;
+
+  static TyVar get_implicit_infer_var (Location locus);
+
+  static TyVar subst_covariant_var (TyTy::BaseType *orig,
+				    TyTy::BaseType *subst);
+
+private:
+  HirId ref;
+};
+
+class TyWithLocation
+{
+public:
+  explicit TyWithLocation (BaseType *ty, Location locus);
+  explicit TyWithLocation (BaseType *ty);
+
+  BaseType *get_ty () const { return ty; }
+  Location get_locus () const { return locus; }
+
+private:
+  BaseType *ty;
+  Location locus;
+};
+
+} // namespace TyTy
+} // namespace Rust
+
+#endif // RUST_TYTY_UTIL_H
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index 9358919ffaf..333e7ab7875 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -330,96 +330,6 @@ BaseType::debug () const
 	      debug_str ().c_str ());
 }
 
-TyVar::TyVar (HirId ref) : ref (ref)
-{
-  // ensure this reference is defined within the context
-  auto context = Resolver::TypeCheckContext::get ();
-  BaseType *lookup = nullptr;
-  bool ok = context->lookup_type (ref, &lookup);
-  rust_assert (ok);
-}
-
-BaseType *
-TyVar::get_tyty () const
-{
-  auto context = Resolver::TypeCheckContext::get ();
-  BaseType *lookup = nullptr;
-  bool ok = context->lookup_type (ref, &lookup);
-  rust_assert (ok);
-  return lookup;
-}
-
-TyVar
-TyVar::get_implicit_infer_var (Location locus)
-{
-  auto mappings = Analysis::Mappings::get ();
-  auto context = Resolver::TypeCheckContext::get ();
-
-  InferType *infer = new InferType (mappings->get_next_hir_id (),
-				    InferType::InferTypeKind::GENERAL, locus);
-  context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
-					       UNKNOWN_NODEID,
-					       infer->get_ref (),
-					       UNKNOWN_LOCAL_DEFID),
-			infer);
-  mappings->insert_location (infer->get_ref (), locus);
-
-  return TyVar (infer->get_ref ());
-}
-
-TyVar
-TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst)
-{
-  if (orig->get_kind () != TyTy::TypeKind::PARAM)
-    return TyVar (subst->get_ty_ref ());
-  else if (subst->get_kind () == TyTy::TypeKind::PARAM)
-    {
-      TyTy::ParamType *p = static_cast<TyTy::ParamType *> (subst);
-      if (p->resolve ()->get_kind () == TyTy::TypeKind::PARAM)
-	{
-	  return TyVar (subst->get_ty_ref ());
-	}
-    }
-
-  return TyVar (subst->get_ref ());
-}
-
-TyVar
-TyVar::clone () const
-{
-  TyTy::BaseType *c = get_tyty ()->clone ();
-  return TyVar (c->get_ref ());
-}
-
-TyVar
-TyVar::monomorphized_clone () const
-{
-  auto mappings = Analysis::Mappings::get ();
-  auto context = Resolver::TypeCheckContext::get ();
-
-  // this needs a new hirid
-  TyTy::BaseType *c = get_tyty ()->monomorphized_clone ();
-  c->set_ref (mappings->get_next_hir_id ());
-
-  // insert it
-  context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (),
-					       UNKNOWN_NODEID, c->get_ref (),
-					       UNKNOWN_LOCAL_DEFID),
-			c);
-
-  return TyVar (c->get_ref ());
-}
-
-TyWithLocation::TyWithLocation (BaseType *ty, Location locus)
-  : ty (ty), locus (locus)
-{}
-
-TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty)
-{
-  auto mappings = Analysis::Mappings::get ();
-  locus = mappings->lookup_location (ty->get_ref ());
-}
-
 void
 InferType::accept_vis (TyVisitor &vis)
 {
diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index 365bf7d1b3d..5c564fa5876 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -25,6 +25,8 @@
 #include "rust-abi.h"
 #include "rust-common.h"
 #include "rust-identifier.h"
+#include "rust-tyty-bounds.h"
+#include "rust-tyty-util.h"
 
 namespace Rust {
 
@@ -76,57 +78,6 @@ public:
   static std::string to_string (TypeKind kind);
 };
 
-class BaseType;
-class TypeBoundPredicate;
-class TypeBoundPredicateItem
-{
-public:
-  TypeBoundPredicateItem (const TypeBoundPredicate *parent,
-			  const Resolver::TraitItemReference *trait_item_ref);
-
-  static TypeBoundPredicateItem error ();
-
-  bool is_error () const;
-
-  BaseType *get_tyty_for_receiver (const TyTy::BaseType *receiver);
-
-  const Resolver::TraitItemReference *get_raw_item () const;
-
-  bool needs_implementation () const;
-
-  const TypeBoundPredicate *get_parent () const;
-
-  Location get_locus () const;
-
-private:
-  const TypeBoundPredicate *parent;
-  const Resolver::TraitItemReference *trait_item_ref;
-};
-
-class TypeBoundsMappings
-{
-protected:
-  TypeBoundsMappings (std::vector<TypeBoundPredicate> specified_bounds);
-
-public:
-  std::vector<TypeBoundPredicate> &get_specified_bounds ();
-
-  const std::vector<TypeBoundPredicate> &get_specified_bounds () const;
-
-  size_t num_specified_bounds () const;
-
-  std::string raw_bounds_as_string () const;
-
-  std::string bounds_as_string () const;
-
-  std::string raw_bounds_as_name () const;
-
-protected:
-  void add_bound (TypeBoundPredicate predicate);
-
-  std::vector<TypeBoundPredicate> specified_bounds;
-};
-
 extern void
 set_cmp_autoderef_mode ();
 extern void
@@ -268,43 +219,6 @@ protected:
   Analysis::Mappings *mappings;
 };
 
-// this is a placeholder for types that can change like inference variables
-class TyVar
-{
-public:
-  explicit TyVar (HirId ref);
-
-  HirId get_ref () const { return ref; }
-
-  BaseType *get_tyty () const;
-
-  TyVar clone () const;
-
-  TyVar monomorphized_clone () const;
-
-  static TyVar get_implicit_infer_var (Location locus);
-
-  static TyVar subst_covariant_var (TyTy::BaseType *orig,
-				    TyTy::BaseType *subst);
-
-private:
-  HirId ref;
-};
-
-class TyWithLocation
-{
-public:
-  explicit TyWithLocation (BaseType *ty, Location locus);
-  explicit TyWithLocation (BaseType *ty);
-
-  BaseType *get_ty () const { return ty; }
-  Location get_locus () const { return locus; }
-
-private:
-  BaseType *ty;
-  Location locus;
-};
-
 class InferType : public BaseType
 {
 public:

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-02-07 17:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-07 17:55 [gcc/devel/rust/master] gccrs: Refactor TyVar and TypeBoundPredicates Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).