From 999026853a8ddbb19a7238cbed3f40e2cb111d79 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Fri, 8 Mar 2024 14:42:10 +0100 Subject: [PATCH] WIP stuff on hir refactor gcc/rust/ChangeLog: * backend/rust-compile-base.cc: * backend/rust-compile-expr.cc (CompileExpr::visit): * backend/rust-compile-intrinsic.cc (compile_fn_params): (transmute_handler): (assume_handler): * backend/rust-compile-type.cc (TyTyResolveCompile::visit): * checks/errors/borrowck/rust-bir-builder-expr-stmt.cc (ExprStmtBuilder::visit): * checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::visit): * checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): * hir/tree/rust-hir-expr.h: * hir/tree/rust-hir-item.h: * hir/tree/rust-hir.h: * typecheck/rust-hir-dot-operator.cc (MethodResolver::Select): * typecheck/rust-hir-type-check-expr.cc: * typecheck/rust-hir-type-check-implitem.cc (TypeCheckTopLevelExternItem::visit): (TypeCheckImplItem::visit): * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): * typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): * typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): (TypeCheckMethodCallExpr::check): * typecheck/rust-tyty-cmp.h: * typecheck/rust-tyty.cc (BaseType::monomorphized_clone): (BaseType::is_concrete): (FnType::as_string): (FnType::is_equal): (FnType::clone): (FnType::handle_substitions): * typecheck/rust-tyty.h (class FnParam): * typecheck/rust-unify.cc (UnifyRules::expect_fndef): (UnifyRules::expect_fnptr): Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/backend/rust-compile-base.cc | 2 +- gcc/rust/backend/rust-compile-expr.cc | 4 +- gcc/rust/backend/rust-compile-intrinsic.cc | 17 ++--- gcc/rust/backend/rust-compile-type.cc | 4 +- .../borrowck/rust-bir-builder-expr-stmt.cc | 2 +- .../errors/privacy/rust-privacy-reporter.cc | 11 +-- gcc/rust/checks/errors/rust-unsafe-checker.cc | 2 +- gcc/rust/hir/tree/rust-hir-expr.h | 2 + gcc/rust/hir/tree/rust-hir-item.h | 2 - gcc/rust/hir/tree/rust-hir.h | 5 +- gcc/rust/typecheck/rust-hir-dot-operator.cc | 2 +- .../typecheck/rust-hir-type-check-expr.cc | 4 +- .../typecheck/rust-hir-type-check-implitem.cc | 28 +++----- .../typecheck/rust-hir-type-check-item.cc | 6 +- gcc/rust/typecheck/rust-hir-type-check.cc | 18 ++--- gcc/rust/typecheck/rust-tyty-call.cc | 20 +++--- gcc/rust/typecheck/rust-tyty-cmp.h | 6 +- gcc/rust/typecheck/rust-tyty.cc | 30 ++++---- gcc/rust/typecheck/rust-tyty.h | 69 ++++++++++++------- gcc/rust/typecheck/rust-unify.cc | 6 +- 20 files changed, 125 insertions(+), 115 deletions(-) diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 75d3e9a324f3..8e46256e3490 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -704,7 +704,7 @@ HIRCompileBase::compile_function ( for (auto &referenced_param : function_params) { auto &tyty_param = fntype->param_at (i++); - auto param_tyty = tyty_param.second; + auto param_tyty = tyty_param.get_type (); auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty); location_t param_locus = referenced_param.get_locus (); diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 56844ce88818..d829c87043ce 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -1201,7 +1201,7 @@ CompileExpr::visit (HIR::CallExpr &expr) const TyTy::FnType *fn = static_cast (base); auto ¶m = fn->param_at (index); - *result = param.second; + *result = param.get_type (); return true; }; @@ -1340,7 +1340,7 @@ CompileExpr::visit (HIR::MethodCallExpr &expr) // assignments are coercion sites so lets convert the rvalue if // necessary, offset from the already adjusted implicit self bool ok; - TyTy::BaseType *expected = fntype->param_at (i + 1).second; + TyTy::BaseType *expected = fntype->param_at (i + 1).get_type (); TyTy::BaseType *actual = nullptr; ok = ctx->get_tyctx ()->lookup_type ( diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc index 4057e832f7aa..a5c600ac444b 100644 --- a/gcc/rust/backend/rust-compile-intrinsic.cc +++ b/gcc/rust/backend/rust-compile-intrinsic.cc @@ -315,13 +315,13 @@ compile_fn_params (Context *ctx, TyTy::FnType *fntype, tree fndecl, { for (auto &parm : fntype->get_params ()) { - auto &referenced_param = parm.first; - auto ¶m_tyty = parm.second; + auto &referenced_param = parm.get_pattern (); + auto param_tyty = parm.get_type (); auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty); - location_t param_locus = referenced_param->get_locus (); + location_t param_locus = referenced_param.get_locus (); Bvariable *compiled_param_var - = CompileFnParam::compile (ctx, fndecl, *referenced_param, + = CompileFnParam::compile (ctx, fndecl, referenced_param, compiled_param_type, param_locus); compiled_param_variables->push_back (compiled_param_var); @@ -496,9 +496,10 @@ transmute_handler (Context *ctx, TyTy::FnType *fntype) rust_error_at (fntype->get_locus (), "cannot transmute between types of different sizes, or " "dependently-sized types"); - rust_inform (fntype->get_ident ().locus, "source type: %qs (%lu bits)", - fntype->get_params ().at (0).second->as_string ().c_str (), - (unsigned long) source_size); + rust_inform ( + fntype->get_ident ().locus, "source type: %qs (%lu bits)", + fntype->get_params ().at (0).get_type ()->as_string ().c_str (), + (unsigned long) source_size); rust_inform (fntype->get_ident ().locus, "target type: %qs (%lu bits)", fntype->get_return_type ()->as_string ().c_str (), (unsigned long) target_size); @@ -1226,7 +1227,7 @@ assume_handler (Context *ctx, TyTy::FnType *fntype) // TODO: make sure this is actually helping the compiler optimize rust_assert (fntype->get_params ().size () == 1); - rust_assert (fntype->param_at (0).second->get_kind () + rust_assert (fntype->param_at (0).get_type ()->get_kind () == TyTy::TypeKind::BOOL); tree lookup = NULL_TREE; diff --git a/gcc/rust/backend/rust-compile-type.cc b/gcc/rust/backend/rust-compile-type.cc index db8e530c71f4..83442272f672 100644 --- a/gcc/rust/backend/rust-compile-type.cc +++ b/gcc/rust/backend/rust-compile-type.cc @@ -208,12 +208,12 @@ TyTyResolveCompile::visit (const TyTy::FnType &type) for (auto ¶m_pair : type.get_params ()) { - auto param_tyty = param_pair.second; + auto param_tyty = param_pair.get_type (); auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty, trait_object_mode); auto compiled_param = Backend::typed_identifier ( - param_pair.first->as_string (), compiled_param_type, + param_pair.get_pattern ().as_string (), compiled_param_type, ctx->get_mappings ()->lookup_location (param_tyty->get_ref ())); parameters.push_back (compiled_param); diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc index 6dc77b58322c..3ef890c17cda 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc @@ -253,7 +253,7 @@ ExprStmtBuilder::visit (HIR::CallExpr &expr) { for (size_t i = 0; i < fn_type->get_params ().size (); ++i) { - coercion_site (arguments[i], fn_type->get_params ()[i].second); + coercion_site (arguments[i], fn_type->get_params ()[i].get_type ()); } } else if (auto fn_ptr_type = call_type->try_as ()) diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index 560a90c6ca8f..104483409104 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -488,8 +488,8 @@ PrivacyReporter::visit (HIR::BlockExpr &expr) for (auto &stmt : expr.get_statements ()) stmt->accept_vis (*this); - auto &last_expr = expr.get_final_expr (); - last_expr.accept_vis (*this); + if (expr.has_final_expr ()) + expr.get_final_expr ().accept_vis (*this); } void @@ -499,8 +499,8 @@ PrivacyReporter::visit (HIR::ContinueExpr &) void PrivacyReporter::visit (HIR::BreakExpr &expr) { - auto &break_expr = expr.get_expr (); - break_expr.accept_vis (*this); + if (expr.has_break_expr ()) + expr.get_expr ().accept_vis (*this); } void @@ -542,7 +542,8 @@ PrivacyReporter::visit (HIR::RangeToInclExpr &) void PrivacyReporter::visit (HIR::ReturnExpr &expr) { - expr.get_expr ().accept_vis (*this); + if (expr.has_expr ()) + expr.get_expr ().accept_vis (*this); } void diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index 551d35d9c64f..30fd7e794c11 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -445,7 +445,7 @@ UnsafeChecker::visit (MethodCallExpr &expr) context.lookup_type (expr.get_method_name ().get_mappings ().get_hirid (), &method_type); - auto fn = *static_cast (method_type); + auto &fn = static_cast (*method_type); auto method = mappings.lookup_hir_implitem (fn.get_ref (), nullptr); if (!unsafe_context.is_in_context () && method) diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h index cd29c27349dd..61123c9eef9b 100644 --- a/gcc/rust/hir/tree/rust-hir-expr.h +++ b/gcc/rust/hir/tree/rust-hir-expr.h @@ -2225,6 +2225,7 @@ class BlockExpr : public ExprWithBlock, public WithInnerAttrs bool is_final_stmt (Stmt *stmt) { return statements.back ().get () == stmt; } + bool has_final_expr () { return expr != nullptr; } Expr &get_final_expr () { return *expr; } std::vector > &get_statements () { return statements; } @@ -2772,6 +2773,7 @@ class ReturnExpr : public ExprWithoutBlock void accept_vis (HIRFullVisitor &vis) override; void accept_vis (HIRExpressionVisitor &vis) override; + bool has_expr () { return return_expr != nullptr; } Expr &get_expr () { return *return_expr; } ExprType get_expression_type () const override final diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 7ee1e4d4018b..6d6915dea4e9 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -551,8 +551,6 @@ struct FunctionParam Pattern &get_param_name () { return *param_name; } - std::unique_ptr take_param_name () { return std::move (param_name); } - Type &get_type () { return *type; } const Analysis::NodeMapping &get_mappings () const { return mappings; } diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index 2c3af76b6cc2..d886008dc4ab 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -463,7 +463,10 @@ class Type : public Node, public FullVisitable virtual void accept_vis (HIRTypeVisitor &vis) = 0; - virtual Analysis::NodeMapping get_mappings () const { return mappings; } + virtual const Analysis::NodeMapping &get_mappings () const + { + return mappings; + } virtual location_t get_locus () const { return locus; } protected: diff --git a/gcc/rust/typecheck/rust-hir-dot-operator.cc b/gcc/rust/typecheck/rust-hir-dot-operator.cc index 9ab87bf7a0fc..8283bab1c03d 100644 --- a/gcc/rust/typecheck/rust-hir-dot-operator.cc +++ b/gcc/rust/typecheck/rust-hir-dot-operator.cc @@ -62,7 +62,7 @@ MethodResolver::Select (std::set &candidates, for (size_t i = 0; i < arguments.size (); i++) { TyTy::BaseType *arg = arguments.at (i); - TyTy::BaseType *param = fn.get_params ().at (i + 1).second; + TyTy::BaseType *param = fn.get_params ().at (i + 1).get_type (); TyTy::BaseType *coerced = try_coercion (0, TyTy::TyWithLocation (param), TyTy::TyWithLocation (arg), UNDEF_LOCATION); diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 1f4bab2cde5b..bfc600dafeff 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -1793,7 +1793,7 @@ TypeCheckExpr::resolve_operator_overload ( // typecheck the self unify_site (expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (fnparam.second), + TyTy::TyWithLocation (fnparam.get_type ()), TyTy::TyWithLocation (adjusted_self), expr.get_locus ()); if (rhs == nullptr) { @@ -1804,7 +1804,7 @@ TypeCheckExpr::resolve_operator_overload ( rust_assert (type->num_params () == 2); auto &fnparam = type->param_at (1); unify_site (expr.get_mappings ().get_hirid (), - TyTy::TyWithLocation (fnparam.second), + TyTy::TyWithLocation (fnparam.get_type ()), TyTy::TyWithLocation (rhs), expr.get_locus ()); } diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc index 518c900bdaba..2ea8081261db 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc @@ -126,8 +126,7 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function) function.get_return_type ().get_mappings ().get_hirid ()); } - std::vector > params; - std::unique_ptr param_pattern = nullptr; + std::vector params; for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on @@ -139,14 +138,12 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function) mappings->get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); - param_pattern = Rust::make_unique ( + auto param_pattern = Rust::make_unique ( HIR::IdentifierPattern (mapping, param.get_param_name (), UNDEF_LOCATION, false, Mutability::Imm, std::unique_ptr (nullptr))); - params.push_back ( - std::pair (param_pattern.get (), - param_tyty)); + params.push_back (TyTy::FnParam (std::move (param_pattern), param_tyty)); context->insert_type (param.get_mappings (), param_tyty); @@ -244,8 +241,7 @@ TypeCheckImplItem::visit (HIR::Function &function) function.get_return_type ().get_mappings ().get_hirid ()); } - std::vector > params; - std::unique_ptr self_pattern = nullptr; + std::vector params; if (function.is_method ()) { // these are implicit mappings and not used @@ -259,10 +255,11 @@ TypeCheckImplItem::visit (HIR::Function &function) // reuse the HIR identifier pattern which requires it HIR::SelfParam &self_param = function.get_self_param (); // FIXME: which location should be used for Rust::Identifier for `self`? - self_pattern = Rust::make_unique ( - HIR::IdentifierPattern (mapping, {"self"}, self_param.get_locus (), - self_param.is_ref (), self_param.get_mut (), - std::unique_ptr (nullptr))); + std::unique_ptr self_pattern + = Rust::make_unique ( + HIR::IdentifierPattern (mapping, {"self"}, self_param.get_locus (), + self_param.is_ref (), self_param.get_mut (), + std::unique_ptr (nullptr))); // might have a specified type TyTy::BaseType *self_type = nullptr; @@ -319,9 +316,7 @@ TypeCheckImplItem::visit (HIR::Function &function) } context->insert_type (self_param.get_mappings (), self_type); - params.push_back ( - std::pair (self_pattern.get (), - self_type)); + params.push_back (TyTy::FnParam (std::move (self_pattern), self_type)); } for (auto ¶m : function.get_function_params ()) @@ -333,8 +328,7 @@ TypeCheckImplItem::visit (HIR::Function &function) TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); params.push_back ( - std::pair (¶m.get_param_name (), - param_tyty)); + TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty)); } const CanonicalPath *canonical_path = nullptr; diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc index fc7cac72e52e..5a13c0624bfb 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc @@ -487,15 +487,15 @@ TypeCheckItem::visit (HIR::Function &function) function.get_return_type ().get_mappings ().get_hirid ()); } - std::vector> params; + std::vector params; for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on auto param_tyty = TypeCheckType::Resolve (param.get_type ()); - params.emplace_back (¶m.get_param_name (), param_tyty); - context->insert_type (param.get_mappings (), param_tyty); TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); + params.push_back ( + TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty)); } const CanonicalPath *canonical_path = nullptr; diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc index 0eb62046fbaf..e039461e862a 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.cc +++ b/gcc/rust/typecheck/rust-hir-type-check.cc @@ -218,9 +218,8 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const function.get_return_type ().get_mappings ().get_hirid ()); } - std::vector > params; + std::vector params; - std::unique_ptr self_pattern = nullptr; if (function.is_method ()) { // these are implicit mappings and not used @@ -234,7 +233,7 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const // for compilation to know parameter names. The types are ignored // but we reuse the HIR identifier pattern which requires it HIR::SelfParam &self_param = function.get_self (); - self_pattern + std::unique_ptr self_pattern = Rust::make_unique (HIR::IdentifierPattern ( mapping, {"self"}, self_param.get_locus (), self_param.is_ref (), self_param.is_mut () ? Mutability::Mut : Mutability::Imm, @@ -286,21 +285,18 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const } context->insert_type (self_param.get_mappings (), self_type); - params.push_back ( - std::pair (self_pattern.get (), - self_type)); + params.push_back (TyTy::FnParam (std::move (self_pattern), self_type)); } for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on auto param_tyty = TypeCheckType::Resolve (param.get_type ()); - params.push_back ( - std::pair (¶m.get_param_name (), - param_tyty)); - context->insert_type (param.get_mappings (), param_tyty); TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); + // FIXME: Should we take the name ? Use a shared pointer instead ? + params.push_back ( + TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty)); } auto mappings = Analysis::Mappings::get (); @@ -315,7 +311,7 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const function.get_function_name ().as_string (), ident, function.is_method () ? TyTy::FnType::FNTYPE_IS_METHOD_FLAG : TyTy::FnType::FNTYPE_DEFAULT_FLAGS, - ABI::RUST, params, ret_type, substitutions, + ABI::RUST, std::move (params), ret_type, substitutions, TyTy::SubstitutionArgumentMappings::empty ( context->get_lifetime_resolver ().get_num_bound_regions ()), region_constraints); diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc index 08078aabd802..8a85a3d1d15a 100644 --- a/gcc/rust/typecheck/rust-tyty-call.cc +++ b/gcc/rust/typecheck/rust-tyty-call.cc @@ -152,12 +152,12 @@ TypeCheckCallExpr::visit (FnType &type) if (i < type.num_params ()) { auto &fnparam = type.param_at (i); - auto &fn_param_pattern = fnparam.first; - BaseType *param_ty = fnparam.second; + auto &fn_param_pattern = fnparam.get_pattern (); + BaseType *param_ty = fnparam.get_type (); location_t param_locus - = fn_param_pattern == nullptr - ? mappings->lookup_location (param_ty->get_ref ()) - : fn_param_pattern->get_locus (); + = fnparam.has_pattern () + ? fn_param_pattern.get_locus () + : mappings->lookup_location (param_ty->get_ref ()); HirId coercion_side_id = argument->get_mappings ().get_hirid (); auto resolved_argument_type @@ -375,12 +375,12 @@ TypeCheckMethodCallExpr::check (FnType &type) location_t arg_locus = argument.get_locus (); auto &fnparam = type.param_at (i); - HIR::Pattern *fn_param_pattern = fnparam.first; - BaseType *param_ty = fnparam.second; + HIR::Pattern &fn_param_pattern = fnparam.get_pattern (); + BaseType *param_ty = fnparam.get_type (); location_t param_locus - = fn_param_pattern == nullptr - ? mappings->lookup_location (param_ty->get_ref ()) - : fn_param_pattern->get_locus (); + = fnparam.has_pattern () + ? fn_param_pattern.get_locus () + : mappings->lookup_location (param_ty->get_ref ()); auto argument_expr_tyty = argument.get_argument_type (); HirId coercion_side_id = argument.get_mappings ().get_hirid (); diff --git a/gcc/rust/typecheck/rust-tyty-cmp.h b/gcc/rust/typecheck/rust-tyty-cmp.h index 98ff1dc664ed..77a9073b2dac 100644 --- a/gcc/rust/typecheck/rust-tyty-cmp.h +++ b/gcc/rust/typecheck/rust-tyty-cmp.h @@ -735,8 +735,8 @@ class FnCmp : public BaseCmp for (size_t i = 0; i < base->num_params (); i++) { - auto a = base->param_at (i).second; - auto b = type.param_at (i).second; + auto a = base->param_at (i).get_type (); + auto b = type.param_at (i).get_type (); if (!a->can_eq (b, emit_error_flag)) { @@ -831,7 +831,7 @@ class FnptrCmp : public BaseCmp for (size_t i = 0; i < base->num_params (); i++) { auto this_param = base->get_param_type_at (i); - auto other_param = type.param_at (i).second; + auto other_param = type.param_at (i).get_type (); if (!this_param->can_eq (other_param, emit_error_flag)) { BaseCmp::visit (type); diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 4cdef0b5761e..551b1bb3f2cd 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -594,10 +594,9 @@ BaseType::monomorphized_clone () const } else if (auto fn = x->try_as ()) { - std::vector> cloned_params; + std::vector cloned_params; for (auto &p : fn->get_params ()) - cloned_params.push_back (std::pair ( - p.first, p.second->monomorphized_clone ())); + cloned_params.push_back (p.clone ()); BaseType *retty = fn->get_return_type ()->monomorphized_clone (); return new FnType (fn->get_ref (), fn->get_ty_ref (), fn->get_id (), @@ -688,7 +687,7 @@ BaseType::is_concrete () const { for (const auto ¶m : fn->get_params ()) { - if (!param.second->is_concrete ()) + if (!param.get_type ()->is_concrete ()) return false; } return fn->get_return_type ()->is_concrete (); @@ -1875,9 +1874,9 @@ FnType::as_string () const std::string params_str = ""; for (auto ¶m : params) { - auto &pattern = param.first; - auto ty = param.second; - params_str += pattern->as_string () + " " + ty->as_string (); + auto &pattern = param.get_pattern (); + auto ty = param.get_type (); + params_str += pattern.as_string () + " " + ty->as_string (); params_str += ","; } @@ -1898,7 +1897,7 @@ FnType::is_equal (const BaseType &other) const if (get_kind () != other.get_kind ()) return false; - auto other2 = static_cast (other); + auto &other2 = static_cast (other); if (get_identifier ().compare (other2.get_identifier ()) != 0) return false; @@ -1932,8 +1931,8 @@ FnType::is_equal (const BaseType &other) const for (size_t i = 0; i < num_params (); i++) { - auto lhs = param_at (i).second; - auto rhs = other2.param_at (i).second; + auto lhs = param_at (i).get_type (); + auto rhs = other2.param_at (i).get_type (); if (!lhs->is_equal (*rhs)) return false; } @@ -1943,10 +1942,9 @@ FnType::is_equal (const BaseType &other) const BaseType * FnType::clone () const { - std::vector> cloned_params; + std::vector cloned_params; for (auto &p : params) - cloned_params.push_back ( - std::pair (p.first, p.second->clone ())); + cloned_params.push_back (p.clone ()); return new FnType (get_ref (), get_ty_ref (), get_id (), get_identifier (), ident, flags, abi, std::move (cloned_params), @@ -2020,7 +2018,7 @@ FnType::handle_substitions (SubstitutionArgumentMappings &subst_mappings) for (auto ¶m : fn->get_params ()) { - auto fty = param.second; + auto fty = param.get_type (); bool is_param_ty = fty->get_kind () == TypeKind::PARAM; if (is_param_ty) @@ -2039,7 +2037,7 @@ FnType::handle_substitions (SubstitutionArgumentMappings &subst_mappings) { auto new_field = argt->clone (); new_field->set_ref (fty->get_ref ()); - param.second = new_field; + param.set_type (new_field); } else { @@ -2063,7 +2061,7 @@ FnType::handle_substitions (SubstitutionArgumentMappings &subst_mappings) auto new_field = concrete->clone (); new_field->set_ref (fty->get_ref ()); - param.second = new_field; + param.set_type (new_field); } } diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 31ed2bdc5f12..4eedfb243f5b 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -765,6 +765,34 @@ class ADTType : public BaseType, public SubstitutionRef ReprOptions repr; }; +class FnParam +{ +public: + FnParam (std::unique_ptr pattern, BaseType *type) + : pattern (std::move (pattern)), type (type) + {} + + FnParam (const FnParam &) = delete; + FnParam (FnParam &&) = default; + FnParam &operator= (FnParam &&) = default; + + HIR::Pattern &get_pattern () { return *pattern; } + const HIR::Pattern &get_pattern () const { return *pattern; } + + bool has_pattern () { return pattern != nullptr; } + BaseType *get_type () const { return type; } + void set_type (BaseType *new_type) { type = new_type; } + + FnParam clone () const + { + return FnParam (pattern->clone_pattern (), type->monomorphized_clone ()); + } + +private: + std::unique_ptr pattern; + BaseType *type; +}; + class FnType : public CallableTypeInterface, public SubstitutionRef { public: @@ -776,25 +804,23 @@ class FnType : public CallableTypeInterface, public SubstitutionRef static const uint8_t FNTYPE_IS_VARADIC_FLAG = 0X04; FnType (HirId ref, DefId id, std::string identifier, RustIdent ident, - uint8_t flags, ABI abi, - std::vector> params, - BaseType *type, std::vector subst_refs, + uint8_t flags, ABI abi, std::vector params, BaseType *type, + std::vector subst_refs, SubstitutionArgumentMappings substitution_argument_mappings, RegionConstraints region_constraints, std::set refs = std::set ()) : CallableTypeInterface (ref, ref, TypeKind::FNDEF, ident, refs), SubstitutionRef (std::move (subst_refs), substitution_argument_mappings, region_constraints), - params (params), type (type), flags (flags), identifier (identifier), - id (id), abi (abi) + params (std::move (params)), type (type), flags (flags), + identifier (identifier), id (id), abi (abi) { LocalDefId local_def_id = id.localDefId; rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID); } FnType (HirId ref, HirId ty_ref, DefId id, std::string identifier, - RustIdent ident, uint8_t flags, ABI abi, - std::vector> params, + RustIdent ident, uint8_t flags, ABI abi, std::vector params, BaseType *type, std::vector subst_refs, SubstitutionArgumentMappings substitution_argument_mappings, RegionConstraints region_constraints, @@ -809,6 +835,9 @@ class FnType : public CallableTypeInterface, public SubstitutionRef rust_assert (local_def_id != UNKNOWN_LOCAL_DEFID); } + FnType (const FnType &) = delete; + FnType (FnType &&) = default; + void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; @@ -842,28 +871,16 @@ class FnType : public CallableTypeInterface, public SubstitutionRef BaseType *get_self_type () const { rust_assert (is_method ()); - return param_at (0).second; + return param_at (0).get_type (); } - std::vector> &get_params () - { - return params; - } + std::vector &get_params () { return params; } - const std::vector> &get_params () const - { - return params; - } + const std::vector &get_params () const { return params; } - std::pair ¶m_at (size_t idx) - { - return params.at (idx); - } + FnParam ¶m_at (size_t idx) { return params.at (idx); } - const std::pair ¶m_at (size_t idx) const - { - return params.at (idx); - } + const FnParam ¶m_at (size_t idx) const { return params.at (idx); } BaseType *clone () const final override; @@ -880,7 +897,7 @@ class FnType : public CallableTypeInterface, public SubstitutionRef WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override { - return param_at (index).second; + return param_at (index).get_type (); } WARN_UNUSED_RESULT BaseType *get_return_type () const override @@ -889,7 +906,7 @@ class FnType : public CallableTypeInterface, public SubstitutionRef } private: - std::vector> params; + std::vector params; BaseType *type; uint8_t flags; std::string identifier; diff --git a/gcc/rust/typecheck/rust-unify.cc b/gcc/rust/typecheck/rust-unify.cc index 8b43380cc592..802cd6e4a17e 100644 --- a/gcc/rust/typecheck/rust-unify.cc +++ b/gcc/rust/typecheck/rust-unify.cc @@ -919,8 +919,8 @@ UnifyRules::expect_fndef (TyTy::FnType *ltype, TyTy::BaseType *rtype) for (size_t i = 0; i < ltype->num_params (); i++) { - auto a = ltype->param_at (i).second; - auto b = type.param_at (i).second; + auto a = ltype->param_at (i).get_type (); + auto b = type.param_at (i).get_type (); auto unified_param = UnifyRules::Resolve (TyTy::TyWithLocation (a), @@ -1059,7 +1059,7 @@ UnifyRules::expect_fnptr (TyTy::FnPtr *ltype, TyTy::BaseType *rtype) for (size_t i = 0; i < ltype->num_params (); i++) { auto this_param = ltype->get_param_type_at (i); - auto other_param = type.param_at (i).second; + auto other_param = type.param_at (i).get_type (); auto unified_param = UnifyRules::Resolve (TyTy::TyWithLocation (this_param),