mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-20 11:54:02 +00:00
Right now, most parts of the compiler write to the "current output stream".
This was a pragmatic choice at the time, but has not aged well. The problem
is that the answer to the question "where is the current output stream going?"
is not obvious in *all* places in the compiler (although it is obvious in
most). When using such implicit streams, finding where the output is going
to in a given predicate requires inspecting not just the ancestors of that
predicate, but also all their older siblings (since any of them could have
changed the current stream), *including* their entire call trees. This is
usually an infeasible task. By constrast, if we explicitly pass streams
to all output operations, we need only follow the places where the variable
representing that stream is bound, which the mode system makes easy.
This diff switches large parts of the compiler over to doing output only
to explicitly passed streams, never to the implicit "current output stream".
The parts it switches over are the parts that rely to a significant degree
on the innermost change, which is to the "output" typeclass in
parse_tree_out_info.m. This is the part that has to be switched over to
explicit streams first, because (a) many modules such as mercury_to_mercury.m
rely on the output typeclass, and (b) most other modules that do output
call predicates in these modules. Starting anywhere else would be like
building a skyscraper starting at the top.
This typeclass, output(U), has two instances: output(io), and output(string),
so you could output either to the current output stream, or to a string.
To allow the specification of the destination stream in the first case,
this diff changes the typeclass to output(S, U) with a functional dependency
from U to S, with the two instances being output(io.text_output_stream, io)
and output(unit, string). (The unit arg is ignored in the second case.)
There is a complication with the output typeclass method, add_list, that
outputs a list of items. The complication is that each item is output
by a predicate supplied by the caller, but the separator between the items
(usually a comma) is output by add_list itself. We don't want to give
callers of this method the opportunity to screw up by specifying (possibly
implicitly) two different output streams for these two purposes, so we want
(a) the caller to tell add_list where to put the separators, and then
(b) for add_list, not its caller, tell the user-supplied predicate what
stream to write to. This works only if the stream argument is just before
the di,uo pair of I/O state arguments, which differs from our usual practice
of passing the stream at or near the left edge of the argument list,
not near the right. The result of this complication is that two categories
of predicates that are and are not used to print items in a list differ
in where they put the stream in their argument lists. This makes it easy
to pass the stream in the wrong argument position if you call a predicate
without looking up its signature, and may require *changing* the argument
order when a predicate is used to print an item in a list for the first time.
A complete switch over to always passing the stream just before !IO
would fix this inconsistency, but is far to big a change to make all at once.
compiler/parse_tree_out_info.m:
Make the changes described above.
Add write_out_list, which is a variant of io.write_list specifically
designed to address the "complication" described above. It also has
the arguments in an order that is better suited for higher-order use.
Make the same change to argument order in the class method add_list
as well.
Almost all of the following changes consist of passing an extra stream
argument to output predicates. In some places, where I thought this would
aid readability, I replaced sequences of calls to output predicates
with a single io.format.
compiler/prog_out.m:
This module had many predicates that wrote things to the current output
stream. This diff adds versions of these predicates that take an
explicit stream argument.
If the originals are still needed after the changes to the other modules,
keep them, but add "_to_cur_stream" to the end of their names.
Otherwise, delete them. (Many of the changes below replace
write_xyz(..., !IO) with io.write_string(Stream, xyz_to_string(...), !IO),
especially when write_xyz did nothing except call xyz_to_string
and wrote out the result.)
compiler/c_util.m:
Add either an explicit stream argument to the argument list, or a
"_current_stream" suffix to the name, of every predicate defined
in this module that does output.
Add a new predicate to print out the block comment containing
input for mkinit. This factors out common code in the LLDS and MLDS
backends.
compiler/name_mangle.m:
Delete all predicates that used to write to the current output stream,
after replacing them if necessary with functions that return a string,
which the caller can print to wherever it wants. (The "if necessary"
part is there because some of the "replacement" functions already
existed.)
When converting a proc_label to a string, *always* require the caller
to say whether the label prefix should be added to the string,
instead of silently assuming "yes, add it", as calls to one of the old,
now deleted predicates had it.
compiler/file_util.m:
Add output_to_file_stream, a version of output_to_file which
simply passes the output file stream it opens to the predicate
that is intended to define the contents of the newly created or
updated file. The existing output_to_file, which instead sets
and resets the current output stream around the equivalent
predicate call, is still needed e.g. by the MLDS backend,
but hopefully for not too long.
compiler/mercury_to_mercury.m:
compiler/parse_tree_out.m:
compiler/parse_tree_out_clause.m:
compiler/parse_tree_out_inst.m:
compiler/parse_tree_out_pragma.m:
compiler/parse_tree_out_pred_decl.m:
compiler/parse_tree_out_term.m:
compiler/parse_tree_out_type_repn.m:
Change the code writing out parse trees to explicitly pass a stream
to every predicate that does output.
In some places, this allows us to avoid changing the identity
of the current output stream.
compiler/hlds_out.m:
compiler/hlds_out_goal.m:
compiler/hlds_out_mode.m:
compiler/hlds_out_module.m:
compiler/hlds_out_pred.m:
compiler/hlds_out_util.m:
compiler/intermod.m:
Change the code writing out HLDS code to explicitly pass a stream
to every predicate that does output. (The changes to these modules
belong in this diff because these modules call many of the output
predicates in the parse tree package.)
In hlds_out_util.m, delete some write_to_xyz(...) predicates that wrote
the result of xyz_to_string(...) to the current output stream.
Replace calls to the deleted predicates with calls to io.write_string
with the string being written being computed by xyz_to_string.
Add a predicate to hlds_out_util.m that outputs a comment containing
the current context, if it is valid. This factors out code that used
to be common to several of the other modules.
In a few places in hlds_out_module.m, the new code generates a
slighly different set of blank lines, but this should not be a problem.
compiler/layout_out.m:
compiler/llds_out_code_addr.m:
compiler/llds_out_data.m:
compiler/llds_out_file.m:
compiler/llds_out_global.m:
compiler/llds_out_instr.m:
compiler/llds_out_util.m:
compiler/opt_debug.m:
compiler/rtti_out.m:
Change the code writing out the LLDS to explicitly pass a stream
to every predicate that does output. (The changes to these modules
belong in this diff because layout_out.m and rtti_out.m call
many of the output predicates in the parse tree package,
and through them, the rest of the LLDS backend is affected as well.)
compiler/make.module_dep_file.m:
compiler/mercury_compile_main.m:
compiler/mercury_compile_middle_passes.m:
Replace code that sets and resets the current output stream
with code that simply passes an explicit output stream to a
predicate that now *takes* an explicit stream as an argument.
compiler/accumulator.m:
compiler/add_clause.m:
compiler/code_gen.m:
compiler/code_loc_dep.m:
compiler/cse_detection.m:
compiler/delay_partial_inst.m:
compiler/dep_par_conj.m:
compiler/det_analysis.m:
compiler/error_msg_inst.m:
compiler/export.m:
compiler/format_call.m:
compiler/goal_expr_to_goal.m:
compiler/ite_gen.m:
compiler/lco.m:
compiler/liveness.m:
compiler/lp_rational.m:
compiler/mercury_compile_front_end.m:
compiler/mercury_compile_llds_back_end.m:
compiler/mlds_to_c_file.m:
compiler/mlds_to_c_global.m:
compiler/mode_debug.m:
compiler/mode_errors.m:
compiler/modes.m:
compiler/optimize.m:
compiler/passes_aux.m:
compiler/pd_debug.m:
compiler/pragma_c_gen.m:
compiler/proc_gen.m:
compiler/prog_ctgc.m:
compiler/push_goals_together.m:
compiler/rat.m:
compiler/recompilation.m:
compiler/recompilation.usage.m:
compiler/recompilation.version.m:
compiler/rtti.m:
compiler/saved_vars.m:
compiler/simplify_goal_conj.m:
compiler/stack_opt.m:
compiler/structure_reuse.analysis.m:
compiler/structure_reuse.domain.m:
compiler/structure_reuse.indirect.m:
compiler/structure_sharing.analysis.m:
compiler/superhomogeneous.m:
compiler/term_constr_build.m:
compiler/term_constr_data.m:
compiler/term_constr_fixpoint.m:
compiler/term_constr_pass2.m:
compiler/term_constr_util.m:
compiler/tupling.m:
compiler/type_assign.m:
compiler/unneeded_code.m:
compiler/write_deps_file.m:
Conform to the changes above, mostly by passing streams explicitly.
compiler/hlds_dependency_graph.m:
Conform to the changes above, mostly by passing streams explicitly.
Move a predicate's definition next it only use.
compiler/Mercury.options:
Specify --warn-implicit-stream-calls for all the modules in which
this diff has replaced all implicit streams with explicit streams.
(Unfortunately, debugging this diff has shown that --warn-implicit-
stream-calls detects only *some*, and not *all*, uses of implicit
streams.)
library/term_io.m:
Fix documentation.
542 lines
20 KiB
Mathematica
542 lines
20 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 2001-2007, 2011 The University of Melbourne.
|
|
% This file may only be copied under the terms of the GNU General
|
|
% Public License - see the file COPYING in the Mercury distribution.
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% File: recompilation.m.
|
|
% Main author: stayl.
|
|
%
|
|
% Type declarations for smart recompilation.
|
|
% Predicates to record program items used by a compilation.
|
|
%
|
|
% A module must be recompiled if
|
|
% - The file itself has changed.
|
|
% - An imported item used in compiling the module has changed or been removed.
|
|
% - An item has been added to an imported module which could cause an
|
|
% ambiguity with an item used in compiling the module.
|
|
%
|
|
% Currently smart recompilation does not work properly with
|
|
% inter-module optimization. If a `.opt' file changes, all modules
|
|
% importing it need to be recompiled.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module recompilation.
|
|
:- interface.
|
|
|
|
:- import_module libs.
|
|
:- import_module libs.timestamp.
|
|
:- import_module mdbcomp.
|
|
:- import_module mdbcomp.prim_data.
|
|
:- import_module mdbcomp.sym_name.
|
|
:- import_module parse_tree.
|
|
:- import_module parse_tree.prog_data.
|
|
|
|
:- import_module io.
|
|
:- import_module map.
|
|
:- import_module maybe.
|
|
:- import_module pair.
|
|
:- import_module set.
|
|
:- import_module term.
|
|
|
|
:- include_module recompilation.check.
|
|
:- include_module recompilation.usage.
|
|
:- include_module recompilation.version.
|
|
|
|
% Identify a particular version of a program item.
|
|
% This could be done using a timestamp or a hash value.
|
|
:- type version_number == timestamp.
|
|
|
|
:- func term_to_version_number(term(T)) = version_number is semidet.
|
|
|
|
:- func term_to_timestamp(term(T)) = timestamp is semidet.
|
|
|
|
:- pred write_version_number(io.text_output_stream::in, version_number::in,
|
|
io::di, io::uo) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% XXX ITEM_LIST Choose a base name for these types that DOESN'T clash
|
|
% with the item type in the parse tree. While the types here are closely
|
|
% related to prog_item.item, they are NOT the same. Using the same name
|
|
% here encourages thinking that they are, which may lead to bugs.
|
|
%
|
|
% XXX ITEM_LIST Document what prog_item.item, or what sequence of
|
|
% prog_item.items, each item_type may correspond to.
|
|
|
|
:- type item_id
|
|
---> item_id(item_type, item_name).
|
|
|
|
:- type item_name
|
|
---> item_name(sym_name, arity).
|
|
|
|
:- type item_type
|
|
---> type_abstract_item
|
|
% Just the name of the type, not its body. It is common
|
|
% for a value of a type to be passed through a predicate without
|
|
% inspecting the value -- such predicates do not need to be
|
|
% recompiled if the body of the type changes (except for
|
|
% equivalence types).
|
|
; type_body_item
|
|
; mode_item
|
|
; inst_item
|
|
; typeclass_item
|
|
; functor_item % The RHS of a var-functor unification.
|
|
; predicate_item
|
|
; function_item
|
|
; mutable_item
|
|
; foreign_proc_item.
|
|
|
|
:- inst simple_item for item_type/0
|
|
---> type_abstract_item
|
|
; type_body_item
|
|
; mode_item
|
|
; inst_item
|
|
; typeclass_item.
|
|
|
|
:- inst pred_or_func_item for item_type/0
|
|
---> predicate_item
|
|
; function_item.
|
|
|
|
:- func pred_or_func_to_item_type(pred_or_func::in)
|
|
= (item_type::out(pred_or_func_item)) is det.
|
|
|
|
:- pred is_simple_item_type(item_type::(ground >> simple_item)) is semidet.
|
|
|
|
:- pred is_pred_or_func_item_type(item_type::(ground >> pred_or_func_item))
|
|
is semidet.
|
|
|
|
:- pred string_to_item_type(string, item_type).
|
|
:- mode string_to_item_type(in, out) is semidet.
|
|
:- mode string_to_item_type(out, in) is det.
|
|
|
|
:- func type_ctor_to_item_name(type_ctor) = item_name.
|
|
:- func inst_ctor_to_item_name(inst_ctor) = item_name.
|
|
:- func mode_ctor_to_item_name(mode_ctor) = item_name.
|
|
|
|
:- func item_name_to_type_ctor(item_name) = type_ctor.
|
|
:- func item_name_to_inst_ctor(item_name) = inst_ctor.
|
|
:- func item_name_to_mode_ctor(item_name) = mode_ctor.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- type recompilation_info
|
|
---> recompilation_info(
|
|
% Name of the current module.
|
|
recomp_module_name :: module_name,
|
|
|
|
% Used items imported from other modules.
|
|
recomp_used_items :: used_items,
|
|
|
|
% For now we only record dependencies of imported items
|
|
% on equivalence types. The rest of the dependencies can be
|
|
% found by examining the pred_infos, type_defns etc of the
|
|
% items recorded in the used_items field above.
|
|
recomp_dependencies :: map(item_id, set(item_id)),
|
|
|
|
recomp_version_numbers :: map(module_name, version_numbers)
|
|
).
|
|
|
|
:- func init_recompilation_info(module_name) = recompilation_info.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- type item_id_set(Map, Set, Cons)
|
|
---> item_id_set(
|
|
types :: Map,
|
|
type_bodies :: Map,
|
|
modes :: Map,
|
|
insts :: Map,
|
|
typeclasses :: Map,
|
|
functors :: Cons,
|
|
predicates :: Set,
|
|
functions :: Set,
|
|
mutables :: Set,
|
|
foreign_procs :: Set
|
|
).
|
|
|
|
:- type item_id_set(T) == item_id_set(T, T, T).
|
|
|
|
:- func init_item_id_set(T) = item_id_set(T).
|
|
|
|
:- func init_item_id_set(Simple, PorF, Cons) = item_id_set(Simple, PorF, Cons).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% A simple_item_set records the single possible match for an item.
|
|
%
|
|
:- type simple_item_set ==
|
|
map(pair(string, arity), map(module_qualifier, module_name)).
|
|
|
|
% For constructors, predicates and functions, we can't work out
|
|
% which item is actually used until we have run typechecking.
|
|
%
|
|
:- type pred_or_func_set == simple_item_set.
|
|
|
|
:- type functor_set == simple_item_set.
|
|
|
|
% Items which are used by local items.
|
|
:- type used_items ==
|
|
item_id_set(
|
|
simple_item_set,
|
|
pred_or_func_set,
|
|
functor_set
|
|
).
|
|
|
|
:- func init_used_items = used_items.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%
|
|
% Access functions for item_id_sets.
|
|
%
|
|
|
|
:- func extract_simple_item_set(item_id_set(Simple, PorF, Cons)::in,
|
|
item_type::in(simple_item)) = (Simple::out) is det.
|
|
|
|
:- pred update_simple_item_set(item_type::in(simple_item), Simple::in,
|
|
item_id_set(Simple, PorF, Cons)::in,
|
|
item_id_set(Simple, PorF, Cons)::out) is det.
|
|
|
|
:- func extract_pred_or_func_set(item_id_set(Simple, PorF, Cons)::in,
|
|
item_type::in(pred_or_func_item)) = (PorF::out) is det.
|
|
|
|
:- pred update_pred_or_func_set(item_type::in(pred_or_func_item), PorF::in,
|
|
item_id_set(Simple, PorF, Cons)::in,
|
|
item_id_set(Simple, PorF, Cons)::out) is det.
|
|
|
|
:- func extract_ids(item_id_set(T), item_type) = T.
|
|
|
|
:- pred update_ids(item_type::in, T::in,
|
|
item_id_set(T)::in, item_id_set(T)::out) is det.
|
|
|
|
:- func map_ids((func(item_type, T) = U), item_id_set(T), U) = item_id_set(U).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% Version numbers for items in a single module.
|
|
:- type version_numbers
|
|
---> version_numbers(
|
|
item_version_numbers,
|
|
instance_version_numbers
|
|
).
|
|
|
|
% Map modules' names to their version number info.
|
|
:- type module_version_numbers_map == map(module_name, version_numbers).
|
|
|
|
% The constructors set should always be empty -
|
|
% constructors are never imported separately.
|
|
:- type item_version_numbers == item_id_set(version_number_map).
|
|
|
|
:- type version_number_map == map(pair(string, arity), version_number).
|
|
|
|
% For each interface file, we keep a version number for each class.
|
|
:- type instance_version_numbers == map(item_name, version_number).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% unqualified("") if the symbol was unqualified.
|
|
:- type module_qualifier == module_name.
|
|
|
|
:- func find_module_qualifier(sym_name) = module_qualifier.
|
|
|
|
:- func module_qualify_name(module_qualifier, string) = sym_name.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% recompilation.add_used_item(ItemType, UnqualifiedId, QualifiedId,
|
|
% !Info).
|
|
%
|
|
% Record a reference to UnqualifiedId, for which QualifiedId
|
|
% is the only match. If a new declaration is added so that
|
|
% QualifiedId is not the only match, we need to recompile.
|
|
%
|
|
:- pred record_used_item(item_type::in, item_name::in, item_name::in,
|
|
recompilation_info::in, recompilation_info::out) is det.
|
|
|
|
% For each imported item we need to record which equivalence types
|
|
% are used because equiv_type.m removes all references to the
|
|
% equivalence types, and at that point we don't know which imported
|
|
% items are going to be used by the compilation.
|
|
%
|
|
% For predicates declared using `with_type` annotations,
|
|
% the version number in the interface file and the
|
|
% version_numbers map will refer to the arity before expansion
|
|
% of the `with_type` annotation, so that needs to be recorded
|
|
% here as well.
|
|
%
|
|
:- pred record_expanded_items(item_id::in, set(item_id)::in,
|
|
recompilation_info::in, recompilation_info::out) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- type eqv_expanded_info == maybe(eqv_expanded_item_set).
|
|
:- type eqv_expanded_item_set
|
|
---> eqv_expanded_item_set(module_name, set(item_id)).
|
|
|
|
% For smart recompilation we need to record which items were expanded
|
|
% in each declaration. Any items which depend on that declaration also
|
|
% depend on the expanded items.
|
|
%
|
|
:- pred maybe_start_recording_expanded_items(module_name::in, sym_name::in,
|
|
maybe(recompilation_info)::in, eqv_expanded_info::out) is det.
|
|
|
|
:- pred record_expanded_item(item_id::in,
|
|
eqv_expanded_info::in, eqv_expanded_info::out) is det.
|
|
|
|
% Record all the expanded items in the recompilation_info.
|
|
%
|
|
:- pred finish_recording_expanded_items(item_id::in, eqv_expanded_info::in,
|
|
maybe(recompilation_info)::in, maybe(recompilation_info)::out) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module list.
|
|
:- import_module require.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
term_to_version_number(Term) = term_to_timestamp(Term).
|
|
|
|
term_to_timestamp(term.functor(term.string(TimestampString), [], _)) =
|
|
string_to_timestamp(TimestampString).
|
|
|
|
write_version_number(Stream, VersionNumber, !IO) :-
|
|
io.write_string(Stream, """", !IO),
|
|
io.write_string(Stream, timestamp_to_string(VersionNumber), !IO),
|
|
io.write_string(Stream, """", !IO).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
pred_or_func_to_item_type(pf_predicate) = predicate_item.
|
|
pred_or_func_to_item_type(pf_function) = function_item.
|
|
|
|
is_simple_item_type(type_abstract_item).
|
|
is_simple_item_type(type_body_item).
|
|
is_simple_item_type(inst_item).
|
|
is_simple_item_type(mode_item).
|
|
is_simple_item_type(typeclass_item).
|
|
|
|
is_pred_or_func_item_type(predicate_item).
|
|
is_pred_or_func_item_type(function_item).
|
|
|
|
string_to_item_type("type", type_abstract_item).
|
|
string_to_item_type("type_body", type_body_item).
|
|
string_to_item_type("inst", inst_item).
|
|
string_to_item_type("mode", mode_item).
|
|
string_to_item_type("typeclass", typeclass_item).
|
|
string_to_item_type("predicate", predicate_item).
|
|
string_to_item_type("function", function_item).
|
|
string_to_item_type("functor", functor_item).
|
|
string_to_item_type("mutable", mutable_item).
|
|
string_to_item_type("foreign_proc", foreign_proc_item).
|
|
|
|
type_ctor_to_item_name(type_ctor(SymName, Arity)) = item_name(SymName, Arity).
|
|
inst_ctor_to_item_name(inst_ctor(SymName, Arity)) = item_name(SymName, Arity).
|
|
mode_ctor_to_item_name(mode_ctor(SymName, Arity)) = item_name(SymName, Arity).
|
|
|
|
item_name_to_type_ctor(item_name(SymName, Arity)) = type_ctor(SymName, Arity).
|
|
item_name_to_inst_ctor(item_name(SymName, Arity)) = inst_ctor(SymName, Arity).
|
|
item_name_to_mode_ctor(item_name(SymName, Arity)) = mode_ctor(SymName, Arity).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
init_recompilation_info(ModuleName) =
|
|
recompilation_info(
|
|
ModuleName,
|
|
init_used_items,
|
|
map.init,
|
|
map.init
|
|
).
|
|
|
|
init_item_id_set(Init) =
|
|
item_id_set(Init, Init, Init, Init, Init, Init, Init, Init, Init, Init).
|
|
|
|
init_item_id_set(Simple, PorF, Cons) =
|
|
item_id_set(Simple, Simple, Simple, Simple, Simple, Cons, PorF, PorF,
|
|
PorF, PorF).
|
|
|
|
init_used_items = item_id_set(map.init, map.init, map.init, map.init,
|
|
map.init, map.init, map.init, map.init, map.init, map.init).
|
|
|
|
extract_simple_item_set(ItemIdSet, type_abstract_item) = ItemIdSet ^ types.
|
|
extract_simple_item_set(ItemIdSet, type_body_item) = ItemIdSet ^ type_bodies.
|
|
extract_simple_item_set(ItemIdSet, mode_item) = ItemIdSet ^ modes.
|
|
extract_simple_item_set(ItemIdSet, inst_item) = ItemIdSet ^ insts.
|
|
extract_simple_item_set(ItemIdSet, typeclass_item) = ItemIdSet ^ typeclasses.
|
|
|
|
update_simple_item_set(type_abstract_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ types := IdMap.
|
|
update_simple_item_set(type_body_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ type_bodies := IdMap.
|
|
update_simple_item_set(mode_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ modes := IdMap.
|
|
update_simple_item_set(inst_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ insts := IdMap.
|
|
update_simple_item_set(typeclass_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ typeclasses := IdMap.
|
|
|
|
extract_pred_or_func_set(ItemIdSet, predicate_item) = ItemIdSet ^ predicates.
|
|
extract_pred_or_func_set(ItemIdSet, function_item) = ItemIdSet ^ functions.
|
|
|
|
update_pred_or_func_set(predicate_item, Set, !ItemIdSet) :-
|
|
!ItemIdSet ^ predicates := Set.
|
|
update_pred_or_func_set(function_item, Set, !ItemIdSet) :-
|
|
!ItemIdSet ^ functions := Set.
|
|
|
|
extract_ids(ItemIdSet, type_abstract_item) = ItemIdSet ^ types.
|
|
extract_ids(ItemIdSet, type_body_item) = ItemIdSet ^ type_bodies.
|
|
extract_ids(ItemIdSet, mode_item) = ItemIdSet ^ modes.
|
|
extract_ids(ItemIdSet, inst_item) = ItemIdSet ^ insts.
|
|
extract_ids(ItemIdSet, typeclass_item) = ItemIdSet ^ typeclasses.
|
|
extract_ids(ItemIdSet, functor_item) = ItemIdSet ^ functors.
|
|
extract_ids(ItemIdSet, predicate_item) = ItemIdSet ^ predicates.
|
|
extract_ids(ItemIdSet, function_item) = ItemIdSet ^ functions.
|
|
extract_ids(ItemIdSet, mutable_item) = ItemIdSet ^ mutables.
|
|
extract_ids(ItemIdSet, foreign_proc_item) = ItemIdSet ^ foreign_procs.
|
|
|
|
update_ids(type_abstract_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ types := IdMap.
|
|
update_ids(type_body_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ type_bodies := IdMap.
|
|
update_ids(mode_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ modes := IdMap.
|
|
update_ids(inst_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ insts := IdMap.
|
|
update_ids(typeclass_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ typeclasses := IdMap.
|
|
update_ids(predicate_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ predicates := IdMap.
|
|
update_ids(function_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ functions := IdMap.
|
|
update_ids(functor_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ functors := IdMap.
|
|
update_ids(mutable_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ mutables := IdMap.
|
|
update_ids(foreign_proc_item, IdMap, !ItemIdSet) :-
|
|
!ItemIdSet ^ foreign_procs := IdMap.
|
|
|
|
map_ids(Func, Items0, Init) = Items :-
|
|
% XXX ITEM_LIST Why wite this code in a way that
|
|
% (a) does not guarantee that all fields of the original item_id_set
|
|
% are transformed, and (b) actually DOES miss transforming some fields,
|
|
% such as mutable_item and foreign_proc_item?
|
|
Items1 = init_item_id_set(Init),
|
|
Items = list.foldl(
|
|
( func(ItemType, NewItems0) = NewItems :-
|
|
update_ids(ItemType, Func(ItemType, extract_ids(Items0, ItemType)),
|
|
NewItems0, NewItems)
|
|
),
|
|
[type_abstract_item, type_body_item, mode_item, inst_item,
|
|
typeclass_item, functor_item, predicate_item, function_item],
|
|
Items1).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
find_module_qualifier(unqualified(_)) = unqualified("").
|
|
find_module_qualifier(qualified(ModuleName, _)) = ModuleName.
|
|
|
|
module_qualify_name(Qualifier, Name) =
|
|
( if Qualifier = unqualified("") then
|
|
unqualified(Name)
|
|
else
|
|
qualified(Qualifier, Name)
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
record_used_item(ItemType, Id, QualifiedId, !Info) :-
|
|
QualifiedId = item_name(QualifiedName, Arity),
|
|
( if
|
|
% Don't record builtin items (QualifiedId may be unqualified
|
|
% for predicates, functions and functors because they aren't
|
|
% qualified until after typechecking).
|
|
ItemType \= predicate_item,
|
|
ItemType \= function_item,
|
|
ItemType \= functor_item,
|
|
QualifiedName = unqualified(_)
|
|
then
|
|
true
|
|
else
|
|
ItemSet0 = !.Info ^ recomp_used_items,
|
|
IdSet0 = extract_ids(ItemSet0, ItemType),
|
|
UnqualifiedName = unqualify_name(QualifiedName),
|
|
ModuleName = find_module_qualifier(QualifiedName),
|
|
UnqualifiedId = UnqualifiedName - Arity,
|
|
Id = item_name(SymName, _),
|
|
ModuleQualifier = find_module_qualifier(SymName),
|
|
( if map.search(IdSet0, UnqualifiedId, MatchingNames0) then
|
|
MatchingNames1 = MatchingNames0
|
|
else
|
|
map.init(MatchingNames1)
|
|
),
|
|
( if map.contains(MatchingNames1, ModuleQualifier) then
|
|
true
|
|
else
|
|
map.det_insert(ModuleQualifier, ModuleName,
|
|
MatchingNames1, MatchingNames),
|
|
map.set(UnqualifiedId, MatchingNames, IdSet0, IdSet),
|
|
update_ids(ItemType, IdSet, ItemSet0, ItemSet),
|
|
!Info ^ recomp_used_items := ItemSet
|
|
)
|
|
).
|
|
|
|
record_expanded_items(Item, ExpandedItems, !Info) :-
|
|
( if set.is_empty(ExpandedItems) then
|
|
true
|
|
else
|
|
DepsMap0 = !.Info ^ recomp_dependencies,
|
|
( if map.search(DepsMap0, Item, Deps0) then
|
|
Deps1 = Deps0
|
|
else
|
|
set.init(Deps1)
|
|
),
|
|
set.union(Deps1, ExpandedItems, Deps),
|
|
map.set(Item, Deps, DepsMap0, DepsMap),
|
|
!Info ^ recomp_dependencies := DepsMap
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
maybe_start_recording_expanded_items(_, _, no, no).
|
|
maybe_start_recording_expanded_items(ModuleName, SymName, yes(_), MaybeInfo) :-
|
|
( if SymName = qualified(ModuleName, _) then
|
|
MaybeInfo = no
|
|
else
|
|
MaybeInfo = yes(eqv_expanded_item_set(ModuleName, set.init))
|
|
).
|
|
|
|
record_expanded_item(Item, !EquivTypeInfo) :-
|
|
map_maybe(record_expanded_item_2(Item), !EquivTypeInfo).
|
|
|
|
:- pred record_expanded_item_2(item_id::in,
|
|
eqv_expanded_item_set::in, eqv_expanded_item_set::out) is det.
|
|
|
|
record_expanded_item_2(ItemId, ExpandedItemSet0, ExpandedItemSet) :-
|
|
ExpandedItemSet0 = eqv_expanded_item_set(ModuleName, Items0),
|
|
ItemId = item_id(_, ItemName),
|
|
( if ItemName = item_name(qualified(ModuleName, _), _) then
|
|
% We don't need to record local types.
|
|
ExpandedItemSet = ExpandedItemSet0
|
|
else
|
|
set.insert(ItemId, Items0, Items),
|
|
ExpandedItemSet = eqv_expanded_item_set(ModuleName, Items)
|
|
).
|
|
|
|
finish_recording_expanded_items(_, no, no, no).
|
|
finish_recording_expanded_items(_, no, yes(Info), yes(Info)).
|
|
finish_recording_expanded_items(_, yes(_), no, _) :-
|
|
unexpected($pred, "items but no info").
|
|
finish_recording_expanded_items(Item,
|
|
yes(eqv_expanded_item_set(_, ExpandedItems)), yes(Info0), yes(Info)) :-
|
|
record_expanded_items(Item, ExpandedItems, Info0, Info).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- end_module recompilation.
|
|
%-----------------------------------------------------------------------------%
|