Files
mercury/compiler/add_class.m
Zoltan Somogyi f070e2a1b7 Convert the make_hlds stage of the compiler from printing out error messages
Estimated hours taken: 14
Branches: main

Convert the make_hlds stage of the compiler from printing out error messages
one at a time to gathering them all up and printing them all at once after
sorting and deleting duplicates. This approach makes it much easier to be
consistent about updating the exit status in the I/O state and the error count
in the module info, and indeed this diff fixes some bugs in this area.

This approach also means that instead of threading a pair of I/O states
through these modules, we now mostly thread through a list of error
specifications. In a couple of places, we create the I/O states we need
for printing progress messages using trace goals.

configure.in:
	Check that the installed compiler supports trace goals (perhaps with
	warnings), since the compiler now uses them.

compiler/Mercury.options:
	Temporarily compensate for a bug in the handling of trace goals.

compiler/add_class.m:
compiler/add_clause.m:
compiler/add_mode.m:
compiler/add_pragma.m:
compiler/add_pred.m:
compiler/add_solver.m:
compiler/add_type.m:
compiler/field_access.m:
compiler/foreign.m:
compiler/make_hlds_error.m:
compiler/make_hlds_passes.m:
compiler/make_hlds_warn.m:
compiler/module_qual.m:
compiler/modules.m:
compiler/qual_info.m:
compiler/state_var.m:
compiler/superhomogeneous.m:
	Make the change described at the top. In many cases, this required
	changing code to error util instead of io.write_strings to create the
	error messages.

	In some cases, move a predicate used in one module but defined in
	another module to the first module.

	Delete some predicates whose job used to be to test options to see
	whether a message should be generated, since we can now embed the
	option value that a message depends on in the error message itself.

	In module_qual.m, remove unnecessary module qualifications.

	In modules.m, give explicit names to a bunch of lambda expressions.

	Reformat comments to exploit the available columns.

compiler/check_typeclass.m:
	Conform to the changes above. Mark with XXX the places where we are
	ignoring the proper update of the error count in module_infos.

compiler/modes.m:
compiler/post_typecheck.m:
compiler/stratify.m:
compiler/table_gen.m:
compiler/unused_args.m:
	Use error_specs instead of plain pieces to print error messages.

compiler/options.m:
	Rename an option that conflicts with a language keyword.

compiler/handle_options.m:
	Conform to the change to options.m.

compiler/prog_data.m:
	Rename some function symbols that conflict with language keywords.

compiler/prog_out.m:
compiler/prog_io_util.m:
	Conform the change above, and delete some predicates that have
	now become unused.

compiler/mercury_compile.m:
	Rename a predicate to avoid an ambiguity.

	Conform to the changes above.

compiler/hlds_out.m:
compiler/make.module_dep_file.m:
compiler/make_hlds.m:
compiler/mercury_to_mercury.m:
compiler/mode_errors.m:
compiler/prog_io.m:
	Conform to the changes above. In some cases, delete predicates
	that aren't needed anymore.

tests/invalid/errors.err_exp:
tests/invalid/errors1.err_exp:
tests/invalid/state_vars_test3.err_exp:
tests/invalid/undef_inst.err_exp:
	Update this expected output to reflect the fact that we now sort
	the error messages.

tests/invalid/missing_interface_import2.err_exp:
tests/warnings/double_underscore.exp:
	Update this expected output to reflect the fact that we no longer
	print the same error message twice.

tests/invalid/missing_det_decls.err_exp:
	Update this expected output to reflect the fact that we now indent
	an error messages correctly.

tests/invalid/multimode_syntax.err_exp:
	Update this expected output to reflect the fact that we now use
	error_util instead of plain io.writes to create an error message.

tests/invalid/typeclass_test.err_exp:
tests/invalid/unsatisfiable_constraint.err_exp:
	Update this expected output to reflect minor improvements in the
	formatting of an error message.
2006-09-10 23:39:17 +00:00

672 lines
28 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
% Copyright (C) 1993-2006 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.
%-----------------------------------------------------------------------------%
:- module hlds.make_hlds.add_class.
:- interface.
:- import_module hlds.hlds_module.
:- import_module hlds.hlds_pred.
:- import_module hlds.make_hlds.make_hlds_passes.
:- import_module hlds.make_hlds.qual_info.
:- import_module mdbcomp.prim_data.
:- import_module parse_tree.error_util.
:- import_module parse_tree.prog_data.
:- import_module list.
:- import_module term.
%-----------------------------------------------------------------------------%
:- pred module_add_class_defn(list(prog_constraint)::in,
list(prog_fundep)::in, sym_name::in, list(tvar)::in, class_interface::in,
tvarset::in, prog_context::in, item_status::in,
module_info::in, module_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
:- pred module_add_instance_defn(module_name::in, list(prog_constraint)::in,
sym_name::in, list(mer_type)::in, instance_body::in, tvarset::in,
import_status::in, prog_context::in,
module_info::in, module_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
% Given the definition for a predicate or function from a
% type class instance declaration, produce the clauses_info
% for that definition.
%
:- pred do_produce_instance_method_clauses(instance_proc_def::in,
pred_or_func::in, arity::in, list(mer_type)::in, pred_markers::in,
term.context::in, import_status::in, clauses_info::out,
module_info::in, module_info::out, qual_info::in, qual_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module check_hlds.clause_to_proc.
:- import_module hlds.hlds_data.
:- import_module hlds.hlds_goal.
:- import_module hlds.hlds_rtti.
:- import_module hlds.make_hlds.add_clause.
:- import_module hlds.make_hlds.add_pred.
:- import_module hlds.make_hlds.add_type.
:- import_module hlds.make_hlds.make_hlds_error.
:- import_module hlds.make_hlds.make_hlds_warn.
:- import_module hlds.make_hlds.state_var.
:- import_module hlds.pred_table.
:- import_module libs.compiler_util.
:- import_module parse_tree.prog_out.
:- import_module parse_tree.prog_type.
:- import_module parse_tree.prog_type_subst.
:- import_module parse_tree.prog_util.
:- import_module bool.
:- import_module int.
:- import_module map.
:- import_module multi_map.
:- import_module pair.
:- import_module set.
:- import_module solutions.
:- import_module string.
:- import_module varset.
module_add_class_defn(Constraints, FunDeps, Name, Vars, Interface, VarSet,
Context, Status, !ModuleInfo, !Specs) :-
module_info_get_class_table(!.ModuleInfo, Classes0),
module_info_get_superclass_table(!.ModuleInfo, SuperClasses0),
list.length(Vars, ClassArity),
ClassId = class_id(Name, ClassArity),
Status = item_status(ImportStatus0, _),
( Interface = abstract ->
make_status_abstract(ImportStatus0, ImportStatus1)
;
ImportStatus1 = ImportStatus0
),
HLDSFunDeps = list.map(make_hlds_fundep(Vars), FunDeps),
(
% The typeclass is exported if *any* occurrence is exported,
% even a previous abstract occurrence.
map.search(Classes0, ClassId, OldDefn)
->
OldDefn = hlds_class_defn(OldStatus, OldConstraints, OldFunDeps,
_OldAncestors, OldVars, _OldKinds, OldInterface, OldMethods,
OldVarSet, OldContext),
combine_status(ImportStatus1, OldStatus, ImportStatus),
(
OldInterface = concrete(_),
ClassMethods0 = OldMethods,
ClassInterface = OldInterface
;
OldInterface = abstract,
ClassMethods0 = [],
ClassInterface = Interface
),
(
\+ superclass_constraints_are_identical(OldVars, OldVarSet,
OldConstraints, Vars, VarSet, Constraints)
->
% Always report the error, even in `.opt' files.
DummyStatus = status_local,
Extras = [words("The superclass constraints do not match."), nl],
multiple_def_error(DummyStatus, Name, ClassArity, "typeclass",
Context, OldContext, Extras, !Specs),
ErrorOrPrevDef = yes
;
\+ class_fundeps_are_identical(OldFunDeps, HLDSFunDeps)
->
% Always report the error, even in `.opt' files.
DummyStatus = status_local,
Extras = [words("The functional dependencies do not match."), nl],
multiple_def_error(DummyStatus, Name, ClassArity, "typeclass",
Context, OldContext, Extras, !Specs),
ErrorOrPrevDef = yes
;
Interface = concrete(_),
OldInterface = concrete(_)
->
multiple_def_error(ImportStatus, Name, ClassArity,
"typeclass", Context, OldContext, [], !Specs),
ErrorOrPrevDef = yes
;
ErrorOrPrevDef = no
),
IsNewDefn = no
;
IsNewDefn = yes `with_type` bool,
ErrorOrPrevDef = no `with_type` bool,
ClassMethods0 = [],
ClassInterface = Interface,
ImportStatus = ImportStatus1
),
(
ErrorOrPrevDef = no,
(
Interface = concrete(Methods),
module_add_class_interface(Name, Vars, Methods,
Status, PredProcIds0, !ModuleInfo, !Specs),
% Get rid of the `no's from the list of maybes
IsYes = (pred(Maybe::in, PredProcId::out) is semidet :-
Maybe = yes(Pred - Proc),
PredProcId = hlds_class_proc(Pred, Proc)
),
list.filter_map(IsYes, PredProcIds0, PredProcIds1),
% The list must be sorted on pred_id and then proc_id --
% check_typeclass.m assumes this when it is generating the
% corresponding list of pred_proc_ids for instance definitions.
list.sort(PredProcIds1, ClassMethods)
;
Interface = abstract,
ClassMethods = ClassMethods0
),
% Ancestors is not set until check_typeclass.
Ancestors = [],
% XXX kind inference:
% We set all the kinds to `star' at the moment. This should be
% done differently when we have a proper kind system.
Kinds = map.init,
Defn = hlds_class_defn(ImportStatus, Constraints, HLDSFunDeps,
Ancestors, Vars, Kinds, ClassInterface, ClassMethods, VarSet,
Context),
map.set(Classes0, ClassId, Defn, Classes),
module_info_set_class_table(Classes, !ModuleInfo),
(
IsNewDefn = yes,
update_superclass_table(ClassId, Vars, VarSet, Constraints,
SuperClasses0, SuperClasses),
module_info_set_superclass_table(SuperClasses, !ModuleInfo),
% When we find the class declaration, make an
% entry for the instances.
module_info_get_instance_table(!.ModuleInfo, Instances0),
map.det_insert(Instances0, ClassId, [], Instances),
module_info_set_instance_table(Instances, !ModuleInfo)
;
IsNewDefn = no
)
;
ErrorOrPrevDef = yes
).
:- func make_hlds_fundep(list(tvar), prog_fundep) = hlds_class_fundep.
make_hlds_fundep(TVars, fundep(Domain0, Range0)) = fundep(Domain, Range) :-
Domain = make_hlds_fundep_2(TVars, Domain0),
Range = make_hlds_fundep_2(TVars, Range0).
:- func make_hlds_fundep_2(list(tvar), list(tvar)) = set(hlds_class_argpos).
make_hlds_fundep_2(TVars, List) = list.foldl(Func, List, set.init) :-
Func = (func(TVar, Set0) = set.insert(Set0, N) :-
N = get_list_index(TVars, 1, TVar)
).
:- func get_list_index(list(T), hlds_class_argpos, T) = hlds_class_argpos.
get_list_index([], _, _) = _ :-
unexpected(this_file, "get_list_index: element not found").
get_list_index([E | Es], N, X) =
( X = E ->
N
;
get_list_index(Es, N + 1, X)
).
:- pred superclass_constraints_are_identical(list(tvar)::in, tvarset::in,
list(prog_constraint)::in, list(tvar)::in, tvarset::in,
list(prog_constraint)::in) is semidet.
superclass_constraints_are_identical(OldVars0, OldVarSet, OldConstraints0,
Vars, VarSet, Constraints) :-
tvarset_merge_renaming(VarSet, OldVarSet, _, Renaming),
apply_variable_renaming_to_prog_constraint_list(Renaming, OldConstraints0,
OldConstraints1),
apply_variable_renaming_to_tvar_list(Renaming, OldVars0, OldVars),
map.from_corresponding_lists(OldVars, Vars, VarRenaming),
apply_variable_renaming_to_prog_constraint_list(VarRenaming,
OldConstraints1, OldConstraints),
OldConstraints = Constraints.
:- pred class_fundeps_are_identical(hlds_class_fundeps::in,
hlds_class_fundeps::in) is semidet.
class_fundeps_are_identical(OldFunDeps0, FunDeps0) :-
% Allow for the functional dependencies to be in a different order.
% we rely on the fact that sets (ordered lists) have a canonical
% representation.
sort_and_remove_dups(OldFunDeps0, OldFunDeps),
sort_and_remove_dups(FunDeps0, FunDeps),
OldFunDeps = FunDeps.
:- pred module_add_class_interface(sym_name::in, list(tvar)::in,
class_methods::in, item_status::in,
list(maybe(pair(pred_id, proc_id)))::out,
module_info::in, module_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
module_add_class_interface(Name, Vars, Methods, Status, PredProcIds,
!ModuleInfo, !Specs) :-
list.filter(is_class_method_mode_item, Methods, ModeMethods,
PredOrFuncMethods),
some [!PPIds] (
add_class_pred_or_func_methods(Name, Vars, PredOrFuncMethods, Status,
!:PPIds, !ModuleInfo, !Specs),
% Add the pred_or_func_mode decls. Since we have already added the
% predicate/function method decls there should already be an entry in
% the predicate table corresponding to the mode item we are about to
% add. If not, report an error.
list.foldl3(add_class_pred_or_func_mode_method(Name, Vars, Status),
ModeMethods, !PPIds, !ModuleInfo, !Specs),
check_method_modes(Methods, !.PPIds, PredProcIds, !ModuleInfo, !Specs)
).
:- pred is_class_method_mode_item(class_method::in) is semidet.
is_class_method_mode_item(Method) :-
Method = method_pred_or_func_mode(_, _, _, _, _, _, _, _).
:- pred add_class_pred_or_func_mode_method(sym_name::in,
list(tvar)::in, item_status::in, class_method::in,
list(maybe(pair(pred_id, proc_id)))::in,
list(maybe(pair(pred_id, proc_id)))::out,
module_info::in, module_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
add_class_pred_or_func_mode_method(Name, Vars, Status, Method,
!PredProcIds, !ModuleInfo, !Specs) :-
(
Method = method_pred_or_func(_, _, _, _, _, _, _, _, _, _, _, _, _),
unexpected(this_file,
"add_class_pred_or_func_mode_method: pred_or_func method item")
;
Method = method_pred_or_func_mode(_VarSet, MaybePredOrFunc, PredName,
Modes, _WithInst, _MaybeDet, _Cond, Context)
),
module_info_get_predicate_table(!.ModuleInfo, PredTable),
PredArity = list.length(Modes) : int,
(
% The only way this could have happened now is if a `with_inst`
% annotation was not expanded.
MaybePredOrFunc = no,
unexpected(this_file, "add_class_pred_or_func_mode_method: " ++
"unexpanded `with_inst` annotation")
;
MaybePredOrFunc = yes(PredOrFunc)
),
(
predicate_table_search_pf_sym_arity(PredTable, is_fully_qualified,
PredOrFunc, PredName, PredArity, Preds),
Preds = [_ | _]
->
(
Preds = [PredId],
module_info_pred_info(!.ModuleInfo, PredId, PredInfo),
pred_info_get_markers(PredInfo, PredMarkers),
( check_marker(PredMarkers, marker_class_method) ->
module_add_class_method(Method, Name, Vars, Status,
PredProcId, !ModuleInfo, !Specs),
list.cons(PredProcId, !PredProcIds)
;
% XXX It may also be worth reporting that although there
% wasn't a matching class method, there was a matching
% predicate/function.
missing_pred_or_func_method_error(PredName, PredArity,
PredOrFunc, Context, !Specs)
)
;
% This shouldn't happen.
Preds = [_, _ | _],
unexpected(this_file, "multiple preds matching method mode")
)
;
missing_pred_or_func_method_error(PredName, PredArity, PredOrFunc,
Context, !Specs)
).
:- pred add_class_pred_or_func_methods(sym_name::in, list(tvar)::in,
class_methods::in, item_status::in,
list(maybe(pair(pred_id, proc_id)))::out,
module_info::in, module_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
add_class_pred_or_func_methods(_, _, [], _, [], !ModuleInfo, !Specs).
add_class_pred_or_func_methods(Name, Vars, [M | Ms], Status, [P | Ps],
!ModuleInfo, !Specs) :-
module_add_class_method(M, Name, Vars, Status, P, !ModuleInfo, !Specs),
add_class_pred_or_func_methods(Name, Vars, Ms, Status, Ps, !ModuleInfo,
!Specs).
:- pred module_add_class_method(class_method::in, sym_name::in, list(tvar)::in,
item_status::in, maybe(pair(pred_id, proc_id))::out,
module_info::in, module_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
module_add_class_method(Method, Name, Vars, Status, MaybePredIdProcId,
!ModuleInfo, !Specs) :-
(
Method = method_pred_or_func(TypeVarSet, InstVarSet, ExistQVars,
PredOrFunc, PredName, TypesAndModes, _WithType, _WithInst,
MaybeDet, _Cond, Purity, ClassContext, Context),
% XXX kind inference:
% We set the kinds to `star' at the moment. This will be different
% when we have a kind system.
prog_type.var_list_to_type_list(map.init, Vars, Args),
ClassContext = constraints(UnivCnstrs, ExistCnstrs),
NewUnivCnstrs = [constraint(Name, Args) | UnivCnstrs],
NewClassContext = constraints(NewUnivCnstrs, ExistCnstrs),
init_markers(Markers0),
add_marker(marker_class_method, Markers0, Markers),
module_add_pred_or_func(TypeVarSet, InstVarSet, ExistQVars, PredOrFunc,
PredName, TypesAndModes, MaybeDet, Purity, NewClassContext,
Markers, Context, Status, MaybePredIdProcId, !ModuleInfo, !Specs)
;
Method = method_pred_or_func_mode(VarSet, MaybePredOrFunc, PredName,
Modes, _WithInst, MaybeDet, _Cond, Context),
(
MaybePredOrFunc = yes(PredOrFunc),
Status = item_status(ImportStatus, _),
IsClassMethod = yes,
module_add_mode(VarSet, PredName, Modes, MaybeDet, ImportStatus,
Context, PredOrFunc, IsClassMethod, PredIdProcId, !ModuleInfo,
!Specs),
MaybePredIdProcId = yes(PredIdProcId)
;
MaybePredOrFunc = no,
% equiv_type.m should have either set the
% pred_or_func or removed the item from the list.
unexpected(this_file, "module_add_class_method: " ++
"no pred_or_func on mode declaration")
)
).
% Insert an entry into the super class table for each super class of
% this class.
%
:- pred update_superclass_table(class_id::in, list(tvar)::in, tvarset::in,
list(prog_constraint)::in, superclass_table::in, superclass_table::out)
is det.
update_superclass_table(ClassId, Vars, VarSet, Constraints, !Supers) :-
list.foldl(update_superclass_table_2(ClassId, Vars, VarSet), Constraints,
!Supers).
:- pred update_superclass_table_2(class_id::in, list(tvar)::in, tvarset::in,
prog_constraint::in, superclass_table::in, superclass_table::out) is det.
update_superclass_table_2(ClassId, Vars, VarSet, Constraint, !Supers) :-
Constraint = constraint(SuperName, SuperTypes),
list.length(SuperTypes, SuperClassArity),
SuperClassId = class_id(SuperName, SuperClassArity),
SubClassDetails = subclass_details(SuperTypes, ClassId, Vars, VarSet),
multi_map.set(!.Supers, SuperClassId, SubClassDetails, !:Supers).
% Go through the list of class methods, looking for
% - functions without mode declarations: add a default mode
% - predicates without mode declarations: report an error
% - mode declarations with no determinism: report an error
%
:- pred check_method_modes(class_methods::in,
list(maybe(pair(pred_id, proc_id)))::in,
list(maybe(pair(pred_id, proc_id)))::out,
module_info::in, module_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
check_method_modes([], !PredProcIds, !ModuleInfo, !Specs).
check_method_modes([Method | Methods], !PredProcIds, !ModuleInfo, !Specs) :-
(
Method = method_pred_or_func(_, _, _, PorF, QualName, TypesAndModes,
_WithType, _WithInst, _, _, _, _, _),
(
QualName = qualified(ModuleName0, Name0),
ModuleName = ModuleName0,
Name = Name0
;
QualName = unqualified(_),
% The class interface should be fully module qualified
% by prog_io.m at the time it is read in.
unexpected(this_file,
"add_default_class_method_func_modes: unqualified func")
),
list.length(TypesAndModes, PredArity),
module_info_get_predicate_table(!.ModuleInfo, PredTable),
(
predicate_table_search_pf_m_n_a(PredTable, is_fully_qualified,
PorF, ModuleName, Name, PredArity, [PredId])
->
module_info_pred_info(!.ModuleInfo, PredId, PredInfo0),
(
PorF = function,
maybe_add_default_func_mode(PredInfo0, PredInfo, MaybeProc),
(
MaybeProc = no
;
MaybeProc = yes(ProcId),
NewPredProc = yes(PredId - ProcId),
!:PredProcIds = [NewPredProc | !.PredProcIds],
module_info_set_pred_info(PredId, PredInfo, !ModuleInfo)
)
;
PorF = predicate,
pred_info_get_procedures(PredInfo0, Procs),
( map.is_empty(Procs) ->
pred_method_with_no_modes_error(PredInfo0, !Specs)
;
true
)
)
;
unexpected(this_file, "handle_methods_with_no_modes")
)
;
Method = method_pred_or_func_mode(_, _, _, _, _, _, _, _)
),
check_method_modes(Methods, !PredProcIds, !ModuleInfo, !Specs).
module_add_instance_defn(InstanceModuleName, Constraints, ClassName,
Types, Body0, VarSet, Status, Context, !ModuleInfo, !Specs) :-
module_info_get_class_table(!.ModuleInfo, Classes),
module_info_get_instance_table(!.ModuleInfo, Instances0),
list.length(Types, ClassArity),
ClassId = class_id(ClassName, ClassArity),
Body = expand_bang_state_var_args_in_instance_method_heads(Body0),
( map.search(Classes, ClassId, _) ->
map.init(Empty),
NewInstanceDefn = hlds_instance_defn(InstanceModuleName, Status,
Context, Constraints, Types, Body, no, VarSet, Empty),
map.lookup(Instances0, ClassId, InstanceDefns),
check_for_overlapping_instances(NewInstanceDefn, InstanceDefns,
ClassId, !Specs),
map.det_update(Instances0, ClassId,
[NewInstanceDefn | InstanceDefns], Instances),
module_info_set_instance_table(Instances, !ModuleInfo)
;
undefined_type_class_error(ClassName, ClassArity, Context,
"instance declaration", !Specs)
).
:- pred check_for_overlapping_instances(hlds_instance_defn::in,
list(hlds_instance_defn)::in, class_id::in,
list(error_spec)::in, list(error_spec)::out) is det.
check_for_overlapping_instances(NewInstanceDefn, InstanceDefns, ClassId,
!Specs) :-
IsOverlapping = (pred((Context - OtherContext)::out) is nondet :-
NewInstanceDefn = hlds_instance_defn(_, _Status, Context,
_, Types, Body, _, VarSet, _),
Body \= abstract, % XXX
list.member(OtherInstanceDefn, InstanceDefns),
OtherInstanceDefn = hlds_instance_defn(_, _OtherStatus,
OtherContext, _, OtherTypes, OtherBody, _, OtherVarSet, _),
OtherBody \= abstract, % XXX
tvarset_merge_renaming(VarSet, OtherVarSet, _NewVarSet, Renaming),
apply_variable_renaming_to_type_list(Renaming, OtherTypes,
NewOtherTypes),
type_list_subsumes(Types, NewOtherTypes, _)
),
solutions.aggregate(IsOverlapping,
report_overlapping_instance_declaration(ClassId), !Specs).
:- pred report_overlapping_instance_declaration(class_id::in,
pair(prog_context)::in,
list(error_spec)::in, list(error_spec)::out) is det.
report_overlapping_instance_declaration(class_id(ClassName, ClassArity),
Context - OtherContext, !Specs) :-
Pieces1 = [words("Error: multiply defined (or overlapping)"),
words("instance declarations for class"),
sym_name_and_arity(ClassName / ClassArity),
suffix("."), nl],
Pieces2 = [words("Previous instance declaration was here.")],
Msg1 = simple_msg(Context, [always(Pieces1)]),
Msg2 = error_msg(yes(OtherContext), yes, 0, [always(Pieces2)]),
Spec = error_spec(severity_error, phase_parse_tree_to_hlds, [Msg1, Msg2]),
!:Specs = [Spec | !.Specs].
do_produce_instance_method_clauses(InstanceProcDefn, PredOrFunc, PredArity,
ArgTypes, Markers, Context, Status, ClausesInfo, !ModuleInfo,
!QualInfo, !Specs) :-
(
% Handle the `pred(<MethodName>/<Arity>) is <ImplName>' syntax.
InstanceProcDefn = name(InstancePredName),
% Add the body of the introduced pred.
% First the goal info, ...
goal_info_init(GoalInfo0),
goal_info_set_context(Context, GoalInfo0, GoalInfo1),
set.list_to_set(HeadVars, NonLocals),
goal_info_set_nonlocals(NonLocals, GoalInfo1, GoalInfo2),
( check_marker(Markers, marker_is_impure) ->
goal_info_set_purity(purity_impure, GoalInfo2, GoalInfo)
; check_marker(Markers, marker_is_semipure) ->
goal_info_set_purity(purity_semipure, GoalInfo2, GoalInfo)
;
GoalInfo = GoalInfo2
),
% ... and then the goal itself.
varset.init(VarSet0),
make_n_fresh_vars("HeadVar__", PredArity, HeadVars, VarSet0, VarSet),
construct_pred_or_func_call(invalid_pred_id, PredOrFunc,
InstancePredName, HeadVars, GoalInfo, IntroducedGoal, !QualInfo),
IntroducedClause = clause([], IntroducedGoal, impl_lang_mercury,
Context),
map.from_corresponding_lists(HeadVars, ArgTypes, VarTypes),
map.init(TVarNameMap),
rtti_varmaps_init(RttiVarMaps),
HasForeignClauses = no,
set_clause_list([IntroducedClause], ClausesRep),
ClausesInfo = clauses_info(VarSet, VarTypes, TVarNameMap, VarTypes,
HeadVars, ClausesRep, RttiVarMaps, HasForeignClauses)
;
% Handle the arbitrary clauses syntax.
InstanceProcDefn = clauses(InstanceClauses),
clauses_info_init(PredArity, ClausesInfo0),
list.foldl4(
produce_instance_method_clause(PredOrFunc, Context, Status),
InstanceClauses, !ModuleInfo, !QualInfo,
ClausesInfo0, ClausesInfo, !Specs)
).
:- pred produce_instance_method_clause(pred_or_func::in,
prog_context::in, import_status::in, item::in,
module_info::in, module_info::out, qual_info::in, qual_info::out,
clauses_info::in, clauses_info::out,
list(error_spec)::in, list(error_spec)::out) is det.
produce_instance_method_clause(PredOrFunc, Context, Status, InstanceClause,
!ModuleInfo, !QualInfo, !ClausesInfo, !Specs) :-
(
InstanceClause = item_clause(_Origin, CVarSet, PredOrFunc, PredName,
HeadTerms0, Body)
->
( illegal_state_var_func_result(PredOrFunc, HeadTerms0, StateVar) ->
report_illegal_func_svar_result(Context, CVarSet, StateVar, !Specs)
;
HeadTerms = expand_bang_state_var_args(HeadTerms0),
PredArity = list.length(HeadTerms),
adjust_func_arity(PredOrFunc, Arity, PredArity),
% The tvarset argument is only used for explicit type
% qualifications, of which there are none in this
% clause, so it is set to a dummy value.
varset.init(TVarSet0),
ProcIds = [],
% Means this clause applies to _every_ mode of the procedure.
GoalType = goal_type_none, % goal is not a promise
clauses_info_add_clause(ProcIds, CVarSet, TVarSet0, HeadTerms,
Body, Context, Status, PredOrFunc, Arity, GoalType, Goal,
VarSet, _TVarSet, !ClausesInfo, Warnings, !ModuleInfo,
!QualInfo, !Specs),
SimpleCallId = simple_call_id(PredOrFunc, PredName, Arity),
% Warn about singleton variables.
warn_singletons(VarSet, SimpleCallId, !.ModuleInfo, Goal, !Specs),
% Warn about variables with overlapping scopes.
warn_overlap(Warnings, VarSet, SimpleCallId, !Specs)
)
;
unexpected(this_file, "produce_clause: invalid instance item")
).
:- pred pred_method_with_no_modes_error(pred_info::in,
list(error_spec)::in, list(error_spec)::out) is det.
pred_method_with_no_modes_error(PredInfo, !Specs) :-
pred_info_context(PredInfo, Context),
Module = pred_info_module(PredInfo),
Name = pred_info_name(PredInfo),
Arity = pred_info_orig_arity(PredInfo),
Pieces = [words("Error: no mode declaration"),
words("for type class method predicate"),
sym_name_and_arity(qualified(Module, Name) / Arity), suffix("."), nl],
Msg = simple_msg(Context, [always(Pieces)]),
Spec = error_spec(severity_error, phase_parse_tree_to_hlds, [Msg]),
!:Specs = [Spec | !.Specs].
:- pred undefined_type_class_error(sym_name::in, arity::in, prog_context::in,
string::in, list(error_spec)::in, list(error_spec)::out) is det.
undefined_type_class_error(ClassName, Arity, Context, Description, !Specs) :-
Pieces = [words("Error:"), words(Description), words("for"),
sym_name_and_arity(ClassName / Arity),
words("without preceding typeclass declaration."), nl],
Msg = simple_msg(Context, [always(Pieces)]),
Spec = error_spec(severity_error, phase_parse_tree_to_hlds, [Msg]),
!:Specs = [Spec | !.Specs].
:- pred missing_pred_or_func_method_error(sym_name::in, arity::in,
pred_or_func::in, prog_context::in,
list(error_spec)::in, list(error_spec)::out) is det.
missing_pred_or_func_method_error(Name, Arity, PredOrFunc, Context, !Specs) :-
Pieces = [words("Error: mode declaration for type class method"),
sym_name_and_arity(Name / Arity), words("without corresponding"),
p_or_f(PredOrFunc), words("method declaration."), nl],
Msg = simple_msg(Context, [always(Pieces)]),
Spec = error_spec(severity_error, phase_parse_tree_to_hlds, [Msg]),
!:Specs = [Spec | !.Specs].
%-----------------------------------------------------------------------------%
:- func this_file = string.
this_file = "add_class.m".
%-----------------------------------------------------------------------------%
:- end_module add_class.
%-----------------------------------------------------------------------------%