mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-15 17:33:38 +00:00
... in some contexts.
compiler/unused_types.m:
Implement the easy part of the above, the part that happens
*after* we collect the info about which equivalence types are used.
Document why we can report unused equivalence types only in some contexts.
compiler/prog_data_used_modules.m:
Define an extension of the used_modules type that also records
which equivalence types were expanded in the module.
Define the operations we need on this extended type.
compiler/equiv_type.m:
Invoke one of those operations to record the expansions of equivalence
types, if requested to do so.
Inline the predicate that used to do this at its only call site.
compiler/hlds_module.m:
Replace the used_module field in the module_info with a value of
the extended type that includes not just the old used_modules info,
but also the set of expanded equivalence types.
Delete a utility predicate on the old field. The last call to this
predicate was deleted on 2022 march 30.
compiler/equiv_type_parse_tree.m:
compiler/make_hlds_passes.m:
compiler/mercury_compile_make_hlds.m:
compiler/unused_imports.m:
Conform to the changes above.
tests/warnings/warn_dead_procs.{m,err_exp}:
This test case already tests for warnings about unused du types.
Extend it to also test for unused eqv types.
149 lines
5.6 KiB
Mathematica
149 lines
5.6 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%---------------------------------------------------------------------------%
|
|
% Copyright (C) 2016, 2022, 2025-2026 The Mercury team.
|
|
% This file may only be copied under the terms of the GNU General
|
|
% Public License - see the file COPYING in the Mercury distribution.
|
|
%---------------------------------------------------------------------------%
|
|
%
|
|
% This module defines the used_modules type and its operations.
|
|
%
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- module parse_tree.prog_data_used_modules.
|
|
:- interface.
|
|
|
|
:- import_module mdbcomp.
|
|
:- import_module mdbcomp.sym_name.
|
|
:- import_module parse_tree.prog_data.
|
|
|
|
:- import_module set_tree234.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- type used_eqv_modules
|
|
---> used_eqv_modules(
|
|
% The equivalence types expanded in the interface
|
|
% and in implementation. Each expansion is a "use"
|
|
% of the type; we use this information to warn about
|
|
% unused local equivalence types.
|
|
int_eqv_type_ctors :: set_tree234(type_ctor),
|
|
imp_eqv_type_ctors :: set_tree234(type_ctor),
|
|
|
|
all_used_modules :: used_modules
|
|
).
|
|
|
|
:- type used_modules
|
|
---> used_modules(
|
|
% The modules used in the interface and implementation.
|
|
int_used_modules :: set_tree234(module_name),
|
|
imp_used_modules :: set_tree234(module_name)
|
|
).
|
|
|
|
:- type item_visibility
|
|
---> visibility_public
|
|
; visibility_private.
|
|
|
|
% Initialize the used_eqv_modules structure.
|
|
%
|
|
:- func used_eqv_modules_init = used_eqv_modules.
|
|
|
|
% Record a type_ctor, which should represent an equivalence type,
|
|
% as being expanded.
|
|
%
|
|
:- pred record_expanded_eqv_type_ctor(item_visibility::in, type_ctor::in,
|
|
used_eqv_modules::in, used_eqv_modules::out) is det.
|
|
|
|
% Given a sym_name, call record_module_and_ancestors_as_used on the module
|
|
% part of the name.
|
|
%
|
|
:- pred record_sym_name_module_as_used(item_visibility::in, sym_name::in,
|
|
used_modules::in, used_modules::out) is det.
|
|
|
|
% Given a module name, add the module and all of its parent modules
|
|
% to the used_modules.
|
|
%
|
|
:- pred record_module_and_ancestors_as_used(item_visibility::in, sym_name::in,
|
|
used_modules::in, used_modules::out) is det.
|
|
|
|
:- pred record_format_modules_as_used(used_modules::in, used_modules::out)
|
|
is det.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module mdbcomp.builtin_modules.
|
|
|
|
:- import_module list.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
|
|
used_eqv_modules_init = UsedEqvModules :-
|
|
UsedModules = used_modules(set_tree234.init, set_tree234.init),
|
|
UsedEqvModules = used_eqv_modules(set_tree234.init, set_tree234.init,
|
|
UsedModules).
|
|
|
|
record_expanded_eqv_type_ctor(Visibility, TypeCtor, !UsedEqvModules) :-
|
|
(
|
|
Visibility = visibility_public,
|
|
IntEqvTypeCtors0 = !.UsedEqvModules ^ int_eqv_type_ctors,
|
|
set_tree234.insert(TypeCtor, IntEqvTypeCtors0, IntEqvTypeCtors),
|
|
!UsedEqvModules ^ int_eqv_type_ctors := IntEqvTypeCtors
|
|
;
|
|
Visibility = visibility_private,
|
|
ImpEqvTypeCtors0 = !.UsedEqvModules ^ imp_eqv_type_ctors,
|
|
set_tree234.insert(TypeCtor, ImpEqvTypeCtors0, ImpEqvTypeCtors),
|
|
!UsedEqvModules ^ imp_eqv_type_ctors := ImpEqvTypeCtors
|
|
),
|
|
TypeCtor = type_ctor(SymName, _Arity),
|
|
UsedModules0 = !.UsedEqvModules ^ all_used_modules,
|
|
record_sym_name_module_as_used(Visibility, SymName,
|
|
UsedModules0, UsedModules),
|
|
!UsedEqvModules ^ all_used_modules := UsedModules.
|
|
|
|
record_sym_name_module_as_used(Visibility, SymName, !UsedModules) :-
|
|
(
|
|
SymName = unqualified(_)
|
|
;
|
|
SymName = qualified(ModuleName, _),
|
|
record_module_and_ancestors_as_used(Visibility, ModuleName,
|
|
!UsedModules)
|
|
).
|
|
|
|
record_module_and_ancestors_as_used(Visibility, ModuleName, !UsedModules) :-
|
|
(
|
|
Visibility = visibility_public,
|
|
IntUsedModules0 = !.UsedModules ^ int_used_modules,
|
|
add_module_and_ancestors(ModuleName, IntUsedModules0, IntUsedModules),
|
|
!UsedModules ^ int_used_modules := IntUsedModules
|
|
;
|
|
Visibility = visibility_private,
|
|
ImpUsedModules0 = !.UsedModules ^ imp_used_modules,
|
|
add_module_and_ancestors(ModuleName, ImpUsedModules0, ImpUsedModules),
|
|
!UsedModules ^ imp_used_modules := ImpUsedModules
|
|
).
|
|
|
|
:- pred add_module_and_ancestors(sym_name::in,
|
|
set_tree234(module_name)::in, set_tree234(module_name)::out) is det.
|
|
|
|
add_module_and_ancestors(ModuleName, !UsedModuleNames) :-
|
|
set_tree234.insert(ModuleName, !UsedModuleNames),
|
|
(
|
|
ModuleName = unqualified(_)
|
|
;
|
|
ModuleName = qualified(ParentModuleName, _),
|
|
add_module_and_ancestors(ParentModuleName, !UsedModuleNames)
|
|
).
|
|
|
|
record_format_modules_as_used(!UsedModules) :-
|
|
ImpUsedModules0 = !.UsedModules ^ imp_used_modules,
|
|
FormatModules = [mercury_string_format_module,
|
|
mercury_string_parse_util_module, mercury_stream_module],
|
|
set_tree234.insert_list(FormatModules, ImpUsedModules0, ImpUsedModules),
|
|
!UsedModules ^ imp_used_modules := ImpUsedModules.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
:- end_module parse_tree.prog_data_used_modules.
|
|
%---------------------------------------------------------------------------%
|