diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-08-28 11:39:44 +0200 |
---|---|---|
committer | P-E-P <32375388+P-E-P@users.noreply.github.com> | 2023-09-05 14:05:36 +0000 |
commit | b2af85c218b0646833ab8cadc9cf8aacc182fb6f (patch) | |
tree | dc3f9c6c06ce4aa19e01a0892ebdc22fb488dbce /gcc | |
parent | f1527eff1e1387a19fe7db115f7157962780d72c (diff) | |
download | gcc-b2af85c218b0646833ab8cadc9cf8aacc182fb6f.zip gcc-b2af85c218b0646833ab8cadc9cf8aacc182fb6f.tar.gz gcc-b2af85c218b0646833ab8cadc9cf8aacc182fb6f.tar.bz2 |
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>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/expand/rust-proc-macro.h | 2 | ||||
-rw-r--r-- | gcc/rust/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 a7fb8c8..d994ed9 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 a09c738..33a20cc 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 ()) |