mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-20 16:31:04 +00:00
Packages are modules whose only job is to serve as a container for submodules. Modules like top_level.m, hlds.m, parse_tree.m and ll_backend.m are packages in this (informal) sense. Besides the include_module declarations for their submodules, most of the packages in the compiler used to import some modules, mostly other packages whose component modules their submodules may need. For example, ll_backend.m used to import parse_tree.m. This meant that modules in the ll_backend package did not have to import parse_tree.m before importing modules in the parse_tree package. However, this had a price. When we add a new module to the parse_tree package, parse_tree.int would change, and this would require the recompilation of ALL the modules in the ll_backend package, even the ones that did NOT import ANY of the modules in the parse_tree package. This happened even at one remove. Pretty much all modules in every one of the backend have to import one or more modules in the hlds package, and they therefore have import hlds.m. Since hlds.m imported transform_hlds.m, any addition of a new middle pass to the transform_hlds package required the recompilation of all backend modules, even in the usual case of the two having nothing to do with each other. This diff removes all import_module declarations from the packages, and replaces them with import_module declarations in the modules that need them. This includes only a SUBSET of their child modules and of the non-child modules that import them.
147 lines
6.1 KiB
Mathematica
147 lines
6.1 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 2014 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.
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% File: module_deps_graph.m.
|
|
%
|
|
% XXX document me.
|
|
% The contents of this module used to be in the old giant version of modules.m.
|
|
% If you want to understand it and clean it up, you will also want to look at
|
|
% deps_map.m, whose functionality seems to be quite related.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module parse_tree.module_deps_graph.
|
|
:- interface.
|
|
|
|
:- import_module mdbcomp.
|
|
:- import_module mdbcomp.sym_name.
|
|
:- import_module parse_tree.module_imports.
|
|
|
|
:- import_module digraph.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% (Module1 -> Module2) means Module1 is imported by Module2.
|
|
:- type deps_graph == digraph(module_name).
|
|
|
|
:- type lookup_module_and_imports == (func(module_name) = module_and_imports).
|
|
:- mode lookup_module_and_imports == in(func(in) = out is det).
|
|
|
|
:- pred add_module_and_imports_to_deps_graph(module_and_imports::in,
|
|
lookup_module_and_imports::lookup_module_and_imports,
|
|
deps_graph::in, deps_graph::out, deps_graph::in, deps_graph::out) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module set.
|
|
|
|
:- type deps_graph_key == digraph_key(module_name).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
add_module_and_imports_to_deps_graph(ModuleImports, LookupModuleImports,
|
|
!IntDepsGraph, !ImpDepsGraph) :-
|
|
% Add interface dependencies to the interface deps graph.
|
|
%
|
|
% Note that we need to do this both for the interface imports of this
|
|
% module and for the *implementation* imports of its ancestors.
|
|
% This is because if this module is defined in the implementation section
|
|
% of its parent, then the interface of this module may depend on things
|
|
% imported only by its parent's implementation.
|
|
%
|
|
% If this module was actually defined in the interface section of one
|
|
% of its ancestors, then it should only depend on the interface imports
|
|
% of that ancestor, so the dependencies added here are in fact more
|
|
% conservative than they need to be in that case. However, that should
|
|
% not be a major problem.
|
|
% XXX Actually, I (zs) think it is, because I suspect that after some
|
|
% source code changes, it can lead to the unnecessary recompilation
|
|
% of not just a few, but many modules.
|
|
|
|
ModuleName = ModuleImports ^ mai_module_name,
|
|
ParentDeps = ModuleImports ^ mai_parent_deps,
|
|
digraph.add_vertex(ModuleName, IntModuleKey, !IntDepsGraph),
|
|
add_int_deps(IntModuleKey, ModuleImports, !IntDepsGraph),
|
|
add_parent_imp_deps_set(LookupModuleImports, IntModuleKey, ParentDeps,
|
|
!IntDepsGraph),
|
|
|
|
% Add implementation dependencies to the implementation deps graph.
|
|
% (The implementation dependencies are a superset of the interface
|
|
% dependencies.)
|
|
%
|
|
% Note that we need to do this both for the imports of this module
|
|
% and for the imports of its parents, because this module may depend on
|
|
% things imported only by its parents.
|
|
|
|
digraph.add_vertex(ModuleName, ImpModuleKey, !ImpDepsGraph),
|
|
add_imp_deps(ImpModuleKey, ModuleImports, !ImpDepsGraph),
|
|
add_parent_imp_deps_set(LookupModuleImports, ImpModuleKey, ParentDeps,
|
|
!ImpDepsGraph).
|
|
|
|
% Add interface dependencies to the interface deps graph.
|
|
%
|
|
:- pred add_int_deps(deps_graph_key::in, module_and_imports::in,
|
|
deps_graph::in, deps_graph::out) is det.
|
|
|
|
add_int_deps(ModuleKey, ModuleImports, !DepsGraph) :-
|
|
AddDep = add_dep(ModuleKey),
|
|
set.fold(AddDep, ModuleImports ^ mai_parent_deps, !DepsGraph),
|
|
set.fold(AddDep, ModuleImports ^ mai_int_deps, !DepsGraph).
|
|
|
|
% Add direct implementation dependencies for a module to the
|
|
% implementation deps graph.
|
|
%
|
|
:- pred add_imp_deps(deps_graph_key::in, module_and_imports::in,
|
|
deps_graph::in, deps_graph::out) is det.
|
|
|
|
add_imp_deps(ModuleKey, ModuleImports, !DepsGraph) :-
|
|
% The implementation dependencies are a superset of the
|
|
% interface dependencies, so first we add the interface deps.
|
|
add_int_deps(ModuleKey, ModuleImports, !DepsGraph),
|
|
% then we add the impl deps
|
|
module_and_imports_get_imp_deps(ModuleImports, ImpDeps),
|
|
set.fold(add_dep(ModuleKey), ImpDeps, !DepsGraph).
|
|
|
|
% Add parent implementation dependencies for the given Parent module
|
|
% to the implementation deps graph values for the given ModuleKey.
|
|
%
|
|
:- pred add_parent_imp_deps(
|
|
lookup_module_and_imports::lookup_module_and_imports,
|
|
deps_graph_key::in, module_name::in, deps_graph::in, deps_graph::out)
|
|
is det.
|
|
|
|
add_parent_imp_deps(LookupModuleImports, ModuleKey, Parent, !DepsGraph) :-
|
|
ParentModuleImports = LookupModuleImports(Parent),
|
|
add_imp_deps(ModuleKey, ParentModuleImports, !DepsGraph).
|
|
|
|
:- pred add_parent_imp_deps_set(
|
|
lookup_module_and_imports::lookup_module_and_imports,
|
|
deps_graph_key::in, set(module_name)::in, deps_graph::in, deps_graph::out)
|
|
is det.
|
|
|
|
add_parent_imp_deps_set(LookupModuleImports, ModuleKey, Parents,
|
|
!DepsGraph) :-
|
|
set.fold(add_parent_imp_deps(LookupModuleImports, ModuleKey), Parents,
|
|
!DepsGraph).
|
|
|
|
% Add a single dependency to a graph.
|
|
%
|
|
:- pred add_dep(digraph_key(T)::in, T::in, digraph(T)::in, digraph(T)::out)
|
|
is det.
|
|
|
|
add_dep(ModuleKey, Dep, !DepsGraph) :-
|
|
digraph.add_vertex(Dep, DepKey, !DepsGraph),
|
|
digraph.add_edge(ModuleKey, DepKey, !DepsGraph).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- end_module parse_tree.module_deps_graph.
|
|
%-----------------------------------------------------------------------------%
|