public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7990] gccrs: resolver: Refactor macro insertion
@ 2024-01-16 18:11 Arthur Cohen
0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 18:11 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:86661b8d3d97d18a799468226f1b34d9682e9732
commit r14-7990-g86661b8d3d97d18a799468226f1b34d9682e9732
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date: Mon Aug 28 11:39:44 2023 +0200
gccrs: resolver: Refactor macro insertion
Add a templated function to insert any of the three kind of proc macro
into the resolver context.
gcc/rust/ChangeLog:
* expand/rust-proc-macro.h: Change get_trait_name to get_name in
order to be coherent with the others proc macro type name
convention.
* resolve/rust-toplevel-name-resolver-2.0.cc (insert_macros):
Add a templated funtion that inserts a proc macro into the
context and emit an error on failure.
(TopLevel::visit): Change from manual insertion to a function
call.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diff:
---
gcc/rust/expand/rust-proc-macro.h | 2 +-
.../resolve/rust-toplevel-name-resolver-2.0.cc | 52 +++++++++-------------
2 files changed, 21 insertions(+), 33 deletions(-)
diff --git a/gcc/rust/expand/rust-proc-macro.h b/gcc/rust/expand/rust-proc-macro.h
index a7fb8c893e1..d994ed9bf5e 100644
--- a/gcc/rust/expand/rust-proc-macro.h
+++ b/gcc/rust/expand/rust-proc-macro.h
@@ -70,7 +70,7 @@ public:
CustomDeriveProcMacro (ProcMacro::CustomDerive macro);
CustomDeriveProcMacro () = default;
- const std::string &get_trait_name () const { return trait_name; }
+ const std::string &get_name () const { return trait_name; }
NodeId get_node_id () const { return node_id; }
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
index a09c738267e..33a20cc64c0 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -71,6 +71,23 @@ TopLevel::visit (AST::Module &module)
module.get_name ());
}
+template <typename PROC_MACRO>
+static void
+insert_macros (std::vector<PROC_MACRO> ¯os, NameResolutionContext &ctx)
+{
+ for (auto ¯o : macros)
+ {
+ auto res = ctx.macros.insert (macro.get_name (), macro.get_node_id ());
+
+ if (!res)
+ {
+ rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
+ "macro %qs defined multiple times",
+ macro.get_name ().c_str ());
+ }
+ }
+}
+
void
TopLevel::visit (AST::ExternCrate &crate)
{
@@ -88,40 +105,11 @@ TopLevel::visit (AST::ExternCrate &crate)
auto sub_visitor = [&] () {
if (derive_macros.has_value ())
- for (auto &derive : derive_macros.value ())
- {
- auto res = ctx.macros.insert (derive.get_trait_name (),
- derive.get_node_id ());
- if (!res)
- {
- rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
- "macro %qs defined multiple times",
- derive.get_trait_name ().c_str ());
- }
- }
+ insert_macros (derive_macros.value (), ctx);
if (attribute_macros.has_value ())
- for (auto &attribute : attribute_macros.value ())
- {
- auto res = ctx.macros.insert (attribute.get_name (),
- attribute.get_node_id ());
- if (!res)
- {
- rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
- "macro %qs defined multiple times",
- attribute.get_name ().c_str ());
- }
- }
+ insert_macros (attribute_macros.value (), ctx);
if (bang_macros.has_value ())
- for (auto &bang : bang_macros.value ())
- {
- auto res = ctx.macros.insert (bang.get_name (), bang.get_node_id ());
- if (!res)
- {
- rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
- "macro %qs defined multiple times",
- bang.get_name ().c_str ());
- }
- }
+ insert_macros (bang_macros.value (), ctx);
};
if (crate.has_as_clause ())
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2024-01-16 18:11 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 18:11 [gcc r14-7990] gccrs: resolver: Refactor macro insertion Arthur Cohen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).