diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2023-03-02 14:41:54 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-01-16 18:28:38 +0100 |
commit | 9946ef210f40985ce8ab7e5faae43b23f08988a8 (patch) | |
tree | eba3c4b77f52143c1c42ae22f7f9f4b018e3005e /gcc | |
parent | 848bbbd7735e07fbbdacd3311127b56b0a2b2616 (diff) | |
download | gcc-9946ef210f40985ce8ab7e5faae43b23f08988a8.zip gcc-9946ef210f40985ce8ab7e5faae43b23f08988a8.tar.gz gcc-9946ef210f40985ce8ab7e5faae43b23f08988a8.tar.bz2 |
gccrs: lowering: Add lowering of exported macros
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.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-base.cc | 16 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-base.h | 3 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.cc | 4 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-stmt.cc | 10 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-stmt.h | 1 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower.cc | 15 |
6 files changed, 37 insertions, 12 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index 900d2eb..475c541 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -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 diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h index aeb21b2..699db72 100644 --- a/gcc/rust/hir/rust-ast-lower-base.h +++ b/gcc/rust/hir/rust-ast-lower-base.h @@ -318,6 +318,9 @@ protected: HIR::ExternBlock *lower_extern_block (AST::ExternBlock &extern_block); HIR::ClosureParam lower_closure_param (AST::ClosureParam ¶m); + + /* Lower a macro definition if it should be exported */ + void lower_macro_definition (AST::MacroRulesDefinition &def); }; } // namespace HIR diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc index 1230fa1..c906964 100644 --- a/gcc/rust/hir/rust-ast-lower-item.cc +++ b/gcc/rust/hir/rust-ast-lower-item.cc @@ -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 diff --git a/gcc/rust/hir/rust-ast-lower-stmt.cc b/gcc/rust/hir/rust-ast-lower-stmt.cc index 5ba8db0..6f34181 100644 --- a/gcc/rust/hir/rust-ast-lower-stmt.cc +++ b/gcc/rust/hir/rust-ast-lower-stmt.cc @@ -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 diff --git a/gcc/rust/hir/rust-ast-lower-stmt.h b/gcc/rust/hir/rust-ast-lower-stmt.h index 5cc1770..60ba1f4 100644 --- a/gcc/rust/hir/rust-ast-lower-stmt.h +++ b/gcc/rust/hir/rust-ast-lower-stmt.h @@ -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) {} diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc index e74b8be..381b0297 100644 --- a/gcc/rust/hir/rust-ast-lower.cc +++ b/gcc/rust/hir/rust-ast-lower.cc @@ -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 ()); |