From 688fccb5227d78240735433b51fa27642f81415a Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Thu, 12 Jan 2023 18:00:52 +0000 Subject: [PATCH] gccrs: Refactor TyVar and TypeBoundPredicates 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 a0c5757..2aa61bc 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 0000000..44839bd --- /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 0000000..5037f68 --- /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 0000000..eccbb44 --- /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 fd57a53..55a8123 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 7d32ed5..a8bdf6f 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.7.4