diff options
author | Philip Herron <herron.philip@googlemail.com> | 2023-03-10 14:10:54 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2023-03-17 10:34:36 +0000 |
commit | 75ad892c42fa5bbd6ebd4b11e910e049365694fa (patch) | |
tree | 94096e605c3b1324ec05a35abb88ece60cbec82b | |
parent | 254208dc7aea43712f46eb3d732d17d0d6551bfc (diff) | |
download | gcc-75ad892c42fa5bbd6ebd4b11e910e049365694fa.zip gcc-75ad892c42fa5bbd6ebd4b11e910e049365694fa.tar.gz gcc-75ad892c42fa5bbd6ebd4b11e910e049365694fa.tar.bz2 |
gccrs: refactor is_concrete to not just virtual dispatch
Its easier to debug and maintain one base function for this than relying
on virtual dispatch
Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/ChangeLog:
* typecheck/rust-tyty.cc (BaseType::is_concrete): new implementation
(InferType::is_concrete): remove
(ErrorType::is_concrete): likewise
(StructFieldType::is_concrete): likewise
(ADTType::is_concrete): likewise
(TupleType::is_concrete): likewise
(BoolType::is_concrete): likewise
(IntType::is_concrete): likewise
(UintType::is_concrete): likewise
(FloatType::is_concrete): likewise
(USizeType::is_concrete): likewise
(ISizeType::is_concrete): likewise
(CharType::is_concrete): likewise
(ReferenceType::is_concrete): likewise
(PointerType::is_concrete): likewise
(ParamType::is_concrete): likewise
(StrType::is_concrete): likewise
(NeverType::is_concrete): likewise
(PlaceholderType::is_concrete): likewise
(ProjectionType::is_concrete): likewise
(DynamicObjectType::is_concrete): likewise
* typecheck/rust-tyty.h: update header
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.cc | 275 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.h | 79 |
2 files changed, 127 insertions, 227 deletions
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index e6dfc56..3e399fd 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -549,6 +549,132 @@ BaseType::debug () const debug_str ().c_str ()); } +bool +BaseType::is_concrete () const +{ + const TyTy::BaseType *x = destructure (); + switch (x->get_kind ()) + { + case PARAM: + case PROJECTION: + return false; + + // placeholder is a special case for this case when it is not resolvable + // it means we its just an empty placeholder associated type which is + // concrete + case PLACEHOLDER: + return true; + + case FNDEF: { + const FnType &fn = *static_cast<const FnType *> (x); + for (const auto ¶m : fn.get_params ()) + { + const BaseType *p = param.second; + if (!p->is_concrete ()) + return false; + } + return fn.get_return_type ()->is_concrete (); + } + break; + + case FNPTR: { + const FnPtr &fn = *static_cast<const FnPtr *> (x); + for (const auto ¶m : fn.get_params ()) + { + const BaseType *p = param.get_tyty (); + if (!p->is_concrete ()) + return false; + } + return fn.get_return_type ()->is_concrete (); + } + break; + + case ADT: { + const ADTType &adt = *static_cast<const ADTType *> (x); + if (adt.is_unit ()) + { + return !adt.needs_substitution (); + } + + for (auto &variant : adt.get_variants ()) + { + bool is_num_variant + = variant->get_variant_type () == VariantDef::VariantType::NUM; + if (is_num_variant) + continue; + + for (auto &field : variant->get_fields ()) + { + const BaseType *field_type = field->get_field_type (); + if (!field_type->is_concrete ()) + return false; + } + } + return true; + } + break; + + case ARRAY: { + const ArrayType &arr = *static_cast<const ArrayType *> (x); + return arr.get_element_type ()->is_concrete (); + } + break; + + case SLICE: { + const SliceType &slice = *static_cast<const SliceType *> (x); + return slice.get_element_type ()->is_concrete (); + } + break; + + case POINTER: { + const PointerType &ptr = *static_cast<const PointerType *> (x); + return ptr.get_base ()->is_concrete (); + } + break; + + case REF: { + const ReferenceType &ref = *static_cast<const ReferenceType *> (x); + return ref.get_base ()->is_concrete (); + } + break; + + case TUPLE: { + const TupleType &tuple = *static_cast<const TupleType *> (x); + for (size_t i = 0; i < tuple.num_fields (); i++) + { + if (!tuple.get_field (i)->is_concrete ()) + return false; + } + return true; + } + break; + + case CLOSURE: { + const ClosureType &closure = *static_cast<const ClosureType *> (x); + if (closure.get_parameters ().is_concrete ()) + return false; + return closure.get_result_type ().is_concrete (); + } + break; + + case INFER: + case BOOL: + case CHAR: + case INT: + case UINT: + case FLOAT: + case USIZE: + case ISIZE: + case NEVER: + case STR: + case DYNAMIC: + case ERROR: + return true; + } + + return false; +} + // InferType InferType::InferType (HirId ref, InferTypeKind infer_kind, Location locus, @@ -577,12 +703,6 @@ InferType::get_name () const return as_string (); } -bool -InferType::is_concrete () const -{ - return true; -} - void InferType::accept_vis (TyVisitor &vis) { @@ -698,11 +818,6 @@ ErrorType::is_unit () const { return true; } -bool -ErrorType::is_concrete () const -{ - return false; -} std::string ErrorType::get_name () const @@ -777,12 +892,6 @@ StructFieldType::set_field_type (BaseType *fty) ty = fty; } -bool -StructFieldType::is_concrete () const -{ - return ty->is_concrete (); -} - void StructFieldType::debug () const { @@ -1095,30 +1204,6 @@ ADTType::as_string () const } bool -ADTType::is_concrete () const -{ - if (is_unit ()) - { - return !needs_substitution (); - } - - for (auto &variant : variants) - { - bool is_num_variant - = variant->get_variant_type () == VariantDef::VariantType::NUM; - if (is_num_variant) - continue; - - for (auto &field : variant->get_fields ()) - { - if (!field->is_concrete ()) - return false; - } - } - return true; -} - -bool ADTType::can_eq (const BaseType *other, bool emit_errors) const { ADTCmp r (this, emit_errors); @@ -1315,17 +1400,6 @@ TupleType::num_fields () const return fields.size (); } -bool -TupleType::is_concrete () const -{ - for (size_t i = 0; i < num_fields (); i++) - { - if (!get_field (i)->is_concrete ()) - return false; - } - return true; -} - const std::vector<TyVar> & TupleType::get_fields () const { @@ -2042,12 +2116,6 @@ BoolType::get_name () const return as_string (); } -bool -BoolType::is_concrete () const -{ - return true; -} - void BoolType::accept_vis (TyVisitor &vis) { @@ -2177,12 +2245,6 @@ IntType::is_equal (const BaseType &other) const return get_int_kind () == o.get_int_kind (); } -bool -IntType::is_concrete () const -{ - return true; -} - // UintType UintType::UintType (HirId ref, UintKind kind, std::set<HirId> refs) @@ -2276,12 +2338,6 @@ UintType::is_equal (const BaseType &other) const return get_uint_kind () == o.get_uint_kind (); } -bool -UintType::is_concrete () const -{ - return true; -} - // FloatType FloatType::FloatType (HirId ref, FloatKind kind, std::set<HirId> refs) @@ -2313,12 +2369,6 @@ FloatType::get_float_kind () const return float_kind; } -bool -FloatType::is_concrete () const -{ - return true; -} - void FloatType::accept_vis (TyVisitor &vis) { @@ -2397,12 +2447,6 @@ USizeType::get_name () const return as_string (); } -bool -USizeType::is_concrete () const -{ - return true; -} - void USizeType::accept_vis (TyVisitor &vis) { @@ -2462,12 +2506,6 @@ ISizeType::get_name () const return as_string (); } -bool -ISizeType::is_concrete () const -{ - return true; -} - void ISizeType::accept_vis (TyVisitor &vis) { @@ -2521,12 +2559,6 @@ CharType::CharType (HirId ref, HirId ty_ref, std::set<HirId> refs) refs) {} -bool -CharType::is_concrete () const -{ - return true; -} - std::string CharType::get_name () const { @@ -2590,12 +2622,6 @@ ReferenceType::ReferenceType (HirId ref, HirId ty_ref, TyVar base, base (base), mut (mut) {} -bool -ReferenceType::is_concrete () const -{ - return get_base ()->is_concrete (); -} - Mutability ReferenceType::mutability () const { @@ -2743,12 +2769,6 @@ PointerType::PointerType (HirId ref, HirId ty_ref, TyVar base, Mutability mut, base (base), mut (mut) {} -bool -PointerType::is_concrete () const -{ - return get_base ()->is_concrete (); -} - Mutability PointerType::mutability () const { @@ -2918,16 +2938,6 @@ ParamType::can_resolve () const return get_ref () != get_ty_ref (); } -bool -ParamType::is_concrete () const -{ - auto r = resolve (); - if (r == this) - return false; - - return r->is_concrete (); -} - void ParamType::accept_vis (TyVisitor &vis) { @@ -3097,12 +3107,6 @@ StrType::get_name () const return as_string (); } -bool -StrType::is_concrete () const -{ - return true; -} - BaseType * StrType::clone () const { @@ -3174,12 +3178,6 @@ NeverType::is_unit () const return true; } -bool -NeverType::is_concrete () const -{ - return true; -} - void NeverType::accept_vis (TyVisitor &vis) { @@ -3256,15 +3254,6 @@ PlaceholderType::get_symbol () const return symbol; } -bool -PlaceholderType::is_concrete () const -{ - if (!can_resolve ()) - return true; - - return resolve ()->is_concrete (); -} - void PlaceholderType::accept_vis (TyVisitor &vis) { @@ -3423,12 +3412,6 @@ ProjectionType::get () return base; } -bool -ProjectionType::is_concrete () const -{ - return base->is_concrete (); -} - void ProjectionType::accept_vis (TyVisitor &vis) { @@ -3554,12 +3537,6 @@ DynamicObjectType::DynamicObjectType ( : BaseType (ref, ty_ref, TypeKind::DYNAMIC, ident, specified_bounds, refs) {} -bool -DynamicObjectType::is_concrete () const -{ - return true; -} - void DynamicObjectType::accept_vis (TyVisitor &vis) { diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 7cd8673..d49768e 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -130,7 +130,7 @@ public: virtual bool is_unit () const; - virtual bool is_concrete () const = 0; + bool is_concrete () const; TypeKind get_kind () const; @@ -223,8 +223,6 @@ public: bool default_type (BaseType **type) const; - bool is_concrete () const final override; - private: InferTypeKind infer_kind; }; @@ -250,8 +248,6 @@ public: BaseType *monomorphized_clone () const final override; std::string get_name () const override final; - - bool is_concrete () const final override; }; class ParamType : public BaseType @@ -289,8 +285,6 @@ public: bool is_equal (const BaseType &other) const override; - bool is_concrete () const override final; - ParamType *handle_substitions (SubstitutionArgumentMappings &mappings); void set_implicit_self_trait (); @@ -319,8 +313,6 @@ public: StructFieldType *clone () const; StructFieldType *monomorphized_clone () const; - bool is_concrete () const; - void debug () const; Location get_locus () const; std::string as_string () const; @@ -363,8 +355,6 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final; - const std::vector<TyVar> &get_fields () const; std::string get_name () const override final; @@ -601,8 +591,6 @@ public: return identifier + subst_as_string (); } - bool is_concrete () const override final; - BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; @@ -740,17 +728,6 @@ public: return param_at (0).second; } - bool is_concrete () const override final - { - for (const auto ¶m : params) - { - const BaseType *p = param.second; - if (!p->is_concrete ()) - return false; - } - return get_return_type ()->is_concrete (); - } - std::vector<std::pair<HIR::Pattern *, BaseType *>> &get_params () { return params; @@ -839,28 +816,9 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - void iterate_params (std::function<bool (BaseType *)> cb) const - { - for (auto &p : params) - { - if (!cb (p.get_tyty ())) - return; - } - } - std::vector<TyVar> &get_params () { return params; } const std::vector<TyVar> &get_params () const { return params; } - bool is_concrete () const override final - { - for (auto &p : params) - { - if (!p.get_tyty ()->is_concrete ()) - return false; - } - return result_type.get_tyty ()->is_concrete (); - } - private: std::vector<TyVar> params; TyVar result_type; @@ -918,12 +876,6 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final - { - return parameters->is_concrete () - && result_type.get_tyty ()->is_concrete (); - } - bool needs_generic_substitutions () const override final { return needs_substitution (); @@ -988,11 +940,6 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const final override - { - return get_element_type ()->is_concrete (); - } - HIR::Expr &get_capacity_expr () const { return capacity_expr; } ArrayType *handle_substitions (SubstitutionArgumentMappings &mappings); @@ -1035,11 +982,6 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const final override - { - return get_element_type ()->is_concrete (); - } - SliceType *handle_substitions (SubstitutionArgumentMappings &mappings); private: @@ -1063,7 +1005,6 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final; }; class IntType : public BaseType @@ -1097,7 +1038,6 @@ public: BaseType *monomorphized_clone () const final override; bool is_equal (const BaseType &other) const override; - bool is_concrete () const override final; private: IntKind int_kind; @@ -1135,7 +1075,6 @@ public: BaseType *monomorphized_clone () const final override; bool is_equal (const BaseType &other) const override; - bool is_concrete () const override final; private: UintKind uint_kind; @@ -1169,7 +1108,6 @@ public: BaseType *monomorphized_clone () const final override; bool is_equal (const BaseType &other) const override; - bool is_concrete () const override final; private: FloatKind float_kind; @@ -1192,7 +1130,6 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final; }; class ISizeType : public BaseType @@ -1212,7 +1149,6 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final; }; class CharType : public BaseType @@ -1231,7 +1167,6 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final; }; class StrType : public BaseType @@ -1253,7 +1188,6 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final; }; class ReferenceType : public BaseType @@ -1280,8 +1214,6 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final; - ReferenceType *handle_substitions (SubstitutionArgumentMappings &mappings); Mutability mutability () const; @@ -1322,8 +1254,6 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final; - PointerType *handle_substitions (SubstitutionArgumentMappings &mappings); Mutability mutability () const; @@ -1369,7 +1299,6 @@ public: std::string get_name () const override final; bool is_unit () const override; - bool is_concrete () const override final; }; // used at the type in associated types in traits @@ -1408,8 +1337,6 @@ public: bool is_equal (const BaseType &other) const override; - bool is_concrete () const override final; - private: std::string symbol; }; @@ -1454,8 +1381,6 @@ public: const BaseType *get () const; BaseType *get (); - bool is_concrete () const override final; - ProjectionType * handle_substitions (SubstitutionArgumentMappings &mappings) override final; @@ -1490,8 +1415,6 @@ public: std::string get_name () const override final; - bool is_concrete () const override final; - // this returns a flat list of items including super trait bounds const std::vector< std::pair<const Resolver::TraitItemReference *, const TypeBoundPredicate *>> |