Skip to content

Commit

Permalink
TyTy: TyTy improved subclass casting and checking
Browse files Browse the repository at this point in the history
Associate each subclass with its kind and create cast/match+cast
methods.

gcc/rust/ChangeLog:

	* typecheck/rust-tyty.cc (InferType::InferType): Use static constant for kind information.
	(ErrorType::ErrorType): Use static constant for kind information.
	(TupleType::TupleType): Use static constant for kind information.
	(BoolType::BoolType): Use static constant for kind information.
	(IntType::IntType): Use static constant for kind information.
	(UintType::UintType): Use static constant for kind information.
	(FloatType::FloatType): Use static constant for kind information.
	(USizeType::USizeType): Use static constant for kind information.
	(ISizeType::ISizeType): Use static constant for kind information.
	(CharType::CharType): Use static constant for kind information.
	(ReferenceType::ReferenceType): Use static constant for kind information.
	(PointerType::PointerType): Use static constant for kind information.
	(ParamType::ParamType): Use static constant for kind information.
	(StrType::StrType): Use static constant for kind information.
	(NeverType::NeverType): Use static constant for kind information.
	(PlaceholderType::PlaceholderType): Use static constant for kind information.
	* typecheck/rust-tyty.h: Add static kind information to all TyTy classes.
	Create safe cast and check methods.

Signed-off-by: Jakub Dupak <[email protected]>
  • Loading branch information
jdupak authored and philberty committed Oct 16, 2023
1 parent 522528b commit e3fcc88
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 38 deletions.
76 changes: 38 additions & 38 deletions gcc/rust/typecheck/rust-tyty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -962,14 +962,14 @@ BaseType::needs_generic_substitutions () const

InferType::InferType (HirId ref, InferTypeKind infer_kind, TypeHint hint,
location_t locus, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::INFER,
{Resolver::CanonicalPath::create_empty (), locus}, refs),
: BaseType (ref, ref, KIND, {Resolver::CanonicalPath::create_empty (), locus},
refs),
infer_kind (infer_kind), default_hint (hint)
{}

InferType::InferType (HirId ref, HirId ty_ref, InferTypeKind infer_kind,
TypeHint hint, location_t locus, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::INFER,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), locus}, refs),
infer_kind (infer_kind), default_hint (hint)
{}
Expand Down Expand Up @@ -1274,12 +1274,12 @@ InferType::apply_primitive_type_hint (const BaseType &hint)
// ErrorType

ErrorType::ErrorType (HirId ref, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::ERROR,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), UNDEF_LOCATION}, refs)
{}

ErrorType::ErrorType (HirId ref, HirId ty_ref, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::ERROR,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), UNDEF_LOCATION}, refs)
{}

Expand Down Expand Up @@ -1814,14 +1814,14 @@ ADTType::handle_substitions (SubstitutionArgumentMappings &subst_mappings)

TupleType::TupleType (HirId ref, location_t locus, std::vector<TyVar> fields,
std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::TUPLE,
{Resolver::CanonicalPath::create_empty (), locus}, refs),
: BaseType (ref, ref, KIND, {Resolver::CanonicalPath::create_empty (), locus},
refs),
fields (fields)
{}

TupleType::TupleType (HirId ref, HirId ty_ref, location_t locus,
std::vector<TyVar> fields, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::TUPLE,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), locus}, refs),
fields (fields)
{}
Expand Down Expand Up @@ -2487,13 +2487,13 @@ SliceType::handle_substitions (SubstitutionArgumentMappings &mappings)
// BoolType

BoolType::BoolType (HirId ref, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::BOOL,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}

BoolType::BoolType (HirId ref, HirId ty_ref, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::BOOL,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}
Expand Down Expand Up @@ -2538,14 +2538,14 @@ BoolType::clone () const
// IntType

