From patchwork Thu Aug 1 14:56:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 1967775 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=embecosm.com header.i=@embecosm.com header.a=rsa-sha256 header.s=google header.b=erSaXmnh; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WZXhm5lSxz1ybX for ; Fri, 2 Aug 2024 01:20:24 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EC018385ED4B for ; Thu, 1 Aug 2024 15:20:22 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) by sourceware.org (Postfix) with ESMTPS id 20E1B385F028 for ; Thu, 1 Aug 2024 14:59:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 20E1B385F028 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 20E1B385F028 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::236 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524369; cv=none; b=sBZUZJy4LQ6r06SVCOzwNMMBRKl/yt2ZgYbfKzp8eQ7jrggCZSgXIP8VEVDFuNh9bI0zUeTekt41WdR0/7lubn4VLxUZEDg/yYGoHaUb4Us7VaUhbvZ37axo2BtjrYGN05E7zcPGrP7IqMGuJ8iHM9VxtOmTlpptq8RrJzED8gc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524369; c=relaxed/simple; bh=otIUHPvmhqnADKlK0rnrOnUE61OM9SFSi7Vzl7YxJFA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QadQhYfZt70z5HHbIeCgtesYCKim4Mt6YePHvvG/ejK2ArAYmF4EGh9e/OTVeSQHkXDtk1sb+7o1u+e6ymMy5k8j79JLBh5GeBFsAYTwAbMYY8BXFXtvzyTn+YjfHFo16TGG81c7UCrChdxoSVBo1AdsXpRHZUgl2P7wpJ3OmAE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lj1-x236.google.com with SMTP id 38308e7fff4ca-2f035ae0fd1so81829961fa.2 for ; Thu, 01 Aug 2024 07:59:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524341; x=1723129141; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jcnwOB3ZGkQl3bcd/cqUHaO8yHBQbLFwTxqlsps+/zE=; b=erSaXmnh6qRtLYhEqNgN5pclvJnzyn/Z/ANyqQXYVo8dCxF402ci+r+dP3WVJPQNto Bi/OtlBLiGiVIZ1KHZYbvh2nt+6K62PlzNmFtSD/DCyw/Xp6ZzVbY5SYIHS0r+MGBbor mcMcp0i0Ngw9makK7VP4GCOAIlWUr9Z2M4ouo+E2Ghnf1tlv6FsPpfTDT/m14qpXRnCW sSovA0tZdymhIXgB6Tu1r0m88FY/oDMhrkmiaqnnKsmOXG30/ck3vpV/SB0yvEFaYsni vcUPnaafzKxua5vboMvPtq0OQvbbjhDc08ROiyZ4vbZMT/6nOpsLU+PIwMvQmfzNvYvX nHlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524341; x=1723129141; h=content-transfer-encoding:mime-version: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=jcnwOB3ZGkQl3bcd/cqUHaO8yHBQbLFwTxqlsps+/zE=; b=TpC4k9hIZO9//IgajjCa86P7j+K49GYPlQV9oAWPKgD+2bQ13DChRw9/BRb0c9oq8K 4qXDFalkWuQAUUlBb+eDOGLrMpCWUzWExPR5tgG05IcRyFYJko8Ry59IHLjrmqmX5ZAx 3xjUUj0NMBxGN3IYKvQF5omv6y4K5JiCrT6S8pGiSSuwp18Czr/8sHSe2D+iwWNG8sqr k4ztx2YhZnYAGxW8Gp3toTtQgNmMZ0Z1rJcvZi9Ja/fOaDQZUycBL3ggqHLdbrD0Fx+K dV1misn+KtKCqlUT69t0L/dSrBBVUNsFg3NY8E97K8OurLhrYQSpwtqF2MCd4QVB306t maCw== X-Gm-Message-State: AOJu0Yy3N3PLEw5WiqwyZL+vCmeINbWpByiYTVLrduGL36PmEGVodNWA 92pFyvUq+HKtoBkj7SI+nIdrJpLJSSyD1NusIgyHo+ArsiflVbfGP8O4N+GXIf+M+LW1eTsxWCz ih9zT X-Google-Smtp-Source: AGHT+IE1cq//TM4aOET4922ef9YJCUWQTM8pu9ZCz1ggoxA9Ktb8GnvWXh3XAsk1YYV1QX0COi1xaA== X-Received: by 2002:a05:6512:4002:b0:52c:de29:9ff with SMTP id 2adb3069b0e04-530bb39293emr120834e87.2.1722524341203; Thu, 01 Aug 2024 07:59:01 -0700 (PDT) Received: from platypus.lan ([2a04:cec2:9:dc84:3622:6733:ff49:ee91]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac63590592sm10252456a12.25.2024.08.01.07.59.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 07:59:00 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, 0xn4utilus Subject: [PATCH 038/125] gccrs: Add support for external functions Date: Thu, 1 Aug 2024 16:56:34 +0200 Message-ID: <20240801145809.366388-40-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801145809.366388-2-arthur.cohen@embecosm.com> References: <20240801145809.366388-2-arthur.cohen@embecosm.com> MIME-Version: 1.0 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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org From: 0xn4utilus gcc/rust/ChangeLog: * ast/rust-ast.cc (Function::Function): Add `is_external_function` field. (Function::operator=): Likewise. * ast/rust-ast.h: New constructor for ExternalItem. * ast/rust-item.h (class Function): Add `is_external_function` field. Update `get_node_id`. * ast/rust-macro.h: Update copy constructor. Signed-off-by: 0xn4utilus --- gcc/rust/ast/rust-ast.cc | 9 ++++++--- gcc/rust/ast/rust-ast.h | 4 +++- gcc/rust/ast/rust-item.h | 41 +++++++++++++++++++++++++++------------ gcc/rust/ast/rust-macro.h | 9 +++++---- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index fbd795f6718..5d661989904 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -1061,9 +1061,11 @@ Union::as_string () const } Function::Function (Function const &other) - : VisItem (other), qualifiers (other.qualifiers), - function_name (other.function_name), where_clause (other.where_clause), - locus (other.locus), is_default (other.is_default) + : VisItem (other), ExternalItem (other.get_node_id ()), + qualifiers (other.qualifiers), function_name (other.function_name), + where_clause (other.where_clause), locus (other.locus), + is_default (other.is_default), + is_external_function (other.is_external_function) { // guard to prevent null dereference (always required) if (other.return_type != nullptr) @@ -1095,6 +1097,7 @@ Function::operator= (Function const &other) // outer_attrs = other.outer_attrs; locus = other.locus; is_default = other.is_default; + is_external_function = other.is_external_function; // guard to prevent null dereference (always required) if (other.return_type != nullptr) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index c4d5858dd63..92faaf297f9 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -1695,6 +1695,8 @@ class ExternalItem : public Visitable public: ExternalItem () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} + ExternalItem (NodeId node_id) : node_id (node_id) {} + virtual ~ExternalItem () {} // Unique pointer custom clone function @@ -1708,7 +1710,7 @@ public: virtual void mark_for_strip () = 0; virtual bool is_marked_for_strip () const = 0; - NodeId get_node_id () const { return node_id; } + virtual NodeId get_node_id () const { return node_id; } protected: // Clone function implementation as pure virtual method diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 44963ba386e..573888bea5a 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -1289,7 +1289,7 @@ protected: class LetStmt; // Rust function declaration AST node -class Function : public VisItem, public AssociatedItem +class Function : public VisItem, public AssociatedItem, public ExternalItem { FunctionQualifiers qualifiers; Identifier function_name; @@ -1300,6 +1300,7 @@ class Function : public VisItem, public AssociatedItem tl::optional> function_body; location_t locus; bool is_default; + bool is_external_function; public: std::string as_string () const override; @@ -1330,16 +1331,17 @@ public: std::unique_ptr return_type, WhereClause where_clause, tl::optional> function_body, Visibility vis, std::vector outer_attrs, - location_t locus, bool is_default = false) + location_t locus, bool is_default = false, + bool is_external_function = false) : VisItem (std::move (vis), std::move (outer_attrs)), - qualifiers (std::move (qualifiers)), + ExternalItem (Stmt::node_id), qualifiers (std::move (qualifiers)), function_name (std::move (function_name)), generic_params (std::move (generic_params)), function_params (std::move (function_params)), return_type (std::move (return_type)), where_clause (std::move (where_clause)), function_body (std::move (function_body)), locus (locus), - is_default (is_default) + is_default (is_default), is_external_function (is_external_function) {} // TODO: add constructor with less fields @@ -1364,6 +1366,8 @@ public: && function_params.back ()->is_variadic (); } + bool is_external () const { return is_external_function; } + // Invalid if block is null, so base stripping on that. void mark_for_strip () override { function_body = nullptr; } bool is_marked_for_strip () const override @@ -1422,6 +1426,9 @@ public: return function_params[0]; } + // ExternalItem::node_id is same as Stmt::node_id + NodeId get_node_id () const override { return Stmt::node_id; } + protected: /* Use covariance to implement clone function as returning this object * rather than base */ @@ -1433,6 +1440,13 @@ protected: { return new Function (*this); } + + /* Use covariance to implement clone function as returning this object + * rather than base */ + Function *clone_external_item_impl () const override + { + return new Function (*this); + } }; // Rust type alias (i.e. typedef) AST node @@ -3257,10 +3271,11 @@ public: item_name (std::move (item_name)), locus (locus), marked_for_strip (false) {} + // copy constructor ExternalTypeItem (ExternalTypeItem const &other) - : outer_attrs (other.outer_attrs), visibility (other.visibility), - item_name (other.item_name), locus (other.locus), - marked_for_strip (other.marked_for_strip) + : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs), + visibility (other.visibility), item_name (other.item_name), + locus (other.locus), marked_for_strip (other.marked_for_strip) { node_id = other.node_id; } @@ -3340,8 +3355,9 @@ public: // Copy constructor ExternalStaticItem (ExternalStaticItem const &other) - : outer_attrs (other.outer_attrs), visibility (other.visibility), - item_name (other.item_name), locus (other.locus), has_mut (other.has_mut) + : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs), + visibility (other.visibility), item_name (other.item_name), + locus (other.locus), has_mut (other.has_mut) { node_id = other.node_id; // guard to prevent null dereference (only required if error state) @@ -3607,9 +3623,10 @@ public: // Copy constructor with clone ExternalFunctionItem (ExternalFunctionItem const &other) - : outer_attrs (other.outer_attrs), visibility (other.visibility), - item_name (other.item_name), locus (other.locus), - where_clause (other.where_clause), function_params (other.function_params) + : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs), + visibility (other.visibility), item_name (other.item_name), + locus (other.locus), where_clause (other.where_clause), + function_params (other.function_params) { node_id = other.node_id; // guard to prevent null pointer dereference diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index bcf5b0b5a99..bfdebfc4203 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -722,10 +722,11 @@ private: {} MacroInvocation (const MacroInvocation &other) - : TraitItem (other.locus), outer_attrs (other.outer_attrs), - locus (other.locus), node_id (other.node_id), - invoc_data (other.invoc_data), is_semi_coloned (other.is_semi_coloned), - kind (other.kind), builtin_kind (other.builtin_kind) + : TraitItem (other.locus), ExternalItem (Expr::node_id), + outer_attrs (other.outer_attrs), locus (other.locus), + node_id (other.node_id), invoc_data (other.invoc_data), + is_semi_coloned (other.is_semi_coloned), kind (other.kind), + builtin_kind (other.builtin_kind) { if (other.kind == InvocKind::Builtin) for (auto &pending : other.pending_eager_invocs)