From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id BB5DE3851C13 for ; Wed, 5 Apr 2023 14:05:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BB5DE3851C13 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-x331.google.com with SMTP id l10-20020a05600c1d0a00b003f04bd3691eso7160609wms.5 for ; Wed, 05 Apr 2023 07:05:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703554; 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=vZ2ck1LP7RLliorD/w4KWwhmYFaX4KjIMQ5oOsUP1BU=; b=NTv/AmFHVj83y4J3qtZAQm0dMJIu1uMWMKDsDz9pFmBCC0pAJY73mDwVodnM8VQBFU Fk8uj6krTSUhPARbhB/hhRb1HEiyEQpDFF3zZP2JpP02ogCytsi/XKI3sbHbK/0nk2ck 34ZB25XX2blIAkWiN4HrjyJ81MCBcIyLXlRUR+Z6TPZSAW2GFLT5mQDeUuqM8hQqoQhI j5ayqQpEh1iOAYYzmls77wFT6zsX7O6E/SivxL337l0vObXfUTxoBRKkWWbY1RZvz04j 4rcIoHMdyW+X3GdkHO5aSIEQLcUVGRv1Vm8OiuHRTbMdZgQ8k+V1vUKPwbJB53B2AbP1 DmKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703554; 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=vZ2ck1LP7RLliorD/w4KWwhmYFaX4KjIMQ5oOsUP1BU=; b=liD7J9PnjhYenUWdUQG7689V0DyXnk1u5OMitxoK+1CgxjbaYAn596ZtHlVcZSq+8N 2p36KsUy++9YqfBnhhITr1vfoIohaBc+vX5IyAxfmMPqvmH2erXhjX6LNnj+KbGyreAx 98d9846qqTgyapNyYZq+Gon4WR1u6amu8V742fTQl6PV+g9eKjB7LARHiR7x0QTWwHZi J4dBpAG7AYnvEIxlyJ1/N9IUlEeYUoD9vST6HFzpv3WtgosCutR/fanlm0OdxaaVpnhz 5fl8NaYy2zo++3sJ+cGkGkLE7n+qinVQZjpIEfbVQyZPa7fQL1Yj3gh/W3Kv3irgarZp 5AHA== X-Gm-Message-State: AAQBX9dFLyLfkqg10yxepK0CFlw5faRfKFslD3k5tsPh9VXpvTQKZpqs +iIjVA6OdJMzqCWE0wFTSTLr X-Google-Smtp-Source: AKy350ZqtKPQksaHtMcGKLCW18l6CwsNCkb13MRrINoD/oq2n85PAIwRuZvp5olJoGmNrE1Zpa/21g== X-Received: by 2002:a05:600c:246:b0:3ed:e447:1ed0 with SMTP id 6-20020a05600c024600b003ede4471ed0mr4857968wmj.14.1680703554241; Wed, 05 Apr 2023 07:05:54 -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.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:05:53 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [committed 37/88] gccrs: Refactor TyVar and TypeBoundPredicates Date: Wed, 5 Apr 2023 16:03:21 +0200 Message-Id: <20230405140411.3016563-38-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 This extract these helpers into seperate files Signed-off-by: Philip Herron gcc/rust/ChangeLog: * Make-lang.in: update makefile * typecheck/rust-tyty.cc (TyVar::TyVar): move to new file (TyVar::get_tyty): likewise (TyVar::get_implicit_infer_var): likewise (TyVar::subst_covariant_var): likewise (TyVar::clone): likewise (TyVar::monomorphized_clone): likewise (TyWithLocation::TyWithLocation): likewise * typecheck/rust-tyty.h (class BaseType): cleanup (class TypeBoundPredicate): move to its own file (class TypeBoundPredicateItem): likewise (class TypeBoundsMappings): likewise (class TyVar): likewise (class TyWithLocation): likewise * typecheck/rust-tyty-bounds.h: New file. * typecheck/rust-tyty-util.cc: New file. * typecheck/rust-tyty-util.h: New file. --- gcc/rust/Make-lang.in | 1 + gcc/rust/typecheck/rust-tyty-bounds.h | 88 +++++++++++++++++++ gcc/rust/typecheck/rust-tyty-util.cc | 116 ++++++++++++++++++++++++++ gcc/rust/typecheck/rust-tyty-util.h | 69 +++++++++++++++ gcc/rust/typecheck/rust-tyty.cc | 90 -------------------- gcc/rust/typecheck/rust-tyty.h | 90 +------------------- 6 files changed, 276 insertions(+), 178 deletions(-) create mode 100644 gcc/rust/typecheck/rust-tyty-bounds.h create mode 100644 gcc/rust/typecheck/rust-tyty-util.cc create mode 100644 gcc/rust/typecheck/rust-tyty-util.h diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index a0c5757592e..2aa61bcfb19 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -115,6 +115,7 @@ GRS_OBJS = \ rust/rust-pub-restricted-visitor.o \ rust/rust-privacy-reporter.o \ rust/rust-tyty.o \ + rust/rust-tyty-util.o \ rust/rust-tyty-call.o \ rust/rust-tyctx.o \ rust/rust-tyty-bounds.o \ diff --git a/gcc/rust/typecheck/rust-tyty-bounds.h b/gcc/rust/typecheck/rust-tyty-bounds.h new file mode 100644 index 00000000000..44839bd0b40 --- /dev/null +++ b/gcc/rust/typecheck/rust-tyty-bounds.h @@ -0,0 +1,88 @@ +// Copyright (C) 2020-2022 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 +// . + +#ifndef RUST_TYTY_BOUNDS_H +#define RUST_TYTY_BOUNDS_H + +#include "rust-location.h" + +namespace Rust { + +namespace Resolver { +class TraitReference; +class TraitItemReference; +class AssociatedImplTrait; +} // namespace Resolver + +namespace TyTy { + +class BaseType; +class TypeBoundPredicate; +class TypeBoundPredicateItem +{ +public: + TypeBoundPredicateItem (const TypeBoundPredicate *parent, + const Resolver::TraitItemReference *trait_item_ref); + + static TypeBoundPredicateItem error (); + + bool is_error () const; + + BaseType *get_tyty_for_receiver (const TyTy::BaseType *receiver); + + const Resolver::TraitItemReference *get_raw_item () const; + + bool needs_implementation () const; + + const TypeBoundPredicate *get_parent () const; + + Location get_locus () const; + +private: + const TypeBoundPredicate *parent; + const Resolver::TraitItemReference *trait_item_ref; +}; + +class TypeBoundsMappings +{ +protected: + TypeBoundsMappings (std::vector specified_bounds); + +public: + std::vector &get_specified_bounds (); + + const std::vector &get_specified_bounds () const; + + size_t num_specified_bounds () const; + + std::string raw_bounds_as_string () const; + + std::string bounds_as_string () const; + + std::string raw_bounds_as_name () const; + +protected: + void add_bound (TypeBoundPredicate predicate); + + std::vector specified_bounds; +}; + +} // namespace TyTy +} // namespace Rust + +#endif // RUST_TYTY_BOUNDS_H diff --git a/gcc/rust/typecheck/rust-tyty-util.cc b/gcc/rust/typecheck/rust-tyty-util.cc new file mode 100644 index 00000000000..5037f68cb3f --- /dev/null +++ b/gcc/rust/typecheck/rust-tyty-util.cc @@ -0,0 +1,116 @@ +// Copyright (C) 2020-2022 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 +// . + +#include "rust-hir-type-check.h" +#include "rust-tyty.h" + +namespace Rust { +namespace TyTy { + +TyVar::TyVar (HirId ref) : ref (ref) +{ + // ensure this reference is defined within the context + auto context = Resolver::TypeCheckContext::get (); + BaseType *lookup = nullptr; + bool ok = context->lookup_type (ref, &lookup); + rust_assert (ok); +} + +BaseType * +TyVar::get_tyty () const +{ + auto context = Resolver::TypeCheckContext::get (); + BaseType *lookup = nullptr; + bool ok = context->lookup_type (ref, &lookup); + rust_assert (ok); + return lookup; +} + +TyVar +TyVar::get_implicit_infer_var (Location locus) +{ + auto mappings = Analysis::Mappings::get (); + auto context = Resolver::TypeCheckContext::get (); + + InferType *infer = new InferType (mappings->get_next_hir_id (), + InferType::InferTypeKind::GENERAL, locus); + context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (), + UNKNOWN_NODEID, + infer->get_ref (), + UNKNOWN_LOCAL_DEFID), + infer); + mappings->insert_location (infer->get_ref (), locus); + + return TyVar (infer->get_ref ()); +} + +TyVar +TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst) +{ + if (orig->get_kind () != TyTy::TypeKind::PARAM) + return TyVar (subst->get_ty_ref ()); + else if (subst->get_kind () == TyTy::TypeKind::PARAM) + { + TyTy::ParamType *p = static_cast (subst); + if (p->resolve ()->get_kind () == TyTy::TypeKind::PARAM) + { + return TyVar (subst->get_ty_ref ()); + } + } + + return TyVar (subst->get_ref ()); +} + +TyVar +TyVar::clone () const +{ + TyTy::BaseType *c = get_tyty ()->clone (); + return TyVar (c->get_ref ()); +} + +TyVar +TyVar::monomorphized_clone () const +{ + auto mappings = Analysis::Mappings::get (); + auto context = Resolver::TypeCheckContext::get (); + + // this needs a new hirid + TyTy::BaseType *c = get_tyty ()->monomorphized_clone (); + c->set_ref (mappings->get_next_hir_id ()); + + // insert it + context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (), + UNKNOWN_NODEID, c->get_ref (), + UNKNOWN_LOCAL_DEFID), + c); + + return TyVar (c->get_ref ()); +} + +TyWithLocation::TyWithLocation (BaseType *ty, Location locus) + : ty (ty), locus (locus) +{} + +TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty) +{ + auto mappings = Analysis::Mappings::get (); + locus = mappings->lookup_location (ty->get_ref ()); +} + +} // namespace TyTy +} // namespace Rust diff --git a/gcc/rust/typecheck/rust-tyty-util.h b/gcc/rust/typecheck/rust-tyty-util.h new file mode 100644 index 00000000000..eccbb4423d5 --- /dev/null +++ b/gcc/rust/typecheck/rust-tyty-util.h @@ -0,0 +1,69 @@ +// Copyright (C) 2020-2022 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 +// . + +#ifndef RUST_TYTY_UTIL_H +#define RUST_TYTY_UTIL_H + +#include "rust-hir-map.h" + +namespace Rust { +namespace TyTy { + +class BaseType; + +// this is a placeholder for types that can change like inference variables +class TyVar +{ +public: + explicit TyVar (HirId ref); + + HirId get_ref () const { return ref; } + + BaseType *get_tyty () const; + + TyVar clone () const; + + TyVar monomorphized_clone () const; + + static TyVar get_implicit_infer_var (Location locus); + + static TyVar subst_covariant_var (TyTy::BaseType *orig, + TyTy::BaseType *subst); + +private: + HirId ref; +}; + +class TyWithLocation +{ +public: + explicit TyWithLocation (BaseType *ty, Location locus); + explicit TyWithLocation (BaseType *ty); + + BaseType *get_ty () const { return ty; } + Location get_locus () const { return locus; } + +private: + BaseType *ty; + Location locus; +}; + +} // namespace TyTy +} // namespace Rust + +#endif // RUST_TYTY_UTIL_H diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index fd57a534562..55a8123ce87 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -330,96 +330,6 @@ BaseType::debug () const debug_str ().c_str ()); } -TyVar::TyVar (HirId ref) : ref (ref) -{ - // ensure this reference is defined within the context - auto context = Resolver::TypeCheckContext::get (); - BaseType *lookup = nullptr; - bool ok = context->lookup_type (ref, &lookup); - rust_assert (ok); -} - -BaseType * -TyVar::get_tyty () const -{ - auto context = Resolver::TypeCheckContext::get (); - BaseType *lookup = nullptr; - bool ok = context->lookup_type (ref, &lookup); - rust_assert (ok); - return lookup; -} - -TyVar -TyVar::get_implicit_infer_var (Location locus) -{ - auto mappings = Analysis::Mappings::get (); - auto context = Resolver::TypeCheckContext::get (); - - InferType *infer = new InferType (mappings->get_next_hir_id (), - InferType::InferTypeKind::GENERAL, locus); - context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (), - UNKNOWN_NODEID, - infer->get_ref (), - UNKNOWN_LOCAL_DEFID), - infer); - mappings->insert_location (infer->get_ref (), locus); - - return TyVar (infer->get_ref ()); -} - -TyVar -TyVar::subst_covariant_var (TyTy::BaseType *orig, TyTy::BaseType *subst) -{ - if (orig->get_kind () != TyTy::TypeKind::PARAM) - return TyVar (subst->get_ty_ref ()); - else if (subst->get_kind () == TyTy::TypeKind::PARAM) - { - TyTy::ParamType *p = static_cast (subst); - if (p->resolve ()->get_kind () == TyTy::TypeKind::PARAM) - { - return TyVar (subst->get_ty_ref ()); - } - } - - return TyVar (subst->get_ref ()); -} - -TyVar -TyVar::clone () const -{ - TyTy::BaseType *c = get_tyty ()->clone (); - return TyVar (c->get_ref ()); -} - -TyVar -TyVar::monomorphized_clone () const -{ - auto mappings = Analysis::Mappings::get (); - auto context = Resolver::TypeCheckContext::get (); - - // this needs a new hirid - TyTy::BaseType *c = get_tyty ()->monomorphized_clone (); - c->set_ref (mappings->get_next_hir_id ()); - - // insert it - context->insert_type (Analysis::NodeMapping (mappings->get_current_crate (), - UNKNOWN_NODEID, c->get_ref (), - UNKNOWN_LOCAL_DEFID), - c); - - return TyVar (c->get_ref ()); -} - -TyWithLocation::TyWithLocation (BaseType *ty, Location locus) - : ty (ty), locus (locus) -{} - -TyWithLocation::TyWithLocation (BaseType *ty) : ty (ty) -{ - auto mappings = Analysis::Mappings::get (); - locus = mappings->lookup_location (ty->get_ref ()); -} - void InferType::accept_vis (TyVisitor &vis) { diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 7d32ed59494..a8bdf6f3a15 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -25,6 +25,8 @@ #include "rust-abi.h" #include "rust-common.h" #include "rust-identifier.h" +#include "rust-tyty-bounds.h" +#include "rust-tyty-util.h" namespace Rust { @@ -76,57 +78,6 @@ public: static std::string to_string (TypeKind kind); }; -class BaseType; -class TypeBoundPredicate; -class TypeBoundPredicateItem -{ -public: - TypeBoundPredicateItem (const TypeBoundPredicate *parent, - const Resolver::TraitItemReference *trait_item_ref); - - static TypeBoundPredicateItem error (); - - bool is_error () const; - - BaseType *get_tyty_for_receiver (const TyTy::BaseType *receiver); - - const Resolver::TraitItemReference *get_raw_item () const; - - bool needs_implementation () const; - - const TypeBoundPredicate *get_parent () const; - - Location get_locus () const; - -private: - const TypeBoundPredicate *parent; - const Resolver::TraitItemReference *trait_item_ref; -}; - -class TypeBoundsMappings -{ -protected: - TypeBoundsMappings (std::vector specified_bounds); - -public: - std::vector &get_specified_bounds (); - - const std::vector &get_specified_bounds () const; - - size_t num_specified_bounds () const; - - std::string raw_bounds_as_string () const; - - std::string bounds_as_string () const; - - std::string raw_bounds_as_name () const; - -protected: - void add_bound (TypeBoundPredicate predicate); - - std::vector specified_bounds; -}; - extern void set_cmp_autoderef_mode (); extern void @@ -268,43 +219,6 @@ protected: Analysis::Mappings *mappings; }; -// this is a placeholder for types that can change like inference variables -class TyVar -{ -public: - explicit TyVar (HirId ref); - - HirId get_ref () const { return ref; } - - BaseType *get_tyty () const; - - TyVar clone () const; - - TyVar monomorphized_clone () const; - - static TyVar get_implicit_infer_var (Location locus); - - static TyVar subst_covariant_var (TyTy::BaseType *orig, - TyTy::BaseType *subst); - -private: - HirId ref; -}; - -class TyWithLocation -{ -public: - explicit TyWithLocation (BaseType *ty, Location locus); - explicit TyWithLocation (BaseType *ty); - - BaseType *get_ty () const { return ty; } - Location get_locus () const { return locus; } - -private: - BaseType *ty; - Location locus; -}; - class InferType : public BaseType { public: -- 2.40.0