]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: lowering: Add lowering of exported macros
authorArthur Cohen <arthur.cohen@embecosm.com>
Thu, 2 Mar 2023 13:41:54 +0000 (14:41 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:38 +0000 (18:28 +0100)
Macros marked with #[macro_export] need to be lowered to HIR in order
to get exported to the relevant metadata files.

gcc/rust/ChangeLog:

* hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_macro_definition):
New function.
* hir/rust-ast-lower-base.h: Declare `lower_macro_definition`.
* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Lower public
macro definitions.
* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): Likewise.
* hir/rust-ast-lower-stmt.h: Add visitor for `AST::MacroRulesDefinition`.
* hir/rust-ast-lower.cc (ASTLowering::go): Formatting.
(ASTLoweringBlock::visit): Visit `AST::MacroRulesDefinition`
(ASTLoweringIfLetBlock::visit): Formatting.
(ASTLoweringExprWithBlock::visit): Formatting.

gcc/rust/hir/rust-ast-lower-base.cc
gcc/rust/hir/rust-ast-lower-base.h
gcc/rust/hir/rust-ast-lower-item.cc
gcc/rust/hir/rust-ast-lower-stmt.cc
gcc/rust/hir/rust-ast-lower-stmt.h
gcc/rust/hir/rust-ast-lower.cc

index 900d2eb2f136af2aa0c8c4d472ad83f8bf62ec78..475c541107fa17c1dac3beccb6b31f6296e5570f 100644 (file)
@@ -970,5 +970,21 @@ ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block)
   return hir_extern_block;
 }
 
+void
+ASTLoweringBase::lower_macro_definition (AST::MacroRulesDefinition &def)
+{
+  auto is_export = false;
+  for (const auto &attr : def.get_outer_attrs ())
+    if (attr.get_path ().as_string () == "macro_export")
+      is_export = true;
+
+  if (is_export)
+    {
+      mappings->insert_exported_macro (def);
+      mappings->insert_ast_item (&def);
+      mappings->insert_location (def.get_node_id (), def.get_locus ());
+    }
+}
+
 } // namespace HIR
 } // namespace Rust
index aeb21b20efcee5153a0a47a5294f073a288d9f6b..699db7252ff77a1c762066ebcd0579fa48f4ddc9 100644 (file)
@@ -318,6 +318,9 @@ protected:
   HIR::ExternBlock *lower_extern_block (AST::ExternBlock &extern_block);
 
   HIR::ClosureParam lower_closure_param (AST::ClosureParam &param);
+
+  /* Lower a macro definition if it should be exported */
+  void lower_macro_definition (AST::MacroRulesDefinition &def);
 };
 
 } // namespace HIR
index 1230fa1ef6c2a2f8444e97607021067691e6ad8c..c90696457a71bb6245e704d5ed1f4df22ecfa9a7 100644 (file)
@@ -708,9 +708,7 @@ ASTLoweringItem::visit (AST::ExternBlock &extern_block)
 void
 ASTLoweringItem::visit (AST::MacroRulesDefinition &def)
 {
-  for (const auto &attr : def.get_outer_attrs ())
-    if (attr.get_path ().as_string () == "macro_export")
-      mappings->insert_exported_macro (def);
+  lower_macro_definition (def);
 }
 
 HIR::SimplePath
index 5ba8db002d02be23d0826b8a271c2d0717fd9657..6f34181c6299b49eb009007dde62f6ecbee38be6 100644 (file)
@@ -32,7 +32,9 @@ ASTLoweringStmt::translate (AST::Stmt *stmt, bool *terminated)
   ASTLoweringStmt resolver;
   stmt->accept_vis (resolver);
 
-  rust_assert (resolver.translated != nullptr);
+  if (!resolver.translated)
+    return nullptr;
+
   *terminated = resolver.terminated;
   resolver.mappings->insert_location (
     resolver.translated->get_mappings ().get_hirid (),
@@ -406,5 +408,11 @@ ASTLoweringStmt::visit (AST::ExternBlock &extern_block)
   translated = lower_extern_block (extern_block);
 }
 
+void
+ASTLoweringStmt::visit (AST::MacroRulesDefinition &def)
+{
+  lower_macro_definition (def);
+}
+
 } // namespace HIR
 } // namespace Rust
index 5cc17705075041187038259891bcd213391cd7e8..60ba1f4ede78575896d51b10ddbff6b26777f017 100644 (file)
@@ -42,6 +42,7 @@ public:
   void visit (AST::EmptyStmt &empty) override;
   void visit (AST::Function &function) override;
   void visit (AST::ExternBlock &extern_block) override;
+  void visit (AST::MacroRulesDefinition &extern_block) override;
 
 private:
   ASTLoweringStmt () : translated (nullptr), terminated (false) {}
index e74b8bef2d82c25d82b46a7e78b955dad4fc29e7..381b02979f530a032af480df8702ee044e4bfb46 100644 (file)
@@ -72,7 +72,7 @@ ASTLowering::Resolve (AST::Crate &astCrate)
 std::unique_ptr<HIR::Crate>
 ASTLowering::go ()
 {
-  std::vector<std::unique_ptr<HIR::Item> > items;
+  std::vector<std::unique_ptr<HIR::Item>> items;
 
   for (auto it = astCrate.items.begin (); it != astCrate.items.end (); it++)
     {
@@ -95,14 +95,11 @@ ASTLowering::go ()
 void
 ASTLoweringBlock::visit (AST::BlockExpr &expr)
 {
-  std::vector<std::unique_ptr<HIR::Stmt> > block_stmts;
+  std::vector<std::unique_ptr<HIR::Stmt>> block_stmts;
   bool block_did_terminate = false;
 
   for (auto &s : expr.get_statements ())
     {
-      if (s->get_ast_kind () == AST::Kind::MACRO_RULES_DEFINITION)
-       continue;
-
       if (s->get_ast_kind () == AST::Kind::MACRO_INVOCATION)
        rust_fatal_error (
          s->get_locus (),
@@ -115,8 +112,10 @@ ASTLoweringBlock::visit (AST::BlockExpr &expr)
 
       bool terminated = false;
       auto translated_stmt = ASTLoweringStmt::translate (s.get (), &terminated);
-      block_stmts.push_back (std::unique_ptr<HIR::Stmt> (translated_stmt));
       block_did_terminate |= terminated;
+
+      if (translated_stmt)
+       block_stmts.push_back (std::unique_ptr<HIR::Stmt> (translated_stmt));
     }
 
   if (expr.has_tail_expr () && block_did_terminate)
@@ -230,7 +229,7 @@ ASTLoweringIfBlock::visit (AST::IfExprConseqIf &expr)
 void
 ASTLoweringIfLetBlock::visit (AST::IfLetExpr &expr)
 {
-  std::vector<std::unique_ptr<HIR::Pattern> > patterns;
+  std::vector<std::unique_ptr<HIR::Pattern>> patterns;
   for (auto &pattern : expr.get_patterns ())
     {
       HIR::Pattern *ptrn = ASTLoweringPattern::translate (pattern.get ());
@@ -372,7 +371,7 @@ ASTLoweringExprWithBlock::visit (AST::MatchExpr &expr)
            match_case.get_arm ().get_guard_expr ().get ());
        }
 
-      std::vector<std::unique_ptr<HIR::Pattern> > match_arm_patterns;
+      std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns;
       for (auto &pattern : match_case.get_arm ().get_patterns ())
        {
          HIR::Pattern *ptrn = ASTLoweringPattern::translate (pattern.get ());