Skip to content

Attempt to clean up hir map - #4878

Draft
P-E-P wants to merge 5 commits into
Rust-GCC:masterfrom
P-E-P:hir_map_refactor
Draft

P-E-P wants to merge 5 commits into
Rust-GCC:masterfrom
P-E-P:hir_map_refactor

Conversation

@P-E-P

@P-E-P P-E-P commented Sep 10, 2026

Copy link
Copy Markdown
Member

Experimental draft PR to gather feedback

The Mapping class within rust-hir-map is slowly growing. A full clean up might be required in the future but in the meantime we may regroup related fields within specific classes.

I'm still unsure about the whole PtrMapping thing. I'd like to find a way to unify references and pointer mappings under the same class and then specialize it over the Id key.

HIR mappings are getting bigger, this commit attempts to regroup crate
related mappings within a single class.

gcc/rust/ChangeLog:

	* ast/rust-collect-lang-items.cc (CollectLangItems::visit):
	* backend/rust-compile-expr.cc:
	* backend/rust-compile-type.cc (TyTyResolveCompile::get_unit_type):
	* checks/errors/borrowck/rust-borrow-checker.cc (BorrowChecker::go):
	* checks/errors/privacy/rust-visibility-resolver.cc (VisibilityResolver::resolve_module_path):
	* expand/rust-macro-substitute-ctx.cc:
	* hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_lifetime):
	(ASTLoweringBase::lower_loop_label):
	(ASTLoweringBase::lower_path_expr_seg):
	(ASTLoweringBase::lower_self):
	(ASTLoweringBase::lower_extern_block):
	* hir/rust-ast-lower-block.h:
	* hir/rust-ast-lower-enumitem.h:
	* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit):
	* hir/rust-ast-lower-extern.h:
	* hir/rust-ast-lower-implitem.cc (ASTLowerImplItem::visit):
	(ASTLowerTraitItem::visit):
	* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit):
	(ASTLoweringSimplePath::lower):
	* hir/rust-ast-lower-pattern.cc (ASTLoweringPattern::visit):
	* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit):
	* hir/rust-ast-lower-type.cc (ASTLowerTypePath::visit):
	(ASTLowerQualifiedPathInType::visit):
	(ASTLoweringType::visit):
	(ASTLowerGenericParam::visit):
	(ASTLoweringTypeBounds::visit):
	(ASTLowerWhereClauseItem::visit):
	* hir/rust-ast-lower.cc (ASTLowering::go):
	(ASTLoweringBlock::visit):
	(ASTLoweringIfBlock::visit):
	(ASTLoweringIfLetBlock::desugar_iflet):
	(ASTLoweringIfLetBlock::visit):
	(ASTLowerStructExprField::visit):
	(ASTLoweringExprWithBlock::visit):
	(ASTLowerPathInExpression::visit):
	(ASTLoweringBase::lower_qual_path_type):
	(ASTLowerQualPathInExpression::visit):
	* metadata/rust-export-metadata.cc (PublicInterface::gather_export_data):
	(PublicInterface::write_to_object_file):
	(PublicInterface::write_to_path):
	(PublicInterface::expected_metadata_filename):
	* resolve/rust-default-resolver.cc (DefaultResolver::visit):
	* resolve/rust-forever-stack.h:
	* resolve/rust-name-resolution-context.h:
	* rust-session-manager.cc (Session::handle_crate_name):
	(Session::compile_crate):
	(Session::load_extern_crate):
	* typecheck/rust-hir-type-check-base.cc (TypeCheckBase::resolve_literal):
	* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit):
	(TypeCheckExpr::resolve_fn_trait_call):
	* typecheck/rust-hir-type-check-implitem.cc (TypeCheckTopLevelExternItem::visit):
	(TypeCheckImplItem::resolve_function_signature):
	* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit):
	* typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::resolve):
	* typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn):
	* typecheck/rust-type-util.cc (query_type):
	* typecheck/rust-tyty-bounds.cc:
	* typecheck/rust-tyty-subst.cc (SubstitutionParamMapping::override_context):
	* typecheck/rust-tyty-util.cc (TyVar::monomorphized_clone):
	* typecheck/rust-tyty.cc (InferType::clone):
	(VariantDef::clone):
	(VariantDef::monomorphized_clone):
	(ConstInferType::clone):
	* util/rust-hir-map.cc (CrateMappings::CrateMappings):
	(Mappings::Mappings):
	(Mappings::get_next_crate_num):
	(CrateMappings::get_next_crate_num):
	(Mappings::set_current_crate):
	(CrateMappings::set_current_crate):
	(Mappings::get_current_crate):
	(CrateMappings::get_current_crate):
	(Mappings::get_crate_name):
	(CrateMappings::get_crate_name):
	(Mappings::lookup_crate_num):
	(CrateMappings::lookup_crate_num):
	(Mappings::set_crate_name):
	(CrateMappings::set_crate_name):
	(Mappings::get_current_crate_name):
	(CrateMappings::get_current_crate_name):
	(Mappings::lookup_crate_name):
	(CrateMappings::lookup_crate_name):
	(Mappings::crate_num_to_nodeid):
	(CrateMappings::crate_num_to_nodeid):
	(Mappings::node_is_crate):
	(CrateMappings::node_is_crate):
	(Mappings::get_ast_crate):
	(CrateMappings::get_ast_crate):
	(Mappings::get_ast_crate_by_node_id):
	(Mappings::get_ast_crate_by_node_id_raw):
	(CrateMappings::get_ast_crate_by_node_id_raw):
	(Mappings::insert_ast_crate):
	(CrateMappings::insert_ast_crate):
	(Mappings::insert_macro_def):
	(Mappings::insert_glob_container):
	* util/rust-hir-map.h (class Mappings):
	(class CrateMappings):

Signed-off-by: Pierre-Emmanuel Patry <pierre.emmanuel.patry@opensrcsec.com>
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):
	* resolve/rust-early-name-resolver-2.0.cc (Early::visit_derive_attribute):
	(Early::visit_non_builtin_attribute):
	* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit_extern_crate):
	* rust-session-manager.cc (Session::load_extern_crate):
	* util/rust-hir-map.cc (Mappings::insert_derive_proc_macros):
	(Mappings::insert_bang_proc_macros):
	(ProcMacroMappings::insert_bang_proc_macros):
	(Mappings::insert_attribute_proc_macros):
	(Mappings::lookup_derive_proc_macros):
	(ProcMacroMappings::lookup_derive_proc_macros):
	(Mappings::lookup_bang_proc_macros):
	(ProcMacroMappings::lookup_bang_proc_macros):
	(Mappings::lookup_attribute_proc_macros):
	(ProcMacroMappings::lookup_attribute_proc_macros):
	(Mappings::insert_derive_proc_macro_def):
	(ProcMacroMappings::insert_derive_proc_macro_def):
	(Mappings::insert_bang_proc_macro_def):
	(ProcMacroMappings::insert_bang_proc_macro_def):
	(Mappings::insert_attribute_proc_macro_def):
	(ProcMacroMappings::insert_attribute_proc_macro_def):
	(Mappings::lookup_derive_proc_macro_def):
	(ProcMacroMappings::lookup_derive_proc_macro_def):
	(Mappings::lookup_bang_proc_macro_def):
	(ProcMacroMappings::lookup_bang_proc_macro_def):
	(Mappings::lookup_attribute_proc_macro_def):
	(ProcMacroMappings::lookup_attribute_proc_macro_def):
	(Mappings::insert_derive_proc_macro_invocation):
	(Mappings::lookup_derive_proc_macro_invocation):
	(ProcMacroMappings::lookup_derive_proc_macro_invocation):
	(Mappings::insert_bang_proc_macro_invocation):
	(Mappings::lookup_bang_proc_macro_invocation):
	(Mappings::insert_attribute_proc_macro_invocation):
	(Mappings::lookup_attribute_proc_macro_invocation):
	* util/rust-hir-map.h (class ProcMacroMappings):

Signed-off-by: Pierre-Emmanuel Patry <pierre.emmanuel.patry@opensrcsec.com>
Those functions are part of a proc macro context and no longer require
an explicit long name about their context.

gcc/rust/ChangeLog:

	* expand/rust-macro-expand.h (struct MacroExpander):
	* resolve/rust-early-name-resolver-2.0.cc (Early::visit_derive_attribute):
	(Early::visit_non_builtin_attribute):
	* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit_extern_crate):
	* util/rust-hir-map.cc (ProcMacroMappings::insert_bang_proc_macros):
	(ProcMacroMappings::lookup_derive_proc_macros):
	(ProcMacroMappings::lookup_bang_proc_macros):
	(ProcMacroMappings::lookup_attribute_proc_macros):
	(ProcMacroMappings::insert_derive_proc_macro_def):
	(ProcMacroMappings::insert_derive_def):
	(ProcMacroMappings::insert_bang_proc_macro_def):
	(ProcMacroMappings::insert_bang_def):
	(ProcMacroMappings::insert_attribute_proc_macro_def):
	(ProcMacroMappings::insert_attribute_def):
	(ProcMacroMappings::lookup_derive_proc_macro_def):
	(ProcMacroMappings::lookup_derive_def):
	(ProcMacroMappings::lookup_bang_proc_macro_def):
	(ProcMacroMappings::lookup_bang_def):
	(ProcMacroMappings::lookup_attribute_proc_macro_def):
	(ProcMacroMappings::lookup_attribute_def):
	(ProcMacroMappings::insert_derive_invocation):
	(ProcMacroMappings::lookup_derive_proc_macro_invocation):
	(ProcMacroMappings::lookup_derive_invocation):
	(ProcMacroMappings::insert_bang_invocation):
	(ProcMacroMappings::lookup_bang_invocation):
	(ProcMacroMappings::insert_attribute_invocation):
	(ProcMacroMappings::lookup_attribute_invocation):
	* util/rust-hir-map.h (class ProcMacroMappings):

Signed-off-by: Pierre-Emmanuel Patry <pierre.emmanuel.patry@opensrcsec.com>
gcc/rust/ChangeLog:

	* checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::check_violation_inner):
	* checks/errors/privacy/rust-pub-restricted-visitor.cc (PubRestrictedVisitor::is_restriction_valid):
	* checks/errors/privacy/rust-visibility-resolver.cc (VisibilityResolver::go):
	(VisibilityResolver::resolve_and_update):
	(VisibilityResolver::visit):
	* util/rust-hir-map.cc (Mappings::insert_visibility):
	(Mappings::lookup_visibility):
	* util/rust-hir-map.h (class ASTMappings):

Signed-off-by: Pierre-Emmanuel Patry <pierre.emmanuel.patry@opensrcsec.com>
HIR map is getting bigger and most lookup/insert functions are similar,
this commit regroup all simple HIR related mappings under one field.

gcc/rust/ChangeLog:

	* backend/rust-compile-item.cc (CompileItem::visit):
	* backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile):
	* backend/rust-mangle-v0.cc (v0_path):
	* checks/errors/privacy/rust-visibility-resolver.cc (VisibilityResolver::resolve_module_path):
	* checks/errors/rust-const-checker.cc (ConstChecker::check_function_call):
	* checks/errors/rust-readonly-check.cc (ReadonlyChecker::visit):
	* checks/errors/rust-unsafe-checker.cc (UnsafeChecker::check_use_of_static):
	(UnsafeChecker::check_function_call):
	(UnsafeChecker::check_function_attr):
	* checks/lints/rust-lint-marklive.cc (MarkLive::go):
	(MarkLive::visit):
	* checks/lints/unused/rust-unused-checker.cc (UnusedChecker::visit):
	* hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_extern_block):
	* hir/rust-ast-lower-block.h:
	* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::translate):
	* hir/rust-ast-lower-implitem.cc (ASTLowerImplItem::visit):
	(ASTLowerTraitItem::translate):
	(ASTLowerTraitItem::visit):
	* hir/rust-ast-lower-item.cc (ASTLoweringItem::translate):
	(ASTLoweringItem::visit):
	* hir/rust-ast-lower-pattern.cc (ASTLoweringPattern::translate):
	* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::translate):
	* hir/rust-ast-lower-struct-field-expr.h:
	* hir/rust-ast-lower-type.cc (ASTLoweringType::translate):
	(ASTLowerGenericParam::translate):
	* hir/rust-ast-lower.cc (ASTLowerPathInExpression::visit):
	(ASTLowerQualPathInExpression::visit):
	* hir/tree/rust-hir-pattern-abstract.cc (PathPattern::is_refutable):
	* typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_path_to_trait):
	* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit):
	(TypeCheckExpr::resolve_root_path):
	* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit):
	* typecheck/rust-hir-type-check-type.cc (TypeCheckType::resolve_root_path):
	* typecheck/rust-type-util.cc (query_type):
	* util/rust-hir-map.cc (Mappings::insert_hir_item):
	(Mappings::lookup_hir_item):
	(Mappings::insert_hir_trait_item):
	(Mappings::lookup_hir_trait_item):
	(Mappings::insert_hir_extern_block):
	(Mappings::lookup_hir_extern_block):
	(Mappings::insert_module):
	(Mappings::lookup_module):
	(Mappings::insert_hir_expr):
	(Mappings::lookup_hir_expr):
	(Mappings::insert_hir_path_expr_seg):
	(Mappings::lookup_hir_path_expr_seg):
	(Mappings::insert_hir_generic_param):
	(Mappings::lookup_hir_generic_param):
	(Mappings::insert_hir_type):
	(Mappings::lookup_hir_type):
	(Mappings::insert_hir_stmt):
	(Mappings::lookup_hir_stmt):
	(Mappings::insert_hir_param):
	(Mappings::lookup_hir_param):
	(Mappings::insert_hir_self_param):
	(Mappings::lookup_hir_self_param):
	(Mappings::insert_hir_struct_field):
	(Mappings::lookup_hir_struct_field):
	(Mappings::insert_hir_pattern):
	(Mappings::lookup_hir_pattern):
	(Mappings::resolve_nodeid_to_stmt):
	(Mappings::build_impl_indexes):
	(Mappings::insert_trait_item_mapping):
	* util/rust-hir-map.h (class HIRMappings):
	* util/rust-hir-map.hxx: New file.

Signed-off-by: Pierre-Emmanuel Patry <pierre.emmanuel.patry@opensrcsec.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant