From: Pierre-Emmanuel Patry Date: Mon, 28 Aug 2023 09:39:44 +0000 (+0200) Subject: gccrs: resolver: Refactor macro insertion X-Git-Tag: basepoints/gcc-15~2149 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=86661b8d3d97d18a799468226f1b34d9682e9732;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/rust/expand/rust-proc-macro.h b/gcc/rust/expand/rust-proc-macro.h index a7fb8c893e19..d994ed9bf5e0 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 a09c738267e9..33a20cc64c0b 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 +static void +insert_macros (std::vector ¯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 ())