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)
{}
// 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)
{}
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)
{}
// 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)
{}
// 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)
// 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)
// 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)
// 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)
{}
// 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)
{}
// 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)
{}
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)
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)
HIR::GenericParam ¶m,
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),
HirId ref, HirId ty_ref, HIR::GenericParam ¶m,
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),
// 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)
{}
// 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)
{}
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)
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)),
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)),
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
namespace Resolver {
class TraitReference;
+
class TraitItemReference;
+
class AssociatedImplTrait;
} // namespace Resolver
bool satisfies_bound (const TypeBoundPredicate &predicate,
bool emit_error) const;
+
bool bounds_compatible (const BaseType &other, location_t locus,
bool emit_error) const;
+
void inherit_bounds (const BaseType &other);
+
void inherit_bounds (
const std::vector<TyTy::TypeBoundPredicate> &specified_bounds);
// get_combined_refs returns the chain of node refs involved in unification
std::set<HirId> get_combined_refs () const;
+
void append_reference (HirId id);
std::string mappings_str () const;
+
std::string debug_str () const;
+
void debug () const;
// FIXME this will eventually go away
* releasing the memory of the returned ty. */
virtual BaseType *clone () const = 0;
+ // Check if TyTy::BaseType is of a specific type.
+ template <typename T>[[nodiscard]] bool is () const
+ {
+ static_assert (std::is_base_of<BaseType, T>::value,
+ "Can only safely cast to TyTy types.");
+ return this->get_kind () == T::KIND;
+ }
+
+ template <typename T> T *as () const
+ {
+ static_assert (std::is_base_of<BaseType, T>::value,
+ "Can only safely cast to TyTy types.");
+ rust_assert (this->is<T> ());
+ return static_cast<T *> (this);
+ }
+
+ template <typename T> T *as ()
+ {
+ static_assert (std::is_base_of<BaseType, T>::value,
+ "Can only safely cast to TyTy types.");
+ rust_assert (this->is<T> ());
+ return static_cast<T *> (this);
+ }
+
+ // Check if TyTy::BaseType is of a specific type and convert it to that type
+ // if so.
+ // Returns nullptr otherwise. Works as a dynamic_cast, but without compiler
+ // RTTI.
+ template <typename T> T *try_as () const
+ {
+ static_assert (std::is_base_of<BaseType, T>::value,
+ "Can only safely cast to TyTy types.");
+ if (!this->is<T> ())
+ return nullptr;
+
+ return static_cast<T *> (this);
+ }
+
+ // See above.
+ template <typename T> T *try_as ()
+ {
+ static_assert (std::is_base_of<BaseType, T>::value,
+ "Can only safely cast to TyTy types.");
+ if (!this->is<T> ())
+ return nullptr;
+
+ return static_cast<T *> (this);
+ }
+
protected:
BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident,
std::set<HirId> refs = std::set<HirId> ());
class InferType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::INFER;
+
enum InferTypeKind
{
GENERAL,
location_t locus, std::set<HirId> refs = std::set<HirId> ());
void accept_vis (TyVisitor &vis) override;
+
void accept_vis (TyConstVisitor &vis) const override;
std::string as_string () const override;
class ErrorType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::ERROR;
+
ErrorType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
ErrorType (HirId ref, HirId ty_ref,
class ParamType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::PARAM;
+
ParamType (std::string symbol, location_t locus, HirId ref,
HIR::GenericParam ¶m,
std::vector<TypeBoundPredicate> specified_bounds,
class TupleType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::TUPLE;
+
TupleType (HirId ref, location_t locus,
std::vector<TyVar> fields = std::vector<TyVar> (),
std::set<HirId> refs = std::set<HirId> ());
std::string as_string () const;
bool is_equal (const VariantDef &other) const;
+
VariantDef *clone () const;
+
VariantDef *monomorphized_clone () const;
const RustIdent &get_ident () const;
class ADTType : public BaseType, public SubstitutionRef
{
public:
+ static constexpr auto KIND = TypeKind::ADT;
+
enum ADTKind
{
STRUCT_STRUCT,
{}
ADTKind get_adt_kind () const { return adt_kind; }
+
ReprOptions get_repr_options () const { return repr; }
bool is_struct_struct () const { return adt_kind == STRUCT_STRUCT; }
+
bool is_tuple_struct () const { return adt_kind == TUPLE_STRUCT; }
+
bool is_union () const { return adt_kind == UNION; }
+
bool is_enum () const { return adt_kind == ENUM; }
void accept_vis (TyVisitor &vis) override;
+
void accept_vis (TyConstVisitor &vis) const override;
std::string as_string () const override;
size_t number_of_variants () const { return variants.size (); }
std::vector<VariantDef *> &get_variants () { return variants; }
+
const std::vector<VariantDef *> &get_variants () const { return variants; }
bool lookup_variant (const std::string &lookup,
class FnType : public BaseType, public SubstitutionRef
{
public:
+ static constexpr auto KIND = TypeKind::FNDEF;
+
static const uint8_t FNTYPE_DEFAULT_FLAGS = 0x00;
static const uint8_t FNTYPE_IS_METHOD_FLAG = 0x01;
static const uint8_t FNTYPE_IS_EXTERN_FLAG = 0x02;
class FnPtr : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::FNPTR;
+
FnPtr (HirId ref, location_t locus, std::vector<TyVar> params,
TyVar result_type, std::set<HirId> refs = std::set<HirId> ())
: BaseType (ref, ref, TypeKind::FNPTR,
class ClosureType : public BaseType, public SubstitutionRef
{
public:
+ static constexpr auto KIND = TypeKind::CLOSURE;
+
ClosureType (HirId ref, DefId id, RustIdent ident,
TyTy::TupleType *parameters, TyVar result_type,
std::vector<SubstitutionParamMapping> subst_refs,
class ArrayType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::ARRAY;
+
ArrayType (HirId ref, location_t locus, HIR::Expr &capacity_expr, TyVar base,
std::set<HirId> refs = std::set<HirId> ())
: BaseType (ref, ref, TypeKind::ARRAY,
class SliceType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::SLICE;
+
SliceType (HirId ref, location_t locus, TyVar base,
std::set<HirId> refs = std::set<HirId> ())
: BaseType (ref, ref, TypeKind::SLICE,
class BoolType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::BOOL;
+
BoolType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
BoolType (HirId ref, HirId ty_ref, std::set<HirId> refs = std::set<HirId> ());
I128
};
+ static constexpr auto KIND = TypeKind::INT;
+
IntType (HirId ref, IntKind kind, std::set<HirId> refs = std::set<HirId> ());
IntType (HirId ref, HirId ty_ref, IntKind kind,
std::set<HirId> refs = std::set<HirId> ());
class UintType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::UINT;
+
enum UintKind
{
U8,
class FloatType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::FLOAT;
+
enum FloatKind
{
F32,
class USizeType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::USIZE;
+
USizeType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
USizeType (HirId ref, HirId ty_ref,
std::set<HirId> refs = std::set<HirId> ());
class ISizeType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::ISIZE;
+
ISizeType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
ISizeType (HirId ref, HirId ty_ref,
std::set<HirId> refs = std::set<HirId> ());
class CharType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::CHAR;
+
CharType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
CharType (HirId ref, HirId ty_ref, std::set<HirId> refs = std::set<HirId> ());
class StrType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::STR;
+
StrType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
StrType (HirId ref, HirId ty_ref, std::set<HirId> refs = std::set<HirId> ());
class DynamicObjectType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::DYNAMIC;
+
DynamicObjectType (HirId ref, RustIdent ident,
std::vector<TypeBoundPredicate> specified_bounds,
std::set<HirId> refs = std::set<HirId> ());
class ReferenceType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::REF;
+
ReferenceType (HirId ref, TyVar base, Mutability mut,
std::set<HirId> refs = std::set<HirId> ());
ReferenceType (HirId ref, HirId ty_ref, TyVar base, Mutability mut,
class PointerType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::POINTER;
+
PointerType (HirId ref, TyVar base, Mutability mut,
std::set<HirId> refs = std::set<HirId> ());
PointerType (HirId ref, HirId ty_ref, TyVar base, Mutability mut,
class NeverType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::NEVER;
+
NeverType (HirId ref, std::set<HirId> refs = std::set<HirId> ());
+
NeverType (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;
std::string as_string () const override;
class PlaceholderType : public BaseType
{
public:
+ static constexpr auto KIND = TypeKind::PLACEHOLDER;
+
PlaceholderType (std::string symbol, HirId ref,
std::set<HirId> refs = std::set<HirId> ());
PlaceholderType (std::string symbol, HirId ref, HirId ty_ref,
class ProjectionType : public BaseType, public SubstitutionRef
{
public:
+ static constexpr auto KIND = TypeKind::PROJECTION;
+
ProjectionType (HirId ref, BaseType *base,
const Resolver::TraitReference *trait, DefId item,
std::vector<SubstitutionParamMapping> subst_refs,