From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by sourceware.org (Postfix) with ESMTPS id EF39B3857714 for ; Tue, 30 Jan 2024 12:11:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EF39B3857714 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EF39B3857714 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::22d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616747; cv=none; b=oIzk8oQI8kFMiLLam5p/ooHXJoqZe9mTUzo1yFEdlBgOyScBsLMWiNE99cddEcB+40af0xjIkwBYSpKdpx4f0lJyWOhqJXv9ox4KwD4ywCorZKMwN31OTYZSQlX/e20n5tOzNLBkUCO453Z1y2Gc/KZ1rCgF86ikZUSta9SFOn4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616747; c=relaxed/simple; bh=ihdr6rRuy4wV49af+fws74wwJmUv73aB9dnjLfLUdpU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=UmazLcPrlrX007NfdXELNfhCfk2kcaB5Zdxx3Ch8BZX0iP40h1Jys3EaoVUQtcDm3HZ5COhXwH0mKy0tDy9BT+jDqoYG8dDYmLNxe5SCk8tGicKroidgF3ebUk9VaqnaZexuHA71AbuYhlrgMLQ+7O1pzAHwBjGZZoolwR9/W3E= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2d0600551e8so2654051fa.0 for ; Tue, 30 Jan 2024 04:11:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1706616706; x=1707221506; darn=gcc.gnu.org; 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=xm79gVUEsRKM7CogFIXiy9FFbQ7byyhI1qwMqADHFrU=; b=cEy+jZ62f3vYgwWnFx4/NT3326bWLliT20yKPFxuDFL+ubQ+J/nkFofIVtmV9hXcLP UNuHLGklmrRBnqoInSNXy08xXPsf0d8b0NvuMCA6XrLVvSF6Pi7h/uYkAnlZU5g2SWEe gD9xUS8RKqIVYOWabprTkOpY6bIBNp09oPpnhlV7MYkM5QleBkiIKX0HTpctADlOuoED Rq2srDo+QMKvmVpWYVniYtfiWUyp7tHpC34rzHWkBwHUVnXarOsUAgGS+jBhkLzj/1nI AQO2EbTV+jMJN3kDrMc5oYlEMV7w1AHmmXVyipbjpRZWtzomfoeicL/DR/nkrewy3ASp luGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706616706; x=1707221506; 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=xm79gVUEsRKM7CogFIXiy9FFbQ7byyhI1qwMqADHFrU=; b=KkYbea6mmYBpgyA2BWhm5FW3m45rbP/aMjACNJTfWVDplgwm/k1Pf0Gi81aY1Nv33Q 419BWXPBnbSx9/jydOLcwwUrPFkMWWxGXC7VLmiVc2moAJF5xZ7jHgFG/qBp15BhAZYX Rexi6ayxfsVjAC+sD4Nn9RNuB6KBWjzNd1Fp4CG7i93EGa/6PsuHm+pu80Pvd70hI2N1 27HmzioshmZDVNjiwLtgGO/qrbf/YM530SvdNekMxbOO8jykSH6MUjMwW37MPK1f/oaR XE9iJcn5X0X+ndoYBzx/kzMCvoLL8/7DjmVtmmHpVm3JHwV1Y4tzecRjiX2z+VLePpMT GStQ== X-Gm-Message-State: AOJu0YwLOX1GjQAZxpB1Nuv4ZjNAUa92HKRlKwA/FXRgLw5gZl4Hiiee eurO2OjpGsYwwH7WIitayFUniEkFG4eiC9T0xfeB3YnoDcmCJmsohf4piRxuT60RnH4kB7iXTZf Ulw== X-Google-Smtp-Source: AGHT+IGKS06PtHldBLCHBMxwG4XTieYWMbVWDZbv1QG8N7T7kHNIxhhvuWMLNm8niTTPTwfvALIxfg== X-Received: by 2002:a2e:9417:0:b0:2cf:1752:e0fc with SMTP id i23-20020a2e9417000000b002cf1752e0fcmr5491391ljh.42.1706616705998; Tue, 30 Jan 2024 04:11:45 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id f9-20020a056000036900b00339307d9d31sm10569894wrf.112.2024.01.30.04.11.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 04:11:45 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Jakub Dupak Subject: [COMMITTED 076/101] gccrs: TyTy: Common interface for fucntion-like types Date: Tue, 30 Jan 2024 13:07:32 +0100 Message-ID: <20240130121026.807464-79-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20240130121026.807464-2-arthur.cohen@embecosm.com> References: <20240130121026.807464-2-arthur.cohen@embecosm.com> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.1 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,T_SCC_BODY_TEXT_LINE,URIBL_SBL_A 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: Jakub Dupak gcc/rust/ChangeLog: * typecheck/rust-tyty.h (class ClosureType): Inherit interface. (class FnPtr): Inherit interface. (class FnType): Inherit interface. (class CallableTypeInterface): New interface. (BaseType::is): Detect interface members API. Signed-off-by: Jakub Dupak --- gcc/rust/typecheck/rust-tyty.h | 116 ++++++++++++++++++++++++++++----- 1 file changed, 98 insertions(+), 18 deletions(-) diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index da4b901724d..6ce760df66e 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -38,6 +38,10 @@ class AssociatedImplTrait; } // namespace Resolver namespace TyTy { +class ClosureType; +class FnPtr; +class FnType; +class CallableTypeInterface; // https://rustc-dev-guide.rust-lang.org/type-inference.html#inference-variables // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.TyKind.html#variants @@ -242,6 +246,22 @@ protected: Analysis::Mappings *mappings; }; +/** Unified interface for all function-like types. */ +class CallableTypeInterface : public BaseType +{ +public: + explicit CallableTypeInterface (HirId ref, HirId ty_ref, TypeKind kind, + RustIdent ident, + std::set refs = std::set ()) + : BaseType (ref, ty_ref, kind, ident, refs) + {} + + WARN_UNUSED_RESULT virtual size_t get_num_params () const = 0; + WARN_UNUSED_RESULT virtual BaseType * + get_param_type_at (size_t index) const = 0; + WARN_UNUSED_RESULT virtual BaseType *get_return_type () const = 0; +}; + class InferType : public BaseType { public: @@ -736,7 +756,7 @@ private: ReprOptions repr; }; -class FnType : public BaseType, public SubstitutionRef +class FnType : public CallableTypeInterface, public SubstitutionRef { public: static constexpr auto KIND = TypeKind::FNDEF; @@ -751,7 +771,7 @@ public: std::vector> params, BaseType *type, std::vector subst_refs, std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::FNDEF, ident, refs), + : CallableTypeInterface (ref, ref, TypeKind::FNDEF, ident, refs), SubstitutionRef (std::move (subst_refs), SubstitutionArgumentMappings::error ()), params (std::move (params)), type (type), flags (flags), @@ -766,7 +786,7 @@ public: std::vector> params, BaseType *type, std::vector subst_refs, std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::FNDEF, ident, refs), + : CallableTypeInterface (ref, ty_ref, TypeKind::FNDEF, ident, refs), SubstitutionRef (std::move (subst_refs), SubstitutionArgumentMappings::error ()), params (params), type (type), flags (flags), identifier (identifier), @@ -832,8 +852,6 @@ public: return params.at (idx); } - BaseType *get_return_type () const { return type; } - BaseType *clone () const final override; FnType * @@ -842,6 +860,21 @@ public: ABI get_abi () const { return abi; } uint8_t get_flags () const { return flags; } + WARN_UNUSED_RESULT size_t get_num_params () const override + { + return params.size (); + } + + WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override + { + return param_at (index).second; + } + + WARN_UNUSED_RESULT BaseType *get_return_type () const override + { + return type; + } + private: std::vector> params; BaseType *type; @@ -851,33 +884,50 @@ private: ABI abi; }; -class FnPtr : public BaseType +class FnPtr : public CallableTypeInterface { public: static constexpr auto KIND = TypeKind::FNPTR; FnPtr (HirId ref, location_t locus, std::vector params, TyVar result_type, std::set refs = std::set ()) - : BaseType (ref, ref, TypeKind::FNPTR, - {Resolver::CanonicalPath::create_empty (), locus}, refs), + : CallableTypeInterface (ref, ref, TypeKind::FNPTR, + {Resolver::CanonicalPath::create_empty (), locus}, + refs), params (std::move (params)), result_type (result_type) {} FnPtr (HirId ref, HirId ty_ref, location_t locus, std::vector params, TyVar result_type, std::set refs = std::set ()) - : BaseType (ref, ty_ref, TypeKind::FNPTR, - {Resolver::CanonicalPath::create_empty (), locus}, refs), + : CallableTypeInterface (ref, ty_ref, TypeKind::FNPTR, + {Resolver::CanonicalPath::create_empty (), locus}, + refs), params (params), result_type (result_type) {} std::string get_name () const override final { return as_string (); } - BaseType *get_return_type () const { return result_type.get_tyty (); } + WARN_UNUSED_RESULT size_t get_num_params () const override + { + return params.size (); + } + + WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override + { + return params.at (index).get_tyty (); + } + + WARN_UNUSED_RESULT BaseType *get_return_type () const override + { + return result_type.get_tyty (); + } + const TyVar &get_var_return_type () const { return result_type; } size_t num_params () const { return params.size (); } - BaseType *param_at (size_t idx) const { return params.at (idx).get_tyty (); } + // DEPRECATED: Use get_param_type_at + BaseType *param_at (size_t idx) const { return get_param_type_at (idx); } void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; @@ -898,19 +948,19 @@ private: TyVar result_type; }; -class ClosureType : public BaseType, public SubstitutionRef +class ClosureType : public CallableTypeInterface, public SubstitutionRef { public: static constexpr auto KIND = TypeKind::CLOSURE; - ClosureType (HirId ref, DefId id, RustIdent ident, - TyTy::TupleType *parameters, TyVar result_type, + ClosureType (HirId ref, DefId id, RustIdent ident, TupleType *parameters, + TyVar result_type, std::vector subst_refs, std::set captures, std::set refs = std::set (), std::vector specified_bounds = std::vector ()) - : BaseType (ref, ref, TypeKind::CLOSURE, ident, refs), + : CallableTypeInterface (ref, ref, TypeKind::CLOSURE, ident, refs), SubstitutionRef (std::move (subst_refs), SubstitutionArgumentMappings::error ()), parameters (parameters), result_type (std::move (result_type)), id (id), @@ -922,13 +972,13 @@ public: } ClosureType (HirId ref, HirId ty_ref, RustIdent ident, DefId id, - TyTy::TupleType *parameters, TyVar result_type, + TupleType *parameters, TyVar result_type, std::vector subst_refs, std::set captures, std::set refs = std::set (), std::vector specified_bounds = std::vector ()) - : BaseType (ref, ty_ref, TypeKind::CLOSURE, ident, refs), + : CallableTypeInterface (ref, ty_ref, TypeKind::CLOSURE, ident, refs), SubstitutionRef (std::move (subst_refs), SubstitutionArgumentMappings::error ()), parameters (parameters), result_type (std::move (result_type)), id (id), @@ -942,6 +992,21 @@ public: void accept_vis (TyVisitor &vis) override; void accept_vis (TyConstVisitor &vis) const override; + WARN_UNUSED_RESULT size_t get_num_params () const override + { + return parameters->num_fields (); + } + + WARN_UNUSED_RESULT BaseType *get_param_type_at (size_t index) const override + { + return parameters->get_field (index); + } + + WARN_UNUSED_RESULT BaseType *get_return_type () const override + { + return result_type.get_tyty (); + } + std::string as_string () const override; std::string get_name () const override final { return as_string (); } @@ -1495,6 +1560,21 @@ private: DefId item; }; +template <> +WARN_UNUSED_RESULT inline bool +BaseType::is () const +{ + auto kind = this->get_kind (); + return kind == FNPTR || kind == FNDEF || kind == CLOSURE; +} + +template <> +WARN_UNUSED_RESULT inline bool +BaseType::is () const +{ + return this->is (); +} + } // namespace TyTy } // namespace Rust -- 2.42.1