Files
mercury/compiler/parse_tree_out_info.m
Zoltan Somogyi 5f50259d16 Write to explicitly named streams in many modules.
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.
2020-11-14 15:07:55 +11:00

630 lines
23 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 2015-2018 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 provides the basic infrastructure needed to print out
% the parse tree of a Mercury module, in whole or in part.
%
% This infrastructure has two parts.
%
% The first part of the infrastructure is the merc_out_info type. Values of
% this type control those low-level aspects of how parse tree components
% are printed that may differ depending on *why* we want to print them,
% such as whether the goal is to generate valid Mercury code or to print
% as much detail as possible for debugging, even if those details are
% not expressible in Mercury syntax.
%
% The second is the "output" type class. Many (though not all) of the
% procedures that output parts of the parse tree come in groups of three,
% where the three follow the pattern:
%
% :- pred mercury_output_xyz(io.text_output_stream::in, ...,
% io::di, io::uo) is det.
% :- func mercury_xyz_to_string(...) = string.
% :- pred mercury_format_xyz(..., S::in, U::di, U::uo) is det
% <= output(S, U).
%
% In most cases, the first two simply forward all the work to the third.
% This is possible because the tuples (io.text_output_stream, io.state)
% and (unit, string) are members of the output(S, U) typeclass.
%
% For the mercury_output_xyz versions, going through a typeclass interface is
% (for now) a slight slowdown, but the time cost is still small compared to
% the cost of I/O itself.
%
% For the mercury_xyz_to_string versions, the cost is acceptable because
% (for now) we only create relatively small strings this way, e.g. strings that
% go into error messages. The typeclass instance for strings has a quadratic
% complexity in the number of strings being appended but a reasonably low
% constant factor. If we ever want to use these functions to create long
% strings (longer than a few lines), then we should use a typeclass
% instance implementation that represents the entity being converted to string
% as a cord of strings that must be concatenated together at the end using
% cord.list and string.append_list. The complexity of an implementation
% like that can be linear in the size of the string being built, although
% it will have a higher constant factor. The biggest problem with using it
% will be the need for explicit conversion step to a plain old string
% at the end.
%
%---------------------------------------------------------------------------%
:- module parse_tree.parse_tree_out_info.
:- interface.
:- import_module libs.
:- import_module libs.globals.
:- import_module mdbcomp.
:- import_module mdbcomp.sym_name.
:- import_module parse_tree.prog_data.
:- import_module parse_tree.prog_data_pragma.
:- import_module char.
:- import_module io.
:- import_module list.
:- import_module string.
:- import_module term.
:- import_module unit.
%---------------------------------------------------------------------------%
:- type merc_out_info.
:- type maybe_qualified_item_names
---> unqualified_item_names
; qualified_item_names.
:- type maybe_output_line_numbers
---> dont_output_line_numbers
; do_output_line_numbers.
:- type type_repn_for
---> type_repn_for_machines
; type_repn_for_humans.
% Are we generating output that has be able to be read back in as
% valid Mercury, e.g. when the output goes to a .int* or .*opt file,
% or are we generating output only for humans to read?
%
% XXX We should split output for humans into two: one for developers,
% who won't mind, and will often need, variable numbers, and one
% for ordinary users, who don't, and shouldn't have to, know about
% the existence of variable numbers.
%
% XXX Since not all combinations of output_lang and var_name_print
% make sense, we shouldn't pass values of the output_lang and
% var_name_print types next to each other, as we now do in many places.
% Instead, each alternative here should *contain* the var_name_print
% value that we now pass next to it, but *only* if there is more than one
% var_name_print value that makes sense for the value of output_lang.
% This would require putting the two types next to each other.
%
:- type output_lang
---> output_mercury
; output_debug.
:- func init_debug_merc_out_info = merc_out_info.
:- func init_merc_out_info(globals, maybe_qualified_item_names, output_lang)
= merc_out_info.
:- func merc_out_info_disable_line_numbers(merc_out_info) = merc_out_info.
:- func get_maybe_qualified_item_names(merc_out_info)
= maybe_qualified_item_names.
:- func get_output_line_numbers(merc_out_info) = maybe_output_line_numbers.
:- func get_output_lang(merc_out_info) = output_lang.
:- func get_type_repn_for(merc_out_info) = type_repn_for.
:- func get_human_comma_sep(merc_out_info) = string.
:- pred maybe_output_line_number(merc_out_info::in, prog_context::in,
io.text_output_stream::in, io::di, io::uo) is det.
:- pred maybe_unqualify_sym_name(merc_out_info::in,
sym_name::in, sym_name::out) is det.
:- pred write_out_list(
pred(T, io.text_output_stream, io, io)::in(pred(in, in, di, uo) is det),
string::in, list(T)::in, io.text_output_stream::in, io::di, io::uo) is det.
%---------------------------------------------------------------------------%
:- typeclass output(S, U) <= (U -> S) where [
pred add_string(string::in, S::in, U::di, U::uo) is det,
pred add_strings(list(string)::in, S::in, U::di, U::uo) is det,
pred add_char(char::in, S::in, U::di, U::uo) is det,
pred add_int(int::in, S::in, U::di, U::uo) is det,
pred add_uint(uint::in, S::in, U::di, U::uo) is det,
pred add_int8(int8::in, S::in, U::di, U::uo) is det,
pred add_uint8(uint8::in, S::in, U::di, U::uo) is det,
pred add_int16(int16::in, S::in, U::di, U::uo) is det,
pred add_uint16(uint16::in, S::in, U::di, U::uo) is det,
pred add_int32(int32::in, S::in, U::di, U::uo) is det,
pred add_uint32(uint32::in, S::in, U::di, U::uo) is det,
pred add_int64(int64::in, S::in, U::di, U::uo) is det,
pred add_uint64(uint64::in, S::in, U::di, U::uo) is det,
pred add_float(float::in, S::in, U::di, U::uo) is det,
pred add_purity_prefix(purity::in, S::in, U::di, U::uo) is det,
pred add_quoted_atom(string::in, S::in, U::di, U::uo) is det,
pred add_quoted_string(string::in, S::in, U::di, U::uo) is det,
pred add_constant(const::in, S::in, U::di, U::uo) is det,
pred add_eval_method(eval_method::in, S::in, U::di, U::uo) is det,
pred add_lambda_eval_method(lambda_eval_method::in, S::in,
U::di, U::uo) is det,
pred add_escaped_string(string::in, S::in, U::di, U::uo) is det,
pred add_format(string::in, list(poly_type)::in, S::in,
U::di, U::uo) is det,
% The add_list predicate calls the predicate argument to add each
% element of the list to the specified stream, printing the specified
% separator between each pair of elements.
pred add_list(pred(T, S, U, U)::in(pred(in, in, di, uo) is det),
string::in, list(T)::in, S::in, U::di, U::uo) is det
].
:- instance output(io.text_output_stream, io.state).
:- instance output(unit, string).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module libs.options.
:- import_module parse_tree.prog_out.
:- import_module bool.
:- import_module term_io.
%---------------------------------------------------------------------------%
:- type merc_out_info
---> merc_out_info(
moi_qualify_item_names :: maybe_qualified_item_names,
moi_output_line_numbers :: maybe_output_line_numbers,
moi_output_lang :: output_lang,
% When writing out a comma in a type_repn, or some other
% output that humans may want to look at, what should
% we print to separate it from what follows?
%
% For humans, ",\n "; for computers, just ", ".
moi_type_repn_for :: type_repn_for,
moi_human_comma_sep :: string
).
init_debug_merc_out_info = Info :-
Info = merc_out_info(qualified_item_names, dont_output_line_numbers,
output_debug, type_repn_for_machines, ", ").
init_merc_out_info(Globals, MaybeQualifiedItemNames, Lang) = Info :-
globals.lookup_bool_option(Globals, line_numbers, LineNumbersOpt),
globals.lookup_bool_option(Globals, type_repns_for_humans,
TypeRepnsForHumans),
( LineNumbersOpt = no, LineNumbers = dont_output_line_numbers
; LineNumbersOpt = yes, LineNumbers = do_output_line_numbers
),
( TypeRepnsForHumans = no, For = type_repn_for_machines, CommaSep = ", "
; TypeRepnsForHumans = yes, For = type_repn_for_humans, CommaSep = ",\n "
),
Info = merc_out_info(MaybeQualifiedItemNames, LineNumbers, Lang,
For, CommaSep).
merc_out_info_disable_line_numbers(Info0) = Info :-
Info = Info0 ^ moi_output_line_numbers := dont_output_line_numbers.
get_maybe_qualified_item_names(Info) = Info ^ moi_qualify_item_names.
get_output_line_numbers(Info) = Info ^ moi_output_line_numbers.
get_output_lang(Info) = Info ^ moi_output_lang.
get_type_repn_for(Info) = Info ^ moi_type_repn_for.
get_human_comma_sep(Info) = Info ^ moi_human_comma_sep.
%---------------------------------------------------------------------------%
maybe_output_line_number(Info, Context, Stream, !IO) :-
LineNumbers = get_output_line_numbers(Info),
(
LineNumbers = do_output_line_numbers,
io.write_string(Stream, "\t% ", !IO),
prog_out.write_context(Stream, Context, !IO),
io.write_string(Stream, "\n", !IO)
;
LineNumbers = dont_output_line_numbers
).
maybe_unqualify_sym_name(Info, SymName, OutSymName) :-
MaybeQualifiedItemNames = get_maybe_qualified_item_names(Info),
(
MaybeQualifiedItemNames = qualified_item_names,
OutSymName = SymName
;
MaybeQualifiedItemNames = unqualified_item_names,
OutSymName = unqualified(unqualify_name(SymName))
).
%---------------------------------------------------------------------------%
:- instance output(io.text_output_stream, io.state) where [
pred(add_string/4) is write_string,
pred(add_strings/4) is write_strings,
pred(add_char/4) is write_char_literal,
pred(add_int/4) is write_int_literal,
pred(add_uint/4) is write_uint_literal,
pred(add_int8/4) is write_int8_literal,
pred(add_uint8/4) is write_uint8_literal,
pred(add_int16/4) is write_int16_literal,
pred(add_uint16/4) is write_uint16_literal,
pred(add_int32/4) is write_int32_literal,
pred(add_uint32/4) is write_uint32_literal,
pred(add_int64/4) is write_int64_literal,
pred(add_uint64/4) is write_uint64_literal,
pred(add_float/4) is write_float_literal,
pred(add_purity_prefix/4) is write_purity_prefix,
pred(add_quoted_atom/4) is write_quoted_atom,
pred(add_quoted_string/4) is write_quoted_string,
pred(add_constant/4) is write_constant,
pred(add_eval_method/4) is write_eval_eval_method,
pred(add_lambda_eval_method/4) is write_lambda_eval_method,
pred(add_escaped_string/4) is write_escaped_string,
pred(add_format/5) is write_format,
pred(add_list/6) is write_out_list
].
:- instance output(unit, string) where [
pred(add_string/4) is output_string,
pred(add_strings/4) is output_strings,
pred(add_char/4) is output_char,
pred(add_int/4) is output_int,
pred(add_uint/4) is output_uint,
pred(add_int8/4) is output_int8,
pred(add_uint8/4) is output_uint8,
pred(add_int16/4) is output_int16,
pred(add_uint16/4) is output_uint16,
pred(add_int32/4) is output_int32,
pred(add_uint32/4) is output_uint32,
pred(add_int64/4) is output_int64,
pred(add_uint64/4) is output_uint64,
pred(add_float/4) is output_float,
pred(add_purity_prefix/4) is output_purity_prefix,
pred(add_quoted_atom/4) is output_quoted_atom,
pred(add_quoted_string/4) is output_quoted_string,
pred(add_constant/4) is output_constant,
pred(add_eval_method/4) is output_eval_eval_method,
pred(add_lambda_eval_method/4) is output_lambda_eval_method,
pred(add_escaped_string/4) is output_escaped_string,
pred(add_format/5) is output_format,
pred(add_list/6) is output_list
].
%---------------------------------------------------------------------------%
:- pred write_string(string::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_string(Str, Stream, !IO) :-
io.write_string(Stream, Str, !IO).
:- pred write_strings(list(string)::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_strings(Strs, Stream, !IO) :-
io.write_strings(Stream, Strs, !IO).
:- pred write_char_literal(char::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_char_literal(C, Stream, !IO) :-
io.write_char(Stream, C, !IO).
:- pred write_int_literal(int::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_int_literal(Int, Stream, !IO) :-
io.write_int(Stream, Int, !IO).
:- pred write_uint_literal(uint::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_uint_literal(UInt, Stream, !IO) :-
io.write_uint(Stream, UInt, !IO),
io.write_char(Stream, 'u', !IO).
:- pred write_int8_literal(int8::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_int8_literal(Int8, Stream, !IO) :-
io.write_int8(Stream, Int8, !IO),
io.write_string(Stream, "i8", !IO).
:- pred write_uint8_literal(uint8::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_uint8_literal(UInt8, Stream, !IO) :-
io.write_uint8(Stream, UInt8, !IO),
io.write_string(Stream, "u8", !IO).
:- pred write_int16_literal(int16::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_int16_literal(Int16, Stream, !IO) :-
io.write_int16(Stream, Int16, !IO),
io.write_string(Stream, "i16", !IO).
:- pred write_uint16_literal(uint16::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_uint16_literal(UInt16, Stream, !IO) :-
io.write_uint16(Stream, UInt16, !IO),
io.write_string(Stream, "u16", !IO).
:- pred write_int32_literal(int32::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_int32_literal(Int32, Stream, !IO) :-
io.write_int32(Stream, Int32, !IO),
io.write_string(Stream, "i32", !IO).
:- pred write_uint32_literal(uint32::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_uint32_literal(UInt32, Stream, !IO) :-
io.write_uint32(Stream, UInt32, !IO),
io.write_string(Stream, "u32", !IO).
:- pred write_int64_literal(int64::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_int64_literal(Int64, Stream, !IO) :-
io.write_int64(Stream, Int64, !IO),
io.write_string(Stream, "i64", !IO).
:- pred write_uint64_literal(uint64::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_uint64_literal(UInt64, Stream, !IO) :-
io.write_uint64(Stream, UInt64, !IO),
io.write_string(Stream, "u64", !IO).
:- pred write_float_literal(float::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_float_literal(Float, Stream, !IO) :-
io.write_float(Stream, Float, !IO).
%-------------%
:- pred write_purity_prefix(purity::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_purity_prefix(Purity, Stream, !IO) :-
PurityPrefixStr = purity_prefix_to_string(Purity),
io.write_string(Stream, PurityPrefixStr, !IO).
:- pred write_quoted_atom(string::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_quoted_atom(Atom, Stream, !IO) :-
term_io.quote_atom(Stream, Atom, !IO).
:- pred write_quoted_string(string::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_quoted_string(Str, Stream, !IO) :-
term_io.quote_string(Stream, Str, !IO).
:- pred write_constant(const::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_constant(Const, Stream, !IO) :-
term_io.write_constant(Stream, Const, !IO).
:- pred write_eval_eval_method(eval_method::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_eval_eval_method(EvalMethod, Stream, !IO) :-
output_eval_eval_method(EvalMethod, unit, "", EvalMethodStr),
io.write_string(Stream, EvalMethodStr, !IO).
:- pred write_lambda_eval_method(lambda_eval_method::in,
io.text_output_stream::in, io::di, io::uo) is det.
write_lambda_eval_method(LambdaEvalMethod, Stream, !IO) :-
output_lambda_eval_method(LambdaEvalMethod, unit,
"", LambdaEvalMethodStr),
io.write_string(Stream, LambdaEvalMethodStr, !IO).
:- pred write_escaped_string(string::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_escaped_string(Str, Stream, !IO) :-
term_io.write_escaped_string(Stream, Str, !IO).
:- pred write_format(string::in, list(poly_type)::in,
io.text_output_stream::in, io::di, io::uo) is det.
write_format(FormatStr, PolyTypes, Stream, !IO) :-
io.format(Stream, FormatStr, PolyTypes, !IO).
%-------------%
write_out_list(_, _, [], _, !IO).
write_out_list(WritePred, Separator, [Item | Items], Stream, !IO) :-
write_out_list_lag(WritePred, Separator, Item, Items, Stream, !IO).
:- pred write_out_list_lag(
pred(T, io.text_output_stream, io, io)::in(pred(in, in, di, uo) is det),
string::in, T::in, list(T)::in, io.text_output_stream::in,
io::di, io::uo) is det.
write_out_list_lag(WritePred, Separator, Item1, Items2plus, Stream, !IO) :-
WritePred(Item1, Stream, !IO),
(
Items2plus = []
;
Items2plus = [Item2 | Items3plus],
io.write_string(Stream, Separator, !IO),
write_out_list_lag(WritePred, Separator, Item2, Items3plus,
Stream, !IO)
).
%---------------------------------------------------------------------------%
:- pred output_string(string::in, unit::in, string::di, string::uo) is det.
output_string(S, _, Str0, Str) :-
Str = Str0 ++ S.
:- pred output_strings(list(string)::in, unit::in,
string::di, string::uo) is det.
output_strings(Strs, _, Str0, Str) :-
string.append_list([Str0 | Strs], Str).
:- pred output_char(char::in, unit::in, string::di, string::uo) is det.
output_char(C, _, Str0, Str) :-
Str = Str0 ++ string.char_to_string(C).
:- pred output_int(int::in, unit::in, string::di, string::uo) is det.
output_int(I, _, Str0, Str) :-
Str = Str0 ++ string.int_to_string(I).
:- pred output_uint(uint::in, unit::in, string::di, string::uo) is det.
output_uint(U, _, Str0, Str) :-
Str = Str0 ++ string.uint_to_string(U) ++ "u".
:- pred output_int8(int8::in, unit::in, string::di, string::uo) is det.
output_int8(I8, _, Str0, Str) :-
S = string.int8_to_string(I8) ++ "i8",
string.append(Str0, S, Str).
:- pred output_uint8(uint8::in, unit::in, string::di, string::uo) is det.
output_uint8(U8, _, Str0, Str) :-
S = string.uint8_to_string(U8) ++ "u8",
string.append(Str0, S, Str).
:- pred output_int16(int16::in, unit::in, string::di, string::uo) is det.
output_int16(I16, _, Str0, Str) :-
S = string.int16_to_string(I16) ++ "i16",
string.append(Str0, S, Str).
:- pred output_uint16(uint16::in, unit::in, string::di, string::uo) is det.
output_uint16(U16, _, Str0, Str) :-
S = string.uint16_to_string(U16) ++ "u16",
string.append(Str0, S, Str).
:- pred output_int32(int32::in, unit::in, string::di, string::uo) is det.
output_int32(I32, _, Str0, Str) :-
S = string.int32_to_string(I32) ++ "i32",
string.append(Str0, S, Str).
:- pred output_uint32(uint32::in, unit::in, string::di, string::uo) is det.
output_uint32(U32, _, Str0, Str) :-
S = string.uint32_to_string(U32) ++ "u32",
string.append(Str0, S, Str).
:- pred output_int64(int64::in, unit::in, string::di, string::uo) is det.
output_int64(I64, _, Str0, Str) :-
S = string.int64_to_string(I64) ++ "i64",
string.append(Str0, S, Str).
:- pred output_uint64(uint64::in, unit::in, string::di, string::uo) is det.
output_uint64(U64, _, Str0, Str) :-
S = string.uint64_to_string(U64) ++ "u64",
string.append(Str0, S, Str).
:- pred output_float(float::in, unit::in, string::di, string::uo) is det.
output_float(F, _, Str0, Str) :-
string.float_to_string(F, S),
string.append(Str0, S, Str).
:- pred output_purity_prefix(purity::in, unit::in,
string::di, string::uo) is det.
output_purity_prefix(P, _, Str0, Str) :-
S = purity_prefix_to_string(P),
string.append(Str0, S, Str).
:- pred output_quoted_atom(string::in, unit::in,
string::di, string::uo) is det.
output_quoted_atom(A, _, Str0, Str) :-
QA = term_io.quoted_atom(A),
string.append(Str0, QA, Str).
:- pred output_quoted_string(string::in, unit::in,
string::di, string::uo) is det.
output_quoted_string(A, _, Str0, Str) :-
QA = term_io.quoted_string(A),
string.append(Str0, QA, Str).
:- pred output_constant(const::in, unit::in, string::di, string::uo) is det.
output_constant(C, _, Str0, Str) :-
CS = term_io.format_constant(C),
string.append(Str0, CS, Str).
:- pred output_escaped_string(string::in, unit::in,
string::di, string::uo) is det.
output_escaped_string(S, _, Str0, Str) :-
ES = term_io.escaped_string(S),
string.append(Str0, ES, Str).
:- pred output_eval_eval_method(eval_method::in, unit::in,
string::di, string::uo) is det.
output_eval_eval_method(EvalMethod, _, !Str) :-
output_string("eval_", unit, !Str),
output_string(eval_method_to_string(EvalMethod), unit, !Str).
:- pred output_lambda_eval_method(lambda_eval_method::in, unit::in,
string::di, string::uo) is det.
output_lambda_eval_method(lambda_normal, _, !Str) :-
output_string("normal", unit, !Str).
:- pred output_format(string::in, list(poly_type)::in, unit::in,
string::di, string::uo) is det.
output_format(Format, Items, _, Str0, Str) :-
Str = Str0 ++ string.format(Format, Items).
:- pred output_list(
pred(T, unit, string, string)::in(pred(in, in, di, uo) is det),
string::in, list(T)::in, unit::in, string::di, string::uo) is det.
output_list(_, _, [], _, !Str).
output_list(OutputPred, Sep, [Item | Items], _, !Str) :-
output_list_lag(OutputPred, Sep, Item, Items, !Str).
:- pred output_list_lag(
pred(T, unit, string, string)::in(pred(in, in, di, uo) is det),
string::in, T::in, list(T)::in, string::di, string::uo) is det.
output_list_lag(OutputPred, Sep, Item1, Items, !Str) :-
OutputPred(Item1, unit, !Str),
(
Items = []
;
Items = [Item2 | Items3plus],
output_string(Sep, unit, !Str),
output_list_lag(OutputPred, Sep, Item2, Items3plus, !Str)
).
%---------------------------------------------------------------------------%
:- end_module parse_tree.parse_tree_out_info.
%---------------------------------------------------------------------------%