diff options
author | Philip Herron <herron.philip@googlemail.com> | 2023-03-10 14:31:50 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2023-03-17 10:34:36 +0000 |
commit | 712aa0027c6fa6ea06560f0a797208bab19fbb24 (patch) | |
tree | 3b5a7d2722f7045a71fa1e4b05285abf6daaa33f /gcc | |
parent | 75ad892c42fa5bbd6ebd4b11e910e049365694fa (diff) | |
download | gcc-712aa0027c6fa6ea06560f0a797208bab19fbb24.zip gcc-712aa0027c6fa6ea06560f0a797208bab19fbb24.tar.gz gcc-712aa0027c6fa6ea06560f0a797208bab19fbb24.tar.bz2 |
gccrs: refactor TyTy::BaseType::is_unit to not use virtual dispatch
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>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.cc | 81 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.h | 34 |
2 files changed, 55 insertions, 60 deletions
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 3e399fd..dbd4b5c 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -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 { diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index d49768e..47f5516 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -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; |