| // Copyright (C) 2020-2026 Free Software Foundation, Inc. | |
| // This file is part of GCC. | |
| // GCC is free software; you can redistribute it and/or modify it under | |
| // the terms of the GNU General Public License as published by the Free | |
| // Software Foundation; either version 3, or (at your option) any later | |
| // version. | |
| // GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
| // WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| // for more details. | |
| // You should have received a copy of the GNU General Public License | |
| // along with GCC; see the file COPYING3. If not see | |
| // <http://www.gnu.org/licenses/>. | |
| #include "rust-system.h" | |
| #include "rust-tyty.h" | |
| #include "rust-tyty-subst.h" | |
| #include "rust-tyty-visitor.h" | |
| #include "rust-hir-map.h" | |
| #include "rust-location.h" | |
| #include "rust-type-util.h" | |
| #include "rust-hir-type-bounds.h" | |
| #include "rust-substitution-mapper.h" | |
| #include "rust-hir-trait-reference.h" | |
| #include "rust-hir-trait-resolve.h" | |
| #include "rust-hir-type-check.h" | |
| #include "tree-pretty-print.h" | |
| #include "optional.h" | |
| #include "options.h" | |
| #include "tree.h" | |
| #include "fold-const.h" | |
| namespace Rust { | |
| namespace TyTy { | |
| std::string | |
| TypeKindFormat::to_string (TypeKind kind) | |
| { | |
| switch (kind) | |
| { | |
| case TypeKind::INFER: | |
| return "Infer"; | |
| case TypeKind::ADT: | |
| return "ADT"; | |
| case TypeKind::STR: | |
| return "STR"; | |
| case TypeKind::REF: | |
| return "REF"; | |
| case TypeKind::POINTER: | |
| return "POINTER"; | |
| case TypeKind::PARAM: | |
| return "PARAM"; | |
| case TypeKind::ARRAY: | |
| return "ARRAY"; | |
| case TypeKind::SLICE: | |
| return "SLICE"; | |
| case TypeKind::FNDEF: | |
| return "FnDef"; | |
| case TypeKind::FNPTR: | |
| return "FnPtr"; | |
| case TypeKind::TUPLE: | |
| return "Tuple"; | |
| case TypeKind::BOOL: | |
| return "Bool"; | |
| case TypeKind::CHAR: | |
| return "Char"; | |
| case TypeKind::INT: | |
| return "Int"; | |
| case TypeKind::UINT: | |
| return "Uint"; | |
| case TypeKind::FLOAT: | |
| return "Float"; | |
| case TypeKind::USIZE: | |
| return "Usize"; | |
| case TypeKind::ISIZE: | |
| return "Isize"; | |
| case TypeKind::NEVER: | |
| return "Never"; | |
| case TypeKind::PLACEHOLDER: | |
| return "Placeholder"; | |
| case TypeKind::PROJECTION: | |
| return "Projection"; | |
| case TypeKind::DYNAMIC: | |
| return "Dynamic"; | |
| case TypeKind::CLOSURE: | |
| return "Closure"; | |
| case TypeKind::OPAQUE: | |
| return "Opaque"; | |
| case TypeKind::CONST: | |
| return "Const"; | |
| case TypeKind::ERROR: | |
| return "ERROR"; | |
| } | |
| rust_unreachable (); | |
| } | |
| bool | |
| is_primitive_type_kind (TypeKind kind) | |
| { | |
| switch (kind) | |
| { | |
| case TypeKind::BOOL: | |
| case TypeKind::CHAR: | |
| case TypeKind::INT: | |
| case TypeKind::UINT: | |
| case TypeKind::ISIZE: | |
| case TypeKind::USIZE: | |
| case TypeKind::FLOAT: | |
| case TypeKind::NEVER: | |
| case TypeKind::STR: | |
| return true; | |
| default: | |
| return false; | |
| } | |
| } | |
| // 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), | |
| orig_ref (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 (std::move (specified_bounds)), kind (kind), ref (ref), | |
| ty_ref (ty_ref), orig_ref (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; | |
| } | |
| HirId | |
| BaseType::get_orig_ref () const | |
| { | |
| return orig_ref; | |
| } | |
| bool | |
| BaseType::is_equal (const BaseType &other) const | |
| { | |
| return get_kind () == other.get_kind (); | |
| } | |
| bool | |
| BaseType::is_unit () const | |
| { | |
| const TyTy::BaseType *x = destructure (); | |
| switch (x->get_kind ()) | |
| { | |
| case PARAM: | |
| case PROJECTION: | |
| case PLACEHOLDER: | |
| case FNPTR: | |
| case FNDEF: | |
| case ARRAY: | |
| case SLICE: | |
| case POINTER: | |
| case REF: | |
| case CLOSURE: | |
| case INFER: | |
| case BOOL: | |
| case CHAR: | |
| case INT: | |
| case UINT: | |
| case FLOAT: | |
| case USIZE: | |
| case ISIZE: | |
| case OPAQUE: | |
| case STR: | |
| case DYNAMIC: | |
| case CONST: | |
| case ERROR: | |
| return false; | |
| // FIXME ! is coerceable to () so we need to fix that | |
| case NEVER: | |
| return true; | |
| case TUPLE: | |
| { | |
| return x->as<const TupleType> ()->num_fields () == 0; | |
| } | |
| case ADT: | |
| { | |
| auto adt = x->as<const ADTType> (); | |
| if (adt->is_enum ()) | |
| return false; | |
| for (const auto &variant : adt->get_variants ()) | |
| { | |
| if (variant->num_fields () > 0) | |
| return false; | |
| } | |
| return true; | |
| } | |
| } | |
| 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); | |
| } | |
| const RustIdent & | |
| BaseType::get_ident () const | |
| { | |
| return ident; | |
| } | |
| location_t | |
| BaseType::get_locus () const | |
| { | |
| return ident.locus; | |
| } | |
| bool | |
| BaseType::unsize_to (const BaseType *target) const | |
| { | |
| if (this->get_kind () == TyTy::TypeKind::DYNAMIC | |
| && target->get_kind () == TyTy::TypeKind::DYNAMIC) | |
| { | |
| const auto *source_dyn = this->as<const TyTy::DynamicObjectType> (); | |
| const auto *target_dyn = target->as<const TyTy::DynamicObjectType> (); | |
| const auto &source_bounds = source_dyn->get_specified_bounds (); | |
| const auto &target_bounds = target_dyn->get_specified_bounds (); | |
| if (source_bounds.empty () || target_bounds.empty ()) | |
| return false; | |
| if (source_bounds.at (0).get_id () != target_bounds.at (0).get_id ()) | |
| return false; | |
| for (const auto &t_bound : target_dyn->get_specified_bounds ()) | |
| { | |
| bool found = false; | |
| for (const auto &s_bound : source_dyn->get_specified_bounds ()) | |
| { | |
| if (s_bound.get_id () == t_bound.get_id ()) | |
| { | |
| found = true; | |
| break; | |
| } | |
| } | |
| if (!found) | |
| return false; | |
| } | |
| return true; | |
| } | |
| // `T` -> `Trait` | |
| else if (target->get_kind () == TyTy::TypeKind::DYNAMIC) | |
| return true; | |
| // Ambiguous handling is below `T` -> `Trait`, because inference | |
| // variables can still implement `Unsize<Trait>` and nested | |
| // obligations will have the final say (likely deferred). | |
| else if (this->destructure ()->is<InferType> () | |
| || target->destructure ()->is<InferType> ()) | |
| return true; | |
| // `[T; n]` -> `[T]` | |
| else if (this->get_kind () == TyTy::TypeKind::ARRAY | |
| && target->get_kind () == TyTy::TypeKind::SLICE) | |
| return true; | |
| // `Struct<T>` -> `Struct<U>` | |
| else if (this->get_kind () == TyTy::TypeKind::ADT | |
| && target->get_kind () == TyTy::TypeKind::ADT) | |
| { | |
| const auto *source_adt = this->as<const TyTy::ADTType> (); | |
| const auto *target_adt = target->as<const TyTy::ADTType> (); | |
| return (source_adt->is_struct_struct () || source_adt->is_tuple_struct ()) | |
| && source_adt->get_id () == target_adt->get_id (); | |
| } | |
| // `(.., T)` -> `(.., U)` | |
| else if (this->get_kind () == TyTy::TypeKind::TUPLE | |
| && target->get_kind () == TyTy::TypeKind::TUPLE) | |
| { | |
| const auto *source_tuple = this->as<const TyTy::TupleType> (); | |
| const auto *target_tuple = target->as<const TyTy::TupleType> (); | |
| return source_tuple->get_subst_argument_mappings ().size () | |
| == target_tuple->get_subst_argument_mappings ().size (); | |
| } | |
| return false; | |
| } | |
| // FIXME this is missing locus | |
| bool | |
| BaseType::satisfies_bound (const TypeBoundPredicate &predicate, bool emit_error) | |
| { | |
| // see: | |
| // https://github.com/rust-lang/rust/blob/e1884a8e3c3e813aada8254edfa120e85bf5ffca/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs#L673 | |
| if (auto unsize_id = mappings.lookup_lang_item (Rust::LangItem::Kind::UNSIZE)) | |
| { | |
| if (predicate.get_id () == unsize_id) | |
| { | |
| const auto &args = predicate.get_substitution_arguments (); | |
| rust_assert (args.size () == 2 && "Unsize<U>"); | |
| // When this target is defined as `U`, it does not give us the | |
| // realized argument type. Instead, it returns `PARAM`. | |
| TyTy::BaseType *target | |
| = args.get_mappings ().at (1).get_param_ty ()->resolve (); | |
| return this->unsize_to (target); | |
| } | |
| } | |
| const Resolver::TraitReference *query = predicate.get (); | |
| for (const auto &bound : specified_bounds) | |
| { | |
| const Resolver::TraitReference *item = bound.get (); | |
| if (item->satisfies_bound (*query)) | |
| return true; | |
| } | |
| if (destructure ()->is<InferType> ()) | |
| return true; | |
| bool satisfied = false; | |
| auto tref = predicate.get (); | |
| auto probed | |
| = Resolver::TypeBoundsProbe::Probe (this, tref->get_hir_trait_ref ()); | |
| for (const auto &b : probed) | |
| { | |
| const Resolver::TraitReference *bound = b.first; | |
| if (bound->satisfies_bound (*query)) | |
| { | |
| satisfied = true; | |
| break; | |
| } | |
| } | |
| if (!satisfied) | |
| return false; | |
| for (const auto &b : probed) | |
| { | |
| const Resolver::TraitReference *bound = b.first; | |
| if (!bound->is_equal (*query)) | |
| continue; | |
| // builtin ones have no impl-block this needs fixed and use a builtin | |
| // node of somekind | |
| if (b.second == nullptr) | |
| { | |
| return predicate.get_polarity () == BoundPolarity::RegularBound; | |
| } | |
| else | |
| { | |
| const auto &impl = *(b.second); | |
| if (predicate.get_polarity () != impl.get_polarity ()) | |
| continue; | |
| } | |
| // need to check that associated types can match as well | |
| const HIR::ImplBlock &impl = *(b.second); | |
| for (const auto &item : impl.get_impl_items ()) | |
| { | |
| bool is_associated_type = item->get_impl_item_type () | |
| == HIR::ImplItem::ImplItemType::TYPE_ALIAS; | |
| if (!is_associated_type) | |
| continue; | |
| std::string item_name = item->get_impl_item_name (); | |
| tl::optional<TypeBoundPredicateItem> lookup | |
| = predicate.lookup_associated_item (item_name); | |
| if (!lookup.has_value ()) | |
| return false; | |
| const auto *item_ref = lookup->get_raw_item (); | |
| TyTy::BaseType *bound_ty = item_ref->get_tyty (); | |
| const auto &bindings | |
| = predicate.get_substitution_arguments ().get_binding_args (); | |
| auto bind_it = bindings.find (item_name); | |
| if (bind_it != bindings.end ()) | |
| bound_ty = bind_it->second; | |
| else if (auto *proj = bound_ty->try_as<TyTy::ProjectionType> ()) | |
| if (proj->is_trait_position ()) | |
| continue; | |
| TyTy::BaseType *impl_item_ty = nullptr; | |
| Analysis::NodeMapping i = item->get_impl_mappings (); | |
| bool query_ok = Resolver::query_type (i.get_hirid (), &impl_item_ty); | |
| if (!query_ok) | |
| return false; | |
| // If the impl alias still depends on the impl's own generics | |
| // defer the binding check to monomorphization. | |
| // | |
| // The receiver-vs-impl substitution that pins T = i32 only | |
| // happens with a committing unification at the call site | |
| bool impl_item_concrete = impl_item_ty->is_concrete (); | |
| if (auto *p = impl_item_ty->try_as<TyTy::ProjectionType> ()) | |
| if (!p->is_trait_position () && p->get () != nullptr) | |
| impl_item_concrete = p->get ()->is_concrete (); | |
| if (!impl_item_concrete) | |
| continue; | |
| if (!Resolver::types_compatable ( | |
| TyTy::TyWithLocation (bound_ty, predicate.get_locus ()), | |
| TyTy::TyWithLocation (impl_item_ty, item->get_locus ()), | |
| mappings.lookup_location (get_ref ()), false /*emit-error*/, | |
| false /*check-bounds*/)) | |
| return false; | |
| } | |
| return true; | |
| } | |
| return false; | |
| } | |
| bool | |
| BaseType::bounds_compatible (BaseType &other, location_t locus, bool emit_error) | |
| { | |
| std::vector<std::reference_wrapper<const TypeBoundPredicate>> | |
| unsatisfied_bounds; | |
| for (auto &bound : get_specified_bounds ()) | |
| { | |
| if (!other.satisfies_bound (bound, emit_error)) | |
| unsatisfied_bounds.push_back (bound); | |
| } | |
| // lets emit a single error for this | |
| if (unsatisfied_bounds.size () > 0) | |
| { | |
| rich_location r (line_table, locus); | |
| std::string missing_preds; | |
| for (size_t i = 0; i < unsatisfied_bounds.size (); i++) | |
| { | |
| const TypeBoundPredicate &pred = unsatisfied_bounds.at (i); | |
| r.add_range (pred.get_locus ()); | |
| missing_preds += pred.get_name (); | |
| bool have_next = (i + 1) < unsatisfied_bounds.size (); | |
| if (have_next) | |
| missing_preds += ", "; | |
| } | |
| if (emit_error) | |
| { | |
| rust_error_at (r, ErrorCode::E0277, | |
| "bounds not satisfied for %s %qs is not satisfied", | |
| other.get_name ().c_str (), missing_preds.c_str ()); | |
| // rust_assert (!emit_error); | |
| } | |
| } | |
| return unsatisfied_bounds.size () == 0; | |
| } | |
| void | |
| BaseType::inherit_bounds (const BaseType &other) | |
| { | |
| inherit_bounds (other.get_specified_bounds ()); | |
| } | |
| void | |
| BaseType::inherit_bound (const TypeBoundPredicate &bound) | |
| { | |
| add_bound (bound); | |
| } | |
| void | |
| BaseType::inherit_bounds ( | |
| const std::vector<TyTy::TypeBoundPredicate> &specified_bounds) | |
| { | |
| for (auto &bound : specified_bounds) | |
| { | |
| add_bound (bound); | |
| } | |
| } | |
| BaseType * | |
| BaseType::get_root () | |
| { | |
| TyTy::BaseType *root = this; | |
| if (const auto r = root->try_as<const ReferenceType> ()) | |
| { | |
| root = r->get_base ()->get_root (); | |
| } | |
| else if (const auto r = root->try_as<const PointerType> ()) | |
| { | |
| root = r->get_base ()->get_root (); | |
| } | |
| // these are an unsize | |
| else if (const auto r = root->try_as<const SliceType> ()) | |
| { | |
| root = r->get_element_type ()->get_root (); | |
| } | |
| // else if (const auto r = root->try_as<const ArrayType> ()) | |
| // { | |
| // root = r->get_element_type ()->get_root (); | |
| // } | |
| return root; | |
| } | |
| BaseType * | |
| BaseType::destructure () | |
| { | |
| int recurisve_ops = 0; | |
| BaseType *x = this; | |
| while (true) | |
| { | |
| if (recurisve_ops++ >= rust_max_recursion_depth) | |
| { | |
| rust_error_at ( | |
| UNDEF_LOCATION, | |
| "%<recursion depth%> count exceeds limit of %i (use " | |
| "%<frust-max-recursion-depth=%> to increase the limit)", | |
| rust_max_recursion_depth); | |
| return new ErrorType (get_ref ()); | |
| } | |
| if (auto p = x->try_as<ParamType> ()) | |
| { | |
| auto pr = p->resolve (); | |
| if (pr == x) | |
| return pr; | |
| x = pr; | |
| } | |
| else if (x->get_kind () == TypeKind::CONST) | |
| { | |
| auto p = x->as_const_type (); | |
| if (p->const_kind () == BaseConstType::ConstKind::Decl) | |
| { | |
| auto decl = static_cast<ConstParamType *> (p); | |
| auto pr = decl->resolve (); | |
| if (pr == x) | |
| return pr; | |
| x = pr; | |
| } | |
| else | |
| { | |
| return x; | |
| } | |
| } | |
| else if (auto p = x->try_as<PlaceholderType> ()) | |
| { | |
| if (!p->can_resolve ()) | |
| return p; | |
| x = p->resolve (); | |
| } | |
| else | |
| { | |
| return x; | |
| } | |
| } | |
| return x; | |
| } | |
| const BaseType * | |
| BaseType::destructure () const | |
| { | |
| int recurisve_ops = 0; | |
| const BaseType *x = this; | |
| while (true) | |
| { | |
| if (recurisve_ops++ >= rust_max_recursion_depth) | |
| { | |
| rust_error_at ( | |
| UNDEF_LOCATION, | |
| "%<recursion depth%> count exceeds limit of %i (use " | |
| "%<frust-max-recursion-depth=%> to increase the limit)", | |
| rust_max_recursion_depth); | |
| return new ErrorType (get_ref ()); | |
| } | |
| if (auto p = x->try_as<const ParamType> ()) | |
| { | |
| auto pr = p->resolve (); | |
| if (pr == x) | |
| return pr; | |
| x = pr; | |
| } | |
| else if (x->get_kind () == TypeKind::CONST) | |
| { | |
| auto p = x->as_const_type (); | |
| if (p->const_kind () == BaseConstType::ConstKind::Decl) | |
| { | |
| auto decl = static_cast<const ConstParamType *> (p); | |
| auto pr = decl->resolve (); | |
| if (pr == x) | |
| return pr; | |
| x = pr; | |
| } | |
| else | |
| { | |
| return x; | |
| } | |
| } | |
| else if (auto p = x->try_as<const PlaceholderType> ()) | |
| { | |
| if (!p->can_resolve ()) | |
| return p; | |
| x = p->resolve (); | |
| } | |
| else if (auto p = x->try_as<const OpaqueType> ()) | |
| { | |
| auto pr = p->resolve (); | |
| if (pr == x) | |
| return pr; | |
| x = pr; | |
| } | |
| else | |
| { | |
| return x; | |
| } | |
| } | |
| return x; | |
| } | |
| BaseType * | |
| BaseType::monomorphized_clone () const | |
| { | |
| const TyTy::BaseType *x = destructure (); | |
| if (auto arr = x->try_as<const ArrayType> ()) | |
| { | |
| TyVar elm = arr->get_var_element_type ().monomorphized_clone (); | |
| return new ArrayType (arr->get_ref (), arr->get_ty_ref (), ident.locus, | |
| arr->get_capacity_var (), elm, | |
| arr->get_combined_refs ()); | |
| } | |
| else if (auto slice = x->try_as<const SliceType> ()) | |
| { | |
| 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<const PointerType> ()) | |
| { | |
| 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<const ReferenceType> ()) | |
| { | |
| TyVar elm = ref->get_var_element_type ().monomorphized_clone (); | |
| return new ReferenceType (ref->get_ref (), ref->get_ty_ref (), elm, | |
| ref->mutability (), ref->get_region (), | |
| ref->get_combined_refs ()); | |
| } | |
| else if (auto tuple = x->try_as<const TupleType> ()) | |
| { | |
| std::vector<TyVar> 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 (), | |
| ident.locus, cloned_fields, | |
| tuple->get_combined_refs ()); | |
| } | |
| else if (auto fn = x->try_as<const FnType> ()) | |
| { | |
| std::vector<TyTy::FnParam> cloned_params; | |
| for (auto &p : fn->get_params ()) | |
| cloned_params.push_back (p.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_substitution_arguments (), | |
| fn->get_region_constraints (), | |
| fn->get_combined_refs ()); | |
| } | |
| else if (auto fn = x->try_as<const FnPtr> ()) | |
| { | |
| std::vector<TyVar> 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_abi (), | |
| fn->get_unsafety (), fn->get_combined_refs ()); | |
| } | |
| else if (auto adt = x->try_as<const ADTType> ()) | |
| { | |
| if (!adt->has_substitutions_defined () || !adt->needs_substitution ()) | |
| return const_cast<ADTType *> (adt); | |
| std::vector<VariantDef *> cloned_variants; | |
| for (auto &variant : adt->get_variants ()) | |
| cloned_variants.push_back (variant->monomorphized_clone ()); | |
| return new ADTType (adt->get_id (), 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_region_constraints (), | |
| adt->get_combined_refs ()); | |
| } | |
| else if (auto proj = x->try_as<const ProjectionType> ()) | |
| { | |
| TyTy::ProjectionType *xx | |
| = static_cast<TyTy::ProjectionType *> (proj->clone ()); | |
| return Resolver::normalize_projection (xx, UNKNOWN_LOCATION /*FIXME*/, | |
| false, true); | |
| } | |
| else | |
| { | |
| return x->clone (); | |
| } | |
| rust_unreachable (); | |
| return nullptr; | |
| } | |
| std::string | |
| BaseType::mappings_str () const | |
| { | |
| std::string buffer = "Ref: " + std::to_string (get_ref ()) | |
| + " TyRef: " + std::to_string (get_ty_ref ()); | |
| buffer += "["; | |
| for (auto &ref : combined) | |
| buffer += std::to_string (ref) + ","; | |
| buffer += "]"; | |
| return "(" + buffer + ")"; | |
| } | |
| std::string | |
| BaseType::debug_str () const | |
| { | |
| // return TypeKindFormat::to_string (get_kind ()) + ":" + as_string () + ":" | |
| // + mappings_str () + ":" + bounds_as_string (); | |
| return get_name (); | |
| } | |
| void | |
| BaseType::debug () const | |
| { | |
| rust_debug ("[%p] %s", static_cast<const void *> (this), | |
| debug_str ().c_str ()); | |
| } | |
| const TyTy::BaseType * | |
| BaseType::contains_infer () const | |
| { | |
| std::set<const TyTy::BaseType *> visited; | |
| return contains_infer (visited); | |
| } | |
| const TyTy::BaseType * | |
| BaseType::contains_infer (std::set<const TyTy::BaseType *> &visited) const | |
| { | |
| const TyTy::BaseType *x = destructure (); | |
| bool found = visited.find (x) != visited.end (); | |
| if (found) | |
| return nullptr; | |
| visited.insert (x); | |
| if (auto fn = x->try_as<const FnType> ()) | |
| { | |
| for (const auto ¶m : fn->get_params ()) | |
| { | |
| auto infer = param.get_type ()->contains_infer (visited); | |
| if (infer) | |
| return infer; | |
| } | |
| return fn->get_return_type ()->contains_infer (visited); | |
| } | |
| else if (auto fn = x->try_as<const FnPtr> ()) | |
| { | |
| for (const auto ¶m : fn->get_params ()) | |
| { | |
| auto infer = param.get_tyty ()->contains_infer (visited); | |
| if (infer) | |
| return infer; | |
| } | |
| return fn->get_return_type ()->contains_infer (visited); | |
| } | |
| else if (auto adt = x->try_as<const ADTType> ()) | |
| { | |
| // non generic adt's cannot have infer vars on them | |
| if (!adt->has_substitutions_defined ()) | |
| return nullptr; | |
| for (auto &variant : adt->get_variants ()) | |
| { | |
| bool is_num_variant | |
| = variant->get_variant_type () == VariantDef::VariantType::NUM; | |
| bool is_unit_variant | |
| = variant->get_variant_type () == VariantDef::VariantType::UNIT; | |
| if (is_num_variant || is_unit_variant) | |
| continue; | |
| for (auto &field : variant->get_fields ()) | |
| { | |
| const BaseType *field_type = field->get_field_type (); | |
| auto infer = field_type->contains_infer (visited); | |
| if (infer) | |
| return infer; | |
| } | |
| } | |
| return nullptr; | |
| } | |
| else if (auto arr = x->try_as<const ArrayType> ()) | |
| { | |
| auto type_infer = arr->get_element_type ()->contains_infer (visited); | |
| if (type_infer) | |
| return type_infer; | |
| return arr->get_capacity ()->contains_infer (visited); | |
| } | |
| else if (auto slice = x->try_as<const SliceType> ()) | |
| { | |
| return slice->get_element_type ()->contains_infer (visited); | |
| } | |
| else if (auto ptr = x->try_as<const PointerType> ()) | |
| { | |
| return ptr->get_base ()->contains_infer (visited); | |
| } | |
| else if (auto ref = x->try_as<const ReferenceType> ()) | |
| { | |
| return ref->get_base ()->contains_infer (visited); | |
| } | |
| else if (auto tuple = x->try_as<const TupleType> ()) | |
| { | |
| for (size_t i = 0; i < tuple->num_fields (); i++) | |
| { | |
| auto infer = tuple->get_field (i)->contains_infer (visited); | |
| if (infer) | |
| return infer; | |
| } | |
| return nullptr; | |
| } | |
| else if (auto closure = x->try_as<const ClosureType> ()) | |
| { | |
| auto infer = closure->get_parameters ().contains_infer (visited); | |
| if (infer) | |
| return infer; | |
| return closure->get_result_type ().contains_infer (visited); | |
| } | |
| else if (x->is<InferType> ()) | |
| { | |
| return x; | |
| } | |
| else if (x->get_kind () == TyTy::TypeKind::CONST) | |
| { | |
| if (x->as_const_type ()->const_kind () == BaseConstType::Infer) | |
| { | |
| return x; | |
| } | |
| } | |
| return nullptr; | |
| } | |
| bool | |
| BaseType::is_concrete () const | |
| { | |
| std::set<const TyTy::BaseType *> visited; | |
| return is_concrete (visited); | |
| } | |
| bool | |
| BaseType::is_concrete (std::set<const TyTy::BaseType *> &visited) const | |
| { | |
| const TyTy::BaseType *x = destructure (); | |
| bool found = visited.find (x) != visited.end (); | |
| if (found) | |
| return true; | |
| visited.insert (x); | |
| if (x->is<ParamType> ()) | |
| { | |
| return false; | |
| } | |
| else if (x->get_kind () == TyTy::TypeKind::CONST) | |
| { | |
| auto p = x->as_const_type (); | |
| if (p->const_kind () == BaseConstType::ConstKind::Decl) | |
| return false; | |
| return true; | |
| } | |
| else if (x->is<ProjectionType> ()) | |
| { | |
| const auto p = x->as<const TyTy::ProjectionType> (); | |
| return p->get_self ()->is_concrete (visited); | |
| } | |
| // 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<PlaceholderType> ()) | |
| { | |
| return true; | |
| } | |
| else if (auto fn = x->try_as<const FnType> ()) | |
| { | |
| for (const auto ¶m : fn->get_params ()) | |
| { | |
| if (!param.get_type ()->is_concrete (visited)) | |
| return false; | |
| } | |
| return fn->get_return_type ()->is_concrete (visited); | |
| } | |
| else if (auto fn = x->try_as<const FnPtr> ()) | |
| { | |
| for (const auto ¶m : fn->get_params ()) | |
| { | |
| if (!param.get_tyty ()->is_concrete (visited)) | |
| return false; | |
| } | |
| return fn->get_return_type ()->is_concrete (visited); | |
| } | |
| else if (auto adt = x->try_as<const ADTType> ()) | |
| { | |
| if (!adt->has_substitutions_defined ()) | |
| return true; | |
| 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; | |
| bool is_unit_variant | |
| = variant->get_variant_type () == VariantDef::VariantType::UNIT; | |
| if (is_num_variant || is_unit_variant) | |
| continue; | |
| for (auto &field : variant->get_fields ()) | |
| { | |
| const BaseType *field_type = field->get_field_type (); | |
| if (!field_type->is_concrete (visited)) | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| else if (auto arr = x->try_as<const ArrayType> ()) | |
| { | |
| return arr->get_element_type ()->is_concrete (visited) | |
| && arr->get_capacity ()->is_concrete (visited); | |
| } | |
| else if (auto slice = x->try_as<const SliceType> ()) | |
| { | |
| return slice->get_element_type ()->is_concrete (visited); | |
| } | |
| else if (auto ptr = x->try_as<const PointerType> ()) | |
| { | |
| return ptr->get_base ()->is_concrete (visited); | |
| } | |
| else if (auto ref = x->try_as<const ReferenceType> ()) | |
| { | |
| return ref->get_base ()->is_concrete (visited); | |
| } | |
| else if (auto tuple = x->try_as<const TupleType> ()) | |
| { | |
| for (size_t i = 0; i < tuple->num_fields (); i++) | |
| { | |
| if (!tuple->get_field (i)->is_concrete (visited)) | |
| return false; | |
| } | |
| return true; | |
| } | |
| else if (auto closure = x->try_as<const ClosureType> ()) | |
| { | |
| if (closure->get_parameters ().is_concrete (visited)) | |
| return false; | |
| return closure->get_result_type ().is_concrete (visited); | |
| } | |
| else if (x->is<InferType> () || x->is<BoolType> () || x->is<CharType> () | |
| || x->is<IntType> () || x->is<UintType> () || x->is<FloatType> () | |
| || x->is<USizeType> () || x->is<ISizeType> () || x->is<NeverType> () | |
| || x->is<StrType> () || x->is<DynamicObjectType> () | |
| || x->is<ErrorType> ()) | |
| { | |
| return true; | |
| } | |
| return false; | |
| } | |
| bool | |
| BaseType::is_zero_sized () const | |
| { | |
| const TyTy::BaseType *x = destructure (); | |
| switch (x->get_kind ()) | |
| { | |
| // primitives that are always non-zero size | |
| case FNPTR: | |
| case FNDEF: | |
| case SLICE: | |
| case POINTER: | |
| case REF: | |
| case CLOSURE: | |
| case INFER: | |
| case BOOL: | |
| case CHAR: | |
| case INT: | |
| case UINT: | |
| case FLOAT: | |
| case USIZE: | |
| case ISIZE: | |
| case OPAQUE: | |
| case STR: | |
| case DYNAMIC: | |
| case CONST: | |
| case PARAM: | |
| case PROJECTION: | |
| case PLACEHOLDER: | |
| case ERROR: | |
| return false; | |
| case NEVER: | |
| return true; | |
| case TUPLE: | |
| { | |
| const TupleType *tuple_ty = static_cast<const TupleType *> (x); | |
| return tuple_ty->is_zero_sized (); | |
| } | |
| case ARRAY: | |
| { | |
| const ArrayType *array_ty = static_cast<const ArrayType *> (x); | |
| return array_ty->is_zero_sized (); | |
| } | |
| case ADT: | |
| { | |
| const ADTType *adt_ty = static_cast<const ADTType *> (x); | |
| return adt_ty->is_zero_sized (); | |
| } | |
| } | |
| return false; | |
| } | |
| bool | |
| BaseType::has_substitutions_defined () const | |
| { | |
| const auto x = this; | |
| switch (x->get_kind ()) | |
| { | |
| case INFER: | |
| case BOOL: | |
| case CHAR: | |
| case INT: | |
| case UINT: | |
| case FLOAT: | |
| case USIZE: | |
| case ISIZE: | |
| case NEVER: | |
| case STR: | |
| case DYNAMIC: | |
| case ERROR: | |
| case FNPTR: | |
| case ARRAY: | |
| case SLICE: | |
| case POINTER: | |
| case REF: | |
| case TUPLE: | |
| case PARAM: | |
| case PLACEHOLDER: | |
| case CONST: | |
| case OPAQUE: | |
| return false; | |
| case PROJECTION: | |
| { | |
| const ProjectionType &p = *static_cast<const ProjectionType *> (x); | |
| const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (p); | |
| return ref.has_substitutions (); | |
| } | |
| break; | |
| case FNDEF: | |
| { | |
| const FnType &fn = *static_cast<const FnType *> (x); | |
| const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (fn); | |
| return ref.has_substitutions (); | |
| } | |
| break; | |
| case ADT: | |
| { | |
| const ADTType &adt = *static_cast<const ADTType *> (x); | |
| const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (adt); | |
| return ref.has_substitutions (); | |
| } | |
| break; | |
| case CLOSURE: | |
| { | |
| const ClosureType &closure = *static_cast<const ClosureType *> (x); | |
| const SubstitutionRef &ref | |
| = static_cast<const SubstitutionRef &> (closure); | |
| return ref.has_substitutions (); | |
| } | |
| break; | |
| } | |
| return false; | |
| } | |
| bool | |
| BaseType::needs_generic_substitutions () const | |
| { | |
| const TyTy::BaseType *x = destructure (); | |
| switch (x->get_kind ()) | |
| { | |
| case INFER: | |
| case BOOL: | |
| case CHAR: | |
| case INT: | |
| case UINT: | |
| case FLOAT: | |
| case USIZE: | |
| case ISIZE: | |
| case NEVER: | |
| case STR: | |
| case DYNAMIC: | |
| case ERROR: | |
| case FNPTR: | |
| case ARRAY: | |
| case SLICE: | |
| case POINTER: | |
| case REF: | |
| case TUPLE: | |
| case PARAM: | |
| case PLACEHOLDER: | |
| case CONST: | |
| case OPAQUE: | |
| return false; | |
| case PROJECTION: | |
| { | |
| const ProjectionType &p = *static_cast<const ProjectionType *> (x); | |
| const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (p); | |
| return ref.needs_substitution (); | |
| } | |
| break; | |
| case FNDEF: | |
| { | |
| const FnType &fn = *static_cast<const FnType *> (x); | |
| const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (fn); | |
| return ref.needs_substitution (); | |
| } | |
| break; | |
| case ADT: | |
| { | |
| const ADTType &adt = *static_cast<const ADTType *> (x); | |
| const SubstitutionRef &ref = static_cast<const SubstitutionRef &> (adt); | |
| return ref.needs_substitution (); | |
| } | |
| break; | |
| case CLOSURE: | |
| { | |
| const ClosureType &closure = *static_cast<const ClosureType *> (x); | |
| const SubstitutionRef &ref | |
| = static_cast<const SubstitutionRef &> (closure); | |
| return ref.needs_substitution (); | |
| } | |
| break; | |
| } | |
| return false; | |
| } | |
| const SubstitutionArgumentMappings & | |
| BaseType::get_subst_argument_mappings () const | |
| { | |
| static auto empty = SubstitutionArgumentMappings::empty (); | |
| const TyTy::BaseType *x = destructure (); | |
| switch (x->get_kind ()) | |
| { | |
| case PROJECTION: | |
| { | |
| const auto &p = *static_cast<const ProjectionType *> (x); | |
| const auto &ref = static_cast<const SubstitutionRef &> (p); | |
| return ref.get_substitution_arguments (); | |
| } | |
| break; | |
| case FNDEF: | |
| { | |
| const auto &fn = *static_cast<const FnType *> (x); | |
| const auto &ref = static_cast<const SubstitutionRef &> (fn); | |
| return ref.get_substitution_arguments (); | |
| } | |
| break; | |
| case ADT: | |
| { | |
| const auto &adt = *static_cast<const ADTType *> (x); | |
| const auto &ref = static_cast<const SubstitutionRef &> (adt); | |
| return ref.get_substitution_arguments (); | |
| } | |
| break; | |
| case CLOSURE: | |
| { | |
| const auto &closure = *static_cast<const ClosureType *> (x); | |
| const auto &ref = static_cast<const SubstitutionRef &> (closure); | |
| return ref.get_substitution_arguments (); | |
| } | |
| break; | |
| default: | |
| return empty; | |
| } | |
| return empty; | |
| } | |
| // InferType | |
| InferType::InferType (HirId ref, InferTypeKind infer_kind, TypeHint hint, | |
| location_t locus, std::set<HirId> 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), locus}, refs), | |
| infer_kind (infer_kind), default_hint (hint) | |
| {} | |
| InferType::InferTypeKind | |
| InferType::get_infer_kind () const | |
| { | |
| return infer_kind; | |
| } | |
| std::string | |
| InferType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| void | |
| InferType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| InferType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| InferType::as_string () const | |
| { | |
| switch (infer_kind) | |
| { | |
| case GENERAL: | |
| return "T?"; | |
| case INTEGRAL: | |
| return "<integer>"; | |
| case FLOAT: | |
| return "<float>"; | |
| } | |
| return "<infer::error>"; | |
| } | |
| BaseType * | |
| InferType::clone () const | |
| { | |
| // clones for inference variables are special in that they _must_ exist within | |
| // the type check context and we must ensure we don't loose the chain | |
| // otherwise we will end up in the missing type annotations case | |
| // | |
| // This means we cannot simply take over the same reference we must generate a | |
| // new ref just like the get_implicit_infer_var code then we can setup the | |
| // chain of references accordingly to ensure we don't loose the ability to | |
| // update the inference variables when we solve the type | |
| auto &mappings = Analysis::Mappings::get (); | |
| auto context = Resolver::TypeCheckContext::get (); | |
| InferType *clone | |
| = new InferType (mappings.get_next_hir_id (), get_infer_kind (), | |
| default_hint, get_ident ().locus, get_combined_refs ()); | |
| context->insert_type ( | |
| Analysis::NodeMapping (mappings.crate.get_current_crate (), UNKNOWN_NODEID, | |
| clone->get_ref (), UNKNOWN_LOCAL_DEFID), | |
| clone); | |
| mappings.insert_location (clone->get_ref (), | |
| mappings.lookup_location (get_ref ())); | |
| // setup the chain to reference this | |
| clone->append_reference (get_ref ()); | |
| return clone; | |
| } | |
| bool | |
| InferType::default_type (BaseType **type) const | |
| { | |
| auto context = Resolver::TypeCheckContext::get (); | |
| bool ok = false; | |
| // NOTE: Calling this error is misleading. | |
| if (default_hint.kind == TypeKind::ERROR) | |
| { | |
| switch (infer_kind) | |
| { | |
| case GENERAL: | |
| return false; | |
| case INTEGRAL: | |
| { | |
| ok = context->lookup_builtin ("i32", type); | |
| rust_assert (ok); | |
| return ok; | |
| } | |
| case FLOAT: | |
| { | |
| ok = context->lookup_builtin ("f64", type); | |
| rust_assert (ok); | |
| return ok; | |
| } | |
| } | |
| return false; | |
| } | |
| switch (default_hint.kind) | |
| { | |
| case ISIZE: | |
| ok = context->lookup_builtin ("isize", type); | |
| rust_assert (ok); | |
| return ok; | |
| case USIZE: | |
| ok = context->lookup_builtin ("usize", type); | |
| rust_assert (ok); | |
| return ok; | |
| case INT: | |
| switch (default_hint.szhint) | |
| { | |
| case TypeHint::SizeHint::S8: | |
| ok = context->lookup_builtin ("i8", type); | |
| rust_assert (ok); | |
| return ok; | |
| case TypeHint::SizeHint::S16: | |
| ok = context->lookup_builtin ("i16", type); | |
| rust_assert (ok); | |
| return ok; | |
| case TypeHint::SizeHint::S32: | |
| ok = context->lookup_builtin ("i32", type); | |
| rust_assert (ok); | |
| return ok; | |
| case TypeHint::SizeHint::S64: | |
| ok = context->lookup_builtin ("i64", type); | |
| rust_assert (ok); | |
| return ok; | |
| case TypeHint::SizeHint::S128: | |
| ok = context->lookup_builtin ("i128", type); | |
| rust_assert (ok); | |
| return ok; | |
| default: | |
| return false; | |
| } | |
| break; | |
| case UINT: | |
| switch (default_hint.szhint) | |
| { | |
| case TypeHint::SizeHint::S8: | |
| ok = context->lookup_builtin ("u8", type); | |
| rust_assert (ok); | |
| return ok; | |
| case TypeHint::SizeHint::S16: | |
| ok = context->lookup_builtin ("u16", type); | |
| rust_assert (ok); | |
| return ok; | |
| case TypeHint::SizeHint::S32: | |
| ok = context->lookup_builtin ("u32", type); | |
| rust_assert (ok); | |
| return ok; | |
| case TypeHint::SizeHint::S64: | |
| ok = context->lookup_builtin ("u64", type); | |
| rust_assert (ok); | |
| return ok; | |
| case TypeHint::SizeHint::S128: | |
| ok = context->lookup_builtin ("u128", type); | |
| rust_assert (ok); | |
| return ok; | |
| default: | |
| return false; | |
| } | |
| break; | |
| case TypeKind::FLOAT: | |
| switch (default_hint.szhint) | |
| { | |
| case TypeHint::SizeHint::S32: | |
| ok = context->lookup_builtin ("f32", type); | |
| rust_assert (ok); | |
| return ok; | |
| case TypeHint::SizeHint::S64: | |
| ok = context->lookup_builtin ("f64", type); | |
| rust_assert (ok); | |
| return ok; | |
| default: | |
| return false; | |
| } | |
| break; | |
| default: | |
| return false; | |
| } | |
| return false; | |
| } | |
| void | |
| InferType::apply_primitive_type_hint (const BaseType &hint) | |
| { | |
| switch (hint.get_kind ()) | |
| { | |
| case ISIZE: | |
| case USIZE: | |
| infer_kind = INTEGRAL; | |
| default_hint.kind = hint.get_kind (); | |
| break; | |
| case INT: | |
| { | |
| infer_kind = INTEGRAL; | |
| default_hint.kind = hint.get_kind (); | |
| default_hint.shint = TypeHint::SignedHint::SIGNED; | |
| switch (hint.as<const IntType> ()->get_int_kind ()) | |
| { | |
| case IntType::I8: | |
| default_hint.szhint = TypeHint::SizeHint::S8; | |
| break; | |
| case IntType::I16: | |
| default_hint.szhint = TypeHint::SizeHint::S16; | |
| break; | |
| case IntType::I32: | |
| default_hint.szhint = TypeHint::SizeHint::S32; | |
| break; | |
| case IntType::I64: | |
| default_hint.szhint = TypeHint::SizeHint::S64; | |
| break; | |
| case IntType::I128: | |
| default_hint.szhint = TypeHint::SizeHint::S128; | |
| break; | |
| } | |
| } | |
| break; | |
| case UINT: | |
| { | |
| infer_kind = INTEGRAL; | |
| default_hint.kind = hint.get_kind (); | |
| default_hint.shint = TypeHint::SignedHint::UNSIGNED; | |
| switch (hint.as<const UintType> ()->get_uint_kind ()) | |
| { | |
| case UintType::U8: | |
| default_hint.szhint = TypeHint::SizeHint::S8; | |
| break; | |
| case UintType::U16: | |
| default_hint.szhint = TypeHint::SizeHint::S16; | |
| break; | |
| case UintType::U32: | |
| default_hint.szhint = TypeHint::SizeHint::S32; | |
| break; | |
| case UintType::U64: | |
| default_hint.szhint = TypeHint::SizeHint::S64; | |
| break; | |
| case UintType::U128: | |
| default_hint.szhint = TypeHint::SizeHint::S128; | |
| break; | |
| } | |
| } | |
| break; | |
| case TypeKind::FLOAT: | |
| { | |
| infer_kind = FLOAT; | |
| default_hint.shint = TypeHint::SignedHint::SIGNED; | |
| default_hint.kind = hint.get_kind (); | |
| switch (hint.as<const FloatType> ()->get_float_kind ()) | |
| { | |
| case FloatType::F32: | |
| default_hint.szhint = TypeHint::SizeHint::S32; | |
| break; | |
| case FloatType::F64: | |
| default_hint.szhint = TypeHint::SizeHint::S64; | |
| break; | |
| } | |
| } | |
| break; | |
| default: | |
| // TODO bool, char, never?? | |
| break; | |
| } | |
| } | |
| // ErrorType | |
| ErrorType::ErrorType (HirId ref, std::set<HirId> refs) | |
| : 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), UNDEF_LOCATION}, refs) | |
| {} | |
| std::string | |
| ErrorType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| void | |
| ErrorType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ErrorType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ErrorType::as_string () const | |
| { | |
| return "<tyty::error>"; | |
| } | |
| BaseType * | |
| ErrorType::clone () const | |
| { | |
| return new ErrorType (get_ref (), get_ty_ref (), get_combined_refs ()); | |
| } | |
| // Struct Field type | |
| StructFieldType::StructFieldType (HirId ref, std::string name, BaseType *ty, | |
| location_t locus) | |
| : ref (ref), name (name), ty (ty), locus (locus) | |
| {} | |
| HirId | |
| StructFieldType::get_ref () const | |
| { | |
| return ref; | |
| } | |
| std::string | |
| StructFieldType::get_name () const | |
| { | |
| return name; | |
| } | |
| BaseType * | |
| StructFieldType::get_field_type () const | |
| { | |
| return ty; | |
| } | |
| void | |
| StructFieldType::set_field_type (BaseType *fty) | |
| { | |
| ty = fty; | |
| } | |
| void | |
| StructFieldType::debug () const | |
| { | |
| rust_debug ("%s", as_string ().c_str ()); | |
| } | |
| location_t | |
| StructFieldType::get_locus () const | |
| { | |
| return locus; | |
| } | |
| std::string | |
| StructFieldType::as_string () const | |
| { | |
| return name + ":" + get_field_type ()->debug_str (); | |
| } | |
| bool | |
| StructFieldType::is_equal (const StructFieldType &other) const | |
| { | |
| bool names_eq = get_name () == other.get_name (); | |
| TyTy::BaseType *o = other.get_field_type (); | |
| if (auto op = o->try_as<ParamType> ()) | |
| o = op->resolve (); | |
| bool types_eq = get_field_type ()->is_equal (*o); | |
| return names_eq && types_eq; | |
| } | |
| StructFieldType * | |
| StructFieldType::clone () const | |
| { | |
| return new StructFieldType (get_ref (), get_name (), | |
| get_field_type ()->clone (), locus); | |
| } | |
| StructFieldType * | |
| StructFieldType::monomorphized_clone () const | |
| { | |
| return new StructFieldType (get_ref (), get_name (), | |
| get_field_type ()->monomorphized_clone (), locus); | |
| } | |
| // VariantDef | |
| std::string | |
| VariantDef::variant_type_string (VariantType type) | |
| { | |
| switch (type) | |
| { | |
| case NUM: | |
| return "enumeral"; | |
| case TUPLE: | |
| return "tuple"; | |
| case STRUCT: | |
| return "struct"; | |
| case UNIT: | |
| return "unit struct"; | |
| } | |
| rust_unreachable (); | |
| return ""; | |
| } | |
| VariantDef::VariantDef (HirId id, DefId defid, std::string identifier, | |
| RustIdent ident, | |
| tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant) | |
| : id (id), defid (defid), identifier (identifier), ident (ident), | |
| discriminant (std::move (discriminant)) | |
| { | |
| type = VariantType::NUM; | |
| fields = {}; | |
| } | |
| VariantDef::VariantDef (HirId id, DefId defid, std::string identifier, | |
| RustIdent ident, VariantType type, | |
| tl::optional<std::unique_ptr<HIR::Expr>> &&discriminant, | |
| std::vector<StructFieldType *> fields) | |
| : id (id), defid (defid), identifier (identifier), ident (ident), type (type), | |
| discriminant (std::move (discriminant)), fields (fields) | |
| { | |
| rust_assert ((type == VariantType::NUM && fields.empty ()) | |
| || (type == VariantType::UNIT && fields.empty ()) | |
| || type == VariantType::TUPLE || type == VariantType::STRUCT); | |
| } | |
| VariantDef & | |
| VariantDef::get_error_node () | |
| { | |
| static VariantDef node | |
| = VariantDef (UNKNOWN_HIRID, UNKNOWN_DEFID, "", | |
| {Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION}, | |
| tl::nullopt); | |
| return node; | |
| } | |
| bool | |
| VariantDef::is_error () const | |
| { | |
| return get_id () == UNKNOWN_HIRID; | |
| } | |
| HirId | |
| VariantDef::get_id () const | |
| { | |
| return id; | |
| } | |
| DefId | |
| VariantDef::get_defid () const | |
| { | |
| return defid; | |
| } | |
| VariantDef::VariantType | |
| VariantDef::get_variant_type () const | |
| { | |
| return type; | |
| } | |
| bool | |
| VariantDef::is_data_variant () const | |
| { | |
| return type != VariantType::NUM; | |
| } | |
| bool | |
| VariantDef::is_dataless_variant () const | |
| { | |
| return type == VariantType::NUM; | |
| } | |
| std::string | |
| VariantDef::get_identifier () const | |
| { | |
| return identifier; | |
| } | |
| size_t | |
| VariantDef::num_fields () const | |
| { | |
| return fields.size (); | |
| } | |
| StructFieldType * | |
| VariantDef::get_field_at_index (size_t index) | |
| { | |
| rust_assert (index < fields.size ()); | |
| return fields.at (index); | |
| } | |
| std::vector<StructFieldType *> & | |
| VariantDef::get_fields () | |
| { | |
| return fields; | |
| } | |
| bool | |
| VariantDef::lookup_field (const std::string &lookup, | |
| StructFieldType **field_lookup, size_t *index) const | |
| { | |
| size_t i = 0; | |
| for (auto &field : fields) | |
| { | |
| if (field->get_name ().compare (lookup) == 0) | |
| { | |
| if (index != nullptr) | |
| *index = i; | |
| if (field_lookup != nullptr) | |
| *field_lookup = field; | |
| return true; | |
| } | |
| i++; | |
| } | |
| return false; | |
| } | |
| HIR::Expr & | |
| VariantDef::get_discriminant () | |
| { | |
| return *discriminant.value (); | |
| } | |
| const HIR::Expr & | |
| VariantDef::get_discriminant () const | |
| { | |
| return *discriminant.value (); | |
| } | |
| bool | |
| VariantDef::has_discriminant () const | |
| { | |
| return discriminant.has_value (); | |
| } | |
| std::string | |
| VariantDef::as_string () const | |
| { | |
| if (type == VariantType::NUM) | |
| return identifier | |
| + (has_discriminant () ? " = " + get_discriminant ().to_string () | |
| : ""); | |
| std::string buffer; | |
| for (size_t i = 0; i < fields.size (); ++i) | |
| { | |
| buffer += fields.at (i)->as_string (); | |
| if ((i + 1) < fields.size ()) | |
| buffer += ", "; | |
| } | |
| if (type == VariantType::TUPLE) | |
| return identifier + " (" + buffer + ")"; | |
| else | |
| return identifier + " {" + buffer + "}"; | |
| } | |
| bool | |
| VariantDef::is_equal (const VariantDef &other) const | |
| { | |
| if (type != other.type) | |
| return false; | |
| if (identifier.compare (other.identifier) != 0) | |
| return false; | |
| if (fields.size () != other.fields.size ()) | |
| return false; | |
| for (size_t i = 0; i < fields.size (); i++) | |
| { | |
| if (!fields.at (i)->is_equal (*other.fields.at (i))) | |
| return false; | |
| } | |
| return true; | |
| } | |
| VariantDef * | |
| VariantDef::clone () const | |
| { | |
| std::vector<StructFieldType *> cloned_fields; | |
| for (auto &f : fields) | |
| cloned_fields.push_back ((StructFieldType *) f->clone ()); | |
| auto &&discriminant_opt = has_discriminant () | |
| ? tl::optional<std::unique_ptr<HIR::Expr>> ( | |
| get_discriminant ().clone_expr ()) | |
| : tl::nullopt; | |
| return new VariantDef (id, defid, identifier, ident, type, | |
| std::move (discriminant_opt), cloned_fields); | |
| } | |
| VariantDef * | |
| VariantDef::monomorphized_clone () const | |
| { | |
| std::vector<StructFieldType *> cloned_fields; | |
| for (auto &f : fields) | |
| cloned_fields.push_back ((StructFieldType *) f->monomorphized_clone ()); | |
| auto discriminant_opt = has_discriminant () | |
| ? tl::optional<std::unique_ptr<HIR::Expr>> ( | |
| get_discriminant ().clone_expr ()) | |
| : tl::nullopt; | |
| return new VariantDef (id, defid, identifier, ident, type, | |
| std::move (discriminant_opt), cloned_fields); | |
| } | |
| const RustIdent & | |
| VariantDef::get_ident () const | |
| { | |
| return ident; | |
| } | |
| // ADTType | |
| ADTType::ADTType (DefId id, HirId ref, std::string identifier, RustIdent ident, | |
| ADTKind adt_kind, std::vector<VariantDef *> variants, | |
| std::vector<SubstitutionParamMapping> subst_refs, | |
| SubstitutionArgumentMappings generic_arguments, | |
| RegionConstraints region_constraints, std::set<HirId> refs) | |
| : BaseType (ref, ref, TypeKind::ADT, ident, refs), | |
| SubstitutionRef (std::move (subst_refs), std::move (generic_arguments), | |
| region_constraints), | |
| id (id), identifier (identifier), variants (variants), adt_kind (adt_kind) | |
| {} | |
| ADTType::ADTType (DefId id, HirId ref, HirId ty_ref, std::string identifier, | |
| RustIdent ident, ADTKind adt_kind, | |
| std::vector<VariantDef *> variants, | |
| std::vector<SubstitutionParamMapping> subst_refs, | |
| SubstitutionArgumentMappings generic_arguments, | |
| RegionConstraints region_constraints, std::set<HirId> refs) | |
| : BaseType (ref, ty_ref, TypeKind::ADT, ident, refs), | |
| SubstitutionRef (std::move (subst_refs), std::move (generic_arguments), | |
| region_constraints), | |
| id (id), identifier (identifier), variants (variants), adt_kind (adt_kind) | |
| {} | |
| ADTType::ADTType (DefId id, HirId ref, HirId ty_ref, std::string identifier, | |
| RustIdent ident, ADTKind adt_kind, | |
| std::vector<VariantDef *> variants, | |
| std::vector<SubstitutionParamMapping> subst_refs, | |
| ReprOptions repr, | |
| SubstitutionArgumentMappings generic_arguments, | |
| RegionConstraints region_constraints, std::set<HirId> refs) | |
| : BaseType (ref, ty_ref, TypeKind::ADT, ident, refs), | |
| SubstitutionRef (std::move (subst_refs), std::move (generic_arguments), | |
| region_constraints), | |
| id (id), identifier (identifier), variants (variants), adt_kind (adt_kind), | |
| repr (repr) | |
| {} | |
| void | |
| ADTType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ADTType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ADTType::as_string () const | |
| { | |
| std::string variants_buffer; | |
| for (size_t i = 0; i < number_of_variants (); ++i) | |
| { | |
| TyTy::VariantDef *variant = variants.at (i); | |
| variants_buffer += variant->as_string (); | |
| if ((i + 1) < number_of_variants ()) | |
| variants_buffer += ", "; | |
| } | |
| return identifier + subst_as_string () + "{" + variants_buffer + "}"; | |
| } | |
| bool | |
| ADTType::is_equal (const BaseType &other) const | |
| { | |
| if (this == &other) | |
| return true; | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| auto other2 = other.as<const ADTType> (); | |
| if (get_id () != other2->get_id ()) | |
| return false; | |
| if (get_adt_kind () != other2->get_adt_kind ()) | |
| return false; | |
| if (number_of_variants () != other2->number_of_variants ()) | |
| return false; | |
| if (has_substitutions_defined () != other2->has_substitutions_defined ()) | |
| return false; | |
| if (has_substitutions_defined ()) | |
| { | |
| 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 auto &aa = a.get_param_ty (); | |
| const auto &bb = b.get_param_ty (); | |
| if (!aa->is_equal (*bb)) | |
| return false; | |
| } | |
| } | |
| // ADTs are nominal. Their fields describe the definition, not the type's | |
| // identity, and walking them here cannot terminate for recursive ADTs. | |
| return true; | |
| } | |
| bool | |
| ADTType::is_zero_sized () const | |
| { | |
| auto phantom = Analysis::Mappings::get ().lookup_lang_item ( | |
| LangItem::Kind::PHANTOM_DATA); | |
| if (phantom.has_value () && phantom.value () == get_id ()) | |
| return true; | |
| for (auto *variant : get_variants ()) | |
| { | |
| for (size_t i = 0; i < variant->num_fields (); i++) | |
| { | |
| if (!variant->get_field_at_index (i) | |
| ->get_field_type () | |
| ->is_zero_sized ()) | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| DefId | |
| ADTType::get_id () const | |
| { | |
| return id; | |
| } | |
| BaseType * | |
| ADTType::clone () const | |
| { | |
| std::vector<VariantDef *> cloned_variants; | |
| for (auto &variant : variants) | |
| cloned_variants.push_back (variant->clone ()); | |
| return new ADTType (get_id (), get_ref (), get_ty_ref (), identifier, ident, | |
| get_adt_kind (), cloned_variants, clone_substs (), | |
| get_repr_options (), used_arguments, | |
| get_region_constraints (), get_combined_refs ()); | |
| } | |
| static bool | |
| handle_substitions (SubstitutionArgumentMappings &subst_mappings, | |
| StructFieldType *field) | |
| { | |
| auto fty = field->get_field_type (); | |
| if (auto p = fty->try_as<ParamType> ()) | |
| { | |
| SubstitutionArg arg = SubstitutionArg::error (); | |
| bool ok = subst_mappings.get_argument_for_symbol (p, &arg); | |
| if (ok) | |
| { | |
| auto argt = arg.get_tyty (); | |
| bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM; | |
| bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER; | |
| if (arg_is_param || arg_is_concrete) | |
| { | |
| auto new_field = argt->clone (); | |
| new_field->set_ref (fty->get_ref ()); | |
| field->set_field_type (new_field); | |
| } | |
| else | |
| { | |
| field->get_field_type ()->set_ty_ref (argt->get_ref ()); | |
| } | |
| } | |
| } | |
| else if (fty->has_substitutions_defined () || !fty->is_concrete ()) | |
| { | |
| BaseType *concrete | |
| = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings); | |
| if (concrete->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| rust_error_at (subst_mappings.get_locus (), | |
| "Failed to resolve field substitution type: %s", | |
| fty->as_string ().c_str ()); | |
| return false; | |
| } | |
| auto new_field = concrete->clone (); | |
| new_field->set_ref (fty->get_ref ()); | |
| field->set_field_type (new_field); | |
| } | |
| return true; | |
| } | |
| ADTType * | |
| ADTType::handle_substitions (SubstitutionArgumentMappings &subst_mappings) | |
| { | |
| auto context = Resolver::TypeCheckContext::get (); | |
| ADTType *cached = nullptr; | |
| if (context->lookup_adt_substitution (get_id (), subst_mappings, &cached)) | |
| return cached; | |
| auto adt = clone ()->as<ADTType> (); | |
| adt->set_ty_ref (mappings.get_next_hir_id ()); | |
| adt->used_arguments = subst_mappings; | |
| // track these to handle recursive types | |
| context->insert_implicit_type (adt->get_ty_ref (), adt); | |
| context->insert_adt_substitution (get_id (), subst_mappings, adt); | |
| for (auto &sub : adt->get_substs ()) | |
| { | |
| SubstitutionArg arg = SubstitutionArg::error (); | |
| bool ok | |
| = subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg); | |
| if (ok) | |
| sub.fill_param_ty (subst_mappings, subst_mappings.get_locus ()); | |
| } | |
| for (auto &variant : adt->get_variants ()) | |
| { | |
| if (variant->is_dataless_variant ()) | |
| continue; | |
| for (auto &field : variant->get_fields ()) | |
| { | |
| bool ok = ::Rust::TyTy::handle_substitions (subst_mappings, field); | |
| if (!ok) | |
| return adt; | |
| } | |
| } | |
| return adt; | |
| } | |
| bool | |
| ADTType::contains_unsafe_cell () const | |
| { | |
| if (auto unsafe_cell | |
| = mappings.lookup_lang_item (LangItem::Kind::UNSAFE_CELL)) | |
| { | |
| if (get_id () == *unsafe_cell) | |
| return true; | |
| for (auto &variant : get_variants ()) | |
| for (auto &field : variant->get_fields ()) | |
| if (field->get_field_type ()->contains_unsafe_cell ()) | |
| return true; | |
| } | |
| return false; | |
| } | |
| bool | |
| ADTType::is_unsized () const | |
| { | |
| if (is_enum () || is_union () || number_of_variants () == 0) | |
| return false; | |
| auto &variant = get_variants ().front (); | |
| if (variant->num_fields () == 0) | |
| return false; | |
| const TyTy::BaseType *last_field_type | |
| = variant->get_field_at_index (variant->num_fields () - 1) | |
| ->get_field_type (); | |
| return last_field_type->is_unsized (); | |
| } | |
| bool | |
| ADTType::is_box () const | |
| { | |
| if (auto owned_box = mappings.lookup_lang_item (LangItem::Kind::OWNED_BOX)) | |
| if (get_id () == owned_box) | |
| return true; | |
| return false; | |
| } | |
| // TupleType | |
| TupleType::TupleType (HirId ref, location_t locus, std::vector<TyVar> fields, | |
| std::set<HirId> 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), locus}, refs), | |
| fields (fields) | |
| {} | |
| TupleType * | |
| TupleType::get_unit_type () | |
| { | |
| static TupleType *ret = nullptr; | |
| if (ret == nullptr) | |
| ret = new TupleType (Analysis::Mappings::get ().get_next_hir_id (), | |
| BUILTINS_LOCATION); | |
| return ret; | |
| } | |
| size_t | |
| TupleType::num_fields () const | |
| { | |
| return fields.size (); | |
| } | |
| const std::vector<TyVar> & | |
| TupleType::get_fields () const | |
| { | |
| return fields; | |
| } | |
| void | |
| TupleType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| TupleType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| TupleType::as_string () const | |
| { | |
| size_t i = 0; | |
| std::string fields_buffer; | |
| for (const TyVar &field : get_fields ()) | |
| { | |
| fields_buffer += field.get_tyty ()->as_string (); | |
| bool has_next = (i + 1) < get_fields ().size (); | |
| fields_buffer += has_next ? ", " : ""; | |
| i++; | |
| } | |
| return "(" + fields_buffer + ")"; | |
| } | |
| std::string | |
| TupleType::get_name () const | |
| { | |
| size_t i = 0; | |
| std::string fields_buffer; | |
| for (const TyVar &field : get_fields ()) | |
| { | |
| fields_buffer += field.get_tyty ()->get_name (); | |
| bool has_next = (i + 1) < get_fields ().size (); | |
| fields_buffer += has_next ? ", " : ""; | |
| i++; | |
| } | |
| return "(" + fields_buffer + ")"; | |
| } | |
| BaseType * | |
| TupleType::get_field (size_t index) const | |
| { | |
| return fields.at (index).get_tyty (); | |
| } | |
| bool | |
| TupleType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| auto other2 = other.as<const TupleType> (); | |
| 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))) | |
| return false; | |
| } | |
| return true; | |
| } | |
| bool | |
| TupleType::is_zero_sized () const | |
| { | |
| if (num_fields () == 0) | |
| return true; | |
| for (size_t i = 0; i < num_fields (); i++) | |
| { | |
| if (!get_field (i)->is_zero_sized ()) | |
| return false; | |
| } | |
| return true; | |
| } | |
| BaseType * | |
| TupleType::clone () const | |
| { | |
| std::vector<TyVar> cloned_fields; | |
| for (const auto &f : fields) | |
| cloned_fields.push_back (f.clone ()); | |
| return new TupleType (get_ref (), get_ty_ref (), get_ident ().locus, | |
| cloned_fields, get_combined_refs ()); | |
| } | |
| TupleType * | |
| TupleType::handle_substitions (SubstitutionArgumentMappings &mappings) | |
| { | |
| auto &mappings_table = Analysis::Mappings::get (); | |
| auto tuple = clone ()->as<TupleType> (); | |
| tuple->set_ref (mappings_table.get_next_hir_id ()); | |
| tuple->set_ty_ref (mappings_table.get_next_hir_id ()); | |
| for (size_t i = 0; i < tuple->fields.size (); i++) | |
| { | |
| TyVar &field = fields.at (i); | |
| if (!field.get_tyty ()->is_concrete ()) | |
| { | |
| BaseType *concrete | |
| = Resolver::SubstMapperInternal::Resolve (field.get_tyty (), | |
| mappings); | |
| tuple->fields[i] | |
| = TyVar::subst_covariant_var (field.get_tyty (), concrete); | |
| } | |
| } | |
| return tuple; | |
| } | |
| bool | |
| TupleType::contains_unsafe_cell () const | |
| { | |
| for (auto &field : get_fields ()) | |
| if (field.get_tyty ()->contains_unsafe_cell ()) | |
| return true; | |
| return false; | |
| } | |
| void | |
| FnType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| FnType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| FnType::as_string () const | |
| { | |
| std::string params_str = ""; | |
| for (auto ¶m : params) | |
| { | |
| auto &pattern = param.get_pattern (); | |
| auto ty = param.get_type (); | |
| params_str += pattern.to_string () + " " + ty->as_string (); | |
| params_str += ","; | |
| } | |
| std::string ret_str = type->as_string (); | |
| return "fn" + subst_as_string () + " (" + params_str + ") -> " + ret_str; | |
| } | |
| bool | |
| FnType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| auto &other2 = static_cast<const FnType &> (other); | |
| if (get_identifier ().compare (other2.get_identifier ()) != 0) | |
| return false; | |
| if (!get_return_type ()->is_equal (*other2.get_return_type ())) | |
| return false; | |
| if (has_substitutions_defined () != other2.has_substitutions_defined ()) | |
| return false; | |
| if (has_substitutions_defined ()) | |
| { | |
| if (get_num_substitutions () != other2.get_num_substitutions ()) | |
| return false; | |
| const FnType &ofn = static_cast<const FnType &> (other); | |
| for (size_t i = 0; i < get_num_substitutions (); i++) | |
| { | |
| const SubstitutionParamMapping &a = get_substs ().at (i); | |
| const SubstitutionParamMapping &b = ofn.get_substs ().at (i); | |
| const auto *pa = a.get_param_ty (); | |
| const auto *pb = b.get_param_ty (); | |
| if (!pa->is_equal (*pb)) | |
| return false; | |
| } | |
| } | |
| if (num_params () != other2.num_params ()) | |
| return false; | |
| for (size_t i = 0; i < num_params (); i++) | |
| { | |
| auto lhs = param_at (i).get_type (); | |
| auto rhs = other2.param_at (i).get_type (); | |
| if (!lhs->is_equal (*rhs)) | |
| return false; | |
| } | |
| return true; | |
| } | |
| BaseType * | |
| FnType::clone () const | |
| { | |
| std::vector<TyTy::FnParam> cloned_params; | |
| for (auto &p : params) | |
| cloned_params.push_back (p.clone ()); | |
| return new FnType (get_ref (), get_ty_ref (), get_id (), get_identifier (), | |
| ident, flags, abi, std::move (cloned_params), | |
| get_return_type ()->clone (), clone_substs (), | |
| get_substitution_arguments (), get_region_constraints (), | |
| get_combined_refs ()); | |
| } | |
| FnType * | |
| FnType::handle_substitions (SubstitutionArgumentMappings &subst_mappings) | |
| { | |
| FnType *fn = static_cast<FnType *> (clone ()); | |
| fn->set_ty_ref (mappings.get_next_hir_id ()); | |
| fn->used_arguments = subst_mappings; | |
| for (auto &sub : fn->get_substs ()) | |
| { | |
| SubstitutionArg arg = SubstitutionArg::error (); | |
| bool ok | |
| = subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg); | |
| if (ok) | |
| { | |
| sub.fill_param_ty (subst_mappings, subst_mappings.get_locus ()); | |
| } | |
| } | |
| auto fty = fn->get_return_type (); | |
| bool is_param_ty = fty->get_kind () == TypeKind::PARAM; | |
| if (is_param_ty) | |
| { | |
| ParamType *p = static_cast<ParamType *> (fty); | |
| SubstitutionArg arg = SubstitutionArg::error (); | |
| bool ok = subst_mappings.get_argument_for_symbol (p, &arg); | |
| if (ok) | |
| { | |
| auto argt = arg.get_tyty (); | |
| bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM; | |
| bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER; | |
| if (arg_is_param || arg_is_concrete) | |
| { | |
| auto new_field = argt->clone (); | |
| new_field->set_ref (fty->get_ref ()); | |
| fn->type = new_field; | |
| } | |
| else | |
| { | |
| fty->set_ty_ref (argt->get_ref ()); | |
| } | |
| } | |
| } | |
| else if (fty->needs_generic_substitutions () || !fty->is_concrete ()) | |
| { | |
| BaseType *concrete | |
| = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings); | |
| if (concrete == nullptr || concrete->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| rust_error_at (subst_mappings.get_locus (), | |
| "Failed to resolve field substitution type: %s", | |
| fty->as_string ().c_str ()); | |
| return nullptr; | |
| } | |
| auto new_field = concrete->clone (); | |
| new_field->set_ref (fty->get_ref ()); | |
| fn->type = new_field; | |
| } | |
| for (auto ¶m : fn->get_params ()) | |
| { | |
| auto fty = param.get_type (); | |
| bool is_param_ty = fty->get_kind () == TypeKind::PARAM; | |
| if (is_param_ty) | |
| { | |
| ParamType *p = static_cast<ParamType *> (fty); | |
| SubstitutionArg arg = SubstitutionArg::error (); | |
| bool ok = subst_mappings.get_argument_for_symbol (p, &arg); | |
| if (ok) | |
| { | |
| auto argt = arg.get_tyty (); | |
| bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM; | |
| bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER; | |
| if (arg_is_param || arg_is_concrete) | |
| { | |
| auto new_field = argt->clone (); | |
| new_field->set_ref (fty->get_ref ()); | |
| param.set_type (new_field); | |
| } | |
| else | |
| { | |
| fty->set_ty_ref (argt->get_ref ()); | |
| } | |
| } | |
| } | |
| else if (fty->has_substitutions_defined () || !fty->is_concrete ()) | |
| { | |
| BaseType *concrete | |
| = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings); | |
| if (concrete == nullptr | |
| || concrete->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| rust_error_at (subst_mappings.get_locus (), | |
| "Failed to resolve field substitution type: %s", | |
| fty->as_string ().c_str ()); | |
| return nullptr; | |
| } | |
| auto new_field = concrete->clone (); | |
| new_field->set_ref (fty->get_ref ()); | |
| param.set_type (new_field); | |
| } | |
| } | |
| return fn; | |
| } | |
| void | |
| FnPtr::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| FnPtr::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| FnPtr::as_string () const | |
| { | |
| std::string params_str; | |
| auto ¶ms = get_params (); | |
| for (auto &p : params) | |
| { | |
| params_str += p.get_tyty ()->get_name () + " ,"; | |
| } | |
| std::string unsafety = ""; | |
| if (get_unsafety () == Unsafety::Unsafe) | |
| unsafety = "unsafe "; | |
| std::string abi = get_string_from_abi (get_abi ()); | |
| return unsafety + "abi:" + abi + " " + "fnptr (" + params_str + ") -> " | |
| + get_return_type ()->get_name (); | |
| } | |
| bool | |
| FnPtr::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| auto other2 = static_cast<const FnPtr &> (other); | |
| auto this_ret_type = get_return_type (); | |
| auto other_ret_type = other2.get_return_type (); | |
| if (this_ret_type->is_equal (*other_ret_type)) | |
| return false; | |
| if (num_params () != other2.num_params ()) | |
| return false; | |
| for (size_t i = 0; i < num_params (); i++) | |
| { | |
| if (!get_param_type_at (i)->is_equal (*other2.get_param_type_at (i))) | |
| return false; | |
| } | |
| return true; | |
| } | |
| BaseType * | |
| FnPtr::clone () const | |
| { | |
| std::vector<TyVar> cloned_params; | |
| cloned_params.reserve (params.size ()); | |
| for (auto &p : params) | |
| cloned_params.emplace_back (p.get_ref ()); | |
| return new FnPtr (get_ref (), get_ty_ref (), ident.locus, | |
| std::move (cloned_params), result_type, get_abi (), | |
| get_unsafety (), get_combined_refs ()); | |
| } | |
| FnPtr * | |
| FnPtr::handle_substitions (SubstitutionArgumentMappings &mappings) | |
| { | |
| auto &mappings_table = Analysis::Mappings::get (); | |
| auto fn = clone ()->as<FnPtr> (); | |
| fn->set_ref (mappings_table.get_next_hir_id ()); | |
| fn->set_ty_ref (mappings_table.get_next_hir_id ()); | |
| if (!fn->result_type.get_tyty ()->is_concrete ()) | |
| { | |
| BaseType *concrete | |
| = Resolver::SubstMapperInternal::Resolve (fn->result_type.get_tyty (), | |
| mappings); | |
| fn->result_type | |
| = TyVar::subst_covariant_var (fn->result_type.get_tyty (), concrete); | |
| } | |
| for (size_t i = 0; i < fn->params.size (); i++) | |
| { | |
| TyVar &field = fn->params.at (i); | |
| if (!field.get_tyty ()->is_concrete ()) | |
| { | |
| BaseType *concrete | |
| = Resolver::SubstMapperInternal::Resolve (field.get_tyty (), | |
| mappings); | |
| fn->params[i] | |
| = TyVar::subst_covariant_var (field.get_tyty (), concrete); | |
| } | |
| } | |
| return fn; | |
| } | |
| void | |
| ClosureType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ClosureType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ClosureType::as_string () const | |
| { | |
| std::string params_buf = parameters->as_string (); | |
| return "|" + params_buf + "| {" + result_type.get_tyty ()->as_string () + "}"; | |
| } | |
| bool | |
| ClosureType::is_equal (const BaseType &other) const | |
| { | |
| if (other.get_kind () != TypeKind::CLOSURE) | |
| return false; | |
| const ClosureType &other2 = static_cast<const ClosureType &> (other); | |
| if (get_def_id () != other2.get_def_id ()) | |
| return false; | |
| if (!get_parameters ().is_equal (other2.get_parameters ())) | |
| return false; | |
| return get_result_type ().is_equal (other2.get_result_type ()); | |
| } | |
| BaseType * | |
| ClosureType::clone () const | |
| { | |
| return new ClosureType (get_ref (), get_ty_ref (), ident, id, | |
| (TyTy::TupleType *) parameters->clone (), result_type, | |
| clone_substs (), captures, get_combined_refs (), | |
| specified_bounds); | |
| } | |
| ClosureType * | |
| ClosureType::handle_substitions (SubstitutionArgumentMappings &mappings) | |
| { | |
| rust_unreachable (); | |
| return nullptr; | |
| } | |
| void | |
| ArrayType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ArrayType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ArrayType::as_string () const | |
| { | |
| auto cap = get_capacity (); | |
| std::string capacity_str = cap->as_string (); | |
| return "[" + get_element_type ()->as_string () + "; " + capacity_str + "]"; | |
| } | |
| bool | |
| ArrayType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| auto other2 = static_cast<const ArrayType &> (other); | |
| auto this_element_type = get_element_type (); | |
| auto other_element_type = other2.get_element_type (); | |
| return this_element_type->is_equal (*other_element_type); | |
| } | |
| bool | |
| ArrayType::is_zero_sized () const | |
| { | |
| if (element_type.get_tyty ()->is_zero_sized ()) | |
| return true; | |
| auto *capacity_ty = get_capacity (); | |
| if (capacity_ty != nullptr | |
| && capacity_ty->get_kind () == TyTy::TypeKind::CONST) | |
| { | |
| auto *capacity_const = capacity_ty->as_const_type (); | |
| auto &capacity_value | |
| = *static_cast<TyTy::ConstValueType *> (capacity_const); | |
| auto cap_tree = capacity_value.get_value (); | |
| size_t cap_wi = (size_t) wi::to_wide (cap_tree).to_uhwi (); | |
| if (cap_wi == 0) | |
| { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| BaseType * | |
| ArrayType::get_element_type () const | |
| { | |
| return element_type.get_tyty (); | |
| } | |
| const TyVar & | |
| ArrayType::get_var_element_type () const | |
| { | |
| return element_type; | |
| } | |
| BaseType * | |
| ArrayType::get_capacity () const | |
| { | |
| return capacity.get_tyty (); | |
| } | |
| BaseType * | |
| ArrayType::clone () const | |
| { | |
| return new ArrayType (get_ref (), get_ty_ref (), ident.locus, capacity, | |
| element_type, get_combined_refs ()); | |
| } | |
| ArrayType * | |
| ArrayType::handle_substitions (SubstitutionArgumentMappings &mappings) | |
| { | |
| auto &mappings_table = Analysis::Mappings::get (); | |
| ArrayType *ref = static_cast<ArrayType *> (clone ()); | |
| ref->set_ty_ref (mappings_table.get_next_hir_id ()); | |
| // might be &T or &ADT so this needs to be recursive | |
| auto base = ref->get_element_type (); | |
| BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings); | |
| ref->element_type = TyVar::subst_covariant_var (base, concrete); | |
| // handle capacity type | |
| auto cap = ref->get_capacity (); | |
| BaseType *concrete_cap | |
| = Resolver::SubstMapperInternal::Resolve (cap, mappings); | |
| rust_assert (concrete_cap->get_kind () == TyTy::TypeKind::CONST); | |
| ref->capacity = TyVar::subst_covariant_var (cap, concrete_cap); | |
| return ref; | |
| } | |
| bool | |
| ArrayType::contains_unsafe_cell () const | |
| { | |
| return get_element_type ()->contains_unsafe_cell (); | |
| } | |
| void | |
| SliceType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| SliceType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| SliceType::as_string () const | |
| { | |
| return "[" + get_element_type ()->as_string () + "]"; | |
| } | |
| bool | |
| SliceType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| auto other2 = static_cast<const SliceType &> (other); | |
| auto this_element_type = get_element_type (); | |
| auto other_element_type = other2.get_element_type (); | |
| return this_element_type->is_equal (*other_element_type); | |
| } | |
| BaseType * | |
| SliceType::get_element_type () const | |
| { | |
| return element_type.get_tyty (); | |
| } | |
| const TyVar & | |
| SliceType::get_var_element_type () const | |
| { | |
| return element_type; | |
| } | |
| BaseType * | |
| SliceType::clone () const | |
| { | |
| return new SliceType (get_ref (), get_ty_ref (), ident.locus, | |
| element_type.clone (), get_combined_refs ()); | |
| } | |
| SliceType * | |
| SliceType::handle_substitions (SubstitutionArgumentMappings &mappings) | |
| { | |
| auto &mappings_table = Analysis::Mappings::get (); | |
| SliceType *ref = static_cast<SliceType *> (clone ()); | |
| ref->set_ty_ref (mappings_table.get_next_hir_id ()); | |
| // might be &T or &ADT so this needs to be recursive | |
| auto base = ref->get_element_type (); | |
| BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings); | |
| ref->element_type = TyVar::subst_covariant_var (base, concrete); | |
| return ref; | |
| } | |
| bool | |
| SliceType::contains_unsafe_cell () const | |
| { | |
| return get_element_type ()->contains_unsafe_cell (); | |
| } | |
| // BoolType | |
| BoolType::BoolType (HirId ref, std::set<HirId> refs) | |
| : 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs) | |
| {} | |
| std::string | |
| BoolType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| void | |
| BoolType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| BoolType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| BoolType::as_string () const | |
| { | |
| return "bool"; | |
| } | |
| BaseType * | |
| BoolType::clone () const | |
| { | |
| return new BoolType (get_ref (), get_ty_ref (), get_combined_refs ()); | |
| } | |
| // IntType | |
| IntType::IntType (HirId ref, IntKind kind, std::set<HirId> refs) | |
| : 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs), | |
| int_kind (kind) | |
| {} | |
| std::string | |
| IntType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| IntType::IntKind | |
| IntType::get_int_kind () const | |
| { | |
| return int_kind; | |
| } | |
| void | |
| IntType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| IntType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| IntType::as_string () const | |
| { | |
| switch (int_kind) | |
| { | |
| case I8: | |
| return "i8"; | |
| case I16: | |
| return "i16"; | |
| case I32: | |
| return "i32"; | |
| case I64: | |
| return "i64"; | |
| case I128: | |
| return "i128"; | |
| } | |
| rust_unreachable (); | |
| return "__unknown_int_type"; | |
| } | |
| BaseType * | |
| IntType::clone () const | |
| { | |
| return new IntType (get_ref (), get_ty_ref (), get_int_kind (), | |
| get_combined_refs ()); | |
| } | |
| bool | |
| IntType::is_equal (const BaseType &other) const | |
| { | |
| if (!BaseType::is_equal (other)) | |
| return false; | |
| const IntType &o = static_cast<const IntType &> (other); | |
| return get_int_kind () == o.get_int_kind (); | |
| } | |
| // UintType | |
| UintType::UintType (HirId ref, UintKind kind, std::set<HirId> refs) | |
| : 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs), | |
| uint_kind (kind) | |
| {} | |
| std::string | |
| UintType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| UintType::UintKind | |
| UintType::get_uint_kind () const | |
| { | |
| return uint_kind; | |
| } | |
| void | |
| UintType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| UintType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| UintType::as_string () const | |
| { | |
| switch (uint_kind) | |
| { | |
| case U8: | |
| return "u8"; | |
| case U16: | |
| return "u16"; | |
| case U32: | |
| return "u32"; | |
| case U64: | |
| return "u64"; | |
| case U128: | |
| return "u128"; | |
| } | |
| rust_unreachable (); | |
| return "__unknown_uint_type"; | |
| } | |
| BaseType * | |
| UintType::clone () const | |
| { | |
| return new UintType (get_ref (), get_ty_ref (), get_uint_kind (), | |
| get_combined_refs ()); | |
| } | |
| bool | |
| UintType::is_equal (const BaseType &other) const | |
| { | |
| if (!BaseType::is_equal (other)) | |
| return false; | |
| const UintType &o = static_cast<const UintType &> (other); | |
| return get_uint_kind () == o.get_uint_kind (); | |
| } | |
| // FloatType | |
| FloatType::FloatType (HirId ref, FloatKind kind, std::set<HirId> refs) | |
| : 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs), | |
| float_kind (kind) | |
| {} | |
| std::string | |
| FloatType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| FloatType::FloatKind | |
| FloatType::get_float_kind () const | |
| { | |
| return float_kind; | |
| } | |
| void | |
| FloatType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| FloatType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| FloatType::as_string () const | |
| { | |
| switch (float_kind) | |
| { | |
| case F32: | |
| return "f32"; | |
| case F64: | |
| return "f64"; | |
| } | |
| rust_unreachable (); | |
| return "__unknown_float_type"; | |
| } | |
| BaseType * | |
| FloatType::clone () const | |
| { | |
| return new FloatType (get_ref (), get_ty_ref (), get_float_kind (), | |
| get_combined_refs ()); | |
| } | |
| bool | |
| FloatType::is_equal (const BaseType &other) const | |
| { | |
| if (!BaseType::is_equal (other)) | |
| return false; | |
| const FloatType &o = static_cast<const FloatType &> (other); | |
| return get_float_kind () == o.get_float_kind (); | |
| } | |
| // UsizeType | |
| USizeType::USizeType (HirId ref, std::set<HirId> refs) | |
| : 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs) | |
| {} | |
| std::string | |
| USizeType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| void | |
| USizeType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| USizeType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| USizeType::as_string () const | |
| { | |
| return "usize"; | |
| } | |
| BaseType * | |
| USizeType::clone () const | |
| { | |
| return new USizeType (get_ref (), get_ty_ref (), get_combined_refs ()); | |
| } | |
| // ISizeType | |
| ISizeType::ISizeType (HirId ref, std::set<HirId> refs) | |
| : 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs) | |
| {} | |
| std::string | |
| ISizeType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| void | |
| ISizeType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ISizeType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ISizeType::as_string () const | |
| { | |
| return "isize"; | |
| } | |
| BaseType * | |
| ISizeType::clone () const | |
| { | |
| return new ISizeType (get_ref (), get_ty_ref (), get_combined_refs ()); | |
| } | |
| // Char Type | |
| CharType::CharType (HirId ref, std::set<HirId> refs) | |
| : 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs) | |
| {} | |
| std::string | |
| CharType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| void | |
| CharType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| CharType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| CharType::as_string () const | |
| { | |
| return "char"; | |
| } | |
| BaseType * | |
| CharType::clone () const | |
| { | |
| return new CharType (get_ref (), get_ty_ref (), get_combined_refs ()); | |
| } | |
| // Reference Type | |
| ReferenceType::ReferenceType (HirId ref, TyVar base, Mutability mut, | |
| Region region, std::set<HirId> refs) | |
| : BaseType (ref, ref, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| std::move (refs)), | |
| base (base), mut (mut), region (region) | |
| {} | |
| ReferenceType::ReferenceType (HirId ref, HirId ty_ref, TyVar base, | |
| Mutability mut, Region region, | |
| std::set<HirId> refs) | |
| : BaseType (ref, ty_ref, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| std::move (refs)), | |
| base (base), mut (mut), region (region) | |
| {} | |
| Mutability | |
| ReferenceType::mutability () const | |
| { | |
| return mut; | |
| } | |
| bool | |
| ReferenceType::is_mutable () const | |
| { | |
| return mut == Mutability::Mut; | |
| } | |
| Region | |
| ReferenceType::get_region () const | |
| { | |
| return region; | |
| } | |
| bool | |
| ReferenceType::is_dyn_object () const | |
| { | |
| return is_dyn_slice_type () || is_dyn_str_type () || is_dyn_obj_type () | |
| || is_dyn_adt_type () || is_dyn_cstr_type (); | |
| } | |
| static const TyTy::BaseType * | |
| destructure_through_projections (const TyTy::BaseType *t) | |
| { | |
| const TyTy::BaseType *element = t->destructure (); | |
| for (int guard = 0; guard < 16; guard++) | |
| { | |
| auto *proj = element->try_as<const TyTy::ProjectionType> (); | |
| if (proj == nullptr) | |
| break; | |
| auto *normalized = Resolver::normalize_projection ( | |
| const_cast<TyTy::ProjectionType *> (proj), BUILTINS_LOCATION, | |
| false /*emit_errors*/, false /*unify_self*/); | |
| if (normalized == proj || normalized == nullptr | |
| || normalized->get_kind () == TyTy::TypeKind::ERROR) | |
| break; | |
| element = normalized->destructure (); | |
| } | |
| return element; | |
| } | |
| bool | |
| ReferenceType::is_dyn_slice_type (const TyTy::SliceType **slice) const | |
| { | |
| const TyTy::BaseType *element = destructure_through_projections (get_base ()); | |
| if (element->get_kind () != TyTy::TypeKind::SLICE) | |
| return false; | |
| if (slice == nullptr) | |
| return true; | |
| *slice = static_cast<const TyTy::SliceType *> (element); | |
| return true; | |
| } | |
| bool | |
| ReferenceType::is_dyn_str_type (const TyTy::StrType **str) const | |
| { | |
| const TyTy::BaseType *element = destructure_through_projections (get_base ()); | |
| if (element->get_kind () != TyTy::TypeKind::STR) | |
| return false; | |
| if (str == nullptr) | |
| return true; | |
| *str = static_cast<const TyTy::StrType *> (element); | |
| return true; | |
| } | |
| bool | |
| ReferenceType::is_dyn_obj_type (const TyTy::DynamicObjectType **dyn) const | |
| { | |
| const TyTy::BaseType *element = destructure_through_projections (get_base ()); | |
| if (element->get_kind () != TyTy::TypeKind::DYNAMIC) | |
| return false; | |
| if (dyn == nullptr) | |
| return true; | |
| *dyn = static_cast<const TyTy::DynamicObjectType *> (element); | |
| return true; | |
| } | |
| bool | |
| ReferenceType::is_dyn_adt_type (const TyTy::ADTType **adt) const | |
| { | |
| const TyTy::BaseType *element = destructure_through_projections (get_base ()); | |
| if (element->get_kind () != TyTy::TypeKind::ADT) | |
| return false; | |
| const TyTy::ADTType *adt_ty = static_cast<const TyTy::ADTType *> (element); | |
| if (!adt_ty->is_unsized ()) | |
| return false; | |
| if (adt != nullptr) | |
| *adt = adt_ty; | |
| return true; | |
| } | |
| bool | |
| ReferenceType::is_dyn_cstr_type (const TyTy::ADTType **adt) const | |
| { | |
| if (get_base ()->get_kind () != TyTy::TypeKind::ADT) | |
| return false; | |
| const TyTy::ADTType *adt_ty | |
| = static_cast<const TyTy::ADTType *> (get_base ()); | |
| auto &mappings = Analysis::Mappings::get (); | |
| auto cstr_item = mappings.lookup_lang_item (LangItem::Kind::CSTR); | |
| if (!cstr_item.has_value ()) | |
| return false; | |
| if (cstr_item.value () != adt_ty->get_id ()) | |
| return false; | |
| *adt = adt_ty; | |
| return true; | |
| } | |
| void | |
| ReferenceType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ReferenceType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ReferenceType::as_string () const | |
| { | |
| return std::string ("&") + (is_mutable () ? "mut" : "") + " " | |
| + get_base ()->as_string (); | |
| } | |
| std::string | |
| ReferenceType::get_name () const | |
| { | |
| return std::string ("&") + (is_mutable () ? "mut" : "") + " " | |
| + get_base ()->get_name (); | |
| } | |
| bool | |
| ReferenceType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| auto other2 = static_cast<const ReferenceType &> (other); | |
| if (mutability () != other2.mutability ()) | |
| return false; | |
| return get_base ()->is_equal (*other2.get_base ()); | |
| } | |
| BaseType * | |
| ReferenceType::get_base () const | |
| { | |
| return base.get_tyty (); | |
| } | |
| const TyVar & | |
| ReferenceType::get_var_element_type () const | |
| { | |
| return base; | |
| } | |
| BaseType * | |
| ReferenceType::clone () const | |
| { | |
| return new ReferenceType (get_ref (), get_ty_ref (), base, mutability (), | |
| get_region (), get_combined_refs ()); | |
| } | |
| ReferenceType * | |
| ReferenceType::handle_substitions (SubstitutionArgumentMappings &mappings) | |
| { | |
| auto &mappings_table = Analysis::Mappings::get (); | |
| ReferenceType *ref = static_cast<ReferenceType *> (clone ()); | |
| ref->set_ty_ref (mappings_table.get_next_hir_id ()); | |
| // might be &T or &ADT so this needs to be recursive | |
| auto base = ref->get_base (); | |
| BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings); | |
| ref->base = TyVar::subst_covariant_var (base, concrete); | |
| return ref; | |
| } | |
| // PointerType | |
| PointerType::PointerType (HirId ref, TyVar base, Mutability mut, | |
| std::set<HirId> refs) | |
| : 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs), | |
| base (base), mut (mut) | |
| {} | |
| Mutability | |
| PointerType::mutability () const | |
| { | |
| return mut; | |
| } | |
| bool | |
| PointerType::is_mutable () const | |
| { | |
| return mut == Mutability::Mut; | |
| } | |
| bool | |
| PointerType::is_const () const | |
| { | |
| return mut == Mutability::Imm; | |
| } | |
| bool | |
| PointerType::is_dyn_object () const | |
| { | |
| return is_dyn_slice_type () || is_dyn_str_type () || is_dyn_obj_type () | |
| || is_dyn_adt_type (); | |
| } | |
| bool | |
| PointerType::is_dyn_slice_type (const TyTy::SliceType **slice) const | |
| { | |
| const TyTy::BaseType *element = destructure_through_projections (get_base ()); | |
| if (element->get_kind () != TyTy::TypeKind::SLICE) | |
| return false; | |
| if (slice == nullptr) | |
| return true; | |
| *slice = static_cast<const TyTy::SliceType *> (element); | |
| return true; | |
| } | |
| bool | |
| PointerType::is_dyn_str_type (const TyTy::StrType **str) const | |
| { | |
| const TyTy::BaseType *element = destructure_through_projections (get_base ()); | |
| if (element->get_kind () != TyTy::TypeKind::STR) | |
| return false; | |
| if (str == nullptr) | |
| return true; | |
| *str = static_cast<const TyTy::StrType *> (element); | |
| return true; | |
| } | |
| bool | |
| PointerType::is_dyn_obj_type (const TyTy::DynamicObjectType **dyn) const | |
| { | |
| const TyTy::BaseType *element = destructure_through_projections (get_base ()); | |
| if (element->get_kind () != TyTy::TypeKind::DYNAMIC) | |
| return false; | |
| if (dyn == nullptr) | |
| return true; | |
| *dyn = static_cast<const TyTy::DynamicObjectType *> (element); | |
| return true; | |
| } | |
| bool | |
| PointerType::is_dyn_adt_type (const TyTy::ADTType **adt) const | |
| { | |
| const TyTy::BaseType *element = destructure_through_projections (get_base ()); | |
| if (element->get_kind () != TyTy::TypeKind::ADT) | |
| return false; | |
| const TyTy::ADTType *adt_ty = static_cast<const TyTy::ADTType *> (element); | |
| if (!adt_ty->is_unsized ()) | |
| return false; | |
| if (adt != nullptr) | |
| *adt = adt_ty; | |
| return true; | |
| } | |
| void | |
| PointerType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| PointerType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| PointerType::as_string () const | |
| { | |
| return std::string ("* ") + (is_mutable () ? "mut" : "const") + " " | |
| + get_base ()->get_name (); | |
| } | |
| std::string | |
| PointerType::get_name () const | |
| { | |
| return std::string ("* ") + (is_mutable () ? "mut" : "const") + " " | |
| + get_base ()->get_name (); | |
| } | |
| bool | |
| PointerType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| auto other2 = static_cast<const PointerType &> (other); | |
| if (mutability () != other2.mutability ()) | |
| return false; | |
| return get_base ()->is_equal (*other2.get_base ()); | |
| } | |
| BaseType * | |
| PointerType::get_base () const | |
| { | |
| return base.get_tyty (); | |
| } | |
| const TyVar & | |
| PointerType::get_var_element_type () const | |
| { | |
| return base; | |
| } | |
| BaseType * | |
| PointerType::clone () const | |
| { | |
| return new PointerType (get_ref (), get_ty_ref (), base, mutability (), | |
| get_combined_refs ()); | |
| } | |
| PointerType * | |
| PointerType::handle_substitions (SubstitutionArgumentMappings &mappings) | |
| { | |
| auto &mappings_table = Analysis::Mappings::get (); | |
| PointerType *ref = static_cast<PointerType *> (clone ()); | |
| ref->set_ty_ref (mappings_table.get_next_hir_id ()); | |
| // might be &T or &ADT so this needs to be recursive | |
| auto base = ref->get_base (); | |
| BaseType *concrete = Resolver::SubstMapperInternal::Resolve (base, mappings); | |
| ref->base = TyVar::subst_covariant_var (base, concrete); | |
| return ref; | |
| } | |
| // PARAM Type | |
| ParamType::ParamType (std::string symbol, location_t locus, HirId ref, | |
| HirId decl_id, | |
| std::vector<TypeBoundPredicate> specified_bounds, | |
| std::set<HirId> refs) | |
| : BaseGeneric (ref, ref, KIND, | |
| {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), | |
| locus}, | |
| std::move (specified_bounds), refs), | |
| decl_id (decl_id), is_trait_self (false), symbol (symbol) | |
| {} | |
| ParamType::ParamType (bool is_trait_self, std::string symbol, location_t locus, | |
| HirId ref, HirId ty_ref, HirId decl_id, | |
| std::vector<TypeBoundPredicate> specified_bounds, | |
| std::set<HirId> refs) | |
| : BaseGeneric (ref, ty_ref, KIND, | |
| {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), | |
| locus}, | |
| std::move (specified_bounds), refs), | |
| decl_id (decl_id), is_trait_self (is_trait_self), symbol (symbol) | |
| {} | |
| bool | |
| ParamType::can_resolve () const | |
| { | |
| return get_ref () != get_ty_ref (); | |
| } | |
| void | |
| ParamType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ParamType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ParamType::as_string () const | |
| { | |
| if (!can_resolve ()) | |
| { | |
| return get_symbol () + " REF: " + std::to_string (get_ref ()); | |
| } | |
| BaseType *lookup = resolve (); | |
| return get_symbol () + "=" + lookup->as_string (); | |
| } | |
| std::string | |
| ParamType::get_name () const | |
| { | |
| if (!can_resolve ()) | |
| return get_symbol (); | |
| static std::vector<const ParamType *> active; | |
| if (Resolver::ScopedPush<const ParamType *>::contains (active, this)) | |
| return get_symbol (); | |
| Resolver::ScopedPush<const ParamType *> guard (active, this); | |
| return destructure ()->get_name (); | |
| } | |
| BaseType * | |
| ParamType::clone () const | |
| { | |
| static std::vector<HirId> active; | |
| bool cycle = Resolver::ScopedPush<HirId>::contains (active, get_ty_ref ()); | |
| if (cycle) | |
| return new ParamType (is_trait_self, get_symbol (), ident.locus, get_ref (), | |
| get_ty_ref (), get_decl_id (), {}, | |
| get_combined_refs ()); | |
| Resolver::ScopedPush<HirId> guard (active, get_ty_ref ()); | |
| return new ParamType (is_trait_self, get_symbol (), ident.locus, get_ref (), | |
| get_ty_ref (), get_decl_id (), get_specified_bounds (), | |
| get_combined_refs ()); | |
| } | |
| std::string | |
| ParamType::get_symbol () const | |
| { | |
| return symbol; | |
| } | |
| BaseType * | |
| ParamType::resolve () const | |
| { | |
| TyVar var (get_ty_ref ()); | |
| BaseType *r = var.get_tyty (); | |
| while (r->get_kind () == TypeKind::PARAM) | |
| { | |
| ParamType *rr = static_cast<ParamType *> (r); | |
| if (!rr->can_resolve ()) | |
| break; | |
| TyVar v (rr->get_ty_ref ()); | |
| BaseType *n = v.get_tyty (); | |
| // fix infinite loop | |
| if (r == n) | |
| break; | |
| r = n; | |
| } | |
| if (r->get_kind () == TypeKind::PARAM && (r->get_ref () == r->get_ty_ref ())) | |
| return TyVar (r->get_ty_ref ()).get_tyty (); | |
| return r; | |
| } | |
| bool | |
| ParamType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| { | |
| if (!can_resolve ()) | |
| return false; | |
| return resolve ()->is_equal (other); | |
| } | |
| auto other2 = static_cast<const ParamType &> (other); | |
| if (can_resolve () != other2.can_resolve ()) | |
| return false; | |
| if (can_resolve ()) | |
| return Resolver::types_compatable (TyTy::TyWithLocation (resolve ()), | |
| TyTy::TyWithLocation (other2.resolve ()), | |
| UNKNOWN_LOCATION, false, false); | |
| return get_symbol ().compare (other2.get_symbol ()) == 0; | |
| } | |
| ParamType * | |
| ParamType::handle_substitions (SubstitutionArgumentMappings &subst_mappings) | |
| { | |
| SubstitutionArg arg = SubstitutionArg::error (); | |
| bool ok = subst_mappings.get_argument_for_symbol (this, &arg); | |
| if (!ok || arg.is_error ()) | |
| return this; | |
| ParamType *p = static_cast<ParamType *> (clone ()); | |
| const BaseType *resolved = arg.get_tyty (); | |
| if (resolved->get_kind () == TyTy::TypeKind::PARAM) | |
| { | |
| const ParamType &pp = *static_cast<const ParamType *> (resolved); | |
| if (pp.can_resolve ()) | |
| pp.resolve (); | |
| } | |
| // this is the new subst that this needs to pass | |
| p->set_ref (mappings.get_next_hir_id ()); | |
| p->set_ty_ref (arg.get_tyty ()->get_ref ()); | |
| return p; | |
| } | |
| void | |
| ParamType::set_implicit_self_trait () | |
| { | |
| is_trait_self = true; | |
| } | |
| bool | |
| ParamType::is_implicit_self_trait () const | |
| { | |
| return is_trait_self; | |
| } | |
| static std::string | |
| generate_tree_str (tree value) | |
| { | |
| pretty_printer pp; | |
| dump_generic_node (&pp, value, 0, TDF_NONE, true); | |
| std::string result = pp_formatted_text (&pp); | |
| if (!result.empty () && result.back () == '\n') | |
| result.pop_back (); | |
| return result; | |
| } | |
| // --- | |
| ConstParamType::ConstParamType (std::string symbol, location_t locus, | |
| BaseType *type, HirId ref, HirId ty_ref, | |
| std::set<HirId> refs) | |
| : BaseConstType (type), | |
| BaseGeneric (ref, ty_ref, KIND, | |
| {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), | |
| locus}, | |
| {}, refs), | |
| symbol (symbol) | |
| {} | |
| BaseConstType::ConstKind | |
| ConstParamType::const_kind () const | |
| { | |
| return BaseConstType::ConstKind::Decl; | |
| } | |
| std::string | |
| ConstParamType::get_symbol () const | |
| { | |
| return symbol; | |
| } | |
| bool | |
| ConstParamType::can_resolve () const | |
| { | |
| return get_ref () != get_ty_ref (); | |
| } | |
| BaseType * | |
| ConstParamType::resolve () const | |
| { | |
| TyVar var (get_ty_ref ()); | |
| BaseType *r = var.get_tyty (); | |
| while (r->get_kind () == TypeKind::CONST) | |
| { | |
| TyVar v (r->get_ty_ref ()); | |
| BaseType *n = v.get_tyty (); | |
| // fix infinite loop | |
| if (r == n) | |
| break; | |
| r = n; | |
| } | |
| if (r->get_kind () == TypeKind::CONST && (r->get_ref () == r->get_ty_ref ())) | |
| { | |
| auto *const_type = r->as_const_type (); | |
| if (const_type->const_kind () != BaseConstType::ConstKind::Value) | |
| return TyVar (r->get_ty_ref ()).get_tyty (); | |
| } | |
| return r; | |
| } | |
| void | |
| ConstParamType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ConstParamType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ConstParamType::as_string () const | |
| { | |
| if (!can_resolve ()) | |
| { | |
| return get_symbol () + " CONST_REF: " + std::to_string (get_ref ()); | |
| } | |
| BaseType *lookup = resolve (); | |
| // Avoid infinite recursion if resolve() returns this same type | |
| if (lookup == this->as_base_type ()) | |
| { | |
| return get_symbol () + " CONST_REF: " + std::to_string (get_ref ()); | |
| } | |
| return get_symbol () + "=" + lookup->as_string (); | |
| } | |
| BaseType * | |
| ConstParamType::clone () const | |
| { | |
| return new ConstParamType (get_symbol (), ident.locus, specified_type, | |
| get_ref (), get_ty_ref (), get_combined_refs ()); | |
| } | |
| std::string | |
| ConstParamType::get_name () const | |
| { | |
| if (!can_resolve ()) | |
| return get_symbol (); | |
| BaseType *lookup = resolve (); | |
| if (lookup == this->as_base_type ()) | |
| return get_symbol () + ":" + get_specified_type ()->get_name (); | |
| return lookup->get_name (); | |
| } | |
| bool | |
| ConstParamType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| { | |
| if (!can_resolve ()) | |
| return false; | |
| return resolve ()->is_equal (other); | |
| } | |
| auto other_const = other.as_const_type (); | |
| if (other_const->const_kind () != BaseConstType::ConstKind::Decl) | |
| return false; | |
| auto &other2 = static_cast<const ConstParamType &> (*other_const); | |
| if (can_resolve () != other2.can_resolve ()) | |
| return false; | |
| if (can_resolve ()) | |
| { | |
| // Compare the resolved ty_ref values to avoid infinite recursion | |
| // through types_compatable/unification | |
| BaseType *lhs = resolve (); | |
| BaseType *rhs = other2.resolve (); | |
| // If they resolve to the same type (same ty_ref), they're equal | |
| if (lhs->get_ty_ref () == rhs->get_ty_ref ()) | |
| return true; | |
| // Otherwise check if the resolved types are equal | |
| // Avoid recursion by checking if we'd be comparing ConstParamTypes again | |
| if (lhs->get_kind () == TypeKind::CONST | |
| && lhs->as_const_type ()->const_kind () | |
| == BaseConstType::ConstKind::Decl) | |
| return false; // Would cause recursion, so not equal | |
| return lhs->is_equal (*rhs); | |
| } | |
| return get_symbol ().compare (other2.get_symbol ()) == 0; | |
| } | |
| BaseType * | |
| ConstParamType::handle_substitions ( | |
| SubstitutionArgumentMappings &subst_mappings) | |
| { | |
| SubstitutionArg arg = SubstitutionArg::error (); | |
| bool ok = subst_mappings.get_argument_for_symbol (this, &arg); | |
| if (!ok || arg.is_error ()) | |
| return this; | |
| ConstParamType *p = static_cast<ConstParamType *> (clone ()); | |
| const BaseType *resolved = arg.get_tyty (); | |
| // this is the new subst that this needs to pass | |
| p->set_ref (mappings.get_next_hir_id ()); | |
| p->set_ty_ref (resolved->get_ref ()); | |
| return p; | |
| } | |
| // --- ConstValueType | |
| ConstValueType::ConstValueType (tree value, BaseType *type, HirId ref, | |
| HirId ty_ref, std::set<HirId> refs) | |
| : BaseType (ref, ty_ref, KIND, | |
| {Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION}, | |
| refs), | |
| BaseConstType (type), folded_val (value) | |
| {} | |
| BaseConstType::ConstKind | |
| ConstValueType::const_kind () const | |
| { | |
| return BaseConstType::ConstKind::Value; | |
| } | |
| void | |
| ConstValueType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ConstValueType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ConstValueType::as_string () const | |
| { | |
| return generate_tree_str (folded_val); | |
| } | |
| BaseType * | |
| ConstValueType::clone () const | |
| { | |
| return new ConstValueType (folded_val, specified_type, get_ref (), | |
| get_ty_ref (), get_combined_refs ()); | |
| } | |
| std::string | |
| ConstValueType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| bool | |
| ConstValueType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| auto other_const = other.as_const_type (); | |
| if (other_const->const_kind () != BaseConstType::ConstKind::Value) | |
| return false; | |
| auto &other2 = static_cast<const ConstValueType &> (*other_const); | |
| return folded_val == other2.folded_val; | |
| } | |
| tree | |
| ConstValueType::get_value () const | |
| { | |
| return folded_val; | |
| } | |
| // --- ConstInferType | |
| ConstInferType::ConstInferType (BaseType *type, HirId ref, HirId ty_ref, | |
| std::set<HirId> refs) | |
| : BaseType (ref, ty_ref, KIND, | |
| {Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION}, | |
| refs), | |
| BaseConstType (type) | |
| {} | |
| BaseConstType::ConstKind | |
| ConstInferType::const_kind () const | |
| { | |
| return BaseConstType::ConstKind::Infer; | |
| } | |
| void | |
| ConstInferType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ConstInferType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ConstInferType::as_string () const | |
| { | |
| return specified_type->get_name () + "-?"; | |
| } | |
| BaseType * | |
| ConstInferType::clone () const | |
| { | |
| auto &mappings = Analysis::Mappings::get (); | |
| auto context = Resolver::TypeCheckContext::get (); | |
| ConstInferType *clone | |
| = new ConstInferType (specified_type, mappings.get_next_hir_id (), | |
| get_ty_ref (), get_combined_refs ()); | |
| context->insert_type ( | |
| Analysis::NodeMapping (mappings.crate.get_current_crate (), UNKNOWN_NODEID, | |
| clone->get_ref (), UNKNOWN_LOCAL_DEFID), | |
| clone); | |
| mappings.insert_location (clone->get_ref (), | |
| mappings.lookup_location (get_ref ())); | |
| clone->append_reference (get_ref ()); | |
| return clone; | |
| } | |
| std::string | |
| ConstInferType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| bool | |
| ConstInferType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| auto other_const = other.as_const_type (); | |
| if (other_const->const_kind () != BaseConstType::ConstKind::Infer) | |
| return false; | |
| return get_ref () == other.get_ref (); | |
| } | |
| // --- ConstErrorType | |
| ConstErrorType::ConstErrorType (BaseType *type, HirId ref, HirId ty_ref, | |
| std::set<HirId> refs) | |
| : BaseType (ref, ty_ref, KIND, | |
| {Resolver::CanonicalPath::create_empty (), UNKNOWN_LOCATION}, | |
| refs), | |
| BaseConstType (type) | |
| {} | |
| BaseConstType::ConstKind | |
| ConstErrorType::const_kind () const | |
| { | |
| return BaseConstType::ConstKind::Error; | |
| } | |
| void | |
| ConstErrorType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ConstErrorType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ConstErrorType::as_string () const | |
| { | |
| return "<const_error>"; | |
| } | |
| BaseType * | |
| ConstErrorType::clone () const | |
| { | |
| return new ConstErrorType (specified_type, get_ref (), get_ty_ref (), | |
| get_combined_refs ()); | |
| } | |
| std::string | |
| ConstErrorType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| bool | |
| ConstErrorType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| auto other_const = other.as_const_type (); | |
| return other_const->const_kind () == BaseConstType::ConstKind::Error; | |
| } | |
| // OpaqueType | |
| OpaqueType::OpaqueType (location_t locus, HirId ref, | |
| std::vector<TypeBoundPredicate> specified_bounds, | |
| std::set<HirId> refs) | |
| : BaseType (ref, ref, KIND, | |
| {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, "impl"), | |
| locus}, | |
| specified_bounds, refs) | |
| {} | |
| OpaqueType::OpaqueType (location_t locus, HirId ref, HirId ty_ref, | |
| std::vector<TypeBoundPredicate> specified_bounds, | |
| std::set<HirId> refs) | |
| : BaseType (ref, ty_ref, KIND, | |
| {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, "impl"), | |
| locus}, | |
| specified_bounds, refs) | |
| {} | |
| bool | |
| OpaqueType::can_resolve () const | |
| { | |
| return get_ref () != get_ty_ref (); | |
| } | |
| void | |
| OpaqueType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| OpaqueType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| OpaqueType::as_string () const | |
| { | |
| return get_name (); | |
| } | |
| std::string | |
| OpaqueType::get_name () const | |
| { | |
| return "impl " + raw_bounds_as_name (); | |
| } | |
| BaseType * | |
| OpaqueType::clone () const | |
| { | |
| return new OpaqueType (ident.locus, get_ref (), get_ty_ref (), | |
| get_specified_bounds (), get_combined_refs ()); | |
| } | |
| BaseType * | |
| OpaqueType::resolve () const | |
| { | |
| TyVar var (get_ty_ref ()); | |
| return var.get_tyty (); | |
| } | |
| bool | |
| OpaqueType::is_equal (const BaseType &other) const | |
| { | |
| auto other2 = static_cast<const OpaqueType &> (other); | |
| if (can_resolve () != other2.can_resolve ()) | |
| return false; | |
| if (num_specified_bounds () != other.num_specified_bounds ()) | |
| return false; | |
| for (const auto &pred : specified_bounds) | |
| { | |
| bool found = false; | |
| for (const auto &opred : other.get_specified_bounds ()) | |
| { | |
| found = pred.is_equal (opred); | |
| if (found) | |
| break; | |
| } | |
| if (!found) | |
| return false; | |
| } | |
| return true; | |
| } | |
| // StrType | |
| StrType::StrType (HirId ref, std::set<HirId> refs) | |
| : 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs) | |
| {} | |
| std::string | |
| StrType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| BaseType * | |
| StrType::clone () const | |
| { | |
| return new StrType (get_ref (), get_ty_ref (), get_combined_refs ()); | |
| } | |
| void | |
| StrType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| StrType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| StrType::as_string () const | |
| { | |
| return "str"; | |
| } | |
| bool | |
| StrType::is_equal (const BaseType &other) const | |
| { | |
| return get_kind () == other.get_kind (); | |
| } | |
| // Never Type | |
| NeverType::NeverType (HirId ref, std::set<HirId> refs) | |
| : 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, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs) | |
| {} | |
| std::string | |
| NeverType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| void | |
| NeverType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| NeverType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| NeverType::as_string () const | |
| { | |
| return "!"; | |
| } | |
| BaseType * | |
| NeverType::clone () const | |
| { | |
| return new NeverType (get_ref (), get_ty_ref (), get_combined_refs ()); | |
| } | |
| // placeholder type | |
| PlaceholderType::PlaceholderType (std::string symbol, DefId id, HirId ref, | |
| std::set<HirId> refs) | |
| : BaseType (ref, ref, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs), | |
| symbol (symbol), defId (id) | |
| {} | |
| PlaceholderType::PlaceholderType (std::string symbol, DefId id, HirId ref, | |
| HirId ty_ref, std::set<HirId> refs) | |
| : BaseType (ref, ty_ref, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs), | |
| symbol (symbol), defId (id) | |
| {} | |
| std::string | |
| PlaceholderType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| std::string | |
| PlaceholderType::get_symbol () const | |
| { | |
| return symbol; | |
| } | |
| void | |
| PlaceholderType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| PlaceholderType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| PlaceholderType::as_string () const | |
| { | |
| return "<placeholder:" + (can_resolve () ? resolve ()->as_string () : "") | |
| + ">"; | |
| } | |
| BaseType * | |
| PlaceholderType::clone () const | |
| { | |
| return new PlaceholderType (get_symbol (), get_def_id (), get_ref (), | |
| get_ty_ref (), get_combined_refs ()); | |
| } | |
| bool | |
| PlaceholderType::can_resolve () const | |
| { | |
| auto context = Resolver::TypeCheckContext::get (); | |
| BaseType *lookup = nullptr; | |
| HirId mapping; | |
| if (!context->lookup_associated_type_mapping (get_ty_ref (), &mapping)) | |
| return false; | |
| if (!context->lookup_type (mapping, &lookup)) | |
| return false; | |
| return lookup != nullptr; | |
| } | |
| BaseType * | |
| PlaceholderType::resolve () const | |
| { | |
| auto context = Resolver::TypeCheckContext::get (); | |
| HirId mapping; | |
| bool ok = context->lookup_associated_type_mapping (get_ty_ref (), &mapping); | |
| rust_assert (ok); | |
| return TyVar (mapping).get_tyty (); | |
| } | |
| bool | |
| PlaceholderType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| { | |
| if (!can_resolve ()) | |
| return false; | |
| return resolve ()->is_equal (other); | |
| } | |
| auto other2 = static_cast<const PlaceholderType &> (other); | |
| return get_symbol ().compare (other2.get_symbol ()) == 0; | |
| } | |
| DefId | |
| PlaceholderType::get_def_id () const | |
| { | |
| return defId; | |
| } | |
| // Projection type | |
| ProjectionType::ProjectionType ( | |
| HirId ref, BaseType *base, const Resolver::TraitReference *trait, DefId item, | |
| std::vector<SubstitutionParamMapping> subst_refs, TyTy::BaseType *self, | |
| SubstitutionArgumentMappings generic_arguments, | |
| RegionConstraints region_constraints, std::set<HirId> refs, | |
| size_t num_trait_substitutions) | |
| : BaseType (ref, ref, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| std::move (refs)), | |
| SubstitutionRef (std::move (subst_refs), std::move (generic_arguments), | |
| std::move (region_constraints)), | |
| base (base), trait (trait), item (item), self (self), | |
| num_trait_substitutions (num_trait_substitutions) | |
| {} | |
| ProjectionType::ProjectionType ( | |
| HirId ref, HirId ty_ref, BaseType *base, | |
| const Resolver::TraitReference *trait, DefId item, | |
| std::vector<SubstitutionParamMapping> subst_refs, TyTy::BaseType *self, | |
| SubstitutionArgumentMappings generic_arguments, | |
| RegionConstraints region_constraints, std::set<HirId> refs, | |
| size_t num_trait_substitutions) | |
| : BaseType (ref, ty_ref, KIND, | |
| {Resolver::CanonicalPath::create_empty (), BUILTINS_LOCATION}, | |
| refs), | |
| SubstitutionRef (std::move (subst_refs), std::move (generic_arguments), | |
| std::move (region_constraints)), | |
| base (base), trait (trait), item (item), self (self), | |
| num_trait_substitutions (num_trait_substitutions) | |
| {} | |
| std::string | |
| ProjectionType::get_name () const | |
| { | |
| return as_string (); | |
| } | |
| bool | |
| ProjectionType::is_trait_position () const | |
| { | |
| return base == nullptr; | |
| } | |
| const BaseType * | |
| ProjectionType::get () const | |
| { | |
| rust_assert (base != nullptr); | |
| return base; | |
| } | |
| BaseType * | |
| ProjectionType::get () | |
| { | |
| rust_assert (base != nullptr); | |
| return base; | |
| } | |
| const BaseType * | |
| ProjectionType::get_self () const | |
| { | |
| return self; | |
| } | |
| BaseType * | |
| ProjectionType::get_self () | |
| { | |
| return self; | |
| } | |
| const Resolver::TraitReference * | |
| ProjectionType::get_trait_ref () const | |
| { | |
| return trait; | |
| } | |
| DefId | |
| ProjectionType::get_item_defid () const | |
| { | |
| return item; | |
| } | |
| void | |
| ProjectionType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| ProjectionType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| ProjectionType::as_string () const | |
| { | |
| return "<Projection=" + subst_as_string () | |
| + "::" + (base == nullptr ? "TRAIT_POSITION" : base->as_string ()) | |
| + "::" + self->as_string () + ">"; | |
| } | |
| BaseType * | |
| ProjectionType::clone () const | |
| { | |
| auto *cloned | |
| = new ProjectionType (get_ref (), get_ty_ref (), | |
| base != nullptr ? base->clone () : nullptr, trait, | |
| item, clone_substs (), self->clone (), used_arguments, | |
| region_constraints, get_combined_refs (), | |
| num_trait_substitutions); | |
| cloned->inherit_bounds (get_specified_bounds ()); | |
| return cloned; | |
| } | |
| ProjectionType * | |
| ProjectionType::handle_substitions ( | |
| SubstitutionArgumentMappings &subst_mappings) | |
| { | |
| ProjectionType *projection = static_cast<ProjectionType *> (clone ()); | |
| projection->set_ty_ref (mappings.get_next_hir_id ()); | |
| projection->used_arguments = subst_mappings; | |
| auto context = Resolver::TypeCheckContext::get (); | |
| context->insert_implicit_type (projection->get_ty_ref (), projection); | |
| for (auto &sub : projection->get_substs ()) | |
| { | |
| SubstitutionArg arg = SubstitutionArg::error (); | |
| bool ok | |
| = subst_mappings.get_argument_for_symbol (sub.get_param_ty (), &arg); | |
| if (ok) | |
| sub.fill_param_ty (subst_mappings, subst_mappings.get_locus ()); | |
| } | |
| auto fty = projection->self; | |
| if (fty->get_kind () == TypeKind::PARAM) | |
| { | |
| ParamType *p = static_cast<ParamType *> (fty); | |
| SubstitutionArg arg = SubstitutionArg::error (); | |
| bool ok = subst_mappings.get_argument_for_symbol (p, &arg); | |
| if (ok) | |
| { | |
| auto argt = arg.get_tyty (); | |
| bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM; | |
| bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER; | |
| if (arg_is_param || arg_is_concrete) | |
| { | |
| auto new_field = argt->clone (); | |
| new_field->set_ref (fty->get_ref ()); | |
| projection->self = new_field; | |
| } | |
| else | |
| { | |
| fty->set_ty_ref (argt->get_ref ()); | |
| } | |
| } | |
| } | |
| else if (fty->needs_generic_substitutions () || !fty->is_concrete ()) | |
| { | |
| BaseType *concrete | |
| = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings); | |
| if (concrete == nullptr || concrete->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| rust_error_at (subst_mappings.get_locus (), | |
| "Failed to resolve field substitution type: %s", | |
| fty->as_string ().c_str ()); | |
| return nullptr; | |
| } | |
| projection->self = concrete; | |
| } | |
| fty = projection->base; | |
| if (fty == nullptr) | |
| return projection; | |
| if (fty->get_kind () == TypeKind::PARAM) | |
| { | |
| ParamType *p = static_cast<ParamType *> (fty); | |
| SubstitutionArg arg = SubstitutionArg::error (); | |
| bool ok = subst_mappings.get_argument_for_symbol (p, &arg); | |
| if (ok) | |
| { | |
| auto argt = arg.get_tyty (); | |
| bool arg_is_param = argt->get_kind () == TyTy::TypeKind::PARAM; | |
| bool arg_is_concrete = argt->get_kind () != TyTy::TypeKind::INFER; | |
| if (arg_is_param || arg_is_concrete) | |
| { | |
| auto new_field = argt->clone (); | |
| new_field->set_ref (fty->get_ref ()); | |
| projection->base = new_field; | |
| } | |
| else | |
| { | |
| fty->set_ty_ref (argt->get_ref ()); | |
| } | |
| } | |
| } | |
| else if (fty->needs_generic_substitutions () || !fty->is_concrete ()) | |
| { | |
| BaseType *concrete | |
| = Resolver::SubstMapperInternal::Resolve (fty, subst_mappings); | |
| if (concrete == nullptr || concrete->get_kind () == TyTy::TypeKind::ERROR) | |
| { | |
| rust_error_at (subst_mappings.get_locus (), | |
| "Failed to resolve field substitution type: %s", | |
| fty->as_string ().c_str ()); | |
| return nullptr; | |
| } | |
| projection->base = concrete; | |
| } | |
| return projection; | |
| } | |
| // DynObjectType | |
| DynamicObjectType::DynamicObjectType ( | |
| HirId ref, RustIdent ident, std::vector<TypeBoundPredicate> specified_bounds, | |
| std::set<HirId> 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, KIND, ident, specified_bounds, refs) | |
| {} | |
| void | |
| DynamicObjectType::accept_vis (TyVisitor &vis) | |
| { | |
| vis.visit (*this); | |
| } | |
| void | |
| DynamicObjectType::accept_vis (TyConstVisitor &vis) const | |
| { | |
| vis.visit (*this); | |
| } | |
| std::string | |
| DynamicObjectType::as_string () const | |
| { | |
| return "dyn [" + raw_bounds_as_string () + "]"; | |
| } | |
| BaseType * | |
| DynamicObjectType::clone () const | |
| { | |
| return new DynamicObjectType (get_ref (), get_ty_ref (), ident, | |
| specified_bounds, get_combined_refs ()); | |
| } | |
| std::string | |
| DynamicObjectType::get_name () const | |
| { | |
| return "dyn [" + raw_bounds_as_name () + "]"; | |
| } | |
| bool | |
| DynamicObjectType::is_equal (const BaseType &other) const | |
| { | |
| if (get_kind () != other.get_kind ()) | |
| return false; | |
| if (num_specified_bounds () != other.num_specified_bounds ()) | |
| return false; | |
| for (const auto &pred : specified_bounds) | |
| { | |
| bool found = false; | |
| for (const auto &opred : other.get_specified_bounds ()) | |
| { | |
| found = pred.is_equal (opred); | |
| if (found) | |
| break; | |
| } | |
| if (!found) | |
| return false; | |
| } | |
| return true; | |
| } | |
| const std::vector< | |
| std::pair<const Resolver::TraitItemReference *, const TypeBoundPredicate *>> | |
| DynamicObjectType::get_object_items () const | |
| { | |
| std::vector< | |
| std::pair<const Resolver::TraitItemReference *, const TypeBoundPredicate *>> | |
| items; | |
| for (const TypeBoundPredicate &bound : get_specified_bounds ()) | |
| { | |
| const Resolver::TraitReference *trait = bound.get (); | |
| std::vector<const Resolver::TraitItemReference *> trait_items; | |
| trait->get_trait_items_and_supers (trait_items); | |
| for (auto &item : trait_items) | |
| { | |
| if (item->get_trait_item_type () | |
| == Resolver::TraitItemReference::TraitItemType::FN | |
| && item->is_object_safe ()) | |
| items.emplace_back (item, &bound); | |
| } | |
| } | |
| return items; | |
| } | |
| WARN_UNUSED_RESULT tl::optional<BaseType *> | |
| try_get_box_inner_type (BaseType *base) | |
| { | |
| if (base->get_kind () != TypeKind::ADT) | |
| return tl::nullopt; | |
| ADTType *adt = static_cast<ADTType *> (base); | |
| auto owned_box_lookup | |
| = Analysis::Mappings::get ().lookup_lang_item (LangItem::Kind::OWNED_BOX); | |
| if (owned_box_lookup && adt->get_id () == *owned_box_lookup) | |
| { | |
| auto args = adt->get_substitution_arguments (); | |
| if (!args.is_empty ()) | |
| { | |
| auto inner = args.get_mappings ().front ().get_tyty (); | |
| rust_assert (inner != nullptr); | |
| return inner; | |
| } | |
| } | |
| return tl::nullopt; | |
| } | |
| } // namespace TyTy | |
| } // namespace Rust |