]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: refactor TyTy::BaseType::is_unit to not use virtual dispatch
authorPhilip Herron <herron.philip@googlemail.com>
Fri, 10 Mar 2023 14:31:50 +0000 (14:31 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:21:11 +0000 (18:21 +0100)
gcc/rust/ChangeLog:

* typecheck/rust-tyty.cc (BaseType::is_unit): new implementation
(ErrorType::is_unit): remove
(TupleType::is_unit): likewise
(NeverType::is_unit): likewise
(PlaceholderType::is_unit): likewise
(ProjectionType::is_unit): likewise
* typecheck/rust-tyty.h: update header

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-tyty.cc
gcc/rust/typecheck/rust-tyty.h

index 8102858d8cdac9689bc9a7b827448d78fd541ba7..aeab07821f4f6483ba2fcb1a256090b38d435f00 100644 (file)
@@ -188,6 +188,56 @@ BaseType::is_equal (const BaseType &other) const
 bool
 BaseType::is_unit () const
 {
+  const TyTy::BaseType *x = destructure ();
+  switch (x->get_kind ())
+    {
+    case PARAM:
+    case PROJECTION:
+    case PLACEHOLDER:
+    case FNPTR:
+    case FNDEF:
+    case ARRAY:
+    case SLICE:
+    case POINTER:
+    case REF:
+    case CLOSURE:
+    case INFER:
+    case BOOL:
+    case CHAR:
+    case INT:
+    case UINT:
+    case FLOAT:
+    case USIZE:
+    case ISIZE:
+
+    case STR:
+    case DYNAMIC:
+    case ERROR:
+      return false;
+
+      // FIXME ! is coerceable to () so we need to fix that
+    case NEVER:
+      return true;
+
+      case TUPLE: {
+       const TupleType &tuple = *static_cast<const TupleType *> (x);
+       return tuple.num_fields () == 0;
+      }
+
+      case ADT: {
+       const ADTType &adt = *static_cast<const ADTType *> (x);
+       if (adt.is_enum ())
+         return false;
+
+       for (const auto &variant : adt.get_variants ())
+         {
+           if (variant->num_fields () > 0)
+             return false;
+         }
+
+       return true;
+      }
+    }
   return false;
 }
 
@@ -813,12 +863,6 @@ ErrorType::ErrorType (HirId ref, HirId ty_ref, std::set<HirId> refs)
              {Resolver::CanonicalPath::create_empty (), Location ()}, refs)
 {}
 
-bool
-ErrorType::is_unit () const
-{
-  return true;
-}
-
 std::string
 ErrorType::get_name () const
 {
@@ -1388,12 +1432,6 @@ TupleType::get_unit_type (HirId ref)
   return new TupleType (ref, Linemap::predeclared_location ());
 }
 
-bool
-TupleType::is_unit () const
-{
-  return this->fields.empty ();
-}
-
 size_t
 TupleType::num_fields () const
 {
@@ -3172,12 +3210,6 @@ NeverType::get_name () const
   return as_string ();
 }
 
-bool
-NeverType::is_unit () const
-{
-  return true;
-}
-
 void
 NeverType::accept_vis (TyVisitor &vis)
 {
@@ -3241,13 +3273,6 @@ PlaceholderType::get_name () const
   return as_string ();
 }
 
-bool
-PlaceholderType::is_unit () const
-{
-  rust_assert (can_resolve ());
-  return resolve ()->is_unit ();
-}
-
 std::string
 PlaceholderType::get_symbol () const
 {
@@ -3371,12 +3396,6 @@ ProjectionType::ProjectionType (
     base (base), trait (trait), item (item)
 {}
 
-bool
-ProjectionType::is_unit () const
-{
-  return false;
-}
-
 std::string
 ProjectionType::get_name () const
 {
index 0be189d92206c27a46490af9a743d49fa478fe6c..60887190a186a73f836a3c288dc2dcc8cae0c145 100644 (file)
@@ -85,19 +85,15 @@ public:
   virtual ~BaseType ();
 
   HirId get_ref () const;
-
   void set_ref (HirId id);
 
   HirId get_ty_ref () const;
-
   void set_ty_ref (HirId id);
 
   virtual void accept_vis (TyVisitor &vis) = 0;
-
   virtual void accept_vis (TyConstVisitor &vis) const = 0;
 
   virtual std::string as_string () const = 0;
-
   virtual std::string get_name () const = 0;
 
   // similar to unify but does not actually perform type unification but
@@ -119,19 +115,20 @@ public:
   virtual bool is_equal (const BaseType &other) const;
 
   bool satisfies_bound (const TypeBoundPredicate &predicate) const;
-
   bool bounds_compatible (const BaseType &other, Location locus,
                          bool emit_error) const;
-
   void inherit_bounds (const BaseType &other);
-
   void inherit_bounds (
     const std::vector<TyTy::TypeBoundPredicate> &specified_bounds);
 
-  virtual bool is_unit () const;
+  // is_unit returns whether this is just a unit-struct
+  bool is_unit () const;
 
+  // is_concrete returns true if the type is fully resolved to concrete
+  // primitives
   bool is_concrete () const;
 
+  // return the type-kind
   TypeKind get_kind () const;
 
   /* Returns a pointer to a clone of this. The caller is responsible for
@@ -238,8 +235,6 @@ public:
   void accept_vis (TyVisitor &vis) override;
   void accept_vis (TyConstVisitor &vis) const override;
 
-  bool is_unit () const override;
-
   std::string as_string () const override;
 
   bool can_eq (const BaseType *other, bool emit_errors) const override final;
@@ -340,8 +335,6 @@ public:
   void accept_vis (TyVisitor &vis) override;
   void accept_vis (TyConstVisitor &vis) const override;
 
-  bool is_unit () const override;
-
   std::string as_string () const override;
 
   bool can_eq (const BaseType *other, bool emit_errors) const override final;
@@ -564,17 +557,6 @@ public:
   bool is_union () const { return adt_kind == UNION; }
   bool is_enum () const { return adt_kind == ENUM; }
 
-  bool is_unit () const override
-  {
-    if (number_of_variants () == 0)
-      return true;
-
-    if (number_of_variants () == 1)
-      return variants.at (0)->num_fields () == 0;
-
-    return false;
-  }
-
   void accept_vis (TyVisitor &vis) override;
   void accept_vis (TyConstVisitor &vis) const override;
 
@@ -1297,8 +1279,6 @@ public:
   BaseType *monomorphized_clone () const final override;
 
   std::string get_name () const override final;
-
-  bool is_unit () const override;
 };
 
 // used at the type in associated types in traits
@@ -1323,8 +1303,6 @@ public:
 
   std::string get_name () const override final;
 
-  bool is_unit () const override;
-
   std::string get_symbol () const;
 
   void set_associated_type (HirId ref);
@@ -1370,8 +1348,6 @@ public:
 
   std::string get_name () const override final;
 
-  bool is_unit () const override;
-
   bool needs_generic_substitutions () const override final;
 
   bool supports_substitutions () const override final;