From patchwork Tue Feb 21 12:01:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 59987 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1831485wrn; Tue, 21 Feb 2023 04:24:15 -0800 (PST) X-Google-Smtp-Source: AK7set8NUIadJMW49ZF/4X/gzP1JvHPVBmytDlEWQnxQuqxNsmwIYpTWpx3IySTK/yydcor1UEKZ X-Received: by 2002:a50:fb06:0:b0:4ac:be42:5c66 with SMTP id d6-20020a50fb06000000b004acbe425c66mr4697962edq.11.1676982255807; Tue, 21 Feb 2023 04:24:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676982255; cv=none; d=google.com; s=arc-20160816; b=XsSFLagaY9laEDvhkI0exN0olDDtfBdS6ixTtCEnMwquWvtOduzTSBBagjBgK2Aqyv NqGKKwIRx6NkQsU6UHVO6IEpOFFUE5bRsW8oBeCf0gCxhz3kyIwKhaho2FvIDsoLEVFJ tmfVu1YOHEb/8A1GH2xy2ia550QtabsrWweFfa1I3Q9TmieLerXLvV6WDTJZLSkd8qoX 3ZWBJXzZNg9LiSfiznWAmc+tn1UdELc2Gimsl78i1fu2PSY5H8qwExuQTSLmQ0XVCkOU 6ADM2ftFeOF5yRVuSrVJsRRm2x0y4QJzluCA77ZF5VdAohUQjvX3mSyU9F+qNBsMdOZ8 6VJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dmarc-filter :delivered-to; bh=xEzckGszM+/tMO7iCyDUccsFnmL8QHKThigENVqmI3Q=; b=PsNaHqXSabcEJs7uOPZr5xX/4MVu2Gshl8o5JxQNWeGRSAxij6XtQVM+Wk6Rdi2ekz TFhXENCMN6IHRFAtwatMWazc7vNM6cV97HfhGVQ+LMBZwA1MqcOcoUQflY477ZOuxhwI yhQ9TT3C6CLf/C7aQcsSsYL37rQqIrxq193MVvbfskLSxshYAnGNC6ho1ss0CcjJFuAp JiSKBZtW8ExAhmCTUMAG5igJr916S28dj6rQfNIqyAE77Mc5lkvHy/APdFkKqnmR84Dr LNfwmX/xaVo3XryDCVbaKQFE1CfaEZm2Gw4cSFSZQq5UH88fJ0iPKKkJ/PCc0pj78zYr TM/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=cMJmXM4G; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id v1-20020aa7d9c1000000b004ad7ceba692si15775974eds.20.2023.02.21.04.24.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:24:15 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=cMJmXM4G; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 715BA395B43E for ; Tue, 21 Feb 2023 12:09:53 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id A0631385480B for ; Tue, 21 Feb 2023 12:03:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A0631385480B 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-x435.google.com with SMTP id l25so3758199wrb.3 for ; Tue, 21 Feb 2023 04:03:56 -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=xEzckGszM+/tMO7iCyDUccsFnmL8QHKThigENVqmI3Q=; b=cMJmXM4GJnVdEV1XrzEOrdBkELeJgix0v6kZ1A7acU50WdxKbWuQmPEcOABY9S8Wgj 5vS/oITFS/dZmr2t+XgqZ2CtRJhpsmorq+rbyHJv1mf3Fpi6Lob4dct75mQsiFhTZZB8 ukEkSPL6LoUy+ZbV6PNYjiys/CK8fjcffoG6Yx7HMtNBfS1NcGwlRP6F2cGoPxW9B9oM MVNL8cTLJxDNN6jP90/r0IBHNA4CmzrD8C6JXfw/NnXNtKOCOngGmcaym+uShqWTPeZl m8uBUVU27AYHuwrmu6TkcaTihFeKuQEdqRokM4nwkc0HpDdGKq87G4etKEaiBpG6Vhwd AHzg== 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=xEzckGszM+/tMO7iCyDUccsFnmL8QHKThigENVqmI3Q=; b=AK/yU1Bq8qT6cduYXWL1ugER7xkLaoxJ5sNXBbavcd80siCkZ0P5jMUBCLsocGOVcV OgxIHkRLhk9wtoeB+4mR0BjmtaVQIyvWSsACJ7HxOHfcGUhnAUSp0Z4Z2RV38BdC7i5p nOj6NCnKuwgrRxCpl/9sEyzdLsmwGr9pZ8t+dLAmfB8jMZE3JuINGhGyZvFTnnmgps1s n6tPySb7YIO1V9LY3n504T8cNQDUST0XMhMtOPLUs4wZKzoY3LqnYfsOximcUvoz1jkU Y2FW3kvo27oaNsoRxZkCZSGa3LOlCedDkXV3i2CfTrTv67AYum3J3cJbViQIyZEyVWmQ 3/MA== X-Gm-Message-State: AO0yUKX4cMbqjjWLJGJBLqRRBZQe6fSvihC8/NdFVvPv3pYeFzxHYUgW uSXYzt9BmgNkSEn+RUDl0HtAuXq2+1oxjDQhag== X-Received: by 2002:adf:f883:0:b0:2c7:3f9:7053 with SMTP id u3-20020adff883000000b002c703f97053mr2065114wrp.52.1676981034950; Tue, 21 Feb 2023 04:03:54 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id c15-20020adffb4f000000b002c55b0e6ef1sm5013811wrs.4.2023.02.21.04.03.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:03:54 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [committed 040/103] gccrs: ast: Refactor ASTFragment -> Fragment class Date: Tue, 21 Feb 2023 13:01:30 +0100 Message-Id: <20230221120230.596966-41-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> MIME-Version: 1.0 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, KAM_SHORT, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: arthur.cohen@embecosm.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758443345986274125?= X-GMAIL-MSGID: =?utf-8?q?1758443345986274125?= From: Arthur Cohen gcc/rust/ChangeLog: * Make-lang.in: Add `rust-ast-fragment.o` object file. * ast/rust-ast-fragment.cc: New file. * ast/rust-ast-fragment.h: New file. --- gcc/rust/Make-lang.in | 1 + gcc/rust/ast/rust-ast-fragment.cc | 168 ++++++++++++++++++++++++++++++ gcc/rust/ast/rust-ast-fragment.h | 104 ++++++++++++++++++ 3 files changed, 273 insertions(+) create mode 100644 gcc/rust/ast/rust-ast-fragment.cc create mode 100644 gcc/rust/ast/rust-ast-fragment.h diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index a906516bd33..c5960530d0a 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -74,6 +74,7 @@ GRS_OBJS = \ rust/rust-cfg-parser.o \ rust/rust-parse.o \ rust/rust-ast-full-test.o \ + rust/rust-ast-fragment.o \ rust/rust-ast-dump.o \ rust/rust-hir-dump.o \ rust/rust-session-manager.o \ diff --git a/gcc/rust/ast/rust-ast-fragment.cc b/gcc/rust/ast/rust-ast-fragment.cc new file mode 100644 index 00000000000..1a2dd99835a --- /dev/null +++ b/gcc/rust/ast/rust-ast-fragment.cc @@ -0,0 +1,168 @@ +// 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-ast-fragment.h" + +namespace Rust { +namespace AST { + +Fragment::Fragment (std::vector nodes, bool fragment_is_error) + : kind (fragment_is_error ? FragmentKind::Error : FragmentKind::Complete), + nodes (std::move (nodes)) +{ + if (fragment_is_error) + rust_assert (nodes.empty ()); +} + +Fragment::Fragment (Fragment const &other) : kind (other.get_kind ()) +{ + nodes.clear (); + nodes.reserve (other.nodes.size ()); + for (auto &n : other.nodes) + { + nodes.push_back (n); + } +} + +Fragment & +Fragment::operator= (Fragment const &other) +{ + nodes.clear (); + nodes.reserve (other.nodes.size ()); + kind = other.get_kind (); + for (auto &n : other.nodes) + { + nodes.push_back (n); + } + + return *this; +} + +Fragment +Fragment::create_error () +{ + return Fragment ({}, true); +} + +std::vector & +Fragment::get_nodes () +{ + return nodes; +} + +FragmentKind +Fragment::get_kind () const +{ + return kind; +} + +bool +Fragment::is_error () const +{ + return get_kind () == FragmentKind::Error; +} + +bool +Fragment::should_expand () const +{ + return !is_error (); +} + +bool +Fragment::is_expression_fragment () const +{ + return is_single_fragment_of_kind (SingleASTNode::NodeType::EXPRESSION); +} + +bool +Fragment::is_type_fragment () const +{ + return is_single_fragment_of_kind (SingleASTNode::NodeType::TYPE); +} + +std::unique_ptr +Fragment::take_expression_fragment () +{ + assert_single_fragment (SingleASTNode::NodeType::EXPRESSION); + return nodes[0].take_expr (); +} + +std::unique_ptr +Fragment::take_type_fragment () +{ + assert_single_fragment (SingleASTNode::NodeType::TYPE); + return nodes[0].take_type (); +} + +void +Fragment::accept_vis (ASTVisitor &vis) +{ + for (auto &node : nodes) + node.accept_vis (vis); +} + +bool +Fragment::is_single_fragment () const +{ + return nodes.size () == 1; +} + +bool +Fragment::is_single_fragment_of_kind (SingleASTNode::NodeType expected) const +{ + return is_single_fragment () && nodes[0].get_kind () == expected; +} + +void +Fragment::assert_single_fragment (SingleASTNode::NodeType expected) const +{ + static const std::map str_map = { + {SingleASTNode::NodeType::IMPL, "impl"}, + {SingleASTNode::NodeType::ITEM, "item"}, + {SingleASTNode::NodeType::TYPE, "type"}, + {SingleASTNode::NodeType::EXPRESSION, "expr"}, + {SingleASTNode::NodeType::STMT, "stmt"}, + {SingleASTNode::NodeType::EXTERN, "extern"}, + {SingleASTNode::NodeType::TRAIT, "trait"}, + {SingleASTNode::NodeType::TRAIT_IMPL, "trait impl"}, + }; + + auto actual = nodes[0].get_kind (); + auto fail = false; + + if (!is_single_fragment ()) + { + rust_error_at (Location (), "fragment is not single"); + fail = true; + } + + if (actual != expected) + { + rust_error_at ( + Location (), + "invalid fragment operation: expected %qs node, got %qs node", + str_map.find (expected)->second, + str_map.find (nodes[0].get_kind ())->second); + fail = true; + } + + rust_assert (!fail); +} + +} // namespace AST +} // namespace Rust diff --git a/gcc/rust/ast/rust-ast-fragment.h b/gcc/rust/ast/rust-ast-fragment.h new file mode 100644 index 00000000000..ee6ab0d8433 --- /dev/null +++ b/gcc/rust/ast/rust-ast-fragment.h @@ -0,0 +1,104 @@ +// 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_AST_FRAGMENT_H +#define RUST_AST_FRAGMENT_H + +#include "rust-ast.h" +#include "rust-system.h" + +namespace Rust { +namespace AST { + +enum class FragmentKind +{ + /** + * If an AST Fragment still contains unexpanded tokens - this should only be + * used in the case of builtin macros which need to be expanded eagerly. + */ + Unexpanded, + /** + * A completely expanded AST Fragment. This signifies that all + * `SingleASTNode`s in the `nodes` vector are valid. + * + * Note that this doesn't imply that the expansion is "done". One of the + * expanded nodes could very well be another macro invocation + */ + Complete, + /** + * An error fragment. + */ + Error, +}; + +/** + * An AST Fragment. Previously named `ASTFragment`. + * + * Basically, a "fragment" that can be incorporated into the AST, created as + * a result of macro expansion. Really annoying to work with due to the fact + * that macros can really expand to anything. As such, horrible representation + * at the moment. + */ +class Fragment +{ +public: + Fragment (std::vector nodes, bool fragment_is_error = false); + Fragment (Fragment const &other); + static Fragment create_error (); + + Fragment &operator= (Fragment const &other); + + FragmentKind get_kind () const; + std::vector &get_nodes (); + + bool is_error () const; + bool should_expand () const; + + bool is_expression_fragment () const; + bool is_type_fragment () const; + + std::unique_ptr take_expression_fragment (); + std::unique_ptr take_type_fragment (); + + void accept_vis (ASTVisitor &vis); + +private: + FragmentKind kind; + + /** + * Basic idea: essentially, a vector of tagged unions of different AST node + * types. Now, this could actually be stored without a tagged union if the + * different AST node types had a unified parent, but that would create + * issues with the diamond problem or significant performance penalties. So + * a tagged union had to be used instead. A vector is used to represent the + * ability for a macro to expand to two statements, for instance. + */ + std::vector nodes; + + /** + * We need to make a special case for Expression and Type fragments as only + * one Node will be extracted from the `nodes` vector + */ + bool is_single_fragment () const; + bool is_single_fragment_of_kind (SingleASTNode::NodeType expected) const; + void assert_single_fragment (SingleASTNode::NodeType expected) const; +}; +} // namespace AST +} // namespace Rust + +#endif // !RUST_AST_FRAGMENT_H