Files
mercury/compiler/compiler_util.m
Zoltan Somogyi 307b1dc148 Split up error_util.m into five modules.
compiler/error_spec.m:
    This new module contains the part of the old error_util.m that defines
    the error_spec type, and some functions that can help construct pieces
    of error_specs. Most modules of the compiler that deal with errors
    will need to import only this part of the old error_util.m.

    This change also renames the format_component type to format_piece,
    which matches our long-standing naming convention for variables containing
    (lists of) values of this type.

compiler/write_error_spec.m:
    This new module contains the part of the old error_util.m that
    writes out error specs, and converts them to strings.

    This diff marks as obsolete the versions of predicates that
    write out error specs to the current output stream, without
    *explicitly* specifying the intended stream.

compiler/error_sort.m:
    This new module contains the part of the old error_util.m that
    sorts lists of error specs and error msgs.

compiler/error_type_util.m:
    This new module contains the part of the old error_util.m that
    convert types to format_pieces that generate readable output.

compiler/parse_tree.m:
compiler/notes/compiler_design.html:
    Include and document the new modules.

compiler/error_util.m:
    The code remaining in the original error_util.m consists of
    general utility predicates and functions that don't fit into
    any of the modules above.

    Delete an unneeded pair of I/O states from the argument list
    of a predicate.

compiler/file_util.m:
    Move the unable_to_open_file predicate here from error_util.m,
    since it belongs here. Mark another predicate that writes
    to the current output stream as obsolete.

compiler/hlds_error_util.m:
    Mark two predicates that wrote out error_spec to the current output
    stream as obsolete, and add versions that take an explicit output stream.

compiler/Mercury.options:
    Compile the modules that call the newly obsoleted predicates
    with --no-warn-obsolete, for the time being.

compiler/*.m:
    Conform to the changes above, mostly by updating import_module
    declarations, and renaming format_component to format_piece.
2022-10-12 20:50:16 +11:00

122 lines
4.1 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1997-2006, 2009-2010 The University of Melbourne.
% Copyright (C) 2014-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.
%-----------------------------------------------------------------------------%
%
% File: compiler_util.
% Main author: zs.
%
% This module contains code that can be helpful in any compiler module.
%
%-----------------------------------------------------------------------------%
:- module libs.compiler_util.
:- interface.
:- import_module libs.globals.
:- import_module parse_tree.
:- import_module parse_tree.error_spec.
:- import_module io.
:- import_module list.
:- import_module maybe.
%-----------------------------------------------------------------------------%
:- pred maybe_is_error(maybe_error::in, string::out) is semidet.
%-----------------------------------------------------------------------------%
% This type is useful when defining options and behaviours that may
% raise either an error or a warning. See
% pragma_require_tail_recursion.
%
:- type warning_or_error
---> we_warning
; we_error.
% warning_or_error_string(we_warning, "warn").
% warning_or_error_string(we_error, "error").
%
:- pred warning_or_error_string(warning_or_error, string).
:- mode warning_or_error_string(in, out) is det.
:- mode warning_or_error_string(out, in) is semidet.
:- pred warning_or_error_severity(warning_or_error::in, error_severity::out)
is det.
%-----------------------------------------------------------------------------%
:- pred add_error(error_phase::in, list(format_piece)::in,
list(error_spec)::in, list(error_spec)::out) is det.
:- pred add_warning(error_phase::in, list(format_piece)::in,
list(error_spec)::in, list(error_spec)::out) is det.
%-----------------------------------------------------------------------------%
% Record the fact that a warning has been issued; set the exit status
% to error if the `--halt-at-warn' option is set.
%
:- pred record_warning(globals::in, io::di, io::uo) is det.
% Report a warning to the specified stream, and set the exit status
% to error if the --halt-at-warn option is set.
%
:- pred report_warning(io.output_stream::in, globals::in, string::in,
io::di, io::uo) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module libs.options.
:- import_module bool.
%-----------------------------------------------------------------------------%
maybe_is_error(error(Error), Error).
%-----------------------------------------------------------------------------%
warning_or_error_string(we_warning, "warn").
warning_or_error_string(we_error, "error").
warning_or_error_severity(we_warning, severity_warning).
warning_or_error_severity(we_error, severity_error).
%-----------------------------------------------------------------------------%
add_error(Phase, Pieces, !Specs) :-
Spec = simplest_no_context_spec($pred, severity_error, Phase, Pieces),
!:Specs = [Spec | !.Specs].
add_warning(Phase, Pieces, !Specs) :-
Spec = simplest_no_context_spec($pred, severity_warning, Phase, Pieces),
!:Specs = [Spec | !.Specs].
%-----------------------------------------------------------------------------%
record_warning(Globals, !IO) :-
globals.lookup_bool_option(Globals, halt_at_warn, HaltAtWarn),
(
HaltAtWarn = yes,
io.set_exit_status(1, !IO)
;
HaltAtWarn = no
).
report_warning(Stream, Globals, Message, !IO) :-
record_warning(Globals, !IO),
io.write_string(Stream, Message, !IO).
%-----------------------------------------------------------------------------%
:- end_module libs.compiler_util.
%-----------------------------------------------------------------------------%