From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id A410A381667C; Wed, 8 Jun 2022 12:14:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A410A381667C Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] macroinvocation: Only allow *stmt* visitors when semicoloned X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 12d156566af84ec834abb7e18feac6e8f5884451 X-Git-Newrev: 58d1721529e99c7c633615e7491b777a6198ed00 Message-Id: <20220608121400.A410A381667C@sourceware.org> Date: Wed, 8 Jun 2022 12:14:00 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:14:00 -0000 https://gcc.gnu.org/g:58d1721529e99c7c633615e7491b777a6198ed00 commit 58d1721529e99c7c633615e7491b777a6198ed00 Author: Arthur Cohen Date: Tue Mar 1 10:41:45 2022 +0100 macroinvocation: Only allow *stmt* visitors when semicoloned Diff: --- gcc/rust/ast/rust-ast-full-test.cc | 13 --- gcc/rust/ast/rust-macro.h | 17 ++-- gcc/rust/expand/rust-macro-expand.cc | 146 ++++++++++++--------------- gcc/rust/expand/rust-macro-expand.h | 3 +- gcc/rust/hir/rust-ast-lower-implitem.h | 40 ++++---- gcc/rust/hir/rust-ast-lower-item.h | 22 ++-- gcc/rust/hir/rust-ast-lower-stmt.h | 22 ++-- gcc/rust/resolve/rust-ast-resolve-implitem.h | 48 +++++---- gcc/rust/resolve/rust-ast-resolve-item.h | 32 +++--- gcc/rust/resolve/rust-ast-resolve-stmt.h | 18 ++-- gcc/rust/resolve/rust-ast-resolve-toplevel.h | 13 ++- 11 files changed, 181 insertions(+), 193 deletions(-) diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index 82abab9f1b1..826d41af658 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -1275,19 +1275,6 @@ TypeAlias::as_string () const return str; } -// FIXME: ARTHUR: Check if this is necessary for MacroInvocation -// std::string -// MacroInvocationSemi::as_string () const -// { -// std::string str = "MacroInvocationSemi: "; -// -// str += append_attributes (outer_attrs, OUTER); -// -// str += "\n" + invoc_data.as_string (); -// -// return str; -// } - std::string ExternBlock::as_string () const { diff --git a/gcc/rust/ast/rust-macro.h b/gcc/rust/ast/rust-macro.h index de1d0a538bc..2b396248ec6 100644 --- a/gcc/rust/ast/rust-macro.h +++ b/gcc/rust/ast/rust-macro.h @@ -469,6 +469,8 @@ class MacroInvocation : public TypeNoBounds, // Important for when we actually expand the macro bool is_semi_coloned; + NodeId node_id; + public: std::string as_string () const override; @@ -477,7 +479,9 @@ public: bool is_semi_coloned = false) : outer_attrs (std::move (outer_attrs)), invoc_data (std::move (invoc_data)), locus (locus), - fragment (ASTFragment::create_empty ()), is_semi_coloned (is_semi_coloned) + fragment (ASTFragment::create_empty ()), + is_semi_coloned (is_semi_coloned), + node_id (Analysis::Mappings::get ()->get_next_node_id ()) {} Location get_locus () const override final { return locus; } @@ -504,6 +508,8 @@ public: return ExprWithoutBlock::get_node_id (); } + NodeId get_macro_node_id () const { return node_id; } + MacroInvocData &get_invoc_data () { return invoc_data; } ASTFragment &get_fragment () { return fragment; } @@ -562,12 +568,9 @@ protected: } ExprWithoutBlock *to_stmt () const override - - - - - { - auto new_impl = clone_macro_invocation_impl(); + + { + auto new_impl = clone_macro_invocation_impl (); new_impl->is_semi_coloned = true; return new_impl; diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index f52b24a2592..be3859fb51f 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -310,31 +310,33 @@ public: // supposedly does not require - cfg does nothing } - // FIXME: ARTHUR: Check to see if necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi ¯o_invoc) override - // { - // // initial strip test based on outer attrs - // expander.expand_cfg_attrs (macro_invoc.get_outer_attrs ()); - // if (expander.fails_cfg_with_expand (macro_invoc.get_outer_attrs ())) - // { - // macro_invoc.mark_for_strip (); - // return; - // } + void visit (AST::MacroInvocation ¯o_invoc) override + { + // initial strip test based on outer attrs + expander.expand_cfg_attrs (macro_invoc.get_outer_attrs ()); + if (expander.fails_cfg_with_expand (macro_invoc.get_outer_attrs ())) + { + macro_invoc.mark_for_strip (); + return; + } - // // can't strip simple path + // can't strip simple path - // // I don't think any macro token trees can be stripped in any way + // I don't think any macro token trees can be stripped in any way - // // TODO: maybe have cfg! macro stripping behaviour here? + // TODO: maybe have cfg! macro stripping behaviour here? - // expander.expand_invoc_semi (macro_invoc); + if (macro_invoc.has_semicolon ()) + expander.expand_invoc_semi (macro_invoc); + else + expander.expand_invoc (macro_invoc); - // // we need to visit the expanded fragments since it may need cfg - // expansion - // // and it may be recursive - // for (auto &node : macro_invoc.get_fragment ().get_nodes ()) - // node.accept_vis (*this); - // } + // we need to visit the expanded fragments since it may need cfg + // expansion + // and it may be recursive + for (auto &node : macro_invoc.get_fragment ().get_nodes ()) + node.accept_vis (*this); + } void visit (AST::PathInExpression &path) override { @@ -2536,28 +2538,6 @@ public: expander.mappings->insert_macro_def (&rules_def); } - void visit (AST::MacroInvocation ¯o_invoc) override - { - // FIXME - // we probably need another recurision check here - - // initial strip test based on outer attrs - expander.expand_cfg_attrs (macro_invoc.get_outer_attrs ()); - if (expander.fails_cfg_with_expand (macro_invoc.get_outer_attrs ())) - { - macro_invoc.mark_for_strip (); - return; - } - - // I don't think any macro token trees can be stripped in any way - expander.expand_invoc (macro_invoc); - - // we need to visit the expanded fragments since it may need cfg expansion - // and it may be recursive - for (auto &node : macro_invoc.get_fragment ().get_nodes ()) - node.accept_vis (*this); - } - void visit (AST::MetaItemPath &) override {} void visit (AST::MetaItemSeq &) override {} void visit (AST::MetaWord &) override {} @@ -3210,48 +3190,46 @@ MacroExpander::expand_invoc (AST::MacroInvocation &invoc) invoc.set_fragment (std::move (fragment)); } -// FIXME: ARTHUR: Check to see if necessary for MacroInvocation -// void -// MacroExpander::expand_invoc_semi (AST::MacroInvocationSemi &invoc) -// { -// if (depth_exceeds_recursion_limit ()) -// { -// rust_error_at (invoc.get_locus (), "reached recursion limit"); -// return; -// } -// -// AST::MacroInvocData &invoc_data = invoc.get_invoc_data (); -// -// // lookup the rules for this macro -// NodeId resolved_node = UNKNOWN_NODEID; -// bool found = resolver->get_macro_scope ().lookup ( -// Resolver::CanonicalPath::new_seg (invoc.get_macro_node_id (), -// invoc_data.get_path ().as_string ()), -// &resolved_node); -// if (!found) -// { -// rust_error_at (invoc.get_locus (), "unknown macro"); -// return; -// } -// -// // lookup the rules -// AST::MacroRulesDefinition *rules_def = nullptr; -// bool ok = mappings->lookup_macro_def (resolved_node, &rules_def); -// rust_assert (ok); -// -// auto fragment = AST::ASTFragment::create_empty (); -// -// if (rules_def->is_builtin ()) -// fragment -// = rules_def->get_builtin_transcriber () (invoc.get_locus (), -// invoc_data); -// else -// fragment -// = expand_decl_macro (invoc.get_locus (), invoc_data, *rules_def, true); -// -// // lets attach this fragment to the invocation -// invoc.set_fragment (std::move (fragment)); -// } +void +MacroExpander::expand_invoc_semi (AST::MacroInvocation &invoc) +{ + if (depth_exceeds_recursion_limit ()) + { + rust_error_at (invoc.get_locus (), "reached recursion limit"); + return; + } + + AST::MacroInvocData &invoc_data = invoc.get_invoc_data (); + + // lookup the rules for this macro + NodeId resolved_node = UNKNOWN_NODEID; + bool found = resolver->get_macro_scope ().lookup ( + Resolver::CanonicalPath::new_seg (invoc.get_macro_node_id (), + invoc_data.get_path ().as_string ()), + &resolved_node); + if (!found) + { + rust_error_at (invoc.get_locus (), "unknown macro"); + return; + } + + // lookup the rules + AST::MacroRulesDefinition *rules_def = nullptr; + bool ok = mappings->lookup_macro_def (resolved_node, &rules_def); + rust_assert (ok); + + auto fragment = AST::ASTFragment::create_empty (); + + if (rules_def->is_builtin ()) + fragment + = rules_def->get_builtin_transcriber () (invoc.get_locus (), invoc_data); + else + fragment + = expand_decl_macro (invoc.get_locus (), invoc_data, *rules_def, true); + + // lets attach this fragment to the invocation + invoc.set_fragment (std::move (fragment)); +} /* Determines whether any cfg predicate is false and hence item with attributes * should be stripped. Note that attributes must be expanded before calling. */ diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index 597680644d2..da95408848b 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -146,10 +146,11 @@ struct MacroExpander // Expands all macros in the crate passed in. void expand_crate (); - /* Expands a macro invocation (not macro invocation semi) - possibly make both + /* Expands a macro invocation - possibly make both * have similar duck-typed interface and use templates?*/ // should this be public or private? void expand_invoc (AST::MacroInvocation &invoc); + void expand_invoc_semi (AST::MacroInvocation &invoc); // Expands a single declarative macro. AST::ASTFragment expand_decl_macro (Location locus, diff --git a/gcc/rust/hir/rust-ast-lower-implitem.h b/gcc/rust/hir/rust-ast-lower-implitem.h index 8d684288f30..3b901f60086 100644 --- a/gcc/rust/hir/rust-ast-lower-implitem.h +++ b/gcc/rust/hir/rust-ast-lower-implitem.h @@ -52,16 +52,18 @@ public: return resolver.translated; } - // FIXME: ARTHUR: See if this is necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi &invoc) override - // { - // AST::ASTFragment &fragment = invoc.get_fragment (); + void visit (AST::MacroInvocation &invoc) override + { + if (!invoc.has_semicolon ()) + return; + + AST::ASTFragment &fragment = invoc.get_fragment (); - // // FIXME - // // this assertion might go away, maybe on failure's to expand a macro? - // rust_assert (!fragment.get_nodes ().empty ()); - // fragment.get_nodes ().at (0).accept_vis (*this); - // } + // FIXME + // this assertion might go away, maybe on failure's to expand a macro? + rust_assert (!fragment.get_nodes ().empty ()); + fragment.get_nodes ().at (0).accept_vis (*this); + } void visit (AST::TypeAlias &alias) override { @@ -319,16 +321,18 @@ public: return resolver.translated; } - // FIXME: ARTHUR: See if this is necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi &invoc) override - // { - // AST::ASTFragment &fragment = invoc.get_fragment (); + void visit (AST::MacroInvocation &invoc) override + { + if (!invoc.has_semicolon ()) + return; + + AST::ASTFragment &fragment = invoc.get_fragment (); - // // FIXME - // // this assertion might go away, maybe on failure's to expand a macro? - // rust_assert (!fragment.get_nodes ().empty ()); - // fragment.get_nodes ().at (0).accept_vis (*this); - // } + // FIXME + // this assertion might go away, maybe on failure's to expand a macro? + rust_assert (!fragment.get_nodes ().empty ()); + fragment.get_nodes ().at (0).accept_vis (*this); + } void visit (AST::TraitItemFunc &func) override { diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h index 05276244dfb..660a30cb619 100644 --- a/gcc/rust/hir/rust-ast-lower-item.h +++ b/gcc/rust/hir/rust-ast-lower-item.h @@ -51,16 +51,18 @@ public: return resolver.translated; } - // FIXME: ARTHUR: See if this is necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi &invoc) override - // { - // AST::ASTFragment &fragment = invoc.get_fragment (); - - // // FIXME - // // this assertion might go away, maybe on failure's to expand a macro? - // rust_assert (!fragment.get_nodes ().empty ()); - // fragment.get_nodes ().at (0).accept_vis (*this); - // } + void visit (AST::MacroInvocation &invoc) override + { + if (!invoc.has_semicolon ()) + return; + + AST::ASTFragment &fragment = invoc.get_fragment (); + + // FIXME + // this assertion might go away, maybe on failure's to expand a macro? + rust_assert (!fragment.get_nodes ().empty ()); + fragment.get_nodes ().at (0).accept_vis (*this); + } void visit (AST::Module &module) override { diff --git a/gcc/rust/hir/rust-ast-lower-stmt.h b/gcc/rust/hir/rust-ast-lower-stmt.h index 0373a386568..484c638c82e 100644 --- a/gcc/rust/hir/rust-ast-lower-stmt.h +++ b/gcc/rust/hir/rust-ast-lower-stmt.h @@ -45,16 +45,18 @@ public: return resolver.translated; } - // FIXME: ARTHUR: See if this implementation is necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi &invoc) override - // { - // AST::ASTFragment &fragment = invoc.get_fragment (); - - // // FIXME - // // this assertion might go away, maybe on failure's to expand a macro? - // rust_assert (!fragment.get_nodes ().empty ()); - // fragment.get_nodes ().at (0).accept_vis (*this); - // } + void visit (AST::MacroInvocation &invoc) override + { + if (!invoc.has_semicolon ()) + return; + + AST::ASTFragment &fragment = invoc.get_fragment (); + + // FIXME + // this assertion might go away, maybe on failure's to expand a macro? + rust_assert (!fragment.get_nodes ().empty ()); + fragment.get_nodes ().at (0).accept_vis (*this); + } void visit (AST::ExprStmtWithBlock &stmt) override { diff --git a/gcc/rust/resolve/rust-ast-resolve-implitem.h b/gcc/rust/resolve/rust-ast-resolve-implitem.h index 32dd90fba17..73933935e75 100644 --- a/gcc/rust/resolve/rust-ast-resolve-implitem.h +++ b/gcc/rust/resolve/rust-ast-resolve-implitem.h @@ -49,13 +49,15 @@ public: item->accept_vis (resolver); } - // FIXME: ARTHUR: See if this is necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi &invoc) override - // { - // AST::ASTFragment &fragment = invoc.get_fragment (); - // for (auto &node : fragment.get_nodes ()) - // node.accept_vis (*this); - // } + void visit (AST::MacroInvocation &invoc) override + { + if (!invoc.has_semicolon ()) + return; + + AST::ASTFragment &fragment = invoc.get_fragment (); + for (auto &node : fragment.get_nodes ()) + node.accept_vis (*this); + } void visit (AST::TypeAlias &type) override { @@ -145,13 +147,15 @@ public: item->accept_vis (resolver); }; - // FIXME: ARTHUR: See if this is necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi &invoc) override - // { - // AST::ASTFragment &fragment = invoc.get_fragment (); - // for (auto &node : fragment.get_nodes ()) - // node.accept_vis (*this); - // } + void visit (AST::MacroInvocation &invoc) override + { + if (!invoc.has_semicolon ()) + return; + + AST::ASTFragment &fragment = invoc.get_fragment (); + for (auto &node : fragment.get_nodes ()) + node.accept_vis (*this); + } void visit (AST::TraitItemFunc &function) override { @@ -256,13 +260,15 @@ public: item->accept_vis (resolver); }; - // FIXME: ARTHUR: See if this is necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi &invoc) override - // { - // AST::ASTFragment &fragment = invoc.get_fragment (); - // for (auto &node : fragment.get_nodes ()) - // node.accept_vis (*this); - // } + void visit (AST::MacroInvocation &invoc) override + { + if (!invoc.has_semicolon ()) + return; + + AST::ASTFragment &fragment = invoc.get_fragment (); + for (auto &node : fragment.get_nodes ()) + node.accept_vis (*this); + } void visit (AST::ExternalFunctionItem &function) override { diff --git a/gcc/rust/resolve/rust-ast-resolve-item.h b/gcc/rust/resolve/rust-ast-resolve-item.h index 5f9500d6e50..2cb00065f0b 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.h +++ b/gcc/rust/resolve/rust-ast-resolve-item.h @@ -45,13 +45,15 @@ public: item->accept_vis (resolver); }; - // FIXME: ARTHUR: See if this is necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi &invoc) override - // { - // AST::ASTFragment &fragment = invoc.get_fragment (); - // for (auto &node : fragment.get_nodes ()) - // node.accept_vis (*this); - // } + void visit (AST::MacroInvocation &invoc) override + { + if (!invoc.has_semicolon ()) + return; + + AST::ASTFragment &fragment = invoc.get_fragment (); + for (auto &node : fragment.get_nodes ()) + node.accept_vis (*this); + } void visit (AST::TraitItemType &type) override { @@ -235,13 +237,15 @@ public: item->accept_vis (resolver); }; - // FIXME: ARTHUR: See if this is necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi &invoc) override - // { - // AST::ASTFragment &fragment = invoc.get_fragment (); - // for (auto &node : fragment.get_nodes ()) - // node.accept_vis (*this); - // } + void visit (AST::MacroInvocation &invoc) override + { + if (!invoc.has_semicolon ()) + return; + + AST::ASTFragment &fragment = invoc.get_fragment (); + for (auto &node : fragment.get_nodes ()) + node.accept_vis (*this); + } void visit (AST::TypeAlias &alias) override { diff --git a/gcc/rust/resolve/rust-ast-resolve-stmt.h b/gcc/rust/resolve/rust-ast-resolve-stmt.h index 81a37b7d413..785f3dea292 100644 --- a/gcc/rust/resolve/rust-ast-resolve-stmt.h +++ b/gcc/rust/resolve/rust-ast-resolve-stmt.h @@ -44,14 +44,16 @@ public: stmt->accept_vis (resolver); }; - // FIXME: ARTHUR: See if this is necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi &invoc) override - // { - // AST::ASTFragment &fragment = invoc.get_fragment (); - - // for (auto &node : fragment.get_nodes ()) - // node.accept_vis (*this); - // } + void visit (AST::MacroInvocation &invoc) override + { + if (!invoc.has_semicolon ()) + return; + + AST::ASTFragment &fragment = invoc.get_fragment (); + + for (auto &node : fragment.get_nodes ()) + node.accept_vis (*this); + } void visit (AST::ExprStmtWithBlock &stmt) override { diff --git a/gcc/rust/resolve/rust-ast-resolve-toplevel.h b/gcc/rust/resolve/rust-ast-resolve-toplevel.h index 762010c6660..1f528fe9550 100644 --- a/gcc/rust/resolve/rust-ast-resolve-toplevel.h +++ b/gcc/rust/resolve/rust-ast-resolve-toplevel.h @@ -43,13 +43,12 @@ public: item->accept_vis (resolver); }; - // FIXME: ARTHUR: See if this is necessary for MacroInvocation - // void visit (AST::MacroInvocationSemi &invoc) override - // { - // AST::ASTFragment &fragment = invoc.get_fragment (); - // for (auto &node : fragment.get_nodes ()) - // node.accept_vis (*this); - // } + void visit (AST::MacroInvocation &invoc) override + { + AST::ASTFragment &fragment = invoc.get_fragment (); + for (auto &node : fragment.get_nodes ()) + node.accept_vis (*this); + } void visit (AST::Module &module) override {