From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id E8A923841811 for ; Tue, 21 Feb 2023 12:03:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E8A923841811 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-x432.google.com with SMTP id c5so4849619wrr.5 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=+QPeXja+bzOj/3nJzTHzFzkaFHCfZRSyNHb0lgsAQ0k=; b=E06iUWT5egYIsF5PBQsF3FUj+dlHCseLfQk3QLdX+zGOaGBYhbOj47L8wHNNvhvO9r zo740QpNogNbY+oWjLIJmOO68QyQpAosJRr+AyrC4ehwEBP3A/QfFzwZbDdrMSeyk2pR r6zHaxevhR0sQhY3q5I9+EOXpGBV4rGh4sBc86Zq+Sb7WSLb32gRRLvD084i2m051bOh lvttVPWBj1cGI8EmcMNDjgM/oyo0l+g43i1Bxo8+rthS2JZtPl/d5h1fiBIty/ZzQFOY 0jC+iTv2+a+zpQhrC8fr+NSvo3qpSsw84G+gWabFWrqj0bQm7Engpneh6OMCL7tgzqLM QhIA== 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=+QPeXja+bzOj/3nJzTHzFzkaFHCfZRSyNHb0lgsAQ0k=; b=rO2xzTWEJ7jpz9b3RLNeaJxQh2hgT5Mho8R1st1CFtEhNn3BI3QsOVqf7hTQ69FrFl 5/nY6dV6G5oisr26gQQxP3cESD6ZN/OU6KEzrxCTSj7VlbSIkx8Aknv50DSxh1OPyeM3 7ActcnFP1/lajUUikQ+PorSE7TWBbV0Cytydsa8cyePP/PpEOulPAk/fmaEZHdMbMsWE JIEpFRwYomQtilQEn+fuSkwonOyCVEEOrgn2lfjGhIRIcTmmkvIc1kUQLHaHiUmOtp6M 7+3/NVHY8ltvzmSfbmENvQngn6QoNP6KNkhOTzcTa/QPnh+1034WWIQF+yPZKyRuAR/0 i9qA== X-Gm-Message-State: AO0yUKUMLNNDY01Q8JdmU2b2/0VqgNs8ZS+hF5Cb+CDRu13eLua1JIzl VQz1OSVLwhmETUOy5hWLWp0b X-Google-Smtp-Source: AK7set9hHm8LyLqvsHM0Bk12mtt03qMIZbkRckOTpmh97jMYLJAFLdwNgAJQTWhHb8V637ce7B1FAQ== X-Received: by 2002:a5d:62ce:0:b0:2c6:d913:58a9 with SMTP id o14-20020a5d62ce000000b002c6d91358a9mr3471152wrv.39.1676981036245; Tue, 21 Feb 2023 04:03:56 -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.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:03:55 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [committed 041/103] gccrs: rust: Replace uses of ASTFragment -> Fragment Date: Tue, 21 Feb 2023 13:01:31 +0100 Message-Id: <20230221120230.596966-42-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=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: Arthur Cohen gcc/rust/ChangeLog: * ast/rust-ast.h (class ASTFragment): Remove old ASTFragment class. * ast/rust-macro.h (class MacroRulesDefinition): Use new Fragment API. * expand/rust-attribute-visitor.h: Likewise. * expand/rust-macro-builtins.cc (macro_end_token): Likewise. (MacroBuiltin::assert): Likewise. (MacroBuiltin::file): Likewise. (MacroBuiltin::column): Likewise. (MacroBuiltin::include_bytes): Likewise. (MacroBuiltin::include_str): Likewise. (MacroBuiltin::compile_error): Likewise. (MacroBuiltin::concat): Likewise. (MacroBuiltin::env): Likewise. (MacroBuiltin::cfg): Likewise. (MacroBuiltin::include): Likewise. (MacroBuiltin::line): Likewise. * expand/rust-macro-builtins.h: Likewise. * expand/rust-macro-expand.cc (MacroExpander::expand_decl_macro): Likewise. (MacroExpander::expand_invoc): Likewise. (MacroExpander::match_repetition): Likewise. (parse_many): Likewise. (transcribe_many_items): Likewise. (transcribe_many_ext): Likewise. (transcribe_many_trait_items): Likewise. (transcribe_many_impl_items): Likewise. (transcribe_many_trait_impl_items): Likewise. (transcribe_expression): Likewise. (transcribe_type): Likewise. (transcribe_on_delimiter): Likewise. (tokens_to_str): Likewise. * expand/rust-macro-expand.h (struct MacroExpander): Likewise. * util/rust-hir-map.cc (Mappings::insert_macro_def): Likewise. --- gcc/rust/ast/rust-ast.h | 132 ----------------------- gcc/rust/ast/rust-macro.h | 18 ++-- gcc/rust/expand/rust-attribute-visitor.h | 4 +- gcc/rust/expand/rust-macro-builtins.cc | 72 ++++++------- gcc/rust/expand/rust-macro-builtins.h | 40 ++++--- gcc/rust/expand/rust-macro-expand.cc | 42 ++++---- gcc/rust/expand/rust-macro-expand.h | 23 ++-- gcc/rust/util/rust-hir-map.cc | 4 +- 8 files changed, 100 insertions(+), 235 deletions(-) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index ccabccd6aff..3d602b1a379 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -1858,138 +1858,6 @@ public: } }; -/* 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 ASTFragment -{ -private: - /* 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; - bool fragment_is_error; - - /** - * 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 { return nodes.size () == 1; } - - bool is_single_fragment_of_kind (SingleASTNode::NodeType expected) const - { - return is_single_fragment () && nodes[0].get_kind () == expected; - } - - void 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); - } - -public: - ASTFragment (std::vector nodes, bool fragment_is_error = false) - : nodes (std::move (nodes)), fragment_is_error (fragment_is_error) - { - if (fragment_is_error) - rust_assert (nodes.empty ()); - } - - ASTFragment (ASTFragment const &other) - : fragment_is_error (other.fragment_is_error) - { - nodes.clear (); - nodes.reserve (other.nodes.size ()); - for (auto &n : other.nodes) - { - nodes.push_back (n); - } - } - - ASTFragment &operator= (ASTFragment const &other) - { - fragment_is_error = other.fragment_is_error; - nodes.clear (); - nodes.reserve (other.nodes.size ()); - for (auto &n : other.nodes) - { - nodes.push_back (n); - } - - return *this; - } - - static ASTFragment create_error () { return ASTFragment ({}, true); } - - std::vector &get_nodes () { return nodes; } - bool is_error () const { return fragment_is_error; } - - bool should_expand () const { return !is_error (); } - - bool is_expression_fragment () const - { - return is_single_fragment_of_kind (SingleASTNode::NodeType::EXPRESSION); - } - - bool is_type_fragment () const - { - return is_single_fragment_of_kind (SingleASTNode::NodeType::TYPE); - } - - std::unique_ptr take_expression_fragment () - { - assert_single_fragment (SingleASTNode::NodeType::EXPRESSION); - return nodes[0].take_expr (); - } - - std::unique_ptr take_type_fragment () - { - assert_single_fragment (SingleASTNode::NodeType::TYPE); - return nodes[0].take_type (); - } - - void accept_vis (ASTVisitor &vis) - { - for (auto &node : nodes) - node.accept_vis (vis); - } -}; - // A crate AST object - holds all the data for a single compilation unit struct Crate { diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index 798bc974fbb..fc4b5b82fb5 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -21,6 +21,7 @@ #include "rust-system.h" #include "rust-ast.h" +#include "rust-ast-fragment.h" #include "rust-location.h" namespace Rust { @@ -456,8 +457,7 @@ class MacroRulesDefinition : public MacroItem std::vector rules; // inlined form Location locus; - std::function - associated_transcriber; + std::function associated_transcriber; // Since we can't compare std::functions, we need to use an extra boolean bool is_builtin_rule; @@ -468,10 +468,10 @@ class MacroRulesDefinition : public MacroItem * should make use of the actual rules. If the macro is builtin, then another * associated transcriber should be used */ - static ASTFragment dummy_builtin (Location, MacroInvocData &) + static Fragment dummy_builtin (Location, MacroInvocData &) { gcc_unreachable (); - return ASTFragment::create_error (); + return Fragment::create_error (); } /* NOTE: in rustc, macro definitions are considered (and parsed as) a type @@ -491,9 +491,9 @@ public: associated_transcriber (dummy_builtin), is_builtin_rule (false) {} - MacroRulesDefinition (Identifier builtin_name, DelimType delim_type, - std::function - associated_transcriber) + MacroRulesDefinition ( + Identifier builtin_name, DelimType delim_type, + std::function associated_transcriber) : outer_attrs (std::vector ()), rule_name (builtin_name), delim_type (delim_type), rules (std::vector ()), locus (Location ()), associated_transcriber (associated_transcriber), @@ -521,14 +521,14 @@ public: const std::vector &get_rules () const { return rules; } bool is_builtin () const { return is_builtin_rule; } - const std::function & + const std::function & get_builtin_transcriber () const { rust_assert (is_builtin ()); return associated_transcriber; } void set_builtin_transcriber ( - std::function transcriber) + std::function transcriber) { associated_transcriber = transcriber; is_builtin_rule = true; diff --git a/gcc/rust/expand/rust-attribute-visitor.h b/gcc/rust/expand/rust-attribute-visitor.h index 80f9d4b4ded..6b562bd49b1 100644 --- a/gcc/rust/expand/rust-attribute-visitor.h +++ b/gcc/rust/expand/rust-attribute-visitor.h @@ -56,11 +56,11 @@ public: * @return Either the expanded fragment or an empty errored-out fragment * indicating an expansion failure. */ - AST::ASTFragment expand_macro_fragment_recursive () + AST::Fragment expand_macro_fragment_recursive () { auto fragment = expander.take_expanded_fragment (*this); unsigned int original_depth = expander.expansion_depth; - auto final_fragment = AST::ASTFragment ({}, true); + auto final_fragment = AST::Fragment ({}, true); while (fragment.should_expand ()) { diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc index a230ad9f2d8..2a8a3f752f4 100644 --- a/gcc/rust/expand/rust-macro-builtins.cc +++ b/gcc/rust/expand/rust-macro-builtins.cc @@ -66,7 +66,7 @@ macro_end_token (AST::DelimTokenTree &invoc_token_tree, /* Expand and extract an expression from the macro */ -static inline AST::ASTFragment +static inline AST::Fragment try_expand_macro_expression (AST::Expr *expr, MacroExpander *expander) { rust_assert (expander); @@ -264,25 +264,25 @@ load_file_bytes (const char *filename) } } // namespace -AST::ASTFragment +AST::Fragment MacroBuiltin::assert (Location invoc_locus, AST::MacroInvocData &invoc) { rust_debug ("assert!() called"); - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); } -AST::ASTFragment +AST::Fragment MacroBuiltin::file (Location invoc_locus, AST::MacroInvocData &invoc) { auto current_file = Session::get_instance ().linemap->location_file (invoc_locus); auto file_str = AST::SingleASTNode (make_string (invoc_locus, current_file)); - return AST::ASTFragment ({file_str}); + return AST::Fragment ({file_str}); } -AST::ASTFragment +AST::Fragment MacroBuiltin::column (Location invoc_locus, AST::MacroInvocData &invoc) { auto current_column @@ -292,14 +292,14 @@ MacroBuiltin::column (Location invoc_locus, AST::MacroInvocData &invoc) new AST::LiteralExpr (std::to_string (current_column), AST::Literal::INT, PrimitiveCoreType::CORETYPE_U32, {}, invoc_locus))); - return AST::ASTFragment ({column_no}); + return AST::Fragment ({column_no}); } /* Expand builtin macro include_bytes!("filename"), which includes the contents of the given file as reference to a byte array. Yields an expression of type &'static [u8; N]. */ -AST::ASTFragment +AST::Fragment MacroBuiltin::include_bytes (Location invoc_locus, AST::MacroInvocData &invoc) { /* Get target filename from the macro invocation, which is treated as a path @@ -308,7 +308,7 @@ MacroBuiltin::include_bytes (Location invoc_locus, AST::MacroInvocData &invoc) = parse_single_string_literal (invoc.get_delim_tok_tree (), invoc_locus, invoc.get_expander ()); if (lit_expr == nullptr) - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); std::string target_filename = source_relative_path (lit_expr->as_string (), invoc_locus); @@ -335,14 +335,14 @@ MacroBuiltin::include_bytes (Location invoc_locus, AST::MacroInvocData &invoc) new AST::BorrowExpr (std::move (array), false, false, {}, invoc_locus)); auto node = AST::SingleASTNode (std::move (borrow)); - return AST::ASTFragment ({node}); + return AST::Fragment ({node}); } /* Expand builtin macro include_str!("filename"), which includes the contents of the given file as a string. The file must be UTF-8 encoded. Yields an expression of type &'static str. */ -AST::ASTFragment +AST::Fragment MacroBuiltin::include_str (Location invoc_locus, AST::MacroInvocData &invoc) { /* Get target filename from the macro invocation, which is treated as a path @@ -351,7 +351,7 @@ MacroBuiltin::include_str (Location invoc_locus, AST::MacroInvocData &invoc) = parse_single_string_literal (invoc.get_delim_tok_tree (), invoc_locus, invoc.get_expander ()); if (lit_expr == nullptr) - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); std::string target_filename = source_relative_path (lit_expr->as_string (), invoc_locus); @@ -362,30 +362,30 @@ MacroBuiltin::include_str (Location invoc_locus, AST::MacroInvocData &invoc) std::string str ((const char *) &bytes[0], bytes.size ()); auto node = AST::SingleASTNode (make_string (invoc_locus, str)); - return AST::ASTFragment ({node}); + return AST::Fragment ({node}); } /* Expand builtin macro compile_error!("error"), which forces a compile error during the compile time. */ -AST::ASTFragment +AST::Fragment MacroBuiltin::compile_error (Location invoc_locus, AST::MacroInvocData &invoc) { auto lit_expr = parse_single_string_literal (invoc.get_delim_tok_tree (), invoc_locus, invoc.get_expander ()); if (lit_expr == nullptr) - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); std::string error_string = lit_expr->as_string (); rust_error_at (invoc_locus, "%s", error_string.c_str ()); - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); } /* Expand builtin macro concat!(), which joins all the literal parameters into a string with no delimiter. */ -AST::ASTFragment +AST::Fragment MacroBuiltin::concat (Location invoc_locus, AST::MacroInvocData &invoc) { auto invoc_token_tree = invoc.get_delim_tok_tree (); @@ -427,16 +427,16 @@ MacroBuiltin::concat (Location invoc_locus, AST::MacroInvocData &invoc) parser.skip_token (last_token_id); if (has_error) - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); auto node = AST::SingleASTNode (make_string (invoc_locus, str)); - return AST::ASTFragment ({node}); + return AST::Fragment ({node}); } /* Expand builtin macro env!(), which inspects an environment variable at compile time. */ -AST::ASTFragment +AST::Fragment MacroBuiltin::env (Location invoc_locus, AST::MacroInvocData &invoc) { auto invoc_token_tree = invoc.get_delim_tok_tree (); @@ -451,11 +451,11 @@ MacroBuiltin::env (Location invoc_locus, AST::MacroInvocData &invoc) auto expanded_expr = try_expand_many_expr (parser, invoc_locus, last_token_id, invoc.get_expander (), has_error); if (has_error) - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); if (expanded_expr.size () < 1 || expanded_expr.size () > 2) { rust_error_at (invoc_locus, "env! takes 1 or 2 arguments"); - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); } if (expanded_expr.size () > 0) { @@ -463,7 +463,7 @@ MacroBuiltin::env (Location invoc_locus, AST::MacroInvocData &invoc) = try_extract_string_literal_from_fragment (invoc_locus, expanded_expr[0]))) { - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); } } if (expanded_expr.size () > 1) @@ -472,7 +472,7 @@ MacroBuiltin::env (Location invoc_locus, AST::MacroInvocData &invoc) = try_extract_string_literal_from_fragment (invoc_locus, expanded_expr[1]))) { - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); } } @@ -487,14 +487,14 @@ MacroBuiltin::env (Location invoc_locus, AST::MacroInvocData &invoc) lit_expr->as_string ().c_str ()); else rust_error_at (invoc_locus, "%s", error_expr->as_string ().c_str ()); - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); } auto node = AST::SingleASTNode (make_string (invoc_locus, env_value)); - return AST::ASTFragment ({node}); + return AST::Fragment ({node}); } -AST::ASTFragment +AST::Fragment MacroBuiltin::cfg (Location invoc_locus, AST::MacroInvocData &invoc) { // only parse if not already parsed @@ -519,7 +519,7 @@ MacroBuiltin::cfg (Location invoc_locus, AST::MacroInvocData &invoc) /* TODO: assuming that cfg! macros can only have one meta item inner, like cfg * attributes */ if (invoc.get_meta_items ().size () != 1) - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); bool result = invoc.get_meta_items ()[0]->check_cfg_predicate ( Session::get_instance ()); @@ -527,13 +527,13 @@ MacroBuiltin::cfg (Location invoc_locus, AST::MacroInvocData &invoc) new AST::LiteralExpr (result ? "true" : "false", AST::Literal::BOOL, PrimitiveCoreType::CORETYPE_BOOL, {}, invoc_locus))); - return AST::ASTFragment ({literal_exp}); + return AST::Fragment ({literal_exp}); } /* Expand builtin macro include!(), which includes a source file at the current scope compile time. */ -AST::ASTFragment +AST::Fragment MacroBuiltin::include (Location invoc_locus, AST::MacroInvocData &invoc) { /* Get target filename from the macro invocation, which is treated as a path @@ -542,7 +542,7 @@ MacroBuiltin::include (Location invoc_locus, AST::MacroInvocData &invoc) = parse_single_string_literal (invoc.get_delim_tok_tree (), invoc_locus, invoc.get_expander ()); if (lit_expr == nullptr) - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); std::string filename = source_relative_path (lit_expr->as_string (), invoc_locus); @@ -556,7 +556,7 @@ MacroBuiltin::include (Location invoc_locus, AST::MacroInvocData &invoc) { rust_error_at (lit_expr->get_locus (), "cannot open included file %qs: %m", target_filename); - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); } rust_debug ("Attempting to parse included file %s", target_filename); @@ -574,7 +574,7 @@ MacroBuiltin::include (Location invoc_locus, AST::MacroInvocData &invoc) { // inform the user that the errors above are from a included file rust_inform (invoc_locus, "included from here"); - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); } std::vector nodes{}; @@ -584,10 +584,10 @@ MacroBuiltin::include (Location invoc_locus, AST::MacroInvocData &invoc) nodes.push_back (node); } - return AST::ASTFragment (nodes); + return AST::Fragment (nodes); } -AST::ASTFragment +AST::Fragment MacroBuiltin::line (Location invoc_locus, AST::MacroInvocData &invoc) { auto current_line @@ -597,7 +597,7 @@ MacroBuiltin::line (Location invoc_locus, AST::MacroInvocData &invoc) new AST::LiteralExpr (std::to_string (current_line), AST::Literal::INT, PrimitiveCoreType::CORETYPE_U32, {}, invoc_locus))); - return AST::ASTFragment ({line_no}); + return AST::Fragment ({line_no}); } } // namespace Rust diff --git a/gcc/rust/expand/rust-macro-builtins.h b/gcc/rust/expand/rust-macro-builtins.h index f1ea545ed21..c65bd940189 100644 --- a/gcc/rust/expand/rust-macro-builtins.h +++ b/gcc/rust/expand/rust-macro-builtins.h @@ -20,6 +20,7 @@ #define RUST_MACRO_BUILTINS_H #include "rust-ast.h" +#include "rust-ast-fragment.h" #include "rust-location.h" /** @@ -61,6 +62,7 @@ /* If assert is defined as a macro this file will not parse, so undefine this before continuing. */ +// TODO: Rename all functions here `*_handler` #ifdef assert #undef assert #endif @@ -69,38 +71,34 @@ namespace Rust { class MacroBuiltin { public: - static AST::ASTFragment assert (Location invoc_locus, - AST::MacroInvocData &invoc); - - static AST::ASTFragment file (Location invoc_locus, - AST::MacroInvocData &invoc); + static AST::Fragment assert (Location invoc_locus, + AST::MacroInvocData &invoc); - static AST::ASTFragment column (Location invoc_locus, - AST::MacroInvocData &invoc); + static AST::Fragment file (Location invoc_locus, AST::MacroInvocData &invoc); - static AST::ASTFragment include_bytes (Location invoc_locus, - AST::MacroInvocData &invoc); + static AST::Fragment column (Location invoc_locus, + AST::MacroInvocData &invoc); - static AST::ASTFragment include_str (Location invoc_locus, - AST::MacroInvocData &invoc); + static AST::Fragment include_bytes (Location invoc_locus, + AST::MacroInvocData &invoc); - static AST::ASTFragment compile_error (Location invoc_locus, - AST::MacroInvocData &invoc); + static AST::Fragment include_str (Location invoc_locus, + AST::MacroInvocData &invoc); - static AST::ASTFragment concat (Location invoc_locus, - AST::MacroInvocData &invoc); + static AST::Fragment compile_error (Location invoc_locus, + AST::MacroInvocData &invoc); - static AST::ASTFragment env (Location invoc_locus, + static AST::Fragment concat (Location invoc_locus, AST::MacroInvocData &invoc); - static AST::ASTFragment cfg (Location invoc_locus, - AST::MacroInvocData &invoc); + static AST::Fragment env (Location invoc_locus, AST::MacroInvocData &invoc); - static AST::ASTFragment include (Location invoc_locus, - AST::MacroInvocData &invoc); + static AST::Fragment cfg (Location invoc_locus, AST::MacroInvocData &invoc); - static AST::ASTFragment line (Location invoc_locus, + static AST::Fragment include (Location invoc_locus, AST::MacroInvocData &invoc); + + static AST::Fragment line (Location invoc_locus, AST::MacroInvocData &invoc); }; } // namespace Rust diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index d94cd118700..0684a28787f 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -26,7 +26,7 @@ #include "rust-early-name-resolver.h" namespace Rust { -AST::ASTFragment +AST::Fragment MacroExpander::expand_decl_macro (Location invoc_locus, AST::MacroInvocData &invoc, AST::MacroRulesDefinition &rules_def, @@ -103,7 +103,7 @@ MacroExpander::expand_decl_macro (Location invoc_locus, RichLocation r (invoc_locus); r.add_range (rules_def.get_locus ()); rust_error_at (r, "Failed to match any rule within macro"); - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); } return transcribe_rule (*matched_rule, invoc_token_tree, matched_fragments, @@ -139,7 +139,7 @@ MacroExpander::expand_invoc (AST::MacroInvocation &invoc, bool has_semicolon) // - else is unreachable // - derive container macro - unreachable - auto fragment = AST::ASTFragment::create_error (); + auto fragment = AST::Fragment::create_error (); invoc_data.set_expander (this); // lookup the rules @@ -707,7 +707,7 @@ MacroExpander::match_repetition (Parser &parser, /** * Helper function to refactor calling a parsing function 0 or more times */ -static AST::ASTFragment +static AST::Fragment parse_many (Parser &parser, TokenId &delimiter, std::function parse_fn) { @@ -723,13 +723,13 @@ parse_many (Parser &parser, TokenId &delimiter, for (auto err : parser.get_errors ()) err.emit_error (); - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); } nodes.emplace_back (std::move (node)); } - return AST::ASTFragment (std::move (nodes)); + return AST::Fragment (std::move (nodes)); } /** @@ -738,7 +738,7 @@ parse_many (Parser &parser, TokenId &delimiter, * @param parser Parser to extract items from * @param delimiter Id of the token on which parsing should stop */ -static AST::ASTFragment +static AST::Fragment transcribe_many_items (Parser &parser, TokenId &delimiter) { return parse_many (parser, delimiter, [&parser] () { @@ -753,7 +753,7 @@ transcribe_many_items (Parser &parser, TokenId &delimiter) * @param parser Parser to extract items from * @param delimiter Id of the token on which parsing should stop */ -static AST::ASTFragment +static AST::Fragment transcribe_many_ext (Parser &parser, TokenId &delimiter) { return parse_many (parser, delimiter, [&parser] () { @@ -768,7 +768,7 @@ transcribe_many_ext (Parser &parser, TokenId &delimiter) * @param parser Parser to extract items from * @param delimiter Id of the token on which parsing should stop */ -static AST::ASTFragment +static AST::Fragment transcribe_many_trait_items (Parser &parser, TokenId &delimiter) { @@ -784,7 +784,7 @@ transcribe_many_trait_items (Parser &parser, * @param parser Parser to extract items from * @param delimiter Id of the token on which parsing should stop */ -static AST::ASTFragment +static AST::Fragment transcribe_many_impl_items (Parser &parser, TokenId &delimiter) { return parse_many (parser, delimiter, [&parser] () { @@ -799,7 +799,7 @@ transcribe_many_impl_items (Parser &parser, TokenId &delimiter) * @param parser Parser to extract items from * @param delimiter Id of the token on which parsing should stop */ -static AST::ASTFragment +static AST::Fragment transcribe_many_trait_impl_items (Parser &parser, TokenId &delimiter) { @@ -815,7 +815,7 @@ transcribe_many_trait_impl_items (Parser &parser, * @param parser Parser to extract statements from * @param delimiter Id of the token on which parsing should stop */ -static AST::ASTFragment +static AST::Fragment transcribe_many_stmts (Parser &parser, TokenId &delimiter) { auto restrictions = ParseRestrictions (); @@ -835,12 +835,12 @@ transcribe_many_stmts (Parser &parser, TokenId &delimiter) * * @param parser Parser to extract statements from */ -static AST::ASTFragment +static AST::Fragment transcribe_expression (Parser &parser) { auto expr = parser.parse_expr (); - return AST::ASTFragment ({std::move (expr)}); + return AST::Fragment ({std::move (expr)}); } /** @@ -848,17 +848,17 @@ transcribe_expression (Parser &parser) * * @param parser Parser to extract statements from */ -static AST::ASTFragment +static AST::Fragment transcribe_type (Parser &parser) { auto type = parser.parse_type (true); for (auto err : parser.get_errors ()) err.emit_error (); - return AST::ASTFragment ({std::move (type)}); + return AST::Fragment ({std::move (type)}); } -static AST::ASTFragment +static AST::Fragment transcribe_on_delimiter (Parser &parser, bool semicolon, AST::DelimType delimiter, TokenId last_token_id) { @@ -868,7 +868,7 @@ transcribe_on_delimiter (Parser &parser, bool semicolon, return transcribe_expression (parser); } // namespace Rust -static AST::ASTFragment +static AST::Fragment transcribe_context (MacroExpander::ContextType ctx, Parser &parser, bool semicolon, AST::DelimType delimiter, TokenId last_token_id) @@ -929,7 +929,7 @@ tokens_to_str (std::vector> &tokens) return str; } -AST::ASTFragment +AST::Fragment MacroExpander::transcribe_rule ( AST::MacroRule &match_rule, AST::DelimTokenTree &invoc_token_tree, std::map &matched_fragments, @@ -951,7 +951,7 @@ MacroExpander::transcribe_rule ( rust_debug ("substituted tokens: %s", tokens_to_str (substituted_tokens).c_str ()); - // parse it to an ASTFragment + // parse it to an Fragment MacroInvocLexer lex (std::move (substituted_tokens)); Parser parser (lex); @@ -994,7 +994,7 @@ MacroExpander::transcribe_rule ( { for (auto &err : parser.get_errors ()) rust_error_at (err.locus, "%s", err.message.c_str ()); - return AST::ASTFragment::create_error (); + return AST::Fragment::create_error (); } // are all the tokens used? diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index 187d79f9a28..b0c0c23a0c3 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -230,7 +230,7 @@ struct MacroExpander MacroExpander (AST::Crate &crate, ExpansionCfg cfg, Session &session) : cfg (cfg), crate (crate), session (session), sub_stack (SubstitutionScope ()), - expanded_fragment (AST::ASTFragment::create_error ()), + expanded_fragment (AST::Fragment::create_error ()), resolver (Resolver::Resolver::get ()), mappings (Analysis::Mappings::get ()) {} @@ -246,10 +246,9 @@ struct MacroExpander void expand_invoc (AST::MacroInvocation &invoc, bool has_semicolon); // Expands a single declarative macro. - AST::ASTFragment expand_decl_macro (Location locus, - AST::MacroInvocData &invoc, - AST::MacroRulesDefinition &rules_def, - bool semicolon); + AST::Fragment expand_decl_macro (Location locus, AST::MacroInvocData &invoc, + AST::MacroRulesDefinition &rules_def, + bool semicolon); void expand_cfg_attrs (AST::AttrVec &attrs); bool fails_cfg (const AST::AttrVec &attr) const; @@ -260,7 +259,7 @@ struct MacroExpander bool try_match_rule (AST::MacroRule &match_rule, AST::DelimTokenTree &invoc_token_tree); - AST::ASTFragment transcribe_rule ( + AST::Fragment transcribe_rule ( AST::MacroRule &match_rule, AST::DelimTokenTree &invoc_token_tree, std::map &matched_fragments, bool semicolon, ContextType ctx); @@ -314,16 +313,16 @@ struct MacroExpander ContextType peek_context () { return context.back (); } - void set_expanded_fragment (AST::ASTFragment &&fragment) + void set_expanded_fragment (AST::Fragment &&fragment) { expanded_fragment = std::move (fragment); } - AST::ASTFragment take_expanded_fragment (AST::ASTVisitor &vis) + AST::Fragment take_expanded_fragment (AST::ASTVisitor &vis) { - AST::ASTFragment old_fragment = std::move (expanded_fragment); + AST::Fragment old_fragment = std::move (expanded_fragment); auto accumulator = std::vector (); - expanded_fragment = AST::ASTFragment::create_error (); + expanded_fragment = AST::Fragment::create_error (); auto early_name_resolver = Resolver::EarlyNameResolver (); for (auto &node : old_fragment.get_nodes ()) @@ -345,7 +344,7 @@ struct MacroExpander auto new_nodes = expanded_fragment.get_nodes (); std::move (new_nodes.begin (), new_nodes.end (), std::back_inserter (accumulator)); - expanded_fragment = AST::ASTFragment (accumulator); + expanded_fragment = AST::Fragment (accumulator); } expansion_depth--; } @@ -358,7 +357,7 @@ private: Session &session; SubstitutionScope sub_stack; std::vector context; - AST::ASTFragment expanded_fragment; + AST::Fragment expanded_fragment; public: Resolver::Resolver *resolver; diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 4aeb7f5ae18..bee0682e9b4 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -840,8 +840,8 @@ Mappings::iterate_trait_items ( void Mappings::insert_macro_def (AST::MacroRulesDefinition *macro) { - static std::map> + static std::map< + std::string, std::function> builtin_macros = { {"assert", MacroBuiltin::assert}, {"file", MacroBuiltin::file}, -- 2.39.1