diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 6136fa62f948..64db06dd0785 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -220,16 +220,15 @@ BaseType::is_unit () const return true; case TUPLE: { - const TupleType &tuple = *static_cast (x); - return tuple.num_fields () == 0; + return x->as ()->num_fields () == 0; } case ADT: { - const ADTType &adt = *static_cast (x); - if (adt.is_enum ()) + auto adt = x->as (); + if (adt->is_enum ()) return false; - for (const auto &variant : adt.get_variants ()) + for (const auto &variant : adt->get_variants ()) { if (variant->num_fields () > 0) return false; @@ -276,8 +275,6 @@ bool BaseType::satisfies_bound (const TypeBoundPredicate &predicate, bool emit_error) const { - bool is_infer_var = destructure ()->get_kind () == TyTy::TypeKind::INFER; - const Resolver::TraitReference *query = predicate.get (); for (const auto &bound : specified_bounds) { @@ -286,7 +283,7 @@ BaseType::satisfies_bound (const TypeBoundPredicate &predicate, return true; } - if (is_infer_var) + if (destructure ()->is ()) return true; bool satisfied = false; @@ -435,28 +432,24 @@ BaseType::get_root () const { // FIXME this needs to be it its own visitor class with a vector adjustments const TyTy::BaseType *root = this; - if (get_kind () == TyTy::REF) + + if (const auto r = root->try_as ()) { - const ReferenceType *r = static_cast (root); root = r->get_base ()->get_root (); } - else if (get_kind () == TyTy::POINTER) + else if (const auto r = root->try_as ()) { - const PointerType *r = static_cast (root); root = r->get_base ()->get_root (); } - // these are an unsize - else if (get_kind () == TyTy::SLICE) + else if (const auto r = root->try_as ()) { - const SliceType *r = static_cast (root); root = r->get_element_type ()->get_root (); } - // else if (get_kind () == TyTy::ARRAY) - // { - // const ArrayType *r = static_cast (root); - // root = r->get_element_type ()->get_root (); - // } + // else if (const auto r = root->try_as ()) + // { + // root = r->get_element_type ()->get_root (); + // } return root; } @@ -478,34 +471,27 @@ BaseType::destructure () return new ErrorType (get_ref ()); } - switch (x->get_kind ()) + if (auto p = x->try_as ()) { - case TyTy::TypeKind::PARAM: { - TyTy::ParamType *p = static_cast (x); - TyTy::BaseType *pr = p->resolve (); - if (pr == x) - return pr; - - x = pr; - } - break; + auto pr = p->resolve (); + if (pr == x) + return pr; - case TyTy::TypeKind::PLACEHOLDER: { - TyTy::PlaceholderType *p = static_cast (x); - if (!p->can_resolve ()) - return p; - - x = p->resolve (); - } - break; - - case TyTy::TypeKind::PROJECTION: { - TyTy::ProjectionType *p = static_cast (x); - x = p->get (); - } - break; + x = pr; + } + else if (auto p = x->try_as ()) + { + if (!p->can_resolve ()) + return p; - default: + x = p->resolve (); + } + else if (auto p = x->try_as ()) + { + x = p->get (); + } + else + { return x; } } @@ -530,36 +516,27 @@ BaseType::destructure () const return new ErrorType (get_ref ()); } - switch (x->get_kind ()) + if (auto p = x->try_as ()) { - case TyTy::TypeKind::PARAM: { - const TyTy::ParamType *p = static_cast (x); - const TyTy::BaseType *pr = p->resolve (); - if (pr == x) - return pr; - - x = pr; - } - break; - - case TyTy::TypeKind::PLACEHOLDER: { - const TyTy::PlaceholderType *p - = static_cast (x); - if (!p->can_resolve ()) - return p; - - x = p->resolve (); - } - break; + auto pr = p->resolve (); + if (pr == x) + return pr; - case TyTy::TypeKind::PROJECTION: { - const TyTy::ProjectionType *p - = static_cast (x); - x = p->get (); - } - break; + x = pr; + } + else if (auto p = x->try_as ()) + { + if (!p->can_resolve ()) + return p; - default: + x = p->resolve (); + } + else if (auto p = x->try_as ()) + { + x = p->get (); + } + else + { return x; } } @@ -571,112 +548,81 @@ BaseType * BaseType::monomorphized_clone () const { const TyTy::BaseType *x = destructure (); - switch (x->get_kind ()) - { - case PARAM: - case PROJECTION: - case PLACEHOLDER: - case INFER: - case BOOL: - case CHAR: - case INT: - case UINT: - case FLOAT: - case USIZE: - case ISIZE: - case NEVER: - case STR: - case DYNAMIC: - case CLOSURE: - case ERROR: - return x->clone (); - - case ARRAY: { - const ArrayType &arr = *static_cast (x); - TyVar elm = arr.get_var_element_type ().monomorphized_clone (); - return new ArrayType (arr.get_ref (), arr.get_ty_ref (), ident.locus, - arr.get_capacity_expr (), elm, - arr.get_combined_refs ()); - } - break; - - case SLICE: { - const SliceType &slice = *static_cast (x); - TyVar elm = slice.get_var_element_type ().monomorphized_clone (); - return new SliceType (slice.get_ref (), slice.get_ty_ref (), - ident.locus, elm, slice.get_combined_refs ()); - } - break; - - case POINTER: { - const PointerType &ptr = *static_cast (x); - TyVar elm = ptr.get_var_element_type ().monomorphized_clone (); - return new PointerType (ptr.get_ref (), ptr.get_ty_ref (), elm, - ptr.mutability (), ptr.get_combined_refs ()); - } - break; - - case REF: { - const ReferenceType &ref = *static_cast (x); - TyVar elm = ref.get_var_element_type ().monomorphized_clone (); - return new ReferenceType (ref.get_ref (), ref.get_ty_ref (), elm, - ref.mutability (), ref.get_combined_refs ()); - } - break; - - case TUPLE: { - const TupleType &tuple = *static_cast (x); - std::vector cloned_fields; - for (const auto &f : tuple.get_fields ()) - cloned_fields.push_back (f.monomorphized_clone ()); - - return new TupleType (tuple.get_ref (), tuple.get_ty_ref (), - tuple.get_ident ().locus, cloned_fields, - tuple.get_combined_refs ()); - } - break; - - case FNDEF: { - const FnType &fn = *static_cast (x); - std::vector> cloned_params; - for (auto &p : fn.get_params ()) - cloned_params.push_back ({p.first, p.second->monomorphized_clone ()}); - - BaseType *retty = fn.get_return_type ()->monomorphized_clone (); - return new FnType (fn.get_ref (), fn.get_ty_ref (), fn.get_id (), - fn.get_identifier (), fn.ident, fn.get_flags (), - fn.get_abi (), std::move (cloned_params), retty, - fn.clone_substs (), fn.get_combined_refs ()); - } - break; - - case FNPTR: { - const FnPtr &fn = *static_cast (x); - std::vector cloned_params; - for (auto &p : fn.get_params ()) - cloned_params.push_back (p.monomorphized_clone ()); - TyVar retty = fn.get_var_return_type ().monomorphized_clone (); - return new FnPtr (fn.get_ref (), fn.get_ty_ref (), fn.ident.locus, - std::move (cloned_params), retty, - fn.get_combined_refs ()); - } - break; + if (auto arr = x->try_as ()) + { + TyVar elm = arr->get_var_element_type ().monomorphized_clone (); + return new ArrayType (arr->get_ref (), arr->get_ty_ref (), ident.locus, + arr->get_capacity_expr (), elm, + arr->get_combined_refs ()); + } + else if (auto slice = x->try_as ()) + { + TyVar elm = slice->get_var_element_type ().monomorphized_clone (); + return new SliceType (slice->get_ref (), slice->get_ty_ref (), + ident.locus, elm, slice->get_combined_refs ()); + } + else if (auto ptr = x->try_as ()) + { + TyVar elm = ptr->get_var_element_type ().monomorphized_clone (); + return new PointerType (ptr->get_ref (), ptr->get_ty_ref (), elm, + ptr->mutability (), ptr->get_combined_refs ()); + } + else if (auto ref = x->try_as ()) + { + TyVar elm = ref->get_var_element_type ().monomorphized_clone (); + return new ReferenceType (ref->get_ref (), ref->get_ty_ref (), elm, + ref->mutability (), ref->get_combined_refs ()); + } + else if (auto tuple = x->try_as ()) + { + std::vector cloned_fields; + for (const auto &f : tuple->get_fields ()) + cloned_fields.push_back (f.monomorphized_clone ()); - case ADT: { - const ADTType &adt = *static_cast (x); - std::vector cloned_variants; - for (auto &variant : adt.get_variants ()) - cloned_variants.push_back (variant->monomorphized_clone ()); - - return new ADTType (adt.get_ref (), adt.get_ty_ref (), - adt.get_identifier (), adt.ident, - adt.get_adt_kind (), cloned_variants, - adt.clone_substs (), adt.get_repr_options (), - adt.get_used_arguments (), - adt.get_combined_refs ()); - } - break; + return new TupleType (tuple->get_ref (), tuple->get_ty_ref (), + ident.locus, cloned_fields, + tuple->get_combined_refs ()); + } + else if (auto fn = x->try_as ()) + { + std::vector> cloned_params; + for (auto &p : fn->get_params ()) + cloned_params.push_back ({p.first, p.second->monomorphized_clone ()}); + + BaseType *retty = fn->get_return_type ()->monomorphized_clone (); + return new FnType (fn->get_ref (), fn->get_ty_ref (), fn->get_id (), + fn->get_identifier (), fn->ident, fn->get_flags (), + fn->get_abi (), std::move (cloned_params), retty, + fn->clone_substs (), fn->get_combined_refs ()); + } + else if (auto fn = x->try_as ()) + { + std::vector cloned_params; + for (auto &p : fn->get_params ()) + cloned_params.push_back (p.monomorphized_clone ()); + + TyVar retty = fn->get_var_return_type ().monomorphized_clone (); + return new FnPtr (fn->get_ref (), fn->get_ty_ref (), ident.locus, + std::move (cloned_params), retty, + fn->get_combined_refs ()); + } + else if (auto adt = x->try_as ()) + { + std::vector cloned_variants; + for (auto &variant : adt->get_variants ()) + cloned_variants.push_back (variant->monomorphized_clone ()); + + return new ADTType (adt->get_ref (), adt->get_ty_ref (), + adt->get_identifier (), adt->ident, + adt->get_adt_kind (), cloned_variants, + adt->clone_substs (), adt->get_repr_options (), + adt->get_used_arguments (), + adt->get_combined_refs ()); + } + else + { + return x->clone (); } rust_unreachable (); @@ -714,122 +660,94 @@ bool BaseType::is_concrete () const { const TyTy::BaseType *x = destructure (); - switch (x->get_kind ()) + + if (x->is () || x->is ()) { - 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: + } + // 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 + else if (x->is ()) + { return true; + } + else if (auto fn = x->try_as ()) + { + for (const auto ¶m : fn->get_params ()) + { + if (!param.second->is_concrete ()) + return false; + } + return fn->get_return_type ()->is_concrete (); + } + else if (auto fn = x->try_as ()) + { + for (const auto ¶m : fn->get_params ()) + { + if (!param.get_tyty ()->is_concrete ()) + return false; + } + return fn->get_return_type ()->is_concrete (); + } + else if (auto adt = x->try_as ()) + { + if (adt->is_unit ()) + return !adt->needs_substitution (); - case FNDEF: { - const FnType &fn = *static_cast (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 (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 (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 (x); - return arr.get_element_type ()->is_concrete (); - } - break; - - case SLICE: { - const SliceType &slice = *static_cast (x); - return slice.get_element_type ()->is_concrete (); - } - break; - - case POINTER: { - const PointerType &ptr = *static_cast (x); - return ptr.get_base ()->is_concrete (); - } - break; - - case REF: { - const ReferenceType &ref = *static_cast (x); - return ref.get_base ()->is_concrete (); - } - break; - - case TUPLE: { - const TupleType &tuple = *static_cast (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 (x); - if (closure.get_parameters ().is_concrete ()) - return false; - return closure.get_result_type ().is_concrete (); - } - break; + for (auto &variant : adt->get_variants ()) + { + bool is_num_variant + = variant->get_variant_type () == VariantDef::VariantType::NUM; + if (is_num_variant) + continue; - case INFER: - case BOOL: - case CHAR: - case INT: - case UINT: - case FLOAT: - case USIZE: - case ISIZE: - case NEVER: - case STR: - case DYNAMIC: - case ERROR: + for (auto &field : variant->get_fields ()) + { + const BaseType *field_type = field->get_field_type (); + if (!field_type->is_concrete ()) + return false; + } + } + return true; + } + else if (auto arr = x->try_as ()) + { + return arr->get_element_type ()->is_concrete (); + } + else if (auto slice = x->try_as ()) + { + return slice->get_element_type ()->is_concrete (); + } + else if (auto ptr = x->try_as ()) + { + return ptr->get_base ()->is_concrete (); + } + else if (auto ref = x->try_as ()) + { + return ref->get_base ()->is_concrete (); + } + else if (auto tuple = x->try_as ()) + { + for (size_t i = 0; i < tuple->num_fields (); i++) + { + if (!tuple->get_field (i)->is_concrete ()) + return false; + } + return true; + } + else if (auto closure = x->try_as ()) + { + if (closure->get_parameters ().is_concrete ()) + return false; + return closure->get_result_type ().is_concrete (); + } + else if (x->is () || x->is () || x->is () + || x->is () || x->is () || x->is () + || x->is () || x->is () || x->is () + || x->is () || x->is () + || x->is ()) + { return true; } @@ -962,14 +880,14 @@ BaseType::needs_generic_substitutions () const InferType::InferType (HirId ref, InferTypeKind infer_kind, TypeHint hint, location_t locus, std::set 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 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) {} @@ -1197,10 +1115,9 @@ InferType::apply_primitive_type_hint (const BaseType &hint) case INT: { infer_kind = INTEGRAL; - const IntType &i = static_cast (hint); default_hint.kind = hint.get_kind (); default_hint.shint = TypeHint::SignedHint::SIGNED; - switch (i.get_int_kind ()) + switch (hint.as ()->get_int_kind ()) { case IntType::I8: default_hint.szhint = TypeHint::SizeHint::S8; @@ -1223,10 +1140,9 @@ InferType::apply_primitive_type_hint (const BaseType &hint) case UINT: { infer_kind = INTEGRAL; - const UintType &i = static_cast (hint); default_hint.kind = hint.get_kind (); default_hint.shint = TypeHint::SignedHint::UNSIGNED; - switch (i.get_uint_kind ()) + switch (hint.as ()->get_uint_kind ()) { case UintType::U8: default_hint.szhint = TypeHint::SizeHint::S8; @@ -1251,8 +1167,7 @@ InferType::apply_primitive_type_hint (const BaseType &hint) infer_kind = FLOAT; default_hint.shint = TypeHint::SignedHint::SIGNED; default_hint.kind = hint.get_kind (); - const FloatType &i = static_cast (hint); - switch (i.get_float_kind ()) + switch (hint.as ()->get_float_kind ()) { case FloatType::F32: default_hint.szhint = TypeHint::SizeHint::S32; @@ -1274,12 +1189,12 @@ InferType::apply_primitive_type_hint (const BaseType &hint) // ErrorType ErrorType::ErrorType (HirId ref, std::set 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 refs) - : BaseType (ref, ty_ref, TypeKind::ERROR, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), UNDEF_LOCATION}, refs) {} @@ -1371,14 +1286,11 @@ StructFieldType::as_string () const bool StructFieldType::is_equal (const StructFieldType &other) const { - bool names_eq = get_name ().compare (other.get_name ()) == 0; + bool names_eq = get_name () == other.get_name (); TyTy::BaseType *o = other.get_field_type (); - if (o->get_kind () == TypeKind::PARAM) - { - ParamType *op = static_cast (o); - o = op->resolve (); - } + if (auto op = o->try_as ()) + o = op->resolve (); bool types_eq = get_field_type ()->is_equal (*o); @@ -1673,25 +1585,25 @@ ADTType::is_equal (const BaseType &other) const if (get_kind () != other.get_kind ()) return false; - auto other2 = static_cast (other); - if (get_adt_kind () != other2.get_adt_kind ()) + auto other2 = other.as (); + if (get_adt_kind () != other2->get_adt_kind ()) return false; - if (number_of_variants () != other2.number_of_variants ()) + if (number_of_variants () != other2->number_of_variants ()) return false; - if (has_substitutions_defined () != other2.has_substitutions_defined ()) + if (has_substitutions_defined () != other2->has_substitutions_defined ()) return false; if (has_substitutions_defined ()) { - if (get_num_substitutions () != other2.get_num_substitutions ()) + if (get_num_substitutions () != other2->get_num_substitutions ()) return false; for (size_t i = 0; i < get_num_substitutions (); i++) { const SubstitutionParamMapping &a = substitutions.at (i); - const SubstitutionParamMapping &b = other2.substitutions.at (i); + const SubstitutionParamMapping &b = other2->substitutions.at (i); const ParamType *aa = a.get_param_ty (); const ParamType *bb = b.get_param_ty (); @@ -1705,7 +1617,7 @@ ADTType::is_equal (const BaseType &other) const for (size_t i = 0; i < number_of_variants (); i++) { const TyTy::VariantDef *a = get_variants ().at (i); - const TyTy::VariantDef *b = other2.get_variants ().at (i); + const TyTy::VariantDef *b = other2->get_variants ().at (i); if (!a->is_equal (*b)) return false; @@ -1732,11 +1644,8 @@ handle_substitions (SubstitutionArgumentMappings &subst_mappings, StructFieldType *field) { auto fty = field->get_field_type (); - bool is_param_ty = fty->get_kind () == TypeKind::PARAM; - if (is_param_ty) + if (auto p = fty->try_as ()) { - ParamType *p = static_cast (fty); - SubstitutionArg arg = SubstitutionArg::error (); bool ok = subst_mappings.get_argument_for_symbol (p, &arg); if (ok) @@ -1781,7 +1690,7 @@ handle_substitions (SubstitutionArgumentMappings &subst_mappings, ADTType * ADTType::handle_substitions (SubstitutionArgumentMappings &subst_mappings) { - ADTType *adt = static_cast (clone ()); + auto adt = clone ()->as (); adt->set_ty_ref (mappings->get_next_hir_id ()); adt->used_arguments = subst_mappings; @@ -1814,14 +1723,14 @@ ADTType::handle_substitions (SubstitutionArgumentMappings &subst_mappings) TupleType::TupleType (HirId ref, location_t locus, std::vector fields, std::set 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 fields, std::set refs) - : BaseType (ref, ty_ref, TypeKind::TUPLE, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), locus}, refs), fields (fields) {} @@ -1905,13 +1814,13 @@ TupleType::is_equal (const BaseType &other) const if (get_kind () != other.get_kind ()) return false; - auto other2 = static_cast (other); - if (num_fields () != other2.num_fields ()) + auto other2 = other.as (); + if (num_fields () != other2->num_fields ()) return false; for (size_t i = 0; i < num_fields (); i++) { - if (!get_field (i)->is_equal (*other2.get_field (i))) + if (!get_field (i)->is_equal (*other2->get_field (i))) return false; } return true; @@ -1933,7 +1842,7 @@ TupleType::handle_substitions (SubstitutionArgumentMappings &mappings) { auto mappings_table = Analysis::Mappings::get (); - TupleType *tuple = static_cast (clone ()); + auto tuple = clone ()->as (); tuple->set_ref (mappings_table->get_next_hir_id ()); tuple->set_ty_ref (mappings_table->get_next_hir_id ()); @@ -2487,13 +2396,13 @@ SliceType::handle_substitions (SubstitutionArgumentMappings &mappings) // BoolType BoolType::BoolType (HirId ref, std::set 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 refs) - : BaseType (ref, ty_ref, TypeKind::BOOL, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs) {} @@ -2538,14 +2447,14 @@ BoolType::clone () const // IntType IntType::IntType (HirId ref, IntKind kind, std::set 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 refs) - : BaseType (ref, ty_ref, TypeKind::INT, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs), int_kind (kind) @@ -2622,7 +2531,7 @@ IntType::is_equal (const BaseType &other) const // UintType UintType::UintType (HirId ref, UintKind kind, std::set refs) - : BaseType (ref, ref, TypeKind::UINT, + : BaseType (ref, ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs), uint_kind (kind) @@ -2630,7 +2539,7 @@ UintType::UintType (HirId ref, UintKind kind, std::set refs) UintType::UintType (HirId ref, HirId ty_ref, UintKind kind, std::set refs) - : BaseType (ref, ty_ref, TypeKind::UINT, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs), uint_kind (kind) @@ -2707,7 +2616,7 @@ UintType::is_equal (const BaseType &other) const // FloatType FloatType::FloatType (HirId ref, FloatKind kind, std::set refs) - : BaseType (ref, ref, TypeKind::FLOAT, + : BaseType (ref, ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs), float_kind (kind) @@ -2715,7 +2624,7 @@ FloatType::FloatType (HirId ref, FloatKind kind, std::set refs) FloatType::FloatType (HirId ref, HirId ty_ref, FloatKind kind, std::set refs) - : BaseType (ref, ty_ref, TypeKind::FLOAT, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs), float_kind (kind) @@ -2786,13 +2695,13 @@ FloatType::is_equal (const BaseType &other) const // UsizeType USizeType::USizeType (HirId ref, std::set 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 refs) - : BaseType (ref, ty_ref, TypeKind::USIZE, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs) {} @@ -2837,13 +2746,13 @@ USizeType::clone () const // ISizeType ISizeType::ISizeType (HirId ref, std::set 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 refs) - : BaseType (ref, ty_ref, TypeKind::ISIZE, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs) {} @@ -2888,13 +2797,13 @@ ISizeType::clone () const // Char Type CharType::CharType (HirId ref, std::set 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 refs) - : BaseType (ref, ty_ref, TypeKind::CHAR, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs) {} @@ -2940,7 +2849,7 @@ CharType::clone () const ReferenceType::ReferenceType (HirId ref, TyVar base, Mutability mut, std::set refs) - : BaseType (ref, ref, TypeKind::REF, + : BaseType (ref, ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs), base (base), mut (mut) @@ -2948,7 +2857,7 @@ ReferenceType::ReferenceType (HirId ref, TyVar base, Mutability mut, ReferenceType::ReferenceType (HirId ref, HirId ty_ref, TyVar base, Mutability mut, std::set refs) - : BaseType (ref, ty_ref, TypeKind::REF, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs), base (base), mut (mut) @@ -3096,7 +3005,7 @@ ReferenceType::handle_substitions (SubstitutionArgumentMappings &mappings) PointerType::PointerType (HirId ref, TyVar base, Mutability mut, std::set refs) - : BaseType (ref, ref, TypeKind::POINTER, + : BaseType (ref, ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs), base (base), mut (mut) @@ -3104,7 +3013,7 @@ PointerType::PointerType (HirId ref, TyVar base, Mutability mut, PointerType::PointerType (HirId ref, HirId ty_ref, TyVar base, Mutability mut, std::set refs) - : BaseType (ref, ty_ref, TypeKind::POINTER, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs), base (base), mut (mut) @@ -3260,7 +3169,7 @@ ParamType::ParamType (std::string symbol, location_t locus, HirId ref, HIR::GenericParam ¶m, std::vector specified_bounds, std::set refs) - : BaseType (ref, ref, TypeKind::PARAM, + : BaseType (ref, ref, KIND, {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), locus}, specified_bounds, refs), @@ -3271,7 +3180,7 @@ ParamType::ParamType (bool is_trait_self, std::string symbol, location_t locus, HirId ref, HirId ty_ref, HIR::GenericParam ¶m, std::vector specified_bounds, std::set refs) - : BaseType (ref, ty_ref, TypeKind::PARAM, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), locus}, specified_bounds, refs), @@ -3434,13 +3343,13 @@ ParamType::is_implicit_self_trait () const // StrType StrType::StrType (HirId ref, std::set 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 refs) - : BaseType (ref, ty_ref, TypeKind::STR, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs) {} @@ -3491,13 +3400,13 @@ StrType::is_equal (const BaseType &other) const // Never Type NeverType::NeverType (HirId ref, std::set 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 refs) - : BaseType (ref, ty_ref, TypeKind::NEVER, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs) {} @@ -3543,7 +3452,7 @@ NeverType::clone () const PlaceholderType::PlaceholderType (std::string symbol, HirId ref, std::set refs) - : BaseType (ref, ref, TypeKind::PLACEHOLDER, + : BaseType (ref, ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs), symbol (symbol) @@ -3551,7 +3460,7 @@ PlaceholderType::PlaceholderType (std::string symbol, HirId ref, PlaceholderType::PlaceholderType (std::string symbol, HirId ref, HirId ty_ref, std::set refs) - : BaseType (ref, ty_ref, TypeKind::PLACEHOLDER, + : BaseType (ref, ty_ref, KIND, {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, refs), symbol (symbol) @@ -3656,7 +3565,7 @@ ProjectionType::ProjectionType ( HirId ref, BaseType *base, const Resolver::TraitReference *trait, DefId item, std::vector subst_refs, SubstitutionArgumentMappings generic_arguments, std::set 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)), @@ -3668,7 +3577,7 @@ ProjectionType::ProjectionType ( const Resolver::TraitReference *trait, DefId item, std::vector subst_refs, SubstitutionArgumentMappings generic_arguments, std::set 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)), @@ -3730,7 +3639,8 @@ ProjectionType::handle_substitions ( SubstitutionArgumentMappings &subst_mappings) { // // do we really need to substitute this? - // if (base->needs_generic_substitutions () || base->contains_type_parameters + // if (base->needs_generic_substitutions () || + // base->contains_type_parameters // ()) // { // return this; @@ -3802,13 +3712,13 @@ ProjectionType::handle_substitions ( DynamicObjectType::DynamicObjectType ( HirId ref, RustIdent ident, std::vector specified_bounds, std::set 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 specified_bounds, std::set refs) - : BaseType (ref, ty_ref, TypeKind::DYNAMIC, ident, specified_bounds, refs) + : BaseType (ref, ty_ref, KIND, ident, specified_bounds, refs) {} void diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index f146b6b8e3fb..6391bd7be73f 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -31,7 +31,9 @@ namespace Rust { namespace Resolver { class TraitReference; + class TraitItemReference; + class AssociatedImplTrait; } // namespace Resolver @@ -116,9 +118,12 @@ class BaseType : public TypeBoundsMappings 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 &specified_bounds); @@ -138,10 +143,13 @@ class BaseType : public TypeBoundsMappings // get_combined_refs returns the chain of node refs involved in unification std::set 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 @@ -168,6 +176,55 @@ class BaseType : public TypeBoundsMappings * releasing the memory of the returned ty. */ virtual BaseType *clone () const = 0; + // Check if TyTy::BaseType is of a specific type. + template [[nodiscard]] bool is () const + { + static_assert (std::is_base_of::value, + "Can only safely cast to TyTy types."); + return this->get_kind () == T::KIND; + } + + template T *as () const + { + static_assert (std::is_base_of::value, + "Can only safely cast to TyTy types."); + rust_assert (this->is ()); + return static_cast (this); + } + + template T *as () + { + static_assert (std::is_base_of::value, + "Can only safely cast to TyTy types."); + rust_assert (this->is ()); + return static_cast (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 T *try_as () const + { + static_assert (std::is_base_of::value, + "Can only safely cast to TyTy types."); + if (!this->is ()) + return nullptr; + + return static_cast (this); + } + + // See above. + template T *try_as () + { + static_assert (std::is_base_of::value, + "Can only safely cast to TyTy types."); + if (!this->is ()) + return nullptr; + + return static_cast (this); + } + protected: BaseType (HirId ref, HirId ty_ref, TypeKind kind, RustIdent ident, std::set refs = std::set ()); @@ -188,6 +245,8 @@ class BaseType : public TypeBoundsMappings class InferType : public BaseType { public: + static constexpr auto KIND = TypeKind::INFER; + enum InferTypeKind { GENERAL, @@ -231,6 +290,7 @@ class InferType : public BaseType location_t locus, std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; @@ -255,6 +315,8 @@ class InferType : public BaseType class ErrorType : public BaseType { public: + static constexpr auto KIND = TypeKind::ERROR; + ErrorType (HirId ref, std::set refs = std::set ()); ErrorType (HirId ref, HirId ty_ref, @@ -275,6 +337,8 @@ class ErrorType : public BaseType 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 specified_bounds, @@ -348,6 +412,8 @@ class StructFieldType class TupleType : public BaseType { public: + static constexpr auto KIND = TypeKind::TUPLE; + TupleType (HirId ref, location_t locus, std::vector fields = std::vector (), std::set refs = std::set ()); @@ -511,7 +577,9 @@ class VariantDef std::string as_string () const; bool is_equal (const VariantDef &other) const; + VariantDef *clone () const; + VariantDef *monomorphized_clone () const; const RustIdent &get_ident () const; @@ -530,6 +598,8 @@ class VariantDef class ADTType : public BaseType, public SubstitutionRef { public: + static constexpr auto KIND = TypeKind::ADT; + enum ADTKind { STRUCT_STRUCT, @@ -587,14 +657,19 @@ class ADTType : public BaseType, public SubstitutionRef {} 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; @@ -615,6 +690,7 @@ class ADTType : public BaseType, public SubstitutionRef size_t number_of_variants () const { return variants.size (); } std::vector &get_variants () { return variants; } + const std::vector &get_variants () const { return variants; } bool lookup_variant (const std::string &lookup, @@ -663,6 +739,8 @@ class ADTType : public BaseType, public SubstitutionRef 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; @@ -776,6 +854,8 @@ class FnType : public BaseType, public SubstitutionRef class FnPtr : public BaseType { public: + static constexpr auto KIND = TypeKind::FNPTR; + FnPtr (HirId ref, location_t locus, std::vector params, TyVar result_type, std::set refs = std::set ()) : BaseType (ref, ref, TypeKind::FNPTR, @@ -821,6 +901,8 @@ class FnPtr : public BaseType 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 subst_refs, @@ -891,6 +973,8 @@ class ClosureType : public BaseType, public SubstitutionRef 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 refs = std::set ()) : BaseType (ref, ref, TypeKind::ARRAY, @@ -934,6 +1018,8 @@ class ArrayType : public BaseType class SliceType : public BaseType { public: + static constexpr auto KIND = TypeKind::SLICE; + SliceType (HirId ref, location_t locus, TyVar base, std::set refs = std::set ()) : BaseType (ref, ref, TypeKind::SLICE, @@ -973,6 +1059,8 @@ class SliceType : public BaseType class BoolType : public BaseType { public: + static constexpr auto KIND = TypeKind::BOOL; + BoolType (HirId ref, std::set refs = std::set ()); BoolType (HirId ref, HirId ty_ref, std::set refs = std::set ()); @@ -1000,6 +1088,8 @@ class IntType : public BaseType I128 }; + static constexpr auto KIND = TypeKind::INT; + IntType (HirId ref, IntKind kind, std::set refs = std::set ()); IntType (HirId ref, HirId ty_ref, IntKind kind, std::set refs = std::set ()); @@ -1026,6 +1116,8 @@ class IntType : public BaseType class UintType : public BaseType { public: + static constexpr auto KIND = TypeKind::UINT; + enum UintKind { U8, @@ -1062,6 +1154,8 @@ class UintType : public BaseType class FloatType : public BaseType { public: + static constexpr auto KIND = TypeKind::FLOAT; + enum FloatKind { F32, @@ -1094,6 +1188,8 @@ class FloatType : public BaseType class USizeType : public BaseType { public: + static constexpr auto KIND = TypeKind::USIZE; + USizeType (HirId ref, std::set refs = std::set ()); USizeType (HirId ref, HirId ty_ref, std::set refs = std::set ()); @@ -1112,6 +1208,8 @@ class USizeType : public BaseType class ISizeType : public BaseType { public: + static constexpr auto KIND = TypeKind::ISIZE; + ISizeType (HirId ref, std::set refs = std::set ()); ISizeType (HirId ref, HirId ty_ref, std::set refs = std::set ()); @@ -1130,6 +1228,8 @@ class ISizeType : public BaseType class CharType : public BaseType { public: + static constexpr auto KIND = TypeKind::CHAR; + CharType (HirId ref, std::set refs = std::set ()); CharType (HirId ref, HirId ty_ref, std::set refs = std::set ()); @@ -1147,6 +1247,8 @@ class CharType : public BaseType class StrType : public BaseType { public: + static constexpr auto KIND = TypeKind::STR; + StrType (HirId ref, std::set refs = std::set ()); StrType (HirId ref, HirId ty_ref, std::set refs = std::set ()); @@ -1167,6 +1269,8 @@ class StrType : public BaseType class DynamicObjectType : public BaseType { public: + static constexpr auto KIND = TypeKind::DYNAMIC; + DynamicObjectType (HirId ref, RustIdent ident, std::vector specified_bounds, std::set refs = std::set ()); @@ -1197,6 +1301,8 @@ class DynamicObjectType : public BaseType class ReferenceType : public BaseType { public: + static constexpr auto KIND = TypeKind::REF; + ReferenceType (HirId ref, TyVar base, Mutability mut, std::set refs = std::set ()); ReferenceType (HirId ref, HirId ty_ref, TyVar base, Mutability mut, @@ -1236,6 +1342,8 @@ class ReferenceType : public BaseType class PointerType : public BaseType { public: + static constexpr auto KIND = TypeKind::POINTER; + PointerType (HirId ref, TyVar base, Mutability mut, std::set refs = std::set ()); PointerType (HirId ref, HirId ty_ref, TyVar base, Mutability mut, @@ -1284,11 +1392,15 @@ class PointerType : public BaseType class NeverType : public BaseType { public: + static constexpr auto KIND = TypeKind::NEVER; + NeverType (HirId ref, std::set refs = std::set ()); + NeverType (HirId ref, HirId ty_ref, std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; @@ -1305,6 +1417,8 @@ class NeverType : public BaseType class PlaceholderType : public BaseType { public: + static constexpr auto KIND = TypeKind::PLACEHOLDER; + PlaceholderType (std::string symbol, HirId ref, std::set refs = std::set ()); PlaceholderType (std::string symbol, HirId ref, HirId ty_ref, @@ -1340,6 +1454,8 @@ class PlaceholderType : public BaseType 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 subst_refs,