From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by sourceware.org (Postfix) with ESMTPS id BA29938708EA for ; Wed, 5 Apr 2023 14:06:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BA29938708EA Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wm1-x336.google.com with SMTP id m8so10052721wmq.5 for ; Wed, 05 Apr 2023 07:06:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703559; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=64E1C2AfWq3zwFBLOxWZQzWW/3rvPpOdDm+MiYWclwA=; b=E/xod7RaTCtW6H+KtVBF9y4KLYc4BZQxcV6830mBHNKtZaoAwwdUXCoIHfWlA7zq4f Ix1m4k5rGV/wPgqTK01ozUMdhfG0izgb31XosFjvyHfXMAOydCAE3OR4cqNLyzpyUXwv E22MXn3jWUEZOR8gmcC2Sq4/SJgkPBwZkslFYqDyDDLBzN+MsmrKR6q9L5TRAkuLooBu upZBfqdGLA5TGM7Bx20tWai1cSOZTrnltaKzOhLdC2T+HztjbPKopz0pf3QHCShnQ3/N HcV/+r0FgOku3danM7ZYWebQmCAdPDPll3qbxxUJZWfTsLjcB28xZAibFllkPQ9IyVbn GZqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703559; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=64E1C2AfWq3zwFBLOxWZQzWW/3rvPpOdDm+MiYWclwA=; b=33Z+AnqjeCrwbodfmRPe+1j6gNGvLF7cDkkpFNYJ5wy/kJXJM6PIEaNVc2njjeHrx9 VgZ3TyHw/KKUQuxXNVVywpE9hiFT5E+klHM0dWAcxOrBrRciW84F/HBJipe3PsyxhUXL EyydCSFIhL3E8YECMHqKBqaX2c8uVHXrL9sQMpf+qBC+7kwFX10IqdaOeWN0dCPLHCYk q2Xa9bV3D53P/9UeLp7U26FFHULpAFK1JSj4Zby47AVN95DJBkFFrh3m/3ZlyM4z5soU 1eaCC6XG74ilbPIllFAbDZDkDjePS6yYKQgtZisYcx5l4DYpVM8+D/l3s7GXpONSvWuQ T7zQ== X-Gm-Message-State: AAQBX9eKRe1R1HA2GQh65bR4cMNwajFSXDMiDxntqehbgWT6PZlQ/kBD epCjdFP90tYr83eHz6p73ZQs X-Google-Smtp-Source: AKy350aq/WwY4FQrf55UiXhnQNNvcOHHovuRB9DgcP75iIDLtt/B8QjsnJJ5Qxx/jY+E+HXEJmoRLQ== X-Received: by 2002:a7b:c852:0:b0:3eb:5ab3:b6d0 with SMTP id c18-20020a7bc852000000b003eb5ab3b6d0mr4627165wml.6.1680703559043; Wed, 05 Apr 2023 07:05:59 -0700 (PDT) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id ay8-20020a05600c1e0800b003edddae1068sm2330150wmb.9.2023.04.05.07.05.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:05:58 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [committed 43/88] gccrs: Refactor all code out of the rust-tyty.h header Date: Wed, 5 Apr 2023 16:03:27 +0200 Message-Id: <20230405140411.3016563-44-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230405140411.3016563-1-arthur.cohen@embecosm.com> References: <20230405140411.3016563-1-arthur.cohen@embecosm.com> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Philip Herron Signed-off-by: Philip Herron gcc/rust/ChangeLog: * typecheck/rust-hir-type-check.h: refactor * typecheck/rust-tyctx.cc (TypeCheckContext::iterate): refactor (TypeCheckContext::have_loop_context): likewise (TypeCheckContext::push_new_loop_context): likewise (TypeCheckContext::push_new_while_loop_context): likewise (TypeCheckContext::peek_loop_context): likewise (TypeCheckContext::pop_loop_context): likewise (TypeCheckContext::swap_head_loop_context): likewise (TypeCheckContext::insert_trait_reference): likewise (TypeCheckContext::lookup_trait_reference): likewise (TypeCheckContext::insert_receiver): likewise (TypeCheckContext::lookup_receiver): likewise (TypeCheckContext::insert_associated_type_mapping): likewise (TypeCheckContext::clear_associated_type_mapping): likewise (TypeCheckContext::lookup_associated_type_mapping): likewise (TypeCheckContext::insert_variant_definition): likewise (TypeCheckContext::lookup_variant_definition): likewise (TypeCheckContext::insert_operator_overload): likewise (TypeCheckContext::lookup_operator_overload): likewise (TypeCheckContext::insert_unconstrained_check_marker): likewise (TypeCheckContext::have_checked_for_unconstrained): likewise (TypeCheckContext::insert_resolved_predicate): likewise (TypeCheckContext::lookup_predicate): likewise (TypeCheckContext::insert_query): likewise (TypeCheckContext::query_completed): likewise (TypeCheckContext::query_in_progress): likewise (TypeCheckContext::insert_trait_query): likewise (TypeCheckContext::trait_query_completed): likewise (TypeCheckContext::trait_query_in_progress): likewise (TypeCheckContextItem::Item::Item): likewise (TypeCheckContextItem::TypeCheckContextItem): likewise (TypeCheckContextItem::get_item): likewise (TypeCheckContextItem::get_impl_item): likewise (TypeCheckContextItem::get_trait_item): likewise (TypeCheckContextItem::get_type): likewise * typecheck/rust-tyty.cc (StructFieldType::StructFieldType): likewise (StructFieldType::get_ref): likewise (StructFieldType::get_name): likewise (StructFieldType::get_field_type): likewise (StructFieldType::set_field_type): likewise (StructFieldType::is_concrete): likewise (StructFieldType::debug): likewise (StructFieldType::get_locus): likewise (VariantDef::variant_type_string): likewise (VariantDef::VariantDef): likewise (VariantDef::operator=): likewise (VariantDef::get_error_node): likewise (VariantDef::is_error): likewise (VariantDef::get_id): likewise (VariantDef::get_defid): likewise (VariantDef::get_variant_type): likewise (VariantDef::is_data_variant): likewise (VariantDef::is_dataless_variant): likewise (VariantDef::get_identifier): likewise (VariantDef::num_fields): likewise (VariantDef::get_field_at_index): likewise (VariantDef::get_fields): likewise (VariantDef::lookup_field): likewise (VariantDef::get_discriminant): likewise (VariantDef::as_string): likewise (VariantDef::is_equal): likewise (VariantDef::clone): likewise (VariantDef::monomorphized_clone): likewise (VariantDef::get_ident): likewise (TupleType::TupleType): likewise (TupleType::get_unit_type): likewise (TupleType::is_unit): likewise (TupleType::num_fields): likewise (TupleType::is_concrete): likewise (TupleType::get_fields): likewise (BoolType::BoolType): likewise (BoolType::get_name): likewise (BoolType::is_concrete): likewise (IntType::IntType): likewise (IntType::get_name): likewise (IntType::get_int_kind): likewise (IntType::is_concrete): likewise (UintType::UintType): likewise (UintType::get_name): likewise (UintType::get_uint_kind): likewise (UintType::is_concrete): likewise (FloatType::FloatType): likewise (FloatType::get_name): likewise (FloatType::get_float_kind): likewise (FloatType::is_concrete): likewise (USizeType::USizeType): likewise (USizeType::get_name): likewise (USizeType::is_concrete): likewise (ISizeType::ISizeType): likewise (ISizeType::get_name): likewise (ISizeType::is_concrete): likewise (CharType::CharType): likewise (CharType::is_concrete): likewise (CharType::get_name): likewise (ReferenceType::ReferenceType): likewise (ReferenceType::is_concrete): likewise (ReferenceType::mutability): likewise (ReferenceType::is_mutable): likewise (ReferenceType::is_dyn_object): likewise (ReferenceType::is_dyn_slice_type): likewise (ReferenceType::is_dyn_str_type): likewise (PointerType::PointerType): likewise (PointerType::is_concrete): likewise (PointerType::mutability): likewise (PointerType::is_mutable): likewise (PointerType::is_const): likewise (PointerType::is_dyn_object): likewise (PointerType::is_dyn_slice_type): likewise (PointerType::is_dyn_str_type): likewise (ParamType::ParamType): likewise (ParamType::get_generic_param): likewise (ParamType::can_resolve): likewise (ParamType::is_concrete): likewise (StrType::StrType): likewise (StrType::get_name): likewise (StrType::is_concrete): likewise (NeverType::NeverType): likewise (NeverType::get_name): likewise (NeverType::is_unit): likewise (NeverType::is_concrete): likewise (PlaceholderType::PlaceholderType): likewise (PlaceholderType::get_name): likewise (PlaceholderType::is_unit): likewise (PlaceholderType::get_symbol): likewise (PlaceholderType::is_concrete): likewise (ProjectionType::is_unit): likewise (ProjectionType::get_name): likewise (ProjectionType::needs_generic_substitutions): likewise (ProjectionType::supports_substitutions): likewise (ProjectionType::has_subsititions_defined): likewise (ProjectionType::get): likewise (ProjectionType::is_concrete): likewise (DynamicObjectType::is_concrete): likewise * typecheck/rust-tyty.h: likewise --- gcc/rust/typecheck/rust-hir-type-check.h | 332 ++------ gcc/rust/typecheck/rust-tyctx.cc | 379 +++++++++ gcc/rust/typecheck/rust-tyty.cc | 960 ++++++++++++++++++++++- gcc/rust/typecheck/rust-tyty.h | 667 +++------------- 4 files changed, 1508 insertions(+), 830 deletions(-) diff --git a/gcc/rust/typecheck/rust-hir-type-check.h b/gcc/rust/typecheck/rust-hir-type-check.h index adec2f91961..d1eb750a621 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.h +++ b/gcc/rust/typecheck/rust-hir-type-check.h @@ -38,37 +38,17 @@ public: TRAIT_ITEM, }; - TypeCheckContextItem (HIR::Function *item) - : type (ItemType::ITEM), item (item) - {} + TypeCheckContextItem (HIR::Function *item); + TypeCheckContextItem (HIR::ImplBlock *impl_block, HIR::Function *item); + TypeCheckContextItem (HIR::TraitItemFunc *trait_item); - TypeCheckContextItem (HIR::ImplBlock *impl_block, HIR::Function *item) - : type (ItemType::IMPL_ITEM), item (impl_block, item) - {} + ItemType get_type () const; - TypeCheckContextItem (HIR::TraitItemFunc *trait_item) - : type (ItemType::TRAIT_ITEM), item (trait_item) - {} + HIR::Function *get_item (); - ItemType get_type () const { return type; } + std::pair &get_impl_item (); - HIR::Function *get_item () - { - rust_assert (get_type () == ItemType::ITEM); - return item.item; - } - - std::pair &get_impl_item () - { - rust_assert (get_type () == ItemType::IMPL_ITEM); - return item.impl_item; - }; - - HIR::TraitItemFunc *get_trait_item () - { - rust_assert (get_type () == ItemType::TRAIT_ITEM); - return item.trait_item; - } + HIR::TraitItemFunc *get_trait_item (); TyTy::FnType *get_context_type (); @@ -79,13 +59,9 @@ private: std::pair impl_item; HIR::TraitItemFunc *trait_item; - Item (HIR::Function *item) : item (item) {} - - Item (HIR::ImplBlock *impl_block, HIR::Function *item) - : impl_item ({impl_block, item}) - {} - - Item (HIR::TraitItemFunc *trait_item) : trait_item (trait_item) {} + Item (HIR::Function *item); + Item (HIR::ImplBlock *impl_block, HIR::Function *item); + Item (HIR::TraitItemFunc *trait_item); }; ItemType type; @@ -118,283 +94,71 @@ public: void push_return_type (TypeCheckContextItem item, TyTy::BaseType *return_type); void pop_return_type (); + void iterate (std::function cb); - void iterate (std::function cb) - { - for (auto it = resolved.begin (); it != resolved.end (); it++) - { - if (!cb (it->first, it->second)) - return; - } - } - - bool have_loop_context () const { return !loop_type_stack.empty (); } - - void push_new_loop_context (HirId id, Location locus) - { - TyTy::BaseType *infer_var - = new TyTy::InferType (id, TyTy::InferType::InferTypeKind::GENERAL, - locus); - loop_type_stack.push_back (infer_var); - } - - void push_new_while_loop_context (HirId id) - { - TyTy::BaseType *infer_var = new TyTy::ErrorType (id); - loop_type_stack.push_back (infer_var); - } - - TyTy::BaseType *peek_loop_context () { return loop_type_stack.back (); } - - TyTy::BaseType *pop_loop_context () - { - auto back = peek_loop_context (); - loop_type_stack.pop_back (); - return back; - } - - void swap_head_loop_context (TyTy::BaseType *val) - { - loop_type_stack.pop_back (); - loop_type_stack.push_back (val); - } - - void insert_trait_reference (DefId id, TraitReference &&ref) - { - rust_assert (trait_context.find (id) == trait_context.end ()); - trait_context.emplace (id, std::move (ref)); - } - - bool lookup_trait_reference (DefId id, TraitReference **ref) - { - auto it = trait_context.find (id); - if (it == trait_context.end ()) - return false; + bool have_loop_context () const; + void push_new_loop_context (HirId id, Location locus); + void push_new_while_loop_context (HirId id); + TyTy::BaseType *peek_loop_context (); + TyTy::BaseType *pop_loop_context (); - *ref = &it->second; - return true; - } - - void insert_receiver (HirId id, TyTy::BaseType *t) - { - receiver_context[id] = t; - } + void swap_head_loop_context (TyTy::BaseType *val); - bool lookup_receiver (HirId id, TyTy::BaseType **ref) - { - auto it = receiver_context.find (id); - if (it == receiver_context.end ()) - return false; + void insert_trait_reference (DefId id, TraitReference &&ref); + bool lookup_trait_reference (DefId id, TraitReference **ref); - *ref = it->second; - return true; - } + void insert_receiver (HirId id, TyTy::BaseType *t); + bool lookup_receiver (HirId id, TyTy::BaseType **ref); - void insert_associated_trait_impl (HirId id, AssociatedImplTrait &&associated) - { - rust_assert (associated_impl_traits.find (id) - == associated_impl_traits.end ()); - associated_impl_traits.emplace (id, std::move (associated)); - } + void insert_associated_trait_impl (HirId id, + AssociatedImplTrait &&associated); + bool lookup_associated_trait_impl (HirId id, + AssociatedImplTrait **associated); - bool lookup_associated_trait_impl (HirId id, AssociatedImplTrait **associated) - { - auto it = associated_impl_traits.find (id); - if (it == associated_impl_traits.end ()) - return false; - - *associated = &it->second; - return true; - } - - void insert_associated_type_mapping (HirId id, HirId mapping) - { - associated_type_mappings[id] = mapping; - } - - void clear_associated_type_mapping (HirId id) - { - auto it = associated_type_mappings.find (id); - if (it != associated_type_mappings.end ()) - associated_type_mappings.erase (it); - } + void insert_associated_type_mapping (HirId id, HirId mapping); + void clear_associated_type_mapping (HirId id); // lookup any associated type mappings, the out parameter of mapping is // allowed to be nullptr which allows this interface to do a simple does exist // check - bool lookup_associated_type_mapping (HirId id, HirId *mapping) - { - auto it = associated_type_mappings.find (id); - if (it == associated_type_mappings.end ()) - return false; - - if (mapping != nullptr) - *mapping = it->second; - - return true; - } + bool lookup_associated_type_mapping (HirId id, HirId *mapping); void insert_associated_impl_mapping (HirId trait_id, const TyTy::BaseType *impl_type, - HirId impl_id) - { - auto it = associated_traits_to_impls.find (trait_id); - if (it == associated_traits_to_impls.end ()) - { - associated_traits_to_impls[trait_id] = {}; - } - - associated_traits_to_impls[trait_id].push_back ({impl_type, impl_id}); - } - + HirId impl_id); bool lookup_associated_impl_mapping_for_self (HirId trait_id, const TyTy::BaseType *self, - HirId *mapping) - { - auto it = associated_traits_to_impls.find (trait_id); - if (it == associated_traits_to_impls.end ()) - return false; - - for (auto &item : it->second) - { - if (item.first->can_eq (self, false)) - { - *mapping = item.second; - return true; - } - } - return false; - } + HirId *mapping); void insert_autoderef_mappings (HirId id, - std::vector &&adjustments) - { - rust_assert (autoderef_mappings.find (id) == autoderef_mappings.end ()); - autoderef_mappings.emplace (id, std::move (adjustments)); - } - + std::vector &&adjustments); bool lookup_autoderef_mappings (HirId id, - std::vector **adjustments) - { - auto it = autoderef_mappings.find (id); - if (it == autoderef_mappings.end ()) - return false; - - *adjustments = &it->second; - return true; - } + std::vector **adjustments); void insert_cast_autoderef_mappings (HirId id, - std::vector &&adjustments) - { - rust_assert (cast_autoderef_mappings.find (id) - == cast_autoderef_mappings.end ()); - cast_autoderef_mappings.emplace (id, std::move (adjustments)); - } - + std::vector &&adjustments); bool lookup_cast_autoderef_mappings (HirId id, - std::vector **adjustments) - { - auto it = cast_autoderef_mappings.find (id); - if (it == cast_autoderef_mappings.end ()) - return false; + std::vector **adjustments); - *adjustments = &it->second; - return true; - } + void insert_variant_definition (HirId id, HirId variant); + bool lookup_variant_definition (HirId id, HirId *variant); - void insert_variant_definition (HirId id, HirId variant) - { - auto it = variants.find (id); - rust_assert (it == variants.end ()); + void insert_operator_overload (HirId id, TyTy::FnType *call_site); + bool lookup_operator_overload (HirId id, TyTy::FnType **call); - variants[id] = variant; - } + void insert_unconstrained_check_marker (HirId id, bool status); + bool have_checked_for_unconstrained (HirId id, bool *result); - bool lookup_variant_definition (HirId id, HirId *variant) - { - auto it = variants.find (id); - if (it == variants.end ()) - return false; + void insert_resolved_predicate (HirId id, TyTy::TypeBoundPredicate predicate); + bool lookup_predicate (HirId id, TyTy::TypeBoundPredicate *result); - *variant = it->second; - return true; - } - - void insert_operator_overload (HirId id, TyTy::FnType *call_site) - { - auto it = operator_overloads.find (id); - rust_assert (it == operator_overloads.end ()); - - operator_overloads[id] = call_site; - } - - bool lookup_operator_overload (HirId id, TyTy::FnType **call) - { - auto it = operator_overloads.find (id); - if (it == operator_overloads.end ()) - return false; - - *call = it->second; - return true; - } + void insert_query (HirId id); + void query_completed (HirId id); + bool query_in_progress (HirId id) const; - void insert_unconstrained_check_marker (HirId id, bool status) - { - unconstrained[id] = status; - } - - bool have_checked_for_unconstrained (HirId id, bool *result) - { - auto it = unconstrained.find (id); - bool found = it != unconstrained.end (); - if (!found) - return false; - - *result = it->second; - return true; - } - - void insert_resolved_predicate (HirId id, TyTy::TypeBoundPredicate predicate) - { - auto it = predicates.find (id); - rust_assert (it == predicates.end ()); - - predicates.insert ({id, predicate}); - } - - bool lookup_predicate (HirId id, TyTy::TypeBoundPredicate *result) - { - auto it = predicates.find (id); - bool found = it != predicates.end (); - if (!found) - return false; - - *result = it->second; - return true; - } - - void insert_query (HirId id) { querys_in_progress.insert (id); } - - void query_completed (HirId id) { querys_in_progress.erase (id); } - - bool query_in_progress (HirId id) const - { - return querys_in_progress.find (id) != querys_in_progress.end (); - } - - void insert_trait_query (DefId id) { trait_queries_in_progress.insert (id); } - - void trait_query_completed (DefId id) - { - trait_queries_in_progress.erase (id); - } - - bool trait_query_in_progress (DefId id) const - { - return trait_queries_in_progress.find (id) - != trait_queries_in_progress.end (); - } + void insert_trait_query (DefId id); + void trait_query_completed (DefId id); + bool trait_query_in_progress (DefId id) const; private: TypeCheckContext (); diff --git a/gcc/rust/typecheck/rust-tyctx.cc b/gcc/rust/typecheck/rust-tyctx.cc index 886842b0623..27ff96986dc 100644 --- a/gcc/rust/typecheck/rust-tyctx.cc +++ b/gcc/rust/typecheck/rust-tyctx.cc @@ -154,8 +154,387 @@ TypeCheckContext::peek_context () return return_type_stack.back ().first; } +void +TypeCheckContext::iterate (std::function cb) +{ + for (auto it = resolved.begin (); it != resolved.end (); it++) + { + if (!cb (it->first, it->second)) + return; + } +} + +bool +TypeCheckContext::have_loop_context () const +{ + return !loop_type_stack.empty (); +} + +void +TypeCheckContext::push_new_loop_context (HirId id, Location locus) +{ + TyTy::BaseType *infer_var + = new TyTy::InferType (id, TyTy::InferType::InferTypeKind::GENERAL, locus); + loop_type_stack.push_back (infer_var); +} + +void +TypeCheckContext::push_new_while_loop_context (HirId id) +{ + TyTy::BaseType *infer_var = new TyTy::ErrorType (id); + loop_type_stack.push_back (infer_var); +} + +TyTy::BaseType * +TypeCheckContext::peek_loop_context () +{ + return loop_type_stack.back (); +} + +TyTy::BaseType * +TypeCheckContext::pop_loop_context () +{ + auto back = peek_loop_context (); + loop_type_stack.pop_back (); + return back; +} + +void +TypeCheckContext::swap_head_loop_context (TyTy::BaseType *val) +{ + loop_type_stack.pop_back (); + loop_type_stack.push_back (val); +} + +void +TypeCheckContext::insert_trait_reference (DefId id, TraitReference &&ref) +{ + rust_assert (trait_context.find (id) == trait_context.end ()); + trait_context.emplace (id, std::move (ref)); +} + +bool +TypeCheckContext::lookup_trait_reference (DefId id, TraitReference **ref) +{ + auto it = trait_context.find (id); + if (it == trait_context.end ()) + return false; + + *ref = &it->second; + return true; +} + +void +TypeCheckContext::insert_receiver (HirId id, TyTy::BaseType *t) +{ + receiver_context[id] = t; +} + +bool +TypeCheckContext::lookup_receiver (HirId id, TyTy::BaseType **ref) +{ + auto it = receiver_context.find (id); + if (it == receiver_context.end ()) + return false; + + *ref = it->second; + return true; +} + +void +TypeCheckContext::insert_associated_trait_impl ( + HirId id, AssociatedImplTrait &&associated) +{ + rust_assert (associated_impl_traits.find (id) + == associated_impl_traits.end ()); + associated_impl_traits.emplace (id, std::move (associated)); +} + +bool +TypeCheckContext::lookup_associated_trait_impl ( + HirId id, AssociatedImplTrait **associated) +{ + auto it = associated_impl_traits.find (id); + if (it == associated_impl_traits.end ()) + return false; + + *associated = &it->second; + return true; +} + +void +TypeCheckContext::insert_associated_type_mapping (HirId id, HirId mapping) +{ + associated_type_mappings[id] = mapping; +} + +void +TypeCheckContext::clear_associated_type_mapping (HirId id) +{ + auto it = associated_type_mappings.find (id); + if (it != associated_type_mappings.end ()) + associated_type_mappings.erase (it); +} + +// lookup any associated type mappings, the out parameter of mapping is +// allowed to be nullptr which allows this interface to do a simple does exist +// check +bool +TypeCheckContext::lookup_associated_type_mapping (HirId id, HirId *mapping) +{ + auto it = associated_type_mappings.find (id); + if (it == associated_type_mappings.end ()) + return false; + + if (mapping != nullptr) + *mapping = it->second; + + return true; +} + +void +TypeCheckContext::insert_associated_impl_mapping ( + HirId trait_id, const TyTy::BaseType *impl_type, HirId impl_id) +{ + auto it = associated_traits_to_impls.find (trait_id); + if (it == associated_traits_to_impls.end ()) + { + associated_traits_to_impls[trait_id] = {}; + } + + associated_traits_to_impls[trait_id].push_back ({impl_type, impl_id}); +} + +bool +TypeCheckContext::lookup_associated_impl_mapping_for_self ( + HirId trait_id, const TyTy::BaseType *self, HirId *mapping) +{ + auto it = associated_traits_to_impls.find (trait_id); + if (it == associated_traits_to_impls.end ()) + return false; + + for (auto &item : it->second) + { + if (item.first->can_eq (self, false)) + { + *mapping = item.second; + return true; + } + } + return false; +} + +void +TypeCheckContext::insert_autoderef_mappings ( + HirId id, std::vector &&adjustments) +{ + rust_assert (autoderef_mappings.find (id) == autoderef_mappings.end ()); + autoderef_mappings.emplace (id, std::move (adjustments)); +} + +bool +TypeCheckContext::lookup_autoderef_mappings ( + HirId id, std::vector **adjustments) +{ + auto it = autoderef_mappings.find (id); + if (it == autoderef_mappings.end ()) + return false; + + *adjustments = &it->second; + return true; +} + +void +TypeCheckContext::insert_cast_autoderef_mappings ( + HirId id, std::vector &&adjustments) +{ + rust_assert (cast_autoderef_mappings.find (id) + == cast_autoderef_mappings.end ()); + cast_autoderef_mappings.emplace (id, std::move (adjustments)); +} + +bool +TypeCheckContext::lookup_cast_autoderef_mappings ( + HirId id, std::vector **adjustments) +{ + auto it = cast_autoderef_mappings.find (id); + if (it == cast_autoderef_mappings.end ()) + return false; + + *adjustments = &it->second; + return true; +} + +void +TypeCheckContext::insert_variant_definition (HirId id, HirId variant) +{ + auto it = variants.find (id); + rust_assert (it == variants.end ()); + + variants[id] = variant; +} + +bool +TypeCheckContext::lookup_variant_definition (HirId id, HirId *variant) +{ + auto it = variants.find (id); + if (it == variants.end ()) + return false; + + *variant = it->second; + return true; +} + +void +TypeCheckContext::insert_operator_overload (HirId id, TyTy::FnType *call_site) +{ + auto it = operator_overloads.find (id); + rust_assert (it == operator_overloads.end ()); + + operator_overloads[id] = call_site; +} + +bool +TypeCheckContext::lookup_operator_overload (HirId id, TyTy::FnType **call) +{ + auto it = operator_overloads.find (id); + if (it == operator_overloads.end ()) + return false; + + *call = it->second; + return true; +} + +void +TypeCheckContext::insert_unconstrained_check_marker (HirId id, bool status) +{ + unconstrained[id] = status; +} + +bool +TypeCheckContext::have_checked_for_unconstrained (HirId id, bool *result) +{ + auto it = unconstrained.find (id); + bool found = it != unconstrained.end (); + if (!found) + return false; + + *result = it->second; + return true; +} + +void +TypeCheckContext::insert_resolved_predicate (HirId id, + TyTy::TypeBoundPredicate predicate) +{ + auto it = predicates.find (id); + rust_assert (it == predicates.end ()); + + predicates.insert ({id, predicate}); +} + +bool +TypeCheckContext::lookup_predicate (HirId id, TyTy::TypeBoundPredicate *result) +{ + auto it = predicates.find (id); + bool found = it != predicates.end (); + if (!found) + return false; + + *result = it->second; + return true; +} + +void +TypeCheckContext::insert_query (HirId id) +{ + querys_in_progress.insert (id); +} + +void +TypeCheckContext::query_completed (HirId id) +{ + querys_in_progress.erase (id); +} + +bool +TypeCheckContext::query_in_progress (HirId id) const +{ + return querys_in_progress.find (id) != querys_in_progress.end (); +} + +void +TypeCheckContext::insert_trait_query (DefId id) +{ + trait_queries_in_progress.insert (id); +} + +void +TypeCheckContext::trait_query_completed (DefId id) +{ + trait_queries_in_progress.erase (id); +} + +bool +TypeCheckContext::trait_query_in_progress (DefId id) const +{ + return trait_queries_in_progress.find (id) + != trait_queries_in_progress.end (); +} + // TypeCheckContextItem +TypeCheckContextItem::Item::Item (HIR::Function *item) : item (item) {} + +TypeCheckContextItem::Item::Item (HIR::ImplBlock *impl_block, + HIR::Function *item) + : impl_item ({impl_block, item}) +{} + +TypeCheckContextItem::Item::Item (HIR::TraitItemFunc *trait_item) + : trait_item (trait_item) +{} + +TypeCheckContextItem::TypeCheckContextItem (HIR::Function *item) + : type (ItemType::ITEM), item (item) +{} + +TypeCheckContextItem::TypeCheckContextItem (HIR::ImplBlock *impl_block, + HIR::Function *item) + : type (ItemType::IMPL_ITEM), item (impl_block, item) +{} + +TypeCheckContextItem::TypeCheckContextItem (HIR::TraitItemFunc *trait_item) + : type (ItemType::TRAIT_ITEM), item (trait_item) +{} + +HIR::Function * +TypeCheckContextItem::get_item () +{ + rust_assert (get_type () == ItemType::ITEM); + return item.item; +} + +std::pair & +TypeCheckContextItem::get_impl_item () +{ + rust_assert (get_type () == ItemType::IMPL_ITEM); + return item.impl_item; +} + +HIR::TraitItemFunc * +TypeCheckContextItem::get_trait_item () +{ + rust_assert (get_type () == ItemType::TRAIT_ITEM); + return item.trait_item; +} + +TypeCheckContextItem::ItemType +TypeCheckContextItem::get_type () const +{ + return type; +} + TyTy::FnType * TypeCheckContextItem::get_context_type () { diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 079055e870d..61c02a85c53 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -17,17 +17,22 @@ // . #include "rust-tyty.h" -#include "rust-tyty-visitor.h" -#include "rust-tyty-call.h" + #include "rust-hir-type-check-expr.h" #include "rust-hir-type-check-type.h" -#include "rust-tyty-rules.h" -#include "rust-tyty-cmp.h" +#include "rust-tyty-visitor.h" +#include "rust-tyty-call.h" #include "rust-hir-map.h" +#include "rust-location.h" +#include "rust-linemap.h" + #include "rust-substitution-mapper.h" #include "rust-hir-trait-ref.h" #include "rust-hir-type-bounds.h" #include "rust-hir-trait-resolve.h" +#include "rust-tyty-rules.h" +#include "rust-tyty-cmp.h" + #include "options.h" namespace Rust { @@ -658,6 +663,53 @@ ErrorType::monomorphized_clone () const // Struct Field type +StructFieldType::StructFieldType (HirId ref, std::string name, BaseType *ty, + Location 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; +} + +bool +StructFieldType::is_concrete () const +{ + return ty->is_concrete (); +} + +void +StructFieldType::debug () const +{ + rust_debug ("%s", as_string ().c_str ()); +} + +Location +StructFieldType::get_locus () const +{ + return locus; +} + std::string StructFieldType::as_string () const { @@ -695,6 +747,241 @@ StructFieldType::monomorphized_clone () const 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"; + } + gcc_unreachable (); + return ""; +} + +VariantDef::VariantDef (HirId id, DefId defid, std::string identifier, + RustIdent ident, HIR::Expr *discriminant) + : id (id), defid (defid), identifier (identifier), ident (ident), + discriminant (discriminant) + +{ + type = VariantType::NUM; + fields = {}; +} + +VariantDef::VariantDef (HirId id, DefId defid, std::string identifier, + RustIdent ident, VariantType type, + HIR::Expr *discriminant, + std::vector fields) + : id (id), defid (defid), identifier (identifier), ident (ident), type (type), + discriminant (discriminant), fields (fields) +{ + rust_assert ((type == VariantType::NUM && fields.empty ()) + || (type == VariantType::TUPLE || type == VariantType::STRUCT)); +} + +VariantDef::VariantDef (const VariantDef &other) + : id (other.id), defid (other.defid), identifier (other.identifier), + ident (other.ident), type (other.type), discriminant (other.discriminant), + fields (other.fields) +{} + +VariantDef & +VariantDef::operator= (const VariantDef &other) +{ + id = other.id; + identifier = other.identifier; + type = other.type; + discriminant = other.discriminant; + fields = other.fields; + ident = other.ident; + + return *this; +} + +VariantDef & +VariantDef::get_error_node () +{ + static VariantDef node + = VariantDef (UNKNOWN_HIRID, UNKNOWN_DEFID, "", + {Resolver::CanonicalPath::create_empty (), + Linemap::unknown_location ()}, + nullptr); + + 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 & +VariantDef::get_fields () +{ + rust_assert (type != NUM); + 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 () const +{ + rust_assert (discriminant != nullptr); + return discriminant; +} + +std::string +VariantDef::as_string () const +{ + if (type == VariantType::NUM) + return identifier + " = " + discriminant->as_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 (discriminant != other.discriminant) + 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 cloned_fields; + for (auto &f : fields) + cloned_fields.push_back ((StructFieldType *) f->clone ()); + + return new VariantDef (id, defid, identifier, ident, type, discriminant, + cloned_fields); +} + +VariantDef * +VariantDef::monomorphized_clone () const +{ + std::vector cloned_fields; + for (auto &f : fields) + cloned_fields.push_back ((StructFieldType *) f->monomorphized_clone ()); + + return new VariantDef (id, defid, identifier, ident, type, discriminant, + cloned_fields); +} + +const RustIdent & +VariantDef::get_ident () const +{ + return ident; +} + +// ADTType + void ADTType::accept_vis (TyVisitor &vis) { @@ -892,6 +1179,57 @@ ADTType::handle_substitions (SubstitutionArgumentMappings subst_mappings) return adt; } +// TupleType + +TupleType::TupleType (HirId ref, Location locus, std::vector fields, + std::set refs) + : BaseType (ref, ref, TypeKind::TUPLE, + {Resolver::CanonicalPath::create_empty (), locus}, refs), + fields (fields) +{} + +TupleType::TupleType (HirId ref, HirId ty_ref, Location locus, + std::vector fields, std::set refs) + : BaseType (ref, ty_ref, TypeKind::TUPLE, + {Resolver::CanonicalPath::create_empty (), locus}, refs), + fields (fields) +{} + +TupleType * +TupleType::get_unit_type (HirId ref) +{ + return new TupleType (ref, Linemap::predeclared_location ()); +} + +bool +TupleType::is_unit () const +{ + return this->fields.empty (); +} + +size_t +TupleType::num_fields () const +{ + return fields.size (); +} + +bool +TupleType::is_concrete () const +{ + for (size_t i = 0; i < num_fields (); i++) + { + if (!get_field (i)->is_concrete ()) + return false; + } + return true; +} + +const std::vector & +TupleType::get_fields () const +{ + return fields; +} + void TupleType::accept_vis (TyVisitor &vis) { @@ -1622,6 +1960,34 @@ SliceType::handle_substitions (SubstitutionArgumentMappings mappings) return ref; } +// BoolType + +BoolType::BoolType (HirId ref, std::set refs) + : BaseType (ref, ref, TypeKind::BOOL, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +BoolType::BoolType (HirId ref, HirId ty_ref, std::set refs) + : BaseType (ref, ty_ref, TypeKind::BOOL, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +std::string +BoolType::get_name () const +{ + return as_string (); +} + +bool +BoolType::is_concrete () const +{ + return true; +} + void BoolType::accept_vis (TyVisitor &vis) { @@ -1666,6 +2032,36 @@ BoolType::monomorphized_clone () const return clone (); } +// IntType + +IntType::IntType (HirId ref, IntKind kind, std::set refs) + : BaseType (ref, ref, TypeKind::INT, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + int_kind (kind) +{} + +IntType::IntType (HirId ref, HirId ty_ref, IntKind kind, std::set refs) + : BaseType (ref, ty_ref, TypeKind::INT, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_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) { @@ -1735,6 +2131,43 @@ IntType::is_equal (const BaseType &other) const return get_int_kind () == o.get_int_kind (); } +bool +IntType::is_concrete () const +{ + return true; +} + +// UintType + +UintType::UintType (HirId ref, UintKind kind, std::set refs) + : BaseType (ref, ref, TypeKind::UINT, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + uint_kind (kind) +{} + +UintType::UintType (HirId ref, HirId ty_ref, UintKind kind, + std::set refs) + : BaseType (ref, ty_ref, TypeKind::UINT, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_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) { @@ -1804,6 +2237,49 @@ UintType::is_equal (const BaseType &other) const return get_uint_kind () == o.get_uint_kind (); } +bool +UintType::is_concrete () const +{ + return true; +} + +// FloatType + +FloatType::FloatType (HirId ref, FloatKind kind, std::set refs) + : BaseType (ref, ref, TypeKind::FLOAT, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + float_kind (kind) +{} + +FloatType::FloatType (HirId ref, HirId ty_ref, FloatKind kind, + std::set refs) + : BaseType (ref, ty_ref, TypeKind::FLOAT, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + float_kind (kind) +{} + +std::string +FloatType::get_name () const +{ + return as_string (); +} + +FloatType::FloatKind +FloatType::get_float_kind () const +{ + return float_kind; +} + +bool +FloatType::is_concrete () const +{ + return true; +} + void FloatType::accept_vis (TyVisitor &vis) { @@ -1867,6 +2343,34 @@ FloatType::is_equal (const BaseType &other) const return get_float_kind () == o.get_float_kind (); } +// UsizeType + +USizeType::USizeType (HirId ref, std::set refs) + : BaseType (ref, ref, TypeKind::USIZE, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +USizeType::USizeType (HirId ref, HirId ty_ref, std::set refs) + : BaseType (ref, ty_ref, TypeKind::USIZE, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +std::string +USizeType::get_name () const +{ + return as_string (); +} + +bool +USizeType::is_concrete () const +{ + return true; +} + void USizeType::accept_vis (TyVisitor &vis) { @@ -1911,6 +2415,34 @@ USizeType::monomorphized_clone () const return clone (); } +// ISizeType + +ISizeType::ISizeType (HirId ref, std::set refs) + : BaseType (ref, ref, TypeKind::ISIZE, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +ISizeType::ISizeType (HirId ref, HirId ty_ref, std::set refs) + : BaseType (ref, ty_ref, TypeKind::ISIZE, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +std::string +ISizeType::get_name () const +{ + return as_string (); +} + +bool +ISizeType::is_concrete () const +{ + return true; +} + void ISizeType::accept_vis (TyVisitor &vis) { @@ -1955,6 +2487,34 @@ ISizeType::monomorphized_clone () const return clone (); } +// Char Type + +CharType::CharType (HirId ref, std::set refs) + : BaseType (ref, ref, TypeKind::CHAR, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +CharType::CharType (HirId ref, HirId ty_ref, std::set refs) + : BaseType (ref, ty_ref, TypeKind::CHAR, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +bool +CharType::is_concrete () const +{ + return true; +} + +std::string +CharType::get_name () const +{ + return as_string (); +} + void CharType::accept_vis (TyVisitor &vis) { @@ -1999,6 +2559,76 @@ CharType::monomorphized_clone () const return clone (); } +// Reference Type + +ReferenceType::ReferenceType (HirId ref, TyVar base, Mutability mut, + std::set refs) + : BaseType (ref, ref, TypeKind::REF, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + base (base), mut (mut) +{} + +ReferenceType::ReferenceType (HirId ref, HirId ty_ref, TyVar base, + Mutability mut, std::set refs) + : BaseType (ref, ty_ref, TypeKind::REF, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + base (base), mut (mut) +{} + +bool +ReferenceType::is_concrete () const +{ + return get_base ()->is_concrete (); +} + +Mutability +ReferenceType::mutability () const +{ + return mut; +} + +bool +ReferenceType::is_mutable () const +{ + return mut == Mutability::Mut; +} + +bool +ReferenceType::is_dyn_object () const +{ + return is_dyn_slice_type () || is_dyn_str_type (); +} + +bool +ReferenceType::is_dyn_slice_type (const TyTy::SliceType **slice) const +{ + const TyTy::BaseType *element = get_base ()->destructure (); + if (element->get_kind () != TyTy::TypeKind::SLICE) + return false; + if (slice == nullptr) + return true; + + *slice = static_cast (element); + return true; +} + +bool +ReferenceType::is_dyn_str_type (const TyTy::StrType **str) const +{ + const TyTy::BaseType *element = get_base ()->destructure (); + if (element->get_kind () != TyTy::TypeKind::STR) + return false; + if (str == nullptr) + return true; + + *str = static_cast (element); + return true; +} + void ReferenceType::accept_vis (TyVisitor &vis) { @@ -2089,6 +2719,82 @@ ReferenceType::handle_substitions (SubstitutionArgumentMappings mappings) return ref; } +// PointerType + +PointerType::PointerType (HirId ref, TyVar base, Mutability mut, + std::set refs) + : BaseType (ref, ref, TypeKind::POINTER, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + base (base), mut (mut) +{} + +PointerType::PointerType (HirId ref, HirId ty_ref, TyVar base, Mutability mut, + std::set refs) + : BaseType (ref, ty_ref, TypeKind::POINTER, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + base (base), mut (mut) +{} + +bool +PointerType::is_concrete () const +{ + return get_base ()->is_concrete (); +} + +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 (); +} + +bool +PointerType::is_dyn_slice_type (const TyTy::SliceType **slice) const +{ + const TyTy::BaseType *element = get_base ()->destructure (); + if (element->get_kind () != TyTy::TypeKind::SLICE) + return false; + if (slice == nullptr) + return true; + + *slice = static_cast (element); + return true; +} + +bool +PointerType::is_dyn_str_type (const TyTy::StrType **str) const +{ + const TyTy::BaseType *element = get_base ()->destructure (); + if (element->get_kind () != TyTy::TypeKind::STR) + return false; + if (str == nullptr) + return true; + + *str = static_cast (element); + return true; +} + void PointerType::accept_vis (TyVisitor &vis) { @@ -2179,6 +2885,52 @@ PointerType::handle_substitions (SubstitutionArgumentMappings mappings) return ref; } +// PARAM Type + +ParamType::ParamType (std::string symbol, Location locus, HirId ref, + HIR::GenericParam ¶m, + std::vector specified_bounds, + std::set refs) + : BaseType (ref, ref, TypeKind::PARAM, + {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), + locus}, + specified_bounds, refs), + symbol (symbol), param (param) +{} + +ParamType::ParamType (std::string symbol, Location locus, HirId ref, + HirId ty_ref, HIR::GenericParam ¶m, + std::vector specified_bounds, + std::set refs) + : BaseType (ref, ty_ref, TypeKind::PARAM, + {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), + locus}, + specified_bounds, refs), + symbol (symbol), param (param) +{} + +HIR::GenericParam & +ParamType::get_generic_param () +{ + return param; +} + +bool +ParamType::can_resolve () const +{ + return get_ref () != get_ty_ref (); +} + +bool +ParamType::is_concrete () const +{ + auto r = resolve (); + if (r == this) + return false; + + return r->is_concrete (); +} + void ParamType::accept_vis (TyVisitor &vis) { @@ -2320,6 +3072,34 @@ ParamType::handle_substitions (SubstitutionArgumentMappings subst_mappings) return p; } +// StrType + +StrType::StrType (HirId ref, std::set refs) + : BaseType (ref, ref, TypeKind::STR, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +StrType::StrType (HirId ref, HirId ty_ref, std::set refs) + : BaseType (ref, ty_ref, TypeKind::STR, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +std::string +StrType::get_name () const +{ + return as_string (); +} + +bool +StrType::is_concrete () const +{ + return true; +} + BaseType * StrType::clone () const { @@ -2370,6 +3150,40 @@ StrType::is_equal (const BaseType &other) const return get_kind () == other.get_kind (); } +// Never Type + +NeverType::NeverType (HirId ref, std::set refs) + : BaseType (ref, ref, TypeKind::NEVER, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +NeverType::NeverType (HirId ref, HirId ty_ref, std::set refs) + : BaseType (ref, ty_ref, TypeKind::NEVER, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs) +{} + +std::string +NeverType::get_name () const +{ + return as_string (); +} + +bool +NeverType::is_unit () const +{ + return true; +} + +bool +NeverType::is_concrete () const +{ + return true; +} + void NeverType::accept_vis (TyVisitor &vis) { @@ -2416,6 +3230,52 @@ NeverType::monomorphized_clone () const // placeholder type +PlaceholderType::PlaceholderType (std::string symbol, HirId ref, + std::set refs) + : BaseType (ref, ref, TypeKind::PLACEHOLDER, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + symbol (symbol) +{} + +PlaceholderType::PlaceholderType (std::string symbol, HirId ref, HirId ty_ref, + std::set refs) + : BaseType (ref, ty_ref, TypeKind::PLACEHOLDER, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + symbol (symbol) +{} + +std::string +PlaceholderType::get_name () const +{ + return as_string (); +} + +bool +PlaceholderType::is_unit () const +{ + rust_assert (can_resolve ()); + return resolve ()->is_unit (); +} + +std::string +PlaceholderType::get_symbol () const +{ + return symbol; +} + +bool +PlaceholderType::is_concrete () const +{ + if (!can_resolve ()) + return true; + + return resolve ()->is_concrete (); +} + void PlaceholderType::accept_vis (TyVisitor &vis) { @@ -2515,6 +3375,78 @@ PlaceholderType::is_equal (const BaseType &other) const // Projection type +ProjectionType::ProjectionType ( + HirId ref, BaseType *base, const Resolver::TraitReference *trait, DefId item, + std::vector subst_refs, + SubstitutionArgumentMappings generic_arguments, std::set refs) + : BaseType (ref, ref, TypeKind::PROJECTION, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + SubstitutionRef (std::move (subst_refs), std::move (generic_arguments)), + base (base), trait (trait), item (item) +{} + +ProjectionType::ProjectionType ( + HirId ref, HirId ty_ref, BaseType *base, + const Resolver::TraitReference *trait, DefId item, + std::vector subst_refs, + SubstitutionArgumentMappings generic_arguments, std::set refs) + : BaseType (ref, ty_ref, TypeKind::PROJECTION, + {Resolver::CanonicalPath::create_empty (), + Linemap::predeclared_location ()}, + refs), + SubstitutionRef (std::move (subst_refs), std::move (generic_arguments)), + base (base), trait (trait), item (item) +{} + +bool +ProjectionType::is_unit () const +{ + return false; +} + +std::string +ProjectionType::get_name () const +{ + return as_string (); +} + +bool +ProjectionType::needs_generic_substitutions () const +{ + return needs_substitution (); +} + +bool +ProjectionType::supports_substitutions () const +{ + return true; +} + +bool +ProjectionType::has_subsititions_defined () const +{ + return has_substitutions (); +} + +const BaseType * +ProjectionType::get () const +{ + return base; +} +BaseType * +ProjectionType::get () +{ + return base; +} + +bool +ProjectionType::is_concrete () const +{ + return base->is_concrete (); +} + void ProjectionType::accept_vis (TyVisitor &vis) { @@ -2631,6 +3563,26 @@ ProjectionType::handle_substitions (SubstitutionArgumentMappings subst_mappings) return projection; } +// DynObjectType + +DynamicObjectType::DynamicObjectType ( + HirId ref, RustIdent ident, std::vector specified_bounds, + std::set refs) + : BaseType (ref, ref, TypeKind::DYNAMIC, ident, specified_bounds, refs) +{} + +DynamicObjectType::DynamicObjectType ( + HirId ref, HirId ty_ref, RustIdent ident, + std::vector specified_bounds, std::set refs) + : BaseType (ref, ty_ref, TypeKind::DYNAMIC, ident, specified_bounds, refs) +{} + +bool +DynamicObjectType::is_concrete () const +{ + return true; +} + void DynamicObjectType::accept_vis (TyVisitor &vis) { diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 1a6bac3f864..85b3a3b7abc 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -20,11 +20,9 @@ #define RUST_TYTY #include "rust-hir-map.h" -#include "rust-hir-full.h" -#include "rust-diagnostics.h" -#include "rust-abi.h" #include "rust-common.h" #include "rust-identifier.h" +#include "rust-abi.h" #include "rust-tyty-bounds.h" #include "rust-tyty-util.h" #include "rust-tyty-subst.h" @@ -274,24 +272,12 @@ public: ParamType (std::string symbol, Location locus, HirId ref, HIR::GenericParam ¶m, std::vector specified_bounds, - std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::PARAM, - {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), - locus}, - specified_bounds, refs), - symbol (symbol), param (param) - {} + std::set refs = std::set ()); ParamType (std::string symbol, Location locus, HirId ref, HirId ty_ref, HIR::GenericParam ¶m, std::vector specified_bounds, - std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::PARAM, - {Resolver::CanonicalPath::new_seg (UNKNOWN_NODEID, symbol), - locus}, - specified_bounds, refs), - symbol (symbol), param (param) - {} + std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; @@ -306,9 +292,9 @@ public: std::string get_symbol () const; - HIR::GenericParam &get_generic_param () { return param; } + HIR::GenericParam &get_generic_param (); - bool can_resolve () const { return get_ref () != get_ty_ref (); } + bool can_resolve () const; BaseType *resolve () const; @@ -316,14 +302,7 @@ public: bool is_equal (const BaseType &other) const override; - bool is_concrete () const override final - { - auto r = resolve (); - if (r == this) - return false; - - return r->is_concrete (); - } + bool is_concrete () const override final; ParamType *handle_substitions (SubstitutionArgumentMappings mappings); @@ -335,31 +314,25 @@ private: class StructFieldType { public: - StructFieldType (HirId ref, std::string name, BaseType *ty, Location locus) - : ref (ref), name (name), ty (ty), locus (locus) - {} - - HirId get_ref () const { return ref; } + StructFieldType (HirId ref, std::string name, BaseType *ty, Location locus); - std::string as_string () const; + HirId get_ref () const; bool is_equal (const StructFieldType &other) const; - std::string get_name () const { return name; } - - BaseType *get_field_type () const { return ty; } + std::string get_name () const; - void set_field_type (BaseType *fty) { ty = fty; } + BaseType *get_field_type () const; + void set_field_type (BaseType *fty); StructFieldType *clone () const; - StructFieldType *monomorphized_clone () const; - bool is_concrete () const { return ty->is_concrete (); } - - void debug () const { rust_debug ("%s", as_string ().c_str ()); } + bool is_concrete () const; - Location get_locus () const { return locus; } + void debug () const; + Location get_locus () const; + std::string as_string () const; private: HirId ref; @@ -373,29 +346,18 @@ class TupleType : public BaseType public: TupleType (HirId ref, Location locus, std::vector fields = std::vector (), - std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::TUPLE, - {Resolver::CanonicalPath::create_empty (), locus}, refs), - fields (fields) - {} + std::set refs = std::set ()); TupleType (HirId ref, HirId ty_ref, Location locus, std::vector fields = std::vector (), - std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::TUPLE, - {Resolver::CanonicalPath::create_empty (), locus}, refs), - fields (fields) - {} + std::set refs = std::set ()); - static TupleType *get_unit_type (HirId ref) - { - return new TupleType (ref, Linemap::predeclared_location ()); - } + static TupleType *get_unit_type (HirId ref); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; - bool is_unit () const override { return this->fields.empty (); } + bool is_unit () const override; std::string as_string () const override; @@ -404,24 +366,16 @@ public: bool is_equal (const BaseType &other) const override; - size_t num_fields () const { return fields.size (); } + size_t num_fields () const; BaseType *get_field (size_t index) const; BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final - { - for (size_t i = 0; i < num_fields (); i++) - { - if (!get_field (i)->is_concrete ()) - return false; - } - return true; - } + bool is_concrete () const override final; - const std::vector &get_fields () const { return fields; } + const std::vector &get_fields () const; std::string get_name () const override final; @@ -507,185 +461,48 @@ public: STRUCT }; - static std::string variant_type_string (VariantType type) - { - switch (type) - { - case NUM: - return "enumeral"; - case TUPLE: - return "tuple"; - case STRUCT: - return "struct"; - } - gcc_unreachable (); - return ""; - } + static std::string variant_type_string (VariantType type); VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident, - HIR::Expr *discriminant) - : id (id), defid (defid), identifier (identifier), ident (ident), - discriminant (discriminant) - - { - type = VariantType::NUM; - fields = {}; - } + HIR::Expr *discriminant); VariantDef (HirId id, DefId defid, std::string identifier, RustIdent ident, VariantType type, HIR::Expr *discriminant, - std::vector fields) - : id (id), defid (defid), identifier (identifier), ident (ident), - type (type), discriminant (discriminant), fields (fields) - { - rust_assert ( - (type == VariantType::NUM && fields.empty ()) - || (type == VariantType::TUPLE || type == VariantType::STRUCT)); - } + std::vector fields); - VariantDef (const VariantDef &other) - : id (other.id), defid (other.defid), identifier (other.identifier), - ident (other.ident), type (other.type), discriminant (other.discriminant), - fields (other.fields) - {} + VariantDef (const VariantDef &other); - VariantDef &operator= (const VariantDef &other) - { - id = other.id; - identifier = other.identifier; - type = other.type; - discriminant = other.discriminant; - fields = other.fields; - ident = other.ident; - - return *this; - } + VariantDef &operator= (const VariantDef &other); - static VariantDef &get_error_node () - { - static VariantDef node - = VariantDef (UNKNOWN_HIRID, UNKNOWN_DEFID, "", - {Resolver::CanonicalPath::create_empty (), - Linemap::unknown_location ()}, - nullptr); + static VariantDef &get_error_node (); + bool is_error () const; - return node; - } + HirId get_id () const; + DefId get_defid () const; - bool is_error () const { return get_id () == UNKNOWN_HIRID; } + VariantType get_variant_type () const; + bool is_data_variant () const; + bool is_dataless_variant () const; - HirId get_id () const { return id; } - DefId get_defid () const { return defid; } + std::string get_identifier () const; - VariantType get_variant_type () const { return type; } - bool is_data_variant () const { return type != VariantType::NUM; } - bool is_dataless_variant () const { return type == VariantType::NUM; } + size_t num_fields () const; + StructFieldType *get_field_at_index (size_t index); - std::string get_identifier () const { return identifier; } - - size_t num_fields () const { return fields.size (); } - StructFieldType *get_field_at_index (size_t index) - { - rust_assert (index < fields.size ()); - return fields.at (index); - } - - std::vector &get_fields () - { - rust_assert (type != NUM); - return fields; - } + std::vector &get_fields (); bool 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 *get_discriminant () const - { - rust_assert (discriminant != nullptr); - return discriminant; - } - - std::string as_string () const - { - if (type == VariantType::NUM) - return identifier + " = " + discriminant->as_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 is_equal (const VariantDef &other) const - { - if (type != other.type) - return false; - - if (identifier.compare (other.identifier) != 0) - return false; + size_t *index) const; - if (discriminant != other.discriminant) - 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 *clone () const - { - std::vector cloned_fields; - for (auto &f : fields) - cloned_fields.push_back ((StructFieldType *) f->clone ()); + HIR::Expr *get_discriminant () const; - return new VariantDef (id, defid, identifier, ident, type, discriminant, - cloned_fields); - } + std::string as_string () const; - VariantDef *monomorphized_clone () const - { - std::vector cloned_fields; - for (auto &f : fields) - cloned_fields.push_back ((StructFieldType *) f->monomorphized_clone ()); + bool is_equal (const VariantDef &other) const; + VariantDef *clone () const; + VariantDef *monomorphized_clone () const; - return new VariantDef (id, defid, identifier, ident, type, discriminant, - cloned_fields); - } - - const RustIdent &get_ident () const { return ident; } + const RustIdent &get_ident () const; private: HirId id; @@ -1262,33 +1079,22 @@ private: class BoolType : public BaseType { public: - BoolType (HirId ref, std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::BOOL, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} - - BoolType (HirId ref, HirId ty_ref, std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::BOOL, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} + BoolType (HirId ref, std::set refs = std::set ()); + BoolType (HirId ref, HirId ty_ref, std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; - std::string get_name () const override final { return as_string (); } + std::string get_name () const override final; BaseType *unify (BaseType *other) override; bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final { return true; } + bool is_concrete () const override final; }; class IntType : public BaseType @@ -1303,40 +1109,27 @@ public: I128 }; - IntType (HirId ref, IntKind kind, std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::INT, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - int_kind (kind) - {} - + IntType (HirId ref, IntKind kind, std::set refs = std::set ()); IntType (HirId ref, HirId ty_ref, IntKind kind, - std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::INT, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - int_kind (kind) - {} + std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; - std::string get_name () const override final { return as_string (); } + std::string get_name () const override final; BaseType *unify (BaseType *other) override; bool can_eq (const BaseType *other, bool emit_errors) const override final; - IntKind get_int_kind () const { return int_kind; } + IntKind get_int_kind () const; BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; bool is_equal (const BaseType &other) const override; - bool is_concrete () const override final { return true; } + bool is_concrete () const override final; private: IntKind int_kind; @@ -1354,40 +1147,28 @@ public: U128 }; - UintType (HirId ref, UintKind kind, std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::UINT, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - uint_kind (kind) - {} - + UintType (HirId ref, UintKind kind, + std::set refs = std::set ()); UintType (HirId ref, HirId ty_ref, UintKind kind, - std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::UINT, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - uint_kind (kind) - {} + std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; - std::string get_name () const override final { return as_string (); } + std::string get_name () const override final; BaseType *unify (BaseType *other) override; bool can_eq (const BaseType *other, bool emit_errors) const override final; - UintKind get_uint_kind () const { return uint_kind; } + UintKind get_uint_kind () const; BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; bool is_equal (const BaseType &other) const override; - bool is_concrete () const override final { return true; } + bool is_concrete () const override final; private: UintKind uint_kind; @@ -1403,40 +1184,26 @@ public: }; FloatType (HirId ref, FloatKind kind, - std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::FLOAT, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - float_kind (kind) - {} - + std::set refs = std::set ()); FloatType (HirId ref, HirId ty_ref, FloatKind kind, - std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::FLOAT, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - float_kind (kind) - {} + std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; - - std::string get_name () const override final { return as_string (); } + std::string get_name () const override final; BaseType *unify (BaseType *other) override; bool can_eq (const BaseType *other, bool emit_errors) const override final; - FloatKind get_float_kind () const { return float_kind; } + FloatKind get_float_kind () const; BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; bool is_equal (const BaseType &other) const override; - bool is_concrete () const override final { return true; } + bool is_concrete () const override final; private: FloatKind float_kind; @@ -1445,117 +1212,72 @@ private: class USizeType : public BaseType { public: - USizeType (HirId ref, std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::USIZE, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} - - USizeType (HirId ref, HirId ty_ref, std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::USIZE, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} + USizeType (HirId ref, std::set refs = std::set ()); + USizeType (HirId ref, HirId ty_ref, + std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; - - std::string get_name () const override final { return as_string (); } + std::string get_name () const override final; BaseType *unify (BaseType *other) override; bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final { return true; } + bool is_concrete () const override final; }; class ISizeType : public BaseType { public: - ISizeType (HirId ref, std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::ISIZE, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} - - ISizeType (HirId ref, HirId ty_ref, std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::ISIZE, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} + ISizeType (HirId ref, std::set refs = std::set ()); + ISizeType (HirId ref, HirId ty_ref, + std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; - - std::string get_name () const override final { return as_string (); } + std::string get_name () const override final; BaseType *unify (BaseType *other) override; bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final { return true; } + bool is_concrete () const override final; }; class CharType : public BaseType { public: - CharType (HirId ref, std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::CHAR, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} - - CharType (HirId ref, HirId ty_ref, std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::CHAR, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} + CharType (HirId ref, std::set refs = std::set ()); + CharType (HirId ref, HirId ty_ref, std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; - - std::string get_name () const override final { return as_string (); } + std::string get_name () const override final; BaseType *unify (BaseType *other) override; bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final { return true; } + bool is_concrete () const override final; }; class StrType : public BaseType { public: - StrType (HirId ref, std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::STR, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} - - StrType (HirId ref, HirId ty_ref, std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::STR, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} + StrType (HirId ref, std::set refs = std::set ()); + StrType (HirId ref, HirId ty_ref, std::set refs = std::set ()); - std::string get_name () const override final { return as_string (); } + std::string get_name () const override final; void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; @@ -1569,29 +1291,16 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final { return true; } + bool is_concrete () const override final; }; class ReferenceType : public BaseType { public: ReferenceType (HirId ref, TyVar base, Mutability mut, - std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::REF, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - base (base), mut (mut) - {} - + std::set refs = std::set ()); ReferenceType (HirId ref, HirId ty_ref, TyVar base, Mutability mut, - std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::REF, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - base (base), mut (mut) - {} + std::set refs = std::set ()); BaseType *get_base () const; @@ -1610,45 +1319,19 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final - { - return get_base ()->is_concrete (); - } + bool is_concrete () const override final; ReferenceType *handle_substitions (SubstitutionArgumentMappings mappings); - Mutability mutability () const { return mut; } + Mutability mutability () const; - bool is_mutable () const { return mut == Mutability::Mut; } + bool is_mutable () const; - bool is_dyn_object () const - { - return is_dyn_slice_type () || is_dyn_str_type (); - } + bool is_dyn_object () const; - bool is_dyn_slice_type (const TyTy::SliceType **slice = nullptr) const - { - const TyTy::BaseType *element = get_base ()->destructure (); - if (element->get_kind () != TyTy::TypeKind::SLICE) - return false; - if (slice == nullptr) - return true; + bool is_dyn_slice_type (const TyTy::SliceType **slice = nullptr) const; - *slice = static_cast (element); - return true; - } - - bool is_dyn_str_type (const TyTy::StrType **str = nullptr) const - { - const TyTy::BaseType *element = get_base ()->destructure (); - if (element->get_kind () != TyTy::TypeKind::STR) - return false; - if (str == nullptr) - return true; - - *str = static_cast (element); - return true; - } + bool is_dyn_str_type (const TyTy::StrType **str = nullptr) const; private: TyVar base; @@ -1659,22 +1342,9 @@ class PointerType : public BaseType { public: PointerType (HirId ref, TyVar base, Mutability mut, - std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::POINTER, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - base (base), mut (mut) - {} - + std::set refs = std::set ()); PointerType (HirId ref, HirId ty_ref, TyVar base, Mutability mut, - std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::POINTER, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - base (base), mut (mut) - {} + std::set refs = std::set ()); BaseType *get_base () const; @@ -1692,47 +1362,17 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - bool is_concrete () const override final - { - return get_base ()->is_concrete (); - } + bool is_concrete () const override final; PointerType *handle_substitions (SubstitutionArgumentMappings mappings); - Mutability mutability () const { return mut; } - - bool is_mutable () const { return mut == Mutability::Mut; } - - bool is_const () const { return mut == Mutability::Imm; } - - bool is_dyn_object () const - { - return is_dyn_slice_type () || is_dyn_str_type (); - } - - bool is_dyn_slice_type (const TyTy::SliceType **slice = nullptr) const - { - const TyTy::BaseType *element = get_base ()->destructure (); - if (element->get_kind () != TyTy::TypeKind::SLICE) - return false; - if (slice == nullptr) - return true; + Mutability mutability () const; + bool is_mutable () const; + bool is_const () const; + bool is_dyn_object () const; - *slice = static_cast (element); - return true; - } - - bool is_dyn_str_type (const TyTy::StrType **str = nullptr) const - { - const TyTy::BaseType *element = get_base ()->destructure (); - if (element->get_kind () != TyTy::TypeKind::STR) - return false; - if (str == nullptr) - return true; - - *str = static_cast (element); - return true; - } + bool is_dyn_slice_type (const TyTy::SliceType **slice = nullptr) const; + bool is_dyn_str_type (const TyTy::StrType **str = nullptr) const; private: TyVar base; @@ -1752,19 +1392,9 @@ private: class NeverType : public BaseType { public: - NeverType (HirId ref, std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::NEVER, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} - - NeverType (HirId ref, HirId ty_ref, std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::NEVER, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs) - {} + NeverType (HirId ref, std::set refs = std::set ()); + NeverType (HirId ref, HirId ty_ref, + std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; @@ -1777,10 +1407,10 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - std::string get_name () const override final { return as_string (); } + std::string get_name () const override final; - bool is_unit () const override { return true; } - bool is_concrete () const override final { return true; } + bool is_unit () const override; + bool is_concrete () const override final; }; // used at the type in associated types in traits @@ -1789,22 +1419,9 @@ class PlaceholderType : public BaseType { public: PlaceholderType (std::string symbol, HirId ref, - std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::PLACEHOLDER, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - symbol (symbol) - {} - + std::set refs = std::set ()); PlaceholderType (std::string symbol, HirId ref, HirId ty_ref, - std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::PLACEHOLDER, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - symbol (symbol) - {} + std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; @@ -1817,15 +1434,11 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - std::string get_name () const override final { return as_string (); } + std::string get_name () const override final; - bool is_unit () const override - { - rust_assert (can_resolve ()); - return resolve ()->is_unit (); - } + bool is_unit () const override; - std::string get_symbol () const { return symbol; } + std::string get_symbol () const; void set_associated_type (HirId ref); @@ -1837,13 +1450,7 @@ public: bool is_equal (const BaseType &other) const override; - bool is_concrete () const override final - { - if (!can_resolve ()) - return true; - - return resolve ()->is_concrete (); - } + bool is_concrete () const override final; private: std::string symbol; @@ -1857,28 +1464,14 @@ public: std::vector subst_refs, SubstitutionArgumentMappings generic_arguments = SubstitutionArgumentMappings::error (), - std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::PROJECTION, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - SubstitutionRef (std::move (subst_refs), std::move (generic_arguments)), - base (base), trait (trait), item (item) - {} + std::set refs = std::set ()); ProjectionType (HirId ref, HirId ty_ref, BaseType *base, const Resolver::TraitReference *trait, DefId item, std::vector subst_refs, SubstitutionArgumentMappings generic_arguments = SubstitutionArgumentMappings::error (), - std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::PROJECTION, - {Resolver::CanonicalPath::create_empty (), - Linemap::predeclared_location ()}, - refs), - SubstitutionRef (std::move (subst_refs), std::move (generic_arguments)), - base (base), trait (trait), item (item) - {} + std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; @@ -1891,26 +1484,20 @@ public: BaseType *clone () const final override; BaseType *monomorphized_clone () const final override; - std::string get_name () const override final { return as_string (); } + std::string get_name () const override final; - bool is_unit () const override { return false; } + bool is_unit () const override; - bool needs_generic_substitutions () const override final - { - return needs_substitution (); - } + bool needs_generic_substitutions () const override final; - bool supports_substitutions () const override final { return true; } + bool supports_substitutions () const override final; - bool has_subsititions_defined () const override final - { - return has_substitutions (); - } + bool has_subsititions_defined () const override final; - const BaseType *get () const { return base; } - BaseType *get () { return base; } + const BaseType *get () const; + BaseType *get (); - bool is_concrete () const override final { return base->is_concrete (); } + bool is_concrete () const override final; ProjectionType * handle_substitions (SubstitutionArgumentMappings mappings) override final; @@ -1926,15 +1513,11 @@ class DynamicObjectType : public BaseType public: DynamicObjectType (HirId ref, RustIdent ident, std::vector specified_bounds, - std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::DYNAMIC, ident, specified_bounds, refs) - {} + std::set refs = std::set ()); DynamicObjectType (HirId ref, HirId ty_ref, RustIdent ident, std::vector specified_bounds, - std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::DYNAMIC, ident, specified_bounds, refs) - {} + std::set refs = std::set ()); void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; @@ -1951,7 +1534,7 @@ public: std::string get_name () const override final; - bool is_concrete () const override final { return true; } + bool is_concrete () const override final; // this returns a flat list of items including super trait bounds const std::vector< -- 2.40.0