mirror of
https://gcc.gnu.org/git/gcc.git
synced 2026-02-22 20:01:22 -05:00
gccrs: Refactor BaseType, InferType and ErrorType impl into cc file
Signed-off-by: Philip Herron <herron.philip@googlemail.com> gcc/rust/ChangeLog: * typecheck/rust-tyty.cc (BaseType::BaseType): refactor (BaseType::~BaseType): likewise (BaseType::get_ref): likewise (BaseType::set_ref): likewise (BaseType::get_ty_ref): likewise (BaseType::set_ty_ref): likewise (BaseType::is_equal): likewise (BaseType::is_unit): likewise (BaseType::get_kind): likewise (BaseType::get_combined_refs): likewise (BaseType::append_reference): likewise (BaseType::supports_substitutions): likewise (BaseType::has_subsititions_defined): likewise (BaseType::can_substitute): likewise (BaseType::needs_generic_substitutions): likewise (BaseType::contains_type_parameters): likewise (BaseType::get_ident): likewise (BaseType::get_locus): likewise (InferType::InferType): likewise (InferType::get_infer_kind): likewise (InferType::get_name): likewise (InferType::is_concrete): likewise (ErrorType::ErrorType): likewise (ErrorType::is_unit): likewise (ErrorType::is_concrete): likewise (ErrorType::get_name): likewise (ErrorType::monomorphized_clone): likewise * typecheck/rust-tyty.h (class SubstitutionArgumentMappings): likewise
This commit is contained in:
committed by
Arthur Cohen
parent
6abbfb5280
commit
d73082efec
@@ -146,6 +146,122 @@ is_primitive_type_kind (TypeKind kind)
|
||||
}
|
||||
}
|
||||
|
||||
// BASE TYPE
|
||||
|
||||
BaseType::BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
|
||||
std::set<HirId> refs)
|
||||
: TypeBoundsMappings ({}), kind (kind), ref (ref), ty_ref (ty_ref),
|
||||
combined (refs), ident (ident), mappings (Analysis::Mappings::get ())
|
||||
{}
|
||||
|
||||
BaseType::BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
|
||||
std::vector<TypeBoundPredicate> specified_bounds,
|
||||
std::set<HirId> refs)
|
||||
: TypeBoundsMappings (specified_bounds), kind (kind), ref (ref),
|
||||
ty_ref (ty_ref), combined (refs), ident (ident),
|
||||
mappings (Analysis::Mappings::get ())
|
||||
{}
|
||||
|
||||
BaseType::~BaseType () {}
|
||||
|
||||
HirId
|
||||
BaseType::get_ref () const
|
||||
{
|
||||
return ref;
|
||||
}
|
||||
|
||||
void
|
||||
BaseType::set_ref (HirId id)
|
||||
{
|
||||
if (id != ref)
|
||||
append_reference (ref);
|
||||
ref = id;
|
||||
}
|
||||
|
||||
HirId
|
||||
BaseType::get_ty_ref () const
|
||||
{
|
||||
return ty_ref;
|
||||
}
|
||||
|
||||
void
|
||||
BaseType::set_ty_ref (HirId id)
|
||||
{
|
||||
ty_ref = id;
|
||||
}
|
||||
|
||||
bool
|
||||
BaseType::is_equal (const BaseType &other) const
|
||||
{
|
||||
return get_kind () == other.get_kind ();
|
||||
}
|
||||
|
||||
bool
|
||||
BaseType::is_unit () const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
TypeKind
|
||||
BaseType::get_kind () const
|
||||
{
|
||||
return kind;
|
||||
}
|
||||
|
||||
std::set<HirId>
|
||||
BaseType::get_combined_refs () const
|
||||
{
|
||||
return combined;
|
||||
}
|
||||
|
||||
void
|
||||
BaseType::append_reference (HirId id)
|
||||
{
|
||||
combined.insert (id);
|
||||
}
|
||||
|
||||
bool
|
||||
BaseType::supports_substitutions () const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
BaseType::has_subsititions_defined () const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
BaseType::can_substitute () const
|
||||
{
|
||||
return supports_substitutions () && has_subsititions_defined ();
|
||||
}
|
||||
|
||||
bool
|
||||
BaseType::needs_generic_substitutions () const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
BaseType::contains_type_parameters () const
|
||||
{
|
||||
return !is_concrete ();
|
||||
}
|
||||
|
||||
const RustIdent &
|
||||
BaseType::get_ident () const
|
||||
{
|
||||
return ident;
|
||||
}
|
||||
|
||||
Location
|
||||
BaseType::get_locus () const
|
||||
{
|
||||
return ident.locus;
|
||||
}
|
||||
|
||||
bool
|
||||
BaseType::satisfies_bound (const TypeBoundPredicate &predicate) const
|
||||
{
|
||||
@@ -330,6 +446,40 @@ BaseType::debug () const
|
||||
debug_str ().c_str ());
|
||||
}
|
||||
|
||||
// InferType
|
||||
|
||||
InferType::InferType (HirId ref, InferTypeKind infer_kind, Location locus,
|
||||
std::set<HirId> refs)
|
||||
: BaseType (ref, ref, TypeKind::INFER,
|
||||
{Resolver::CanonicalPath::create_empty (), locus}, refs),
|
||||
infer_kind (infer_kind)
|
||||
{}
|
||||
|
||||
InferType::InferType (HirId ref, HirId ty_ref, InferTypeKind infer_kind,
|
||||
Location locus, std::set<HirId> refs)
|
||||
: BaseType (ref, ty_ref, TypeKind::INFER,
|
||||
{Resolver::CanonicalPath::create_empty (), locus}, refs),
|
||||
infer_kind (infer_kind)
|
||||
{}
|
||||
|
||||
InferType::InferTypeKind
|
||||
InferType::get_infer_kind () const
|
||||
{
|
||||
return infer_kind;
|
||||
}
|
||||
|
||||
std::string
|
||||
InferType::get_name () const
|
||||
{
|
||||
return as_string ();
|
||||
}
|
||||
|
||||
bool
|
||||
InferType::is_concrete () const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
InferType::accept_vis (TyVisitor &vis)
|
||||
{
|
||||
@@ -435,6 +585,35 @@ InferType::default_type (BaseType **type) const
|
||||
return false;
|
||||
}
|
||||
|
||||
// ErrorType
|
||||
|
||||
ErrorType::ErrorType (HirId ref, std::set<HirId> refs)
|
||||
: BaseType (ref, ref, TypeKind::ERROR,
|
||||
{Resolver::CanonicalPath::create_empty (), Location ()}, refs)
|
||||
{}
|
||||
|
||||
ErrorType::ErrorType (HirId ref, HirId ty_ref, std::set<HirId> refs)
|
||||
: BaseType (ref, ty_ref, TypeKind::ERROR,
|
||||
{Resolver::CanonicalPath::create_empty (), Location ()}, refs)
|
||||
{}
|
||||
|
||||
bool
|
||||
ErrorType::is_unit () const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
bool
|
||||
ErrorType::is_concrete () const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string
|
||||
ErrorType::get_name () const
|
||||
{
|
||||
return as_string ();
|
||||
}
|
||||
|
||||
void
|
||||
ErrorType::accept_vis (TyVisitor &vis)
|
||||
{
|
||||
@@ -477,6 +656,8 @@ ErrorType::monomorphized_clone () const
|
||||
return clone ();
|
||||
}
|
||||
|
||||
// Struct Field type
|
||||
|
||||
std::string
|
||||
StructFieldType::as_string () const
|
||||
{
|
||||
|
||||
@@ -89,20 +89,15 @@ class TyConstVisitor;
|
||||
class BaseType : public TypeBoundsMappings
|
||||
{
|
||||
public:
|
||||
virtual ~BaseType () {}
|
||||
virtual ~BaseType ();
|
||||
|
||||
HirId get_ref () const { return ref; }
|
||||
HirId get_ref () const;
|
||||
|
||||
void set_ref (HirId id)
|
||||
{
|
||||
if (id != ref)
|
||||
append_reference (ref);
|
||||
ref = id;
|
||||
}
|
||||
void set_ref (HirId id);
|
||||
|
||||
HirId get_ty_ref () const { return ty_ref; }
|
||||
HirId get_ty_ref () const;
|
||||
|
||||
void set_ty_ref (HirId id) { ty_ref = id; }
|
||||
void set_ty_ref (HirId id);
|
||||
|
||||
virtual void accept_vis (TyVisitor &vis) = 0;
|
||||
|
||||
@@ -133,10 +128,7 @@ public:
|
||||
// ty are considered equal if they're of the same kind, and
|
||||
// 1. (For ADTs, arrays, tuples, refs) have the same underlying ty
|
||||
// 2. (For functions) have the same signature
|
||||
virtual bool is_equal (const BaseType &other) const
|
||||
{
|
||||
return get_kind () == other.get_kind ();
|
||||
}
|
||||
virtual bool is_equal (const BaseType &other) const;
|
||||
|
||||
bool satisfies_bound (const TypeBoundPredicate &predicate) const;
|
||||
|
||||
@@ -148,11 +140,11 @@ public:
|
||||
void inherit_bounds (
|
||||
const std::vector<TyTy::TypeBoundPredicate> &specified_bounds);
|
||||
|
||||
virtual bool is_unit () const { return false; }
|
||||
virtual bool is_unit () const;
|
||||
|
||||
virtual bool is_concrete () const = 0;
|
||||
|
||||
TypeKind get_kind () const { return kind; }
|
||||
TypeKind get_kind () const;
|
||||
|
||||
/* Returns a pointer to a clone of this. The caller is responsible for
|
||||
* releasing the memory of the returned ty. */
|
||||
@@ -162,22 +154,19 @@ public:
|
||||
virtual BaseType *monomorphized_clone () const = 0;
|
||||
|
||||
// get_combined_refs returns the chain of node refs involved in unification
|
||||
std::set<HirId> get_combined_refs () const { return combined; }
|
||||
std::set<HirId> get_combined_refs () const;
|
||||
|
||||
void append_reference (HirId id) { combined.insert (id); }
|
||||
void append_reference (HirId id);
|
||||
|
||||
virtual bool supports_substitutions () const { return false; }
|
||||
virtual bool supports_substitutions () const;
|
||||
|
||||
virtual bool has_subsititions_defined () const { return false; }
|
||||
virtual bool has_subsititions_defined () const;
|
||||
|
||||
virtual bool can_substitute () const
|
||||
{
|
||||
return supports_substitutions () && has_subsititions_defined ();
|
||||
}
|
||||
virtual bool can_substitute () const;
|
||||
|
||||
virtual bool needs_generic_substitutions () const { return false; }
|
||||
virtual bool needs_generic_substitutions () const;
|
||||
|
||||
bool contains_type_parameters () const { return !is_concrete (); }
|
||||
bool contains_type_parameters () const;
|
||||
|
||||
std::string mappings_str () const;
|
||||
|
||||
@@ -192,24 +181,17 @@ public:
|
||||
// Projections if available or error
|
||||
const BaseType *destructure () const;
|
||||
|
||||
const RustIdent &get_ident () const { return ident; }
|
||||
const RustIdent &get_ident () const;
|
||||
|
||||
Location get_locus () const { return ident.locus; }
|
||||
Location get_locus () const;
|
||||
|
||||
protected:
|
||||
BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
|
||||
std::set<HirId> refs = std::set<HirId> ())
|
||||
: TypeBoundsMappings ({}), kind (kind), ref (ref), ty_ref (ty_ref),
|
||||
combined (refs), ident (ident), mappings (Analysis::Mappings::get ())
|
||||
{}
|
||||
std::set<HirId> refs = std::set<HirId> ());
|
||||
|
||||
BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
|
||||
std::vector<TypeBoundPredicate> specified_bounds,
|
||||
std::set<HirId> refs = std::set<HirId> ())
|
||||
: TypeBoundsMappings (specified_bounds), kind (kind), ref (ref),
|
||||
ty_ref (ty_ref), combined (refs), ident (ident),
|
||||
mappings (Analysis::Mappings::get ())
|
||||
{}
|
||||
std::set<HirId> refs = std::set<HirId> ());
|
||||
|
||||
TypeKind kind;
|
||||
HirId ref;
|
||||
@@ -231,18 +213,10 @@ public:
|
||||
};
|
||||
|
||||
InferType (HirId ref, InferTypeKind infer_kind, Location locus,
|
||||
std::set<HirId> refs = std::set<HirId> ())
|
||||
: BaseType (ref, ref, TypeKind::INFER,
|
||||
{Resolver::CanonicalPath::create_empty (), locus}, refs),
|
||||
infer_kind (infer_kind)
|
||||
{}
|
||||
std::set<HirId> refs = std::set<HirId> ());
|
||||
|
||||
InferType (HirId ref, HirId ty_ref, InferTypeKind infer_kind, Location locus,
|
||||
std::set<HirId> refs = std::set<HirId> ())
|
||||
: BaseType (ref, ty_ref, TypeKind::INFER,
|
||||
{Resolver::CanonicalPath::create_empty (), locus}, refs),
|
||||
infer_kind (infer_kind)
|
||||
{}
|
||||
std::set<HirId> refs = std::set<HirId> ());
|
||||
|
||||
void accept_vis (TyVisitor &vis) override;
|
||||
void accept_vis (TyConstVisitor &vis) const override;
|
||||
@@ -256,13 +230,13 @@ public:
|
||||
BaseType *clone () const final override;
|
||||
BaseType *monomorphized_clone () const final override;
|
||||
|
||||
InferTypeKind get_infer_kind () const { return infer_kind; }
|
||||
InferTypeKind get_infer_kind () const;
|
||||
|
||||
std::string get_name () const override final { return as_string (); }
|
||||
std::string get_name () const override final;
|
||||
|
||||
bool default_type (BaseType **type) const;
|
||||
|
||||
bool is_concrete () const final override { return true; }
|
||||
bool is_concrete () const final override;
|
||||
|
||||
private:
|
||||
InferTypeKind infer_kind;
|
||||
@@ -271,20 +245,15 @@ private:
|
||||
class ErrorType : public BaseType
|
||||
{
|
||||
public:
|
||||
ErrorType (HirId ref, std::set<HirId> refs = std::set<HirId> ())
|
||||
: BaseType (ref, ref, TypeKind::ERROR,
|
||||
{Resolver::CanonicalPath::create_empty (), Location ()}, refs)
|
||||
{}
|
||||
ErrorType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
|
||||
|
||||
ErrorType (HirId ref, HirId ty_ref, std::set<HirId> refs = std::set<HirId> ())
|
||||
: BaseType (ref, ty_ref, TypeKind::ERROR,
|
||||
{Resolver::CanonicalPath::create_empty (), Location ()}, refs)
|
||||
{}
|
||||
ErrorType (HirId ref, HirId ty_ref,
|
||||
std::set<HirId> refs = std::set<HirId> ());
|
||||
|
||||
void accept_vis (TyVisitor &vis) override;
|
||||
void accept_vis (TyConstVisitor &vis) const override;
|
||||
|
||||
bool is_unit () const override { return true; }
|
||||
bool is_unit () const override;
|
||||
|
||||
std::string as_string () const override;
|
||||
|
||||
@@ -294,12 +263,11 @@ public:
|
||||
BaseType *clone () const final override;
|
||||
BaseType *monomorphized_clone () const final override;
|
||||
|
||||
std::string get_name () const override final { return as_string (); }
|
||||
std::string get_name () const override final;
|
||||
|
||||
bool is_concrete () const final override { return false; }
|
||||
bool is_concrete () const final override;
|
||||
};
|
||||
|
||||
class SubstitutionArgumentMappings;
|
||||
class ParamType : public BaseType
|
||||
{
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user