From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 639CC3838144 for ; Tue, 21 Feb 2023 12:04:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 639CC3838144 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-wr1-x436.google.com with SMTP id o4so4164980wrs.4 for ; Tue, 21 Feb 2023 04:04:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; 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=G3ONC9PX2itSjLz0EzLRU32X9fcrVk/vu9ZHrzlAcDI=; b=Xwun+jU0ZunQTBL51ry3pF9atGRwZwhAx2lTvd1y4ZbaT9PPTK8yqBeSf0rGytZfds h//QiZCyTsL/os/lrLxTKT0SVyhdO5E3LGEvZmMZUvHltYqD13vgwNZWTrlAsEiRJBY+ TY6b98gA7NyG5GO9Tp6mW8IUQ4XytIttiYUcSGNh9pRXM43OvNtnRAW0ZFdA6mAVfqai RfkTq+YiW+xBOOz434Qk0ok5g4snpMyz+UYkFyqlx7JAKFdDp3M6eOMwwa+tlFfDotVQ gYR75VCji8pFB/I2pIv7h5eRv1fVxAfLR495WK0IbKGS+sfzbR5qBNUFriqOznDjOmHR 7Gcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=G3ONC9PX2itSjLz0EzLRU32X9fcrVk/vu9ZHrzlAcDI=; b=SUzTTHoeyRm+KXC5GaHytPdV0Qw7kWeZe7WV7SY9vFGXsuCkqCaWfGcUSQjgv8gPek 8BDYJYjxBF9keFJsTWl0V8wC/ya7e50uG7EjhawtR/8wVAzF8Z7kcQZh3zjqO4BXGj7E LWnep1OgyTKCce4vpufZ4rqnky6nsBEtH72rxeJEF2oGxpwe7noig1HvW00pTR+QElHA COGiYBC5JzegWQNcH7H5x9r/8c6WgCAMCMF695f4ovcWemtAXglQGDuzWmsTCxUqjhRn +5YtIDlXl3bjbMX4LdSDRC0J3JARURifisbEC1R+MbFlvLQjlX/K5UBJpf8QcAoj7nDl gqVQ== X-Gm-Message-State: AO0yUKWdg5cSADO6FRuSw98o8ci+4Bh5vBEH79fUL5y9QKTqOwde16jx 6XvcNviKYu7aovTIetmmPLuk X-Google-Smtp-Source: AK7set/fFqZpzXyfwcIltfTVFvD5WogbQkgR01yYqQykS6JN1nryVXluAhzqtSaeKYzzVY9UjTpYVg== X-Received: by 2002:a05:6000:1cc:b0:2c6:6e35:440f with SMTP id t12-20020a05600001cc00b002c66e35440fmr4042090wrx.6.1676981042269; Tue, 21 Feb 2023 04:04:02 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id c15-20020adffb4f000000b002c55b0e6ef1sm5013811wrs.4.2023.02.21.04.04.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:04:01 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Philip Herron Subject: [committed 047/103] gccrs: Refactor method call type checking Date: Tue, 21 Feb 2023 13:01:37 +0100 Message-Id: <20230221120230.596966-48-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230221120230.596966-1-arthur.cohen@embecosm.com> References: <20230221120230.596966-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.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=unavailable 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 gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Simplify method call type checking by removing visitor and instead using one static cast. Use the new interface. * typecheck/rust-tyty-call.cc (TypeCheckMethodCallExpr::visit): Likewise. (TypeCheckMethodCallExpr::go): Likewise. (TypeCheckMethodCallExpr::check): Likewise. * typecheck/rust-tyty-call.h (class TypeCheckMethodCallExpr): Likewise. (class Argument): Likewise. --- .../typecheck/rust-hir-type-check-expr.cc | 3 +- gcc/rust/typecheck/rust-tyty-call.cc | 98 +++++++++++++------ gcc/rust/typecheck/rust-tyty-call.h | 79 +++++++-------- 3 files changed, 106 insertions(+), 74 deletions(-) diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 6b8c63b77f6..9140b8b1305 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -1162,7 +1162,8 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr) rust_debug ("type-checking method_call: {%s}", lookup->debug_str ().c_str ()); TyTy::BaseType *function_ret_tyty - = TyTy::TypeCheckMethodCallExpr::go (lookup, expr, adjusted_self, context); + = TyTy::TypeCheckMethodCallExpr::go (static_cast (lookup), + expr, adjusted_self, context); if (function_ret_tyty == nullptr || function_ret_tyty->get_kind () == TyTy::TypeKind::ERROR) { diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc index 96c6b93e5c2..0c9e9f488fd 100644 --- a/gcc/rust/typecheck/rust-tyty-call.cc +++ b/gcc/rust/typecheck/rust-tyty-call.cc @@ -216,29 +216,77 @@ TypeCheckCallExpr::visit (FnPtr &type) // method call checker -void -TypeCheckMethodCallExpr::visit (FnType &type) +TypeCheckMethodCallExpr::TypeCheckMethodCallExpr ( + Analysis::NodeMapping call_mappings, std::vector &args, + Location call_locus, Location receiver_locus, TyTy::BaseType *adjusted_self, + Resolver::TypeCheckContext *context) + : call_mappings (call_mappings), arguments (args), call_locus (call_locus), + receiver_locus (receiver_locus), adjusted_self (adjusted_self), + context (context), mappings (Analysis::Mappings::get ()) +{} + +BaseType * +TypeCheckMethodCallExpr::go (FnType *ref, HIR::MethodCallExpr &call, + TyTy::BaseType *adjusted_self, + Resolver::TypeCheckContext *context) +{ + std::vector args; + for (auto &arg : call.get_arguments ()) + { + BaseType *argument_expr_tyty + = Resolver::TypeCheckExpr::Resolve (arg.get ()); + if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) + { + rust_error_at (arg->get_locus (), + "failed to resolve type for argument"); + return new ErrorType (ref->get_ref ()); + } + + Argument a (arg->get_mappings (), argument_expr_tyty, arg->get_locus ()); + args.push_back (std::move (a)); + } + + TypeCheckMethodCallExpr checker (call.get_mappings (), args, + call.get_locus (), + call.get_receiver ()->get_locus (), + adjusted_self, context); + return checker.check (*ref); +} + +BaseType * +TypeCheckMethodCallExpr::go (FnType *ref, Analysis::NodeMapping call_mappings, + std::vector &args, Location call_locus, + Location receiver_locus, + TyTy::BaseType *adjusted_self, + Resolver::TypeCheckContext *context) +{ + TypeCheckMethodCallExpr checker (call_mappings, args, call_locus, + receiver_locus, adjusted_self, context); + return checker.check (*ref); +} + +BaseType * +TypeCheckMethodCallExpr::check (FnType &type) { Resolver::TypeCheckBase::unify_site ( - call.get_mappings ().get_hirid (), TyWithLocation (type.get_self_type ()), - TyWithLocation (adjusted_self, call.get_receiver ()->get_locus ()), - call.get_locus ()); + call_mappings.get_hirid (), TyWithLocation (type.get_self_type ()), + TyWithLocation (adjusted_self, receiver_locus), call_locus); // +1 for the receiver self - size_t num_args_to_call = call.num_params () + 1; + size_t num_args_to_call = arguments.size () + 1; if (num_args_to_call != type.num_params ()) { - rust_error_at (call.get_locus (), + rust_error_at (call_locus, "unexpected number of arguments %lu expected %lu", - (unsigned long) call.num_params (), + (unsigned long) num_args_to_call, (unsigned long) type.num_params ()); - return; + return new ErrorType (type.get_ref ()); } size_t i = 1; - for (auto &argument : call.get_arguments ()) + for (auto &argument : arguments) { - Location arg_locus = argument->get_locus (); + Location arg_locus = argument.get_locus (); auto fnparam = type.param_at (i); HIR::Pattern *fn_param_pattern = fnparam.first; @@ -248,25 +296,15 @@ TypeCheckMethodCallExpr::visit (FnType &type) ? mappings->lookup_location (param_ty->get_ref ()) : fn_param_pattern->get_locus (); - auto argument_expr_tyty - = Resolver::TypeCheckExpr::Resolve (argument.get ()); - if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR) - { - rust_error_at ( - argument->get_locus (), - "failed to resolve type for argument expr in CallExpr"); - return; - } - - HirId coercion_side_id = argument->get_mappings ().get_hirid (); + auto argument_expr_tyty = argument.get_argument_type (); + HirId coercion_side_id = argument.get_mappings ().get_hirid (); auto resolved_argument_type = Resolver::TypeCheckBase::coercion_site ( coercion_side_id, TyWithLocation (param_ty, param_locus), - TyWithLocation (argument_expr_tyty, arg_locus), argument->get_locus ()); + TyWithLocation (argument_expr_tyty, arg_locus), arg_locus); if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR) { - rust_error_at (argument->get_locus (), - "Type Resolution failure on parameter"); - return; + rust_error_at (arg_locus, "Type Resolution failure on parameter"); + return new ErrorType (type.get_ref ()); } i++; @@ -274,15 +312,15 @@ TypeCheckMethodCallExpr::visit (FnType &type) if (i != num_args_to_call) { - rust_error_at (call.get_locus (), + rust_error_at (call_locus, "unexpected number of arguments %lu expected %lu", - (unsigned long) i, (unsigned long) call.num_params ()); - return; + (unsigned long) i, (unsigned long) arguments.size ()); + return new ErrorType (type.get_ref ()); } type.monomorphize (); - resolved = type.get_return_type ()->monomorphized_clone (); + return type.get_return_type ()->monomorphized_clone (); } } // namespace TyTy diff --git a/gcc/rust/typecheck/rust-tyty-call.h b/gcc/rust/typecheck/rust-tyty-call.h index aa47328424e..4d1c81112d6 100644 --- a/gcc/rust/typecheck/rust-tyty-call.h +++ b/gcc/rust/typecheck/rust-tyty-call.h @@ -84,58 +84,51 @@ private: Analysis::Mappings *mappings; }; -class TypeCheckMethodCallExpr : private TyVisitor +class Argument { public: - // Resolve the Method parameters and return back the return type - static BaseType *go (BaseType *ref, HIR::MethodCallExpr &call, - TyTy::BaseType *adjusted_self, - Resolver::TypeCheckContext *context) - { - TypeCheckMethodCallExpr checker (call, adjusted_self, context); - ref->accept_vis (checker); - return checker.resolved; - } + Argument (Analysis::NodeMapping mapping, BaseType *argument_type, + Location locus) + : mapping (mapping), argument_type (argument_type), locus (locus) + {} - void visit (InferType &) override { gcc_unreachable (); } - void visit (TupleType &) override { gcc_unreachable (); } - void visit (ArrayType &) override { gcc_unreachable (); } - void visit (SliceType &) override { gcc_unreachable (); } - void visit (BoolType &) override { gcc_unreachable (); } - void visit (IntType &) override { gcc_unreachable (); } - void visit (UintType &) override { gcc_unreachable (); } - void visit (FloatType &) override { gcc_unreachable (); } - void visit (USizeType &) override { gcc_unreachable (); } - void visit (ISizeType &) override { gcc_unreachable (); } - void visit (ErrorType &) override { gcc_unreachable (); } - void visit (ADTType &) override { gcc_unreachable (); }; - void visit (CharType &) override { gcc_unreachable (); } - void visit (ReferenceType &) override { gcc_unreachable (); } - void visit (PointerType &) override { gcc_unreachable (); } - void visit (ParamType &) override { gcc_unreachable (); } - void visit (StrType &) override { gcc_unreachable (); } - void visit (NeverType &) override { gcc_unreachable (); } - void visit (PlaceholderType &) override { gcc_unreachable (); } - void visit (ProjectionType &) override { gcc_unreachable (); } - void visit (DynamicObjectType &) override { gcc_unreachable (); } + Location get_locus () const { return locus; } - // FIXME - void visit (FnPtr &type) override { gcc_unreachable (); } + BaseType *get_argument_type () { return argument_type; } - // call fns - void visit (FnType &type) override; - void visit (ClosureType &type) override { gcc_unreachable (); } + Analysis::NodeMapping get_mappings () const { return mapping; } private: - TypeCheckMethodCallExpr (HIR::MethodCallExpr &c, + Analysis::NodeMapping mapping; + BaseType *argument_type; + Location locus; +}; + +class TypeCheckMethodCallExpr +{ +public: + static BaseType *go (FnType *ref, HIR::MethodCallExpr &call, + TyTy::BaseType *adjusted_self, + Resolver::TypeCheckContext *context); + + static BaseType *go (FnType *ref, Analysis::NodeMapping call_mappings, + std::vector &args, Location call_locus, + Location receiver_locus, TyTy::BaseType *adjusted_self, + Resolver::TypeCheckContext *context); + +protected: + BaseType *check (FnType &type); + + TypeCheckMethodCallExpr (Analysis::NodeMapping call_mappings, + std::vector &args, Location call_locus, + Location receiver_locus, TyTy::BaseType *adjusted_self, - Resolver::TypeCheckContext *context) - : resolved (nullptr), call (c), adjusted_self (adjusted_self), - context (context), mappings (Analysis::Mappings::get ()) - {} + Resolver::TypeCheckContext *context); - BaseType *resolved; - HIR::MethodCallExpr &call; + Analysis::NodeMapping call_mappings; + std::vector &arguments; + Location call_locus; + Location receiver_locus; TyTy::BaseType *adjusted_self; Resolver::TypeCheckContext *context; Analysis::Mappings *mappings; -- 2.39.1