Files
mercury/compiler/typecheck_msgs.m
Zoltan Somogyi 25f8a6cc99 Add new option --generate-dependencies-ints.
This new option extends --generate-dependencies to take advantage of the
opportunity afforded by the fact that "mmc --generate-dependencies prog.m"
reads in every Mercury source file in the current directory that is
part of "prog". It does this by

- generating the .int3 file of all local-directory modules of the program;
- generating the .int0 file for each these modules that has submodules;
- generating the .int and .int2 files of all local-directory modules
  of the program.

Normally, the process of creating .int0, .int and .int3 files would
need to read in .int0 and .int3 files, but in every one of these cases,
we have just written out those files, so simply holding onto their
parse trees, we can skip this step. On my laptop, on a directory
containing library/*.m, mdbcomp/*.m and compiler/*.m, generating
the dependencies and generating all the .int3/.int0/.int/.int2 files
takes just over 25 seconds. Using the new option, the same process
takes less than 10 seconds.

compiler/options.m:
    Add the new option.

compiler/op_mode.m:
    Add a new variant of the existing op_mode for --generate-dependencies,
    which we select in the presence of the new option.

compiler/mercury_compile_main.m:
    Implement the new op_mode.

    Fix an old issue, which is that "mmc --make-private-interface x.m"
    generated a .int0 file for *every* module in x.m, even the modules
    that don't have any submodules.

compiler/deps_map.m:
    The code implementing the new option needs to know which modules
    of the program are in the current directory. The deps_map structure
    built by the code shared with the implementation of --generate-dependencies
    has not needed that info until now, so add a new field to the deps
    structure of each module to provide this info.

compiler/generate_dep_d_files.m:
    Return the deps_map created by the code that implements both
    --generate-dependencies and the new option to mercury_compile_main.m.

compiler/write_module_interface_files.m:
    Add a flag to the predicates that first construct the parse trees of,
    and then write out, .int3/.int0/.int/.int2 files, that
    mercury_compile_main.m can use to tell them to record the parse trees
    in the have read module maps.

    Add new variants of two of those predicates that take some of their
    arguments from burdened_module structures, since that is the form
    in which mercury_compile_main.m has that info.

compiler/module_baggage.m:
    The predicates in write_module_interface_files.m that generate
    .int0/.int/.int2 files take an argument that should be the timestamp
    of the source file they are being derived from, if that timestamp
    is needed for smart recompilation. Until now, we only ever invoked
    those predicates when we have just read in the source file,
    and this timestamp was readily available. The code implementing
    the new option needs to store this info for a short time, and
    the module baggage is the obvious place to store it, so add this field
    to the baggage.

compiler/error_spec.m:
    An invocation of the compiler with new option may report errors that
    prevent the construction of interface files for several modules.
    The new code in mercury_compile.m prints the error_specs that have
    contexts in the order of those contexts, but we want to print
    the messages without contexts (which in this case report that
    certain files could not be written or copied) to have a useful
    order too. Make this possible by allowing the invisible pieces
    we use for ordering to specify that order via a string (in this case,
    the name of the file that e.g. could not be written), rather than
    the only previous option, an integer.

compiler/grab_modules.m:
compiler/make.get_module_dep_info.m:
compiler/make.module_dep_file.m:
    Fill in the new field in the module baggage.

compiler/check_module_interface.m:
compiler/handle_options.m:
compiler/make_hlds_error.m:
compiler/parse_module.m:
compiler/prog_foreign_enum.m:
compiler/typecheck_error_util.m:
compiler/typecheck_msgs.m:
compiler/write_deps_file.m:
compiler/write_error_spec.m:
    Conform to the changes above.
2023-10-12 06:50:07 +11:00

363 lines
14 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 1993-2012 The University of Melbourne.
% Copyright (C) 2014-2021 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.
%---------------------------------------------------------------------------%
:- module check_hlds.typecheck_msgs.
:- interface.
:- import_module hlds.
:- import_module hlds.hlds_clauses.
:- import_module hlds.hlds_module.
:- import_module hlds.hlds_pred.
:- import_module parse_tree.
:- import_module parse_tree.error_spec.
:- import_module list.
:- import_module set_tree234.
:- func typecheck_report_max_iterations_exceeded(int) = error_spec.
% Construct the inferred `pred' or `func' declarations for a list of
% predicates. Don't construct declarations for the inferred types
% of predicates that represent assertions.
%
:- pred construct_type_inference_messages(module_info::in,
set_tree234(pred_id)::in, list(pred_id)::in,
list(error_spec)::in, list(error_spec)::out) is det.
:- pred maybe_check_for_and_report_any_non_contiguous_clauses(module_info::in,
pred_id::in, pred_info::in, clause_item_numbers::in, list(error_spec)::out)
is det.
%---------------------------------------------------------------------------%
:- implementation.
:- import_module hlds.hlds_error_util.
:- import_module hlds.pred_table.
:- import_module libs.
:- import_module libs.globals.
:- import_module libs.options.
:- import_module mdbcomp.
:- import_module mdbcomp.prim_data.
:- import_module mdbcomp.sym_name.
:- import_module parse_tree.parse_tree_out_pred_decl.
:- import_module parse_tree.parse_tree_out_type.
:- import_module parse_tree.prog_data.
:- import_module parse_tree.prog_type.
:- import_module parse_tree.prog_util.
:- import_module bool.
:- import_module edit_seq.
:- import_module int.
:- import_module maybe.
:- import_module string.
:- import_module varset.
%---------------------------------------------------------------------------%
typecheck_report_max_iterations_exceeded(MaxIterations) = Spec :-
Pieces = [words("Type inference iteration limit exceeded."),
words("This probably indicates that your program has a type error."),
words("You should declare the types explicitly."),
words("(The current limit is"), int_fixed(MaxIterations),
words("iterations."),
words("You can use the"), quote("--type-inference-iteration-limit"),
words("option to increase the limit)."), nl],
Spec = simplest_no_context_spec($pred, severity_error, phase_type_check,
Pieces).
%---------------------------------------------------------------------------%
construct_type_inference_messages(_, _, [], !Specs).
construct_type_inference_messages(ModuleInfo, ValidPredIdSet,
[PredId | PredIds], !Specs) :-
module_info_pred_info(ModuleInfo, PredId, PredInfo),
pred_info_get_markers(PredInfo, Markers),
( if
check_marker(Markers, marker_infer_type),
set_tree234.contains(ValidPredIdSet, PredId),
not pred_info_is_promise(PredInfo, _)
then
Spec = construct_type_inference_message(ModuleInfo, PredId, PredInfo),
!:Specs = [Spec | !.Specs]
else
true
),
construct_type_inference_messages(ModuleInfo, ValidPredIdSet,
PredIds, !Specs).
% Construct a message containing the inferred `pred' or `func' declaration
% for a single predicate.
%
:- func construct_type_inference_message(module_info, pred_id, pred_info)
= error_spec.
construct_type_inference_message(ModuleInfo, PredId, PredInfo) = Spec :-
PredName = pred_info_name(PredInfo),
PredOrFunc = pred_info_is_pred_or_func(PredInfo),
UnqualPredSymName = unqualified(PredName),
pred_info_get_context(PredInfo, Context),
pred_info_get_arg_types(PredInfo, VarSet, ExistQVars, Types0),
strip_module_names_from_type_list(strip_builtin_module_name,
Types0, Types),
pred_info_get_class_context(PredInfo, ClassContext),
pred_info_get_purity(PredInfo, Purity),
MaybeDet = no,
VarNamePrint = print_name_only,
(
PredOrFunc = pf_predicate,
ArgTypes = Types,
MaybeReturnType = no,
TypeStr = mercury_pred_type_to_string(VarSet, VarNamePrint, ExistQVars,
UnqualPredSymName, Types, MaybeDet, Purity, ClassContext)
;
PredOrFunc = pf_function,
pred_args_to_func_args(Types, ArgTypes, ReturnType),
MaybeReturnType = yes(ReturnType),
TypeStr = mercury_func_type_to_string(VarSet, VarNamePrint, ExistQVars,
UnqualPredSymName, ArgTypes, ReturnType, MaybeDet, Purity,
ClassContext)
),
InferredPieces = [invis_order_default_start(2, ""),
words("Inferred"), words(TypeStr), nl],
module_info_get_predicate_table(ModuleInfo, PredicateTable),
ModuleName = pred_info_module(PredInfo),
QualPredSymName = qualified(ModuleName, PredName),
predicate_table_lookup_pf_sym(PredicateTable, is_fully_qualified,
PredOrFunc, QualPredSymName, AllPredIds),
list.delete_all(AllPredIds, PredId, AllOtherPredIds),
PredIsDeclared =
( pred(OtherPredId::in) is semidet :-
module_info_pred_info(ModuleInfo, OtherPredId, OtherPredInfo),
pred_info_get_markers(OtherPredInfo, OtherPredMarkers),
not check_marker(OtherPredMarkers, marker_infer_type)
),
list.filter(PredIsDeclared, AllOtherPredIds, AllOtherDeclaredPredIds),
(
AllOtherDeclaredPredIds = [],
Spec = conditional_spec($pred, inform_inferred_types, yes,
severity_informational, phase_type_check,
[simplest_msg(Context, InferredPieces)])
;
AllOtherDeclaredPredIds = [_ | _],
list.map(
construct_pred_decl_diff(ModuleInfo, ArgTypes, MaybeReturnType),
AllOtherDeclaredPredIds, DiffPieceLists),
Pieces = [invis_order_default_start(2, "")] ++ InferredPieces ++
list.condense(DiffPieceLists),
Spec = simplest_spec($pred, severity_informational, phase_type_check,
Context, Pieces)
).
:- pred construct_pred_decl_diff(module_info::in,
list(mer_type)::in, maybe(mer_type)::in, pred_id::in,
list(format_piece)::out) is det.
construct_pred_decl_diff(ModuleInfo, ActualArgTypes, MaybeActualReturnType,
OtherPredId, Pieces) :-
module_info_pred_info(ModuleInfo, OtherPredId, OtherPredInfo),
pred_info_get_arg_types(OtherPredInfo, OtherTypes),
% It would be nice if we could print the names of type variables,
% but there are two problems. The lesser problem is that the names
% may be different in the varsets of the two predicates, which may be
% confusing. The more deadly problem is that a type variable that
% exists in one of the varsets may not exist in the other.
% Therefore printing variable numbers is the best thing we can do,
% as a general case. (We *could* do better in the special case
% where neither problem arises.)
varset.init(TVarSet),
(
MaybeActualReturnType = no,
list.length(OtherTypes, OtherArity),
pred_decl_lines(TVarSet, ActualArgTypes, ActualLines),
pred_decl_lines(TVarSet, OtherTypes, OtherLines)
;
MaybeActualReturnType = yes(ActualReturnType),
pred_args_to_func_args(OtherTypes, OtherArgTypes, OtherReturnType),
list.length(OtherArgTypes, OtherArity),
func_decl_lines(TVarSet, ActualArgTypes, ActualReturnType,
ActualLines),
func_decl_lines(TVarSet, OtherArgTypes, OtherReturnType,
OtherLines)
),
EditParams = edit_params(1, 1, 2), % cost(replace) = cost(delete + insert).
find_shortest_edit_seq(EditParams, OtherLines, ActualLines, EditSeq),
find_diff_seq(OtherLines, EditSeq, DiffSeq),
DiffPieceLists = list.map(diff_to_pieces, DiffSeq),
list.condense(DiffPieceLists, DiffPieces),
Pieces = [words("The argument list difference from the arity"),
int_fixed(OtherArity), words("version is"), nl] ++ DiffPieces.
:- func diff_to_pieces(diff(string)) = list(format_piece).
diff_to_pieces(Diff) = Pieces :-
(
Diff = unchanged(Line),
Char = " "
;
Diff = deleted(Line),
Char = "-"
;
Diff = inserted(Line),
Char = "+"
),
Pieces = [fixed(Char ++ " " ++ Line), nl].
:- pred pred_decl_lines(tvarset::in, list(mer_type)::in,
list(string)::out) is det.
pred_decl_lines(TVarSet, ArgTypes, Lines) :-
( if list.split_last(ArgTypes, NonLastArgTypes, LastArgType) then
arg_decl_lines("pred", TVarSet, NonLastArgTypes, LastArgType, "",
Lines)
else
Lines = ["pred"]
).
:- pred func_decl_lines(tvarset::in, list(mer_type)::in, mer_type::in,
list(string)::out) is det.
func_decl_lines(TVarSet, ArgTypes, ReturnType, Lines) :-
ReturnTypeStr =
mercury_type_to_string(TVarSet, print_num_only, ReturnType),
ReturnTypeSuffix = " = " ++ ReturnTypeStr,
( if list.split_last(ArgTypes, NonLastArgTypes, LastArgType) then
arg_decl_lines("func", TVarSet, NonLastArgTypes, LastArgType,
ReturnTypeSuffix, Lines)
else
Lines = ["func" ++ ReturnTypeSuffix]
).
:- pred arg_decl_lines(string::in, tvarset::in,
list(mer_type)::in, mer_type::in, string::in, list(string)::out) is det.
arg_decl_lines(PredOrFuncStr, TVarSet, NonLastArgTypes, LastArgType, Suffix,
Lines) :-
NonLastArgTypeStrs = list.map(
mercury_type_to_string(TVarSet, print_num_only), NonLastArgTypes),
AddComma =
(func(NonLastArgTypeStr) = one_indent ++ NonLastArgTypeStr ++ ","),
NonLastArgTypeLines = list.map(AddComma, NonLastArgTypeStrs),
LastArgTypeStr =
mercury_type_to_string(TVarSet, print_num_only, LastArgType),
Lines = [PredOrFuncStr ++ "("] ++
NonLastArgTypeLines ++
[one_indent ++ LastArgTypeStr,
")" ++ Suffix].
:- func one_indent = string.
one_indent = " ".
%---------------------------------------------------------------------------%
maybe_check_for_and_report_any_non_contiguous_clauses(ModuleInfo,
PredId, PredInfo, ItemNumbers, Specs) :-
module_info_get_globals(ModuleInfo, Globals),
globals.lookup_bool_option(Globals, warn_non_contiguous_foreign_procs,
WarnNonContiguousForeignProcs),
(
WarnNonContiguousForeignProcs = yes,
Specs = report_any_non_contiguous_clauses(ModuleInfo,
PredId, PredInfo, ItemNumbers, clauses_and_foreign_procs)
;
WarnNonContiguousForeignProcs = no,
globals.lookup_bool_option(Globals, warn_non_contiguous_clauses,
WarnNonContiguousClauses),
(
WarnNonContiguousClauses = yes,
Specs = report_any_non_contiguous_clauses(ModuleInfo,
PredId, PredInfo, ItemNumbers, only_clauses)
;
WarnNonContiguousClauses = no,
Specs = []
)
).
:- func report_any_non_contiguous_clauses(module_info, pred_id, pred_info,
clause_item_numbers, clause_item_number_types) = list(error_spec).
report_any_non_contiguous_clauses(ModuleInfo, PredId, PredInfo, ItemNumbers,
NumberTypes) = Specs :-
( if
clauses_are_non_contiguous(ItemNumbers, NumberTypes,
FirstRegion, SecondRegion, LaterRegions)
then
Spec = report_non_contiguous_clauses(ModuleInfo, PredId,
PredInfo, FirstRegion, SecondRegion, LaterRegions),
Specs = [Spec]
else
Specs = []
).
:- func report_non_contiguous_clauses(module_info, pred_id, pred_info,
clause_item_number_region, clause_item_number_region,
list(clause_item_number_region)) = error_spec.
report_non_contiguous_clauses(ModuleInfo, PredId, PredInfo,
FirstRegion, SecondRegion, LaterRegions) = Spec :-
PredPieces = describe_one_pred_name(ModuleInfo, should_not_module_qualify,
PredId),
FrontPieces = [words("Warning: non-contiguous clauses for ") | PredPieces]
++ [suffix("."), nl],
pred_info_get_context(PredInfo, Context),
FrontMsg = simplest_msg(Context, FrontPieces),
report_non_contiguous_clause_contexts(PredPieces, 1,
FirstRegion, SecondRegion, LaterRegions, ContextMsgs),
Msgs = [FrontMsg | ContextMsgs],
Spec = error_spec($pred, severity_warning, phase_type_check, Msgs).
:- pred report_non_contiguous_clause_contexts(list(format_piece)::in,
int::in, clause_item_number_region::in, clause_item_number_region::in,
list(clause_item_number_region)::in, list(error_msg)::out) is det.
report_non_contiguous_clause_contexts(PredPieces, GapNumber,
FirstRegion, SecondRegion, LaterRegions, Msgs) :-
FirstRegion =
clause_item_number_region(_FirstLowerNumber, _FirstUpperNumber,
_FirstLowerContext, FirstUpperContext),
SecondRegion =
clause_item_number_region(_SecondLowerNumber, _SecondUpperNumber,
SecondLowerContext, _SecondUpperContext),
( if
GapNumber = 1,
LaterRegions = []
then
% There is only one gap, so don't number it.
GapPieces = []
else
GapPieces = [int_fixed(GapNumber)]
),
% The wording here is chosen be non-confusing even if a clause has a gap
% both before and after it, so that gaps both end and start at the context
% of that clause. We could do better if we had separate contexts for the
% start and the end of the clause, but we don't.
FirstPieces = [words("Gap") | GapPieces] ++
[words("in clauses of") | PredPieces] ++
[words("starts after this clause."), nl],
SecondPieces = [words("Gap") | GapPieces] ++
[words("in clauses of") | PredPieces] ++
[words("ends with this clause."), nl],
FirstMsg = simplest_msg(FirstUpperContext, FirstPieces),
SecondMsg = simplest_msg(SecondLowerContext, SecondPieces),
(
LaterRegions = [],
Msgs = [FirstMsg, SecondMsg]
;
LaterRegions = [FirstLaterRegion | LaterLaterRegions],
report_non_contiguous_clause_contexts(PredPieces, GapNumber + 1,
SecondRegion, FirstLaterRegion, LaterLaterRegions, LaterMsgs),
Msgs = [FirstMsg, SecondMsg | LaterMsgs]
).
%---------------------------------------------------------------------------%
:- end_module check_hlds.typecheck_msgs.
%---------------------------------------------------------------------------%