From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id 5A460385E004; Tue, 21 Feb 2023 11:58:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A460385E004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676980717; bh=2RDzPx7lTLmyEh7/eolQTgGWnnS2qCTzNxVn7J6DRGI=; h=From:To:Subject:Date:From; b=GiODniKCEVptgWxIMXEUdMuXzwRwdcqU10lWwvUgj5zI2iXpT1HtHdGY0n0Neh0X9 E0AR+erX1WQcO53lxYrc51mIfqEnHLDfZyssq1jp1tM/1GUzqUSLcgucdXljLIehN1 8ltgQ/Hu0bidgFN8YozWIYIULYARl3onrgsMCrgo= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-6193] gccrs: ast: Refactor ASTFragment -> Fragment class X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/master X-Git-Oldrev: 643ec1c253d9fb3c915824dc3aa1f6b60e8d43f1 X-Git-Newrev: e2647e5e5ad109320339779474659a4f27135c78 Message-Id: <20230221115837.5A460385E004@sourceware.org> Date: Tue, 21 Feb 2023 11:58:37 +0000 (GMT) List-Id: https://gcc.gnu.org/g:e2647e5e5ad109320339779474659a4f27135c78 commit r13-6193-ge2647e5e5ad109320339779474659a4f27135c78 Author: Arthur Cohen Date: Wed Oct 19 13:54:04 2022 +0200 gccrs: ast: Refactor ASTFragment -> Fragment class 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. Diff: --- 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(+) 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