https://gcc.gnu.org/g:11e24d081697de4bac66de1ba54a9c47fdb14d8d
commit r17-5159-g11e24d081697de4bac66de1ba54a9c47fdb14d8d Author: Pierre-Emmanuel Patry <[email protected]> Date: Thu Sep 10 20:27:43 2026 +0200 gccrs: Regroup proc macro related mappings HIR map is getting bigger and harder to use. This commit regroup all proc macro related operations under one subclass. gcc/rust/ChangeLog: * expand/rust-macro-expand.h (struct MacroExpander): Use new macro category. * resolve/rust-early-name-resolver-2.0.cc (Early::visit_derive_attribute): Likewise. (Early::visit_non_builtin_attribute): Likewise. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit_extern_crate): Likewise. * rust-session-manager.cc (Session::load_extern_crate): Likewise. * util/rust-hir-map.cc (Mappings::insert_derive_proc_macros): Move to specialized macro class declartion. (Mappings::insert_bang_proc_macros): Move from here... (ProcMacroMappings::insert_bang_proc_macros): ... to here. (Mappings::insert_attribute_proc_macros): Likewise. (Mappings::lookup_derive_proc_macros): Likewise. (ProcMacroMappings::lookup_derive_proc_macros): Likewise. (Mappings::lookup_bang_proc_macros): Likewise. (ProcMacroMappings::lookup_bang_proc_macros): Likewise. (Mappings::lookup_attribute_proc_macros): Likewise. (ProcMacroMappings::lookup_attribute_proc_macros): Likewise. (Mappings::insert_derive_proc_macro_def): Likewise. (ProcMacroMappings::insert_derive_proc_macro_def): Likewise. (Mappings::insert_bang_proc_macro_def): Likewise. (ProcMacroMappings::insert_bang_proc_macro_def): Likewise. (Mappings::insert_attribute_proc_macro_def): Likewise. (ProcMacroMappings::insert_attribute_proc_macro_def): Likewise. (Mappings::lookup_derive_proc_macro_def): Likewise. (ProcMacroMappings::lookup_derive_proc_macro_def): Likewise. (Mappings::lookup_bang_proc_macro_def): Likewise. (ProcMacroMappings::lookup_bang_proc_macro_def): Likewise. (Mappings::lookup_attribute_proc_macro_def): Likewise. (ProcMacroMappings::lookup_attribute_proc_macro_def): Likewise. (Mappings::insert_derive_proc_macro_invocation): Likewise. (Mappings::lookup_derive_proc_macro_invocation): Likewise. (ProcMacroMappings::lookup_derive_proc_macro_invocation): Likewise. (Mappings::insert_bang_proc_macro_invocation): Likewise. (Mappings::lookup_bang_proc_macro_invocation): Likewise. (Mappings::insert_attribute_proc_macro_invocation): Likewise. (Mappings::lookup_attribute_proc_macro_invocation): Likewise. * util/rust-hir-map.h (class ProcMacroMappings): Add specialized class for macro mappings. Signed-off-by: Pierre-Emmanuel Patry <[email protected]> Diff: --- gcc/rust/expand/rust-macro-expand.h | 6 +- gcc/rust/resolve/rust-early-name-resolver-2.0.cc | 11 ++- .../resolve/rust-toplevel-name-resolver-2.0.cc | 13 +-- gcc/rust/rust-session-manager.cc | 7 +- gcc/rust/util/rust-hir-map.cc | 50 +++++----- gcc/rust/util/rust-hir-map.h | 106 +++++++++++---------- 6 files changed, 101 insertions(+), 92 deletions(-) diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index 23e98e071eab..ddd433c39369 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -412,7 +412,7 @@ struct MacroExpander AST::Fragment expand_derive_proc_macro (T &item, AST::SimplePath &path) { tl::optional<CustomDeriveProcMacro &> macro - = mappings.lookup_derive_proc_macro_invocation (path); + = mappings.pmacro_mappings.lookup_derive_proc_macro_invocation (path); if (!macro.has_value ()) { rust_error_at (path.get_locus (), "macro not found"); @@ -435,7 +435,7 @@ struct MacroExpander AST::MacroInvocation &invocation) { tl::optional<BangProcMacro &> macro - = mappings.lookup_bang_proc_macro_invocation (invocation); + = mappings.pmacro_mappings.lookup_bang_proc_macro_invocation (invocation); if (!macro.has_value ()) { rust_error_at (invocation.get_locus (), "macro not found"); @@ -457,7 +457,7 @@ struct MacroExpander AST::Fragment expand_attribute_proc_macro (T &item, AST::SimplePath &path) { tl::optional<AttributeProcMacro &> macro - = mappings.lookup_attribute_proc_macro_invocation (path); + = mappings.pmacro_mappings.lookup_attribute_proc_macro_invocation (path); if (!macro.has_value ()) { rust_error_at (path.get_locus (), "macro not found"); diff --git a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc index aae601e92465..d4d0cb8e1ec8 100644 --- a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc @@ -402,11 +402,12 @@ Early::visit_derive_attribute (AST::Attribute &attr, continue; } - auto pm_def = mappings.lookup_derive_proc_macro_def ( + auto pm_def = mappings.pmacro_mappings.lookup_derive_proc_macro_def ( ns_def->definition.get_node_id ()); if (pm_def.has_value ()) - mappings.insert_derive_proc_macro_invocation (trait, pm_def.value ()); + mappings.pmacro_mappings.insert_derive_proc_macro_invocation ( + trait, pm_def.value ()); } } @@ -424,14 +425,14 @@ Early::visit_non_builtin_attribute (AST::Attribute &attr, name.c_str ())); return; } - auto pm_def = mappings.lookup_attribute_proc_macro_def ( + auto pm_def = mappings.pmacro_mappings.lookup_attribute_proc_macro_def ( ns_def->definition.get_node_id ()); if (!pm_def.has_value ()) return; - mappings.insert_attribute_proc_macro_invocation (attr.get_path (), - pm_def.value ()); + mappings.pmacro_mappings.insert_attribute_proc_macro_invocation ( + attr.get_path (), pm_def.value ()); } void 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 c7f35b62859e..88b946649f4b 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc @@ -187,30 +187,31 @@ TopLevel::visit_extern_crate (AST::ExternCrate &extern_crate, AST::Crate &crate, { auto &mappings = Analysis::Mappings::get (); - auto attribute_macros = mappings.lookup_attribute_proc_macros (num); + auto attribute_macros + = mappings.pmacro_mappings.lookup_attribute_proc_macros (num); - auto bang_macros = mappings.lookup_bang_proc_macros (num); + auto bang_macros = mappings.pmacro_mappings.lookup_bang_proc_macros (num); - auto derive_macros = mappings.lookup_derive_proc_macros (num); + auto derive_macros = mappings.pmacro_mappings.lookup_derive_proc_macros (num); // TODO: Find a way to keep this part clean without the double dispatch. if (derive_macros.has_value ()) { insert_macros (derive_macros.value (), ctx); for (auto ¯o : derive_macros.value ()) - mappings.insert_derive_proc_macro_def (macro); + mappings.pmacro_mappings.insert_derive_proc_macro_def (macro); } if (attribute_macros.has_value ()) { insert_macros (attribute_macros.value (), ctx); for (auto ¯o : attribute_macros.value ()) - mappings.insert_attribute_proc_macro_def (macro); + mappings.pmacro_mappings.insert_attribute_proc_macro_def (macro); } if (bang_macros.has_value ()) { insert_macros (bang_macros.value (), ctx); for (auto ¯o : bang_macros.value ()) - mappings.insert_bang_proc_macro_def (macro); + mappings.pmacro_mappings.insert_bang_proc_macro_def (macro); } // We do *NOT* visit the crate because loaded crates are resolved diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index f65b09264476..868216024a1b 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -1366,9 +1366,10 @@ Session::load_extern_crate (const std::string &crate_name, location_t locus) } } - mappings.insert_attribute_proc_macros (crate_num, attribute_macros); - mappings.insert_bang_proc_macros (crate_num, bang_macros); - mappings.insert_derive_proc_macros (crate_num, derive_macros); + mappings.pmacro_mappings.insert_attribute_proc_macros (crate_num, + attribute_macros); + mappings.pmacro_mappings.insert_bang_proc_macros (crate_num, bang_macros); + mappings.pmacro_mappings.insert_derive_proc_macros (crate_num, derive_macros); // always restore the crate_num crate_mapping.set_current_crate (saved_crate_num); diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 6a96102ca3f5..af3d6f222319 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -1134,8 +1134,8 @@ Mappings::get_exported_macros () } void -Mappings::insert_derive_proc_macros (CrateNum num, - std::vector<CustomDeriveProcMacro> macros) +ProcMacroMappings::insert_derive_proc_macros ( + CrateNum num, std::vector<CustomDeriveProcMacro> macros) { auto it = procmacrosDeriveMappings.find (num); rust_assert (it == procmacrosDeriveMappings.end ()); @@ -1144,8 +1144,8 @@ Mappings::insert_derive_proc_macros (CrateNum num, } void -Mappings::insert_bang_proc_macros (CrateNum num, - std::vector<BangProcMacro> macros) +ProcMacroMappings::insert_bang_proc_macros (CrateNum num, + std::vector<BangProcMacro> macros) { auto it = procmacrosBangMappings.find (num); rust_assert (it == procmacrosBangMappings.end ()); @@ -1154,8 +1154,8 @@ Mappings::insert_bang_proc_macros (CrateNum num, } void -Mappings::insert_attribute_proc_macros (CrateNum num, - std::vector<AttributeProcMacro> macros) +ProcMacroMappings::insert_attribute_proc_macros ( + CrateNum num, std::vector<AttributeProcMacro> macros) { auto it = procmacrosAttributeMappings.find (num); rust_assert (it == procmacrosAttributeMappings.end ()); @@ -1164,7 +1164,7 @@ Mappings::insert_attribute_proc_macros (CrateNum num, } tl::optional<std::vector<CustomDeriveProcMacro> &> -Mappings::lookup_derive_proc_macros (CrateNum num) +ProcMacroMappings::lookup_derive_proc_macros (CrateNum num) { auto it = procmacrosDeriveMappings.find (num); if (it == procmacrosDeriveMappings.end ()) @@ -1174,7 +1174,7 @@ Mappings::lookup_derive_proc_macros (CrateNum num) } tl::optional<std::vector<BangProcMacro> &> -Mappings::lookup_bang_proc_macros (CrateNum num) +ProcMacroMappings::lookup_bang_proc_macros (CrateNum num) { auto it = procmacrosBangMappings.find (num); if (it == procmacrosBangMappings.end ()) @@ -1184,7 +1184,7 @@ Mappings::lookup_bang_proc_macros (CrateNum num) } tl::optional<std::vector<AttributeProcMacro> &> -Mappings::lookup_attribute_proc_macros (CrateNum num) +ProcMacroMappings::lookup_attribute_proc_macros (CrateNum num) { auto it = procmacrosAttributeMappings.find (num); if (it == procmacrosAttributeMappings.end ()) @@ -1194,7 +1194,7 @@ Mappings::lookup_attribute_proc_macros (CrateNum num) } void -Mappings::insert_derive_proc_macro_def (CustomDeriveProcMacro macro) +ProcMacroMappings::insert_derive_proc_macro_def (CustomDeriveProcMacro macro) { auto it = procmacroDeriveMappings.find (macro.get_node_id ()); rust_assert (it == procmacroDeriveMappings.end ()); @@ -1203,7 +1203,7 @@ Mappings::insert_derive_proc_macro_def (CustomDeriveProcMacro macro) } void -Mappings::insert_bang_proc_macro_def (BangProcMacro macro) +ProcMacroMappings::insert_bang_proc_macro_def (BangProcMacro macro) { auto it = procmacroBangMappings.find (macro.get_node_id ()); rust_assert (it == procmacroBangMappings.end ()); @@ -1212,7 +1212,7 @@ Mappings::insert_bang_proc_macro_def (BangProcMacro macro) } void -Mappings::insert_attribute_proc_macro_def (AttributeProcMacro macro) +ProcMacroMappings::insert_attribute_proc_macro_def (AttributeProcMacro macro) { auto it = procmacroAttributeMappings.find (macro.get_node_id ()); rust_assert (it == procmacroAttributeMappings.end ()); @@ -1221,7 +1221,7 @@ Mappings::insert_attribute_proc_macro_def (AttributeProcMacro macro) } tl::optional<CustomDeriveProcMacro &> -Mappings::lookup_derive_proc_macro_def (NodeId id) +ProcMacroMappings::lookup_derive_proc_macro_def (NodeId id) { auto it = procmacroDeriveMappings.find (id); if (it == procmacroDeriveMappings.end ()) @@ -1231,7 +1231,7 @@ Mappings::lookup_derive_proc_macro_def (NodeId id) } tl::optional<BangProcMacro &> -Mappings::lookup_bang_proc_macro_def (NodeId id) +ProcMacroMappings::lookup_bang_proc_macro_def (NodeId id) { auto it = procmacroBangMappings.find (id); if (it == procmacroBangMappings.end ()) @@ -1241,7 +1241,7 @@ Mappings::lookup_bang_proc_macro_def (NodeId id) } tl::optional<AttributeProcMacro &> -Mappings::lookup_attribute_proc_macro_def (NodeId id) +ProcMacroMappings::lookup_attribute_proc_macro_def (NodeId id) { auto it = procmacroAttributeMappings.find (id); if (it == procmacroAttributeMappings.end ()) @@ -1251,8 +1251,8 @@ Mappings::lookup_attribute_proc_macro_def (NodeId id) } void -Mappings::insert_derive_proc_macro_invocation (AST::SimplePath &invoc, - CustomDeriveProcMacro def) +ProcMacroMappings::insert_derive_proc_macro_invocation ( + AST::SimplePath &invoc, CustomDeriveProcMacro def) { auto it = procmacroDeriveInvocations.find (invoc.get_node_id ()); rust_assert (it == procmacroDeriveInvocations.end ()); @@ -1261,7 +1261,7 @@ Mappings::insert_derive_proc_macro_invocation (AST::SimplePath &invoc, } tl::optional<CustomDeriveProcMacro &> -Mappings::lookup_derive_proc_macro_invocation (AST::SimplePath &invoc) +ProcMacroMappings::lookup_derive_proc_macro_invocation (AST::SimplePath &invoc) { auto it = procmacroDeriveInvocations.find (invoc.get_node_id ()); if (it == procmacroDeriveInvocations.end ()) @@ -1271,8 +1271,8 @@ Mappings::lookup_derive_proc_macro_invocation (AST::SimplePath &invoc) } void -Mappings::insert_bang_proc_macro_invocation (AST::MacroInvocation &invoc, - BangProcMacro def) +ProcMacroMappings::insert_bang_proc_macro_invocation ( + AST::MacroInvocation &invoc, BangProcMacro def) { auto it = procmacroBangInvocations.find (invoc.get_node_id ()); rust_assert (it == procmacroBangInvocations.end ()); @@ -1281,7 +1281,8 @@ Mappings::insert_bang_proc_macro_invocation (AST::MacroInvocation &invoc, } tl::optional<BangProcMacro &> -Mappings::lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc) +ProcMacroMappings::lookup_bang_proc_macro_invocation ( + AST::MacroInvocation &invoc) { auto it = procmacroBangInvocations.find (invoc.get_node_id ()); if (it == procmacroBangInvocations.end ()) @@ -1291,8 +1292,8 @@ Mappings::lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc) } void -Mappings::insert_attribute_proc_macro_invocation (AST::SimplePath &invoc, - AttributeProcMacro def) +ProcMacroMappings::insert_attribute_proc_macro_invocation ( + AST::SimplePath &invoc, AttributeProcMacro def) { auto it = procmacroAttributeInvocations.find (invoc.get_node_id ()); rust_assert (it == procmacroAttributeInvocations.end ()); @@ -1301,7 +1302,8 @@ Mappings::insert_attribute_proc_macro_invocation (AST::SimplePath &invoc, } tl::optional<AttributeProcMacro &> -Mappings::lookup_attribute_proc_macro_invocation (AST::SimplePath &invoc) +ProcMacroMappings::lookup_attribute_proc_macro_invocation ( + AST::SimplePath &invoc) { auto it = procmacroAttributeInvocations.find (invoc.get_node_id ()); if (it == procmacroAttributeInvocations.end ()) diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index d92f0e0f16a7..a7f631608eb3 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -108,6 +108,60 @@ public: AST::Crate *get_ast_crate_by_node_id_raw (NodeId id); }; +class ProcMacroMappings +{ + std::map<CrateNum, std::vector<CustomDeriveProcMacro>> + procmacrosDeriveMappings; + std::map<CrateNum, std::vector<BangProcMacro>> procmacrosBangMappings; + std::map<CrateNum, std::vector<AttributeProcMacro>> + procmacrosAttributeMappings; + + std::map<NodeId, CustomDeriveProcMacro> procmacroDeriveMappings; + std::map<NodeId, BangProcMacro> procmacroBangMappings; + std::map<NodeId, AttributeProcMacro> procmacroAttributeMappings; + std::map<NodeId, CustomDeriveProcMacro> procmacroDeriveInvocations; + std::map<NodeId, BangProcMacro> procmacroBangInvocations; + std::map<NodeId, AttributeProcMacro> procmacroAttributeInvocations; + +public: + void insert_derive_proc_macros (CrateNum num, + std::vector<CustomDeriveProcMacro> macros); + void insert_bang_proc_macros (CrateNum num, + std::vector<BangProcMacro> macros); + void insert_attribute_proc_macros (CrateNum num, + std::vector<AttributeProcMacro> macros); + + tl::optional<std::vector<CustomDeriveProcMacro> &> + lookup_derive_proc_macros (CrateNum num); + tl::optional<std::vector<BangProcMacro> &> + lookup_bang_proc_macros (CrateNum num); + tl::optional<std::vector<AttributeProcMacro> &> + lookup_attribute_proc_macros (CrateNum num); + + void insert_derive_proc_macro_def (CustomDeriveProcMacro macro); + void insert_bang_proc_macro_def (BangProcMacro macro); + void insert_attribute_proc_macro_def (AttributeProcMacro macro); + + tl::optional<CustomDeriveProcMacro &> + lookup_derive_proc_macro_def (NodeId id); + tl::optional<BangProcMacro &> lookup_bang_proc_macro_def (NodeId id); + tl::optional<AttributeProcMacro &> + lookup_attribute_proc_macro_def (NodeId id); + + tl::optional<CustomDeriveProcMacro &> + lookup_derive_proc_macro_invocation (AST::SimplePath &invoc); + tl::optional<BangProcMacro &> + lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc_id); + tl::optional<AttributeProcMacro &> + lookup_attribute_proc_macro_invocation (AST::SimplePath &invoc); + void insert_derive_proc_macro_invocation (AST::SimplePath &invoc, + CustomDeriveProcMacro def); + void insert_bang_proc_macro_invocation (AST::MacroInvocation &invoc, + BangProcMacro def); + void insert_attribute_proc_macro_invocation (AST::SimplePath &invoc, + AttributeProcMacro def); +}; + class Mappings { public: @@ -115,6 +169,7 @@ public: ~Mappings (); CrateMappings crate_mapping; + ProcMacroMappings pmacro_mappings; NodeId get_next_node_id (); HirId get_next_hir_id () @@ -344,43 +399,6 @@ public: void insert_exported_macro (AST::MacroRulesDefinition &def); std::vector<AST::MacroRulesDefinition> get_exported_macros (); - void insert_derive_proc_macros (CrateNum num, - std::vector<CustomDeriveProcMacro> macros); - void insert_bang_proc_macros (CrateNum num, - std::vector<BangProcMacro> macros); - void insert_attribute_proc_macros (CrateNum num, - std::vector<AttributeProcMacro> macros); - - tl::optional<std::vector<CustomDeriveProcMacro> &> - lookup_derive_proc_macros (CrateNum num); - tl::optional<std::vector<BangProcMacro> &> - lookup_bang_proc_macros (CrateNum num); - tl::optional<std::vector<AttributeProcMacro> &> - lookup_attribute_proc_macros (CrateNum num); - - void insert_derive_proc_macro_def (CustomDeriveProcMacro macro); - void insert_bang_proc_macro_def (BangProcMacro macro); - void insert_attribute_proc_macro_def (AttributeProcMacro macro); - - tl::optional<CustomDeriveProcMacro &> - lookup_derive_proc_macro_def (NodeId id); - tl::optional<BangProcMacro &> lookup_bang_proc_macro_def (NodeId id); - tl::optional<AttributeProcMacro &> - lookup_attribute_proc_macro_def (NodeId id); - - tl::optional<CustomDeriveProcMacro &> - lookup_derive_proc_macro_invocation (AST::SimplePath &invoc); - tl::optional<BangProcMacro &> - lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc_id); - tl::optional<AttributeProcMacro &> - lookup_attribute_proc_macro_invocation (AST::SimplePath &invoc); - void insert_derive_proc_macro_invocation (AST::SimplePath &invoc, - CustomDeriveProcMacro def); - void insert_bang_proc_macro_invocation (AST::MacroInvocation &invoc, - BangProcMacro def); - void insert_attribute_proc_macro_invocation (AST::SimplePath &invoc, - AttributeProcMacro def); - void insert_visibility (NodeId id, Privacy::ModuleVisibility visibility); tl::optional<Privacy::ModuleVisibility &> lookup_visibility (NodeId id); @@ -491,20 +509,6 @@ private: std::map<NodeId, AST::MacroRulesDefinition *> macroInvocations; std::vector<AST::MacroRulesDefinition> exportedMacros; - // Procedural macros - std::map<CrateNum, std::vector<CustomDeriveProcMacro>> - procmacrosDeriveMappings; - std::map<CrateNum, std::vector<BangProcMacro>> procmacrosBangMappings; - std::map<CrateNum, std::vector<AttributeProcMacro>> - procmacrosAttributeMappings; - - std::map<NodeId, CustomDeriveProcMacro> procmacroDeriveMappings; - std::map<NodeId, BangProcMacro> procmacroBangMappings; - std::map<NodeId, AttributeProcMacro> procmacroAttributeMappings; - std::map<NodeId, CustomDeriveProcMacro> procmacroDeriveInvocations; - std::map<NodeId, BangProcMacro> procmacroBangInvocations; - std::map<NodeId, AttributeProcMacro> procmacroAttributeInvocations; - // Low level visibility map for each DefId std::map<NodeId, Privacy::ModuleVisibility> visibility_map;