IntType::IntType (HirId ref, IntKind kind, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::INT,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
int_kind (kind)
{}

IntType::IntType (HirId ref, HirId ty_ref, IntKind kind, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::INT,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
int_kind (kind)
Expand Down Expand Up @@ -2622,15 +2622,15 @@ IntType::is_equal (const BaseType &other) const
// UintType

UintType::UintType (HirId ref, UintKind kind, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::UINT,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
uint_kind (kind)
{}

UintType::UintType (HirId ref, HirId ty_ref, UintKind kind,
std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::UINT,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
uint_kind (kind)
Expand Down Expand Up @@ -2707,15 +2707,15 @@ UintType::is_equal (const BaseType &other) const
// FloatType

FloatType::FloatType (HirId ref, FloatKind kind, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::FLOAT,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
float_kind (kind)
{}

FloatType::FloatType (HirId ref, HirId ty_ref, FloatKind kind,
std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::FLOAT,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
float_kind (kind)
Expand Down Expand Up @@ -2786,13 +2786,13 @@ FloatType::is_equal (const BaseType &other) const
// UsizeType

USizeType::USizeType (HirId ref, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::USIZE,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}

USizeType::USizeType (HirId ref, HirId ty_ref, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::USIZE,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}
Expand Down Expand Up @@ -2837,13 +2837,13 @@ USizeType::clone () const
// ISizeType

ISizeType::ISizeType (HirId ref, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::ISIZE,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}

ISizeType::ISizeType (HirId ref, HirId ty_ref, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::ISIZE,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}
Expand Down Expand Up @@ -2888,13 +2888,13 @@ ISizeType::clone () const
// Char Type

CharType::CharType (HirId ref, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::CHAR,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}

CharType::CharType (HirId ref, HirId ty_ref, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::CHAR,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}
Expand Down Expand Up @@ -2940,15 +2940,15 @@ CharType::clone () const

ReferenceType::ReferenceType (HirId ref, TyVar base, Mutability mut,
std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::REF,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
base (base), mut (mut)
{}

ReferenceType::ReferenceType (HirId ref, HirId ty_ref, TyVar base,
Mutability mut, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::REF,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
base (base), mut (mut)
Expand Down Expand Up @@ -3096,15 +3096,15 @@ ReferenceType::handle_substitions (SubstitutionArgumentMappings &mappings)

PointerType::PointerType (HirId ref, TyVar base, Mutability mut,
std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::POINTER,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
base (base), mut (mut)
{}

PointerType::PointerType (HirId ref, HirId ty_ref, TyVar base, Mutability mut,
std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::POINTER,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
base (base), mut (mut)
Expand Down Expand Up @@ -3260,7 +3260,7 @@ ParamType::ParamType (std::string symbol, location_t locus, HirId ref,
HIR::GenericParam &param,
std::vector<TypeBoundPredicate> specified_bounds,
std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::PARAM,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol),
locus},
specified_bounds, refs),
Expand All @@ -3271,7 +3271,7 @@ ParamType::ParamType (bool is_trait_self, std::string symbol, location_t locus,
HirId ref, HirId ty_ref, HIR::GenericParam &param,
std::vector<TypeBoundPredicate> specified_bounds,
std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::PARAM,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol),
locus},
specified_bounds, refs),
Expand Down Expand Up @@ -3434,13 +3434,13 @@ ParamType::is_implicit_self_trait () const
// StrType

StrType::StrType (HirId ref, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::STR,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}

StrType::StrType (HirId ref, HirId ty_ref, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::STR,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}
Expand Down Expand Up @@ -3491,13 +3491,13 @@ StrType::is_equal (const BaseType &other) const
// Never Type

NeverType::NeverType (HirId ref, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::NEVER,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}

NeverType::NeverType (HirId ref, HirId ty_ref, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::NEVER,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs)
{}
Expand Down Expand Up @@ -3543,15 +3543,15 @@ NeverType::clone () const

PlaceholderType::PlaceholderType (std::string symbol, HirId ref,
std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::PLACEHOLDER,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
symbol (symbol)
{}

PlaceholderType::PlaceholderType (std::string symbol, HirId ref, HirId ty_ref,
std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::PLACEHOLDER,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
symbol (symbol)
Expand Down Expand Up @@ -3656,7 +3656,7 @@ ProjectionType::ProjectionType (
HirId ref, BaseType *base, const Resolver::TraitReference *trait, DefId item,
std::vector<SubstitutionParamMapping> subst_refs,
SubstitutionArgumentMappings generic_arguments, std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::PROJECTION,
: BaseType (ref, ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
SubstitutionRef (std::move (subst_refs), std::move (generic_arguments)),
Expand All @@ -3668,7 +3668,7 @@ ProjectionType::ProjectionType (
const Resolver::TraitReference *trait, DefId item,
std::vector<SubstitutionParamMapping> subst_refs,
SubstitutionArgumentMappings generic_arguments, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::PROJECTION,
: BaseType (ref, ty_ref, KIND,
{Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION},
refs),
SubstitutionRef (std::move (subst_refs), std::move (generic_arguments)),
Expand Down Expand Up @@ -3802,13 +3802,13 @@ ProjectionType::handle_substitions (
DynamicObjectType::DynamicObjectType (
HirId ref, RustIdent ident, std::vector<TypeBoundPredicate> specified_bounds,
std::set<HirId> refs)
: BaseType (ref, ref, TypeKind::DYNAMIC, ident, specified_bounds, refs)
: BaseType (ref, ref, KIND, ident, specified_bounds, refs)
{}

DynamicObjectType::DynamicObjectType (
HirId ref, HirId ty_ref, RustIdent ident,
std::vector<TypeBoundPredicate> specified_bounds, std::set<HirId> refs)
: BaseType (ref, ty_ref, TypeKind::DYNAMIC, ident, specified_bounds, refs)
: BaseType (ref, ty_ref, KIND, ident, specified_bounds, refs)
{}

void
Expand Down
Loading

0 comments on commit e3fcc88

Please sign in to comment.