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.
291 lines
9.9 KiB
Mathematica
291 lines
9.9 KiB
Mathematica
%-----------------------------------------------------------------------------%
|
|
% vim: ft=mercury ts=4 sw=4 et
|
|
%-----------------------------------------------------------------------------%
|
|
% Copyright (C) 2002-2009, 2011 The University of Melbourne.
|
|
% Copyright (C) 2014-2015, 2019-2020 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: source_file_map.m.
|
|
% Author: stayl.
|
|
%
|
|
% Maintain a mapping from module name to source file name.
|
|
%
|
|
% The reason why this module is in the parse_tree package is that discovering
|
|
% what module is stored in a file requires reading the ":- module" declaration
|
|
% in that file.
|
|
%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- module parse_tree.source_file_map.
|
|
:- interface.
|
|
|
|
:- import_module libs.
|
|
:- import_module libs.file_util.
|
|
:- import_module libs.globals.
|
|
:- import_module mdbcomp.
|
|
:- import_module mdbcomp.sym_name.
|
|
|
|
:- import_module bool.
|
|
:- import_module io.
|
|
:- import_module list.
|
|
:- import_module maybe.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
% lookup_module_source_file(ModuleName, MaybeFileName, !IO)
|
|
% Return `yes(FileName)' if FileName is the source file for ModuleName,
|
|
% either through the source file map, or by default. Return `no' if no
|
|
% source file is available for ModuleName because the default file name
|
|
% is mapped to another module.
|
|
%
|
|
:- pred lookup_module_source_file(module_name::in, maybe(file_name)::out,
|
|
io::di, io::uo) is det.
|
|
|
|
:- pred lookup_source_file_module(file_name::in, maybe(module_name)::out,
|
|
io::di, io::uo) is det.
|
|
|
|
% Return `yes' if there is a valid Mercury.modules file.
|
|
%
|
|
:- pred have_source_file_map(bool::out, io::di, io::uo) is det.
|
|
|
|
% Return the default fully qualified source file name.
|
|
%
|
|
:- func default_source_file_name(module_name) = file_name.
|
|
|
|
% Given a list of file names, produce the Mercury.modules file.
|
|
%
|
|
:- pred write_source_file_map(globals::in, list(string)::in,
|
|
io::di, io::uo) is det.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
:- import_module parse_tree.file_names.
|
|
:- import_module parse_tree.find_module.
|
|
:- import_module parse_tree.prog_out.
|
|
|
|
:- import_module bimap.
|
|
:- import_module char.
|
|
:- import_module dir.
|
|
:- import_module string.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
lookup_module_source_file(ModuleName, MaybeFileName, !IO) :-
|
|
get_source_file_map(SourceFileMap, !IO),
|
|
( if bimap.search(SourceFileMap, ModuleName, FileName) then
|
|
MaybeFileName = yes(FileName)
|
|
else
|
|
DefaultFileName = default_source_file_name(ModuleName),
|
|
( if bimap.reverse_search(SourceFileMap, _, DefaultFileName) then
|
|
MaybeFileName = no
|
|
else
|
|
MaybeFileName = yes(DefaultFileName)
|
|
)
|
|
).
|
|
|
|
lookup_source_file_module(FileName, MaybeModuleName, !IO) :-
|
|
get_source_file_map(SourceFileMap, !IO),
|
|
( if bimap.reverse_search(SourceFileMap, ModuleName, FileName) then
|
|
MaybeModuleName = yes(ModuleName)
|
|
else
|
|
( if default_module_name_for_file(FileName, DefaultModuleName) then
|
|
( if bimap.search(SourceFileMap, DefaultModuleName, _) then
|
|
MaybeModuleName = no
|
|
else
|
|
MaybeModuleName = yes(DefaultModuleName)
|
|
)
|
|
else
|
|
MaybeModuleName = no
|
|
)
|
|
).
|
|
|
|
have_source_file_map(HaveMap, !IO) :-
|
|
get_source_file_map(SourceFileMap, !IO),
|
|
( if bimap.is_empty(SourceFileMap) then
|
|
HaveMap = no
|
|
else
|
|
HaveMap = yes
|
|
).
|
|
|
|
default_source_file_name(ModuleName) = sym_name_to_string(ModuleName) ++ ".m".
|
|
|
|
% If the file name ends in ".m", return the module name whose
|
|
% default file name that would be.
|
|
%
|
|
:- pred default_module_name_for_file(file_name::in, module_name::out)
|
|
is semidet.
|
|
|
|
default_module_name_for_file(FileName, DefaultModuleName) :-
|
|
string.remove_suffix(FileName, ".m", FileNameBeforeDotM),
|
|
file_name_to_module_name(FileNameBeforeDotM, DefaultModuleName).
|
|
|
|
% Read the Mercury.modules file (if it exists) to find the mapping
|
|
% from module name to file name.
|
|
%
|
|
:- pred get_source_file_map(source_file_map::out, io::di, io::uo) is det.
|
|
|
|
get_source_file_map(SourceFileMap, !IO) :-
|
|
globals.io_get_maybe_source_file_map(MaybeSourceFileMap0, !IO),
|
|
(
|
|
MaybeSourceFileMap0 = yes(SourceFileMap0),
|
|
SourceFileMap = SourceFileMap0
|
|
;
|
|
MaybeSourceFileMap0 = no,
|
|
io.open_input(modules_file_name, OpenRes, !IO),
|
|
(
|
|
OpenRes = ok(Stream),
|
|
io.set_input_stream(Stream, OldStream, !IO),
|
|
read_source_file_map([], bimap.init, SourceFileMap, !IO),
|
|
io.set_input_stream(OldStream, _, !IO),
|
|
io.close_input(Stream, !IO)
|
|
;
|
|
OpenRes = error(_),
|
|
% If the file doesn't exist, then the mapping is empty.
|
|
SourceFileMap = bimap.init
|
|
),
|
|
globals.io_set_maybe_source_file_map(yes(SourceFileMap), !IO)
|
|
).
|
|
|
|
:- pred read_source_file_map(list(char)::in,
|
|
source_file_map::in, source_file_map::out, io::di, io::uo) is det.
|
|
|
|
read_source_file_map(ModuleChars, !Map, !IO) :-
|
|
read_until_char('\t', [], ModuleCharsResult, !IO),
|
|
(
|
|
ModuleCharsResult = ok(RevModuleChars),
|
|
string.from_rev_char_list(RevModuleChars, ModuleStr),
|
|
ModuleName = string_to_sym_name(ModuleStr),
|
|
read_until_char('\n', [], FileNameCharsResult, !IO),
|
|
(
|
|
FileNameCharsResult = ok(FileNameChars),
|
|
string.from_rev_char_list(FileNameChars, FileName),
|
|
bimap.set(ModuleName, FileName, !Map),
|
|
read_source_file_map(ModuleChars, !Map, !IO)
|
|
;
|
|
FileNameCharsResult = eof,
|
|
io.set_exit_status(1, !IO),
|
|
io.write_string("mercury_compile: unexpected end " ++
|
|
"of file in Mercury.modules file.\n", !IO)
|
|
;
|
|
FileNameCharsResult = error(Error),
|
|
io.set_exit_status(1, !IO),
|
|
io.write_string("mercury_compile: error in " ++
|
|
"Mercury.modules file: ", !IO),
|
|
io.write_string(io.error_message(Error), !IO),
|
|
io.nl(!IO)
|
|
)
|
|
;
|
|
ModuleCharsResult = eof
|
|
;
|
|
ModuleCharsResult = error(Error),
|
|
io.set_exit_status(1, !IO),
|
|
io.write_string("mercury_compile: error in " ++
|
|
"Mercury.modules file: ", !IO),
|
|
io.write_string(io.error_message(Error), !IO),
|
|
io.nl(!IO)
|
|
).
|
|
|
|
:- pred read_until_char(char::in, list(char)::in, io.result(list(char))::out,
|
|
io::di, io::uo) is det.
|
|
|
|
read_until_char(EndChar, Chars0, Result, !IO) :-
|
|
io.read_char(CharRes, !IO),
|
|
(
|
|
CharRes = ok(Char),
|
|
( if Char = EndChar then
|
|
Result = ok(Chars0)
|
|
else
|
|
read_until_char(EndChar, [Char | Chars0], Result, !IO)
|
|
)
|
|
;
|
|
CharRes = eof,
|
|
(
|
|
Chars0 = [],
|
|
Result = eof
|
|
;
|
|
Chars0 = [_ | _],
|
|
Result = ok(Chars0)
|
|
)
|
|
;
|
|
CharRes = error(Error),
|
|
Result = error(Error)
|
|
).
|
|
|
|
write_source_file_map(Globals, FileNames, !IO) :-
|
|
ModulesFileName = modules_file_name,
|
|
io.open_output(ModulesFileName, OpenRes, !IO),
|
|
(
|
|
OpenRes = ok(Stream),
|
|
list.foldl2(write_source_file_map_2(Globals, Stream), FileNames,
|
|
bimap.init, _, !IO),
|
|
io.close_output(Stream, !IO)
|
|
;
|
|
OpenRes = error(Error),
|
|
io.format("mercury_compile: error opening `%s' for output: %s",
|
|
[s(ModulesFileName), s(io.error_message(Error))], !IO),
|
|
io.set_exit_status(1, !IO)
|
|
).
|
|
|
|
:- pred write_source_file_map_2(globals::in, io.output_stream::in,
|
|
file_name::in,
|
|
bimap(module_name, file_name)::in, bimap(module_name, file_name)::out,
|
|
io::di, io::uo) is det.
|
|
|
|
write_source_file_map_2(Globals, MapStream, FileName,
|
|
SeenModules0, SeenModules, !IO) :-
|
|
find_module_name(Globals, FileName, MaybeModuleName, !IO),
|
|
(
|
|
MaybeModuleName = yes(ModuleName),
|
|
( if
|
|
bimap.search(SeenModules0, ModuleName, PrevFileName),
|
|
PrevFileName \= FileName
|
|
then
|
|
io.format("mercury_compile: module `%s' " ++
|
|
"defined in multiple files: %s, %s\n.",
|
|
[s(sym_name_to_string(ModuleName)),
|
|
s(PrevFileName), s(FileName)], !IO),
|
|
io.set_exit_status(1, !IO),
|
|
SeenModules = SeenModules0
|
|
else
|
|
bimap.set(ModuleName, FileName, SeenModules0, SeenModules)
|
|
),
|
|
( if string.remove_suffix(FileName, ".m", PartialFileName0) then
|
|
PartialFileName = PartialFileName0
|
|
else
|
|
PartialFileName = FileName
|
|
),
|
|
file_name_to_module_name(dir.det_basename(PartialFileName),
|
|
DefaultModuleName),
|
|
( if
|
|
% Only include a module in the mapping if the name doesn't match
|
|
% the default.
|
|
dir.dirname(PartialFileName) = dir.this_directory : string,
|
|
ModuleName = DefaultModuleName
|
|
then
|
|
true
|
|
else
|
|
io.set_output_stream(MapStream, OldStream, !IO),
|
|
prog_out.write_sym_name_to_cur_stream(ModuleName, !IO),
|
|
io.write_string("\t", !IO),
|
|
io.write_string(FileName, !IO),
|
|
io.nl(!IO),
|
|
io.set_output_stream(OldStream, _, !IO)
|
|
)
|
|
;
|
|
MaybeModuleName = no,
|
|
SeenModules = SeenModules0
|
|
).
|
|
|
|
:- func modules_file_name = string.
|
|
|
|
modules_file_name = "Mercury.modules".
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- end_module parse_tree.source_file_map.
|
|
%-----------------------------------------------------------------------------%
|