]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: resolver: Refactor macro insertion
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Mon, 28 Aug 2023 09:39:44 +0000 (11:39 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:04:34 +0000 (19:04 +0100)
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>
gcc/rust/expand/rust-proc-macro.h
gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc

index a7fb8c893e19477aeb85afdf61e000e64831df3d..d994ed9bf5e080bd91d9586b19a1566e2f81ad2c 100644 (file)
@@ -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; }
 
index a09c738267e9859fd27ca54335623dcc6c7dfdb0..33a20cc64c0b45f74b624cfd63987d469597c1f2 100644 (file)
@@ -71,6 +71,23 @@ TopLevel::visit (AST::Module &module)
              module.get_name ());
 }
 
+template <typename PROC_MACRO>
+static void
+insert_macros (std::vector<PROC_MACRO> &macros, NameResolutionContext &ctx)
+{
+  for (auto &macro : 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 ())