diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-03-01 10:41:45 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-03-01 11:19:11 +0100 |
commit | 58d1721529e99c7c633615e7491b777a6198ed00 (patch) | |
tree | 494dea8c86717a2efeacd4dc757de0bc5d48ff0a /gcc | |
parent | 12d156566af84ec834abb7e18feac6e8f5884451 (diff) | |
download | gcc-58d1721529e99c7c633615e7491b777a6198ed00.zip gcc-58d1721529e99c7c633615e7491b777a6198ed00.tar.gz gcc-58d1721529e99c7c633615e7491b777a6198ed00.tar.bz2 |
macroinvocation: Only allow *stmt* visitors when semicoloned
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-ast-full-test.cc | 13 | ||||
-rw-r--r-- | gcc/rust/ast/rust-macro.h | 17 | ||||
-rw-r--r-- | gcc/rust/expand/rust-macro-expand.cc | 146 | ||||
-rw-r--r-- | gcc/rust/expand/rust-macro-expand.h | 3 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-implitem.h | 40 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.h | 22 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-stmt.h | 22 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-implitem.h | 48 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-item.h | 32 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-stmt.h | 18 | ||||
-rw-r--r-- | 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 82abab9..826d41a 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 de1d0a5..2b39624 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 f52b24a..be3859f 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 5976806..da95408 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 8d68428..3b901f6 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 0527624..660a30c 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 0373a38..484c638 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 32dd90f..7393393 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 5f9500d..2cb0006 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 81a37b7..785f3de 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 762010c..1f528fe 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 { |