Added better warnings and error messages. Fixed bug with having

Estimated hours taken: 3

Added better warnings and error messages. Fixed bug with having
pragma(c_code, ...) decs for different modes of the same pred, and with having
clauses and pragma(c_code, ...) decs.

hlds.m:
	Added a fields to the pred_info indicating whether the pred contained
	pragma(c_code, ...) decs, clauses, or was currently empty. Also
	updated the access predicates, and require a new argument for
	pred_info_init
lambda.m:
	Pass the new argument to pred_info_init
make_hlds.m:
	Improve error messages and warnings. Fix bug with pragma decs for
	different modes, and bug with clauses an pragmas for same pred.
This commit is contained in:
David Jeffery
1995-11-20 09:57:24 +00:00
parent 1fdaa23c07
commit 970bf7b2f1
3 changed files with 115 additions and 52 deletions

View File

@@ -53,6 +53,11 @@
:- type pred_proc_id == pair(pred_id, proc_id).
% the type of goals that have been given for a pred.
:- type goal_type ---> pragmas % pragma(c_code, ...)
; clauses
; none.
%-----------------------------------------------------------------------------%
:- implementation.
@@ -443,7 +448,7 @@ inst_table_set_shared_insts(inst_table(A, B, C, D, _), SharedInsts,
hlds__goal % The <Else> part
)
% Inline C code from a pragma(c_code) decl.
% C code from a pragma(c_code, ...) decl.
; pragma_c_code(
c_code, % The C code to do the work
pred_id, % The called predicate
@@ -1583,8 +1588,8 @@ make_cons_id(unqualified(Name), Args, _TypeId, cons(Name, Arity)) :-
:- pred pred_info_init(module_name, sym_name, arity, tvarset, list(type),
condition, term__context, clauses_info, import_status,
bool, pred_info).
:- mode pred_info_init(in, in, in, in, in, in, in, in, in, in, out) is det.
bool, goal_type, pred_info).
:- mode pred_info_init(in, in, in, in, in, in, in, in, in, in, in, out) is det.
:- pred pred_info_module(pred_info, module_name).
:- mode pred_info_module(in, out) is det.
@@ -1659,6 +1664,12 @@ make_cons_id(unqualified(Name), Args, _TypeId, cons(Name, Arity)) :-
:- pred pred_info_set_inlined(pred_info, bool, pred_info).
:- mode pred_info_set_inlined(in, in, out) is det.
:- pred pred_info_get_goal_type(pred_info, goal_type).
:- mode pred_info_get_goal_type(in, out) is det.
:- pred pred_info_set_goal_type(pred_info, goal_type, pred_info).
:- mode pred_info_set_goal_type(in, in, out) is det.
%-----------------------------------------------------------------------------%
:- implementation.
@@ -1702,21 +1713,24 @@ predicate_arity(ModuleInfo, PredId, Arity) :-
tvarset, % names of type vars
% in the predicate's type decl
% or in the variable type assignments
bool % whether or not to automatically
bool, % whether or not to automatically
% inline this pred
goal_type % whether the goals seen so far for
% this pred are clauses,
% pragma(c_code, ...) decs, or none
).
pred_info_init(ModuleName, SymName, Arity, TypeVarSet, Types, Cond, Context,
ClausesInfo, Status, Inline, PredInfo) :-
ClausesInfo, Status, Inline, GoalType, PredInfo) :-
map__init(Procs),
unqualify_name(SymName, PredName),
sym_name_get_module_name(SymName, ModuleName, PredModuleName),
PredInfo = predicate(TypeVarSet, Types, Cond, ClausesInfo, Procs,
Context, PredModuleName, PredName, Arity, Status, TypeVarSet,
Inline).
Inline, GoalType).
pred_info_procids(PredInfo, ProcIds) :-
PredInfo = predicate(_, _, _, _, Procs, _, _, _, _, _, _, _),
PredInfo = predicate(_, _, _, _, Procs, _, _, _, _, _, _, _, _),
map__keys(Procs, ProcIds).
pred_info_non_imported_procids(PredInfo, ProcIds) :-
@@ -1732,75 +1746,86 @@ pred_info_non_imported_procids(PredInfo, ProcIds) :-
).
pred_info_clauses_info(PredInfo, Clauses) :-
PredInfo = predicate(_, _, _, Clauses, _, _, _, _, _, _, _, _).
PredInfo = predicate(_, _, _, Clauses, _, _, _, _, _, _, _, _, _).
pred_info_set_clauses_info(PredInfo0, Clauses, PredInfo) :-
PredInfo0 = predicate(A, B, C, _, E, F, G, H, I, J, K, L),
PredInfo = predicate(A, B, C, Clauses, E, F, G, H, I, J, K, L).
PredInfo0 = predicate(A, B, C, _, E, F, G, H, I, J, K, L, M),
PredInfo = predicate(A, B, C, Clauses, E, F, G, H, I, J, K, L, M).
pred_info_arg_types(PredInfo, TypeVars, ArgTypes) :-
PredInfo = predicate(TypeVars, ArgTypes, _, _, _, _, _, _, _, _, _, _).
PredInfo = predicate(TypeVars, ArgTypes,
_, _, _, _, _, _, _, _, _, _, _).
pred_info_set_arg_types(PredInfo0, TypeVarSet, ArgTypes, PredInfo) :-
PredInfo0 = predicate(_, _, C, D, E, F, G, H, I, J, K, L),
PredInfo0 = predicate(_, _, C, D, E, F, G, H, I, J, K, L, M),
PredInfo =
predicate(TypeVarSet, ArgTypes, C, D, E, F, G, H, I, J, K, L).
predicate(TypeVarSet, ArgTypes,
C, D, E, F, G, H, I, J, K, L, M).
pred_info_procedures(PredInfo, Procs) :-
PredInfo = predicate(_, _, _, _, Procs, _, _, _, _, _, _, _).
PredInfo = predicate(_, _, _, _, Procs, _, _, _, _, _, _, _, _).
pred_info_set_procedures(PredInfo0, Procedures, PredInfo) :-
PredInfo0 = predicate(A, B, C, D, _, F, G, H, I, J, K, L),
PredInfo = predicate(A, B, C, D, Procedures, F, G, H, I, J, K, L).
PredInfo0 = predicate(A, B, C, D, _, F, G, H, I, J, K, L, M),
PredInfo = predicate(A, B, C, D, Procedures, F, G, H, I, J, K, L, M).
pred_info_context(PredInfo, Context) :-
PredInfo = predicate(_, _, _, _, _, Context, _, _, _, _, _, _).
PredInfo = predicate(_, _, _, _, _, Context, _, _, _, _, _, _, _).
pred_info_module(PredInfo, Module) :-
PredInfo = predicate(_, _, _, _, _, _, Module, _, _, _, _, _).
PredInfo = predicate(_, _, _, _, _, _, Module, _, _, _, _, _, _).
pred_info_name(PredInfo, PredName) :-
PredInfo = predicate(_, _, _, _, _, _, _, PredName, _, _, _, _).
PredInfo = predicate(_, _, _, _, _, _, _, PredName, _, _, _, _, _).
pred_info_arity(PredInfo, Arity) :-
PredInfo = predicate(_, _, _, _, _, _, _, _, Arity, _, _, _).
PredInfo = predicate(_, _, _, _, _, _, _, _, Arity, _, _, _, _).
pred_info_import_status(PredInfo, ImportStatus) :-
PredInfo = predicate(_, _, _, _, _, _, _, _, _, ImportStatus, _, _).
PredInfo = predicate(_, _, _, _, _, _, _, _, _, ImportStatus, _, _, _).
pred_info_is_imported(PredInfo) :-
PredInfo = predicate(_, _, _, _, _, _, _, _, _, imported, _, _).
PredInfo = predicate(_, _, _, _, _, _, _, _, _, imported, _, _, _).
pred_info_is_pseudo_imported(PredInfo) :-
PredInfo = predicate(_, _, _, _, _, _, _, _, _, pseudo_imported, _, _).
PredInfo = predicate(_, _, _, _, _, _, _, _, _, pseudo_imported,
_, _, _).
pred_info_is_exported(PredInfo) :-
PredInfo = predicate(_, _, _, _, _, _, _, _, _, exported, _, _).
PredInfo = predicate(_, _, _, _, _, _, _, _, _, exported, _, _, _).
pred_info_is_pseudo_exported(PredInfo) :-
PredInfo = predicate(_, _, _, _, _, _, _, _, _, pseudo_exported, _, _).
PredInfo = predicate(_, _, _, _, _, _, _, _, _, pseudo_exported,
_, _, _).
pred_info_mark_as_external(PredInfo0, PredInfo) :-
PredInfo0 = predicate(A, B, C, D, E, F, G, H, I, _, K, L),
PredInfo = predicate(A, B, C, D, E, F, G, H, I, imported, K, L).
PredInfo0 = predicate(A, B, C, D, E, F, G, H, I, _, K, L, M),
PredInfo = predicate(A, B, C, D, E, F, G, H, I, imported, K, L, M).
pred_info_set_status(PredInfo0, Status, PredInfo) :-
PredInfo0 = predicate(A, B, C, D, E, F, G, H, I, _, K, L),
PredInfo = predicate(A, B, C, D, E, F, G, H, I, Status, K, L).
PredInfo0 = predicate(A, B, C, D, E, F, G, H, I, _, K, L, M),
PredInfo = predicate(A, B, C, D, E, F, G, H, I, Status, K, L, M).
pred_info_typevarset(PredInfo, TypeVarSet) :-
PredInfo = predicate(_, _, _, _, _, _, _, _, _, _, TypeVarSet, _).
PredInfo = predicate(_, _, _, _, _, _, _, _, _, _, TypeVarSet, _, _).
pred_info_set_typevarset(PredInfo0, TypeVarSet, PredInfo) :-
PredInfo0 = predicate(A, B, C, D, E, F, G, H, I, J, _, L),
PredInfo = predicate(A, B, C, D, E, F, G, H, I, J, TypeVarSet, L).
PredInfo0 = predicate(A, B, C, D, E, F, G, H, I, J, _, L, M),
PredInfo = predicate(A, B, C, D, E, F, G, H, I, J, TypeVarSet, L, M).
pred_info_is_inlined(PredInfo) :-
PredInfo = predicate(_, _, _, _, _, _, _, _, _, _, _, yes).
PredInfo = predicate(_, _, _, _, _, _, _, _, _, _, _, yes, _).
pred_info_set_inlined(PredInfo0, Inlined, PredInfo) :-
PredInfo0 = predicate(A, B, C, D, E, F, G, H, I, J, K, _),
PredInfo = predicate(A, B, C, D, E, F, G, H, I, J, K, Inlined).
PredInfo0 = predicate(A, B, C, D, E, F, G, H, I, J, K, _, M),
PredInfo = predicate(A, B, C, D, E, F, G, H, I, J, K, Inlined, M).
pred_info_get_goal_type(PredInfo, GoalType) :-
PredInfo = predicate(_, _, _, _, _, _, _, _, _, _, _, _, GoalType).
pred_info_set_goal_type(PredInfo0, GoalType, PredInfo) :-
PredInfo0 = predicate(A, B, C, D, E, F, G, H, I, J, K, L, _),
PredInfo = predicate(A, B, C, D, E, F, G, H, I, J, K, L, GoalType).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%

View File

@@ -314,7 +314,7 @@ XXX this optimization temporarily disabled, see comment above
clauses_info_init(Arity, ClausesInfo),
pred_info_init(ModuleName, PredName, Arity, TVarSet,
ArgTypes, Cond, LambdaContext, ClausesInfo, Status,
no, PredInfo0),
no, none, PredInfo0),
%
% Create a single mode for the new predicate, and insert

View File

@@ -147,7 +147,7 @@ add_item_decl(pragma(Pragma), Context, Status, Module0, Status,
{ Pragma = c_header_code(C_Header) },
{ module_add_c_header(C_Header, Context, Module0, Module) }
;
% Handle pragma(c_code) decls later on (when we process
% Handle pragma(c_code, ...) decls later on (when we process
% clauses).
{ Pragma = c_code(_, _, _, _) },
{ Module = Module0 }
@@ -571,7 +571,7 @@ add_new_pred(Module0, TVarSet, PredName, Types, Cond, Context, Status,
{ module_info_get_predicate_table(Module1, PredicateTable0) },
{ clauses_info_init(Arity, ClausesInfo) },
{ pred_info_init(ModuleName, PredName, Arity, TVarSet, Types,
Cond, Context, ClausesInfo, Status, no,
Cond, Context, ClausesInfo, Status, no, none,
PredInfo0) },
(
{ \+ predicate_table_search_m_n_a(PredicateTable0,
@@ -687,7 +687,7 @@ add_special_pred_decl(SpecialPredId,
Cond = true,
clauses_info_init(Arity, ClausesInfo0),
pred_info_init(ModuleName, PredName, Arity, TVarSet, ArgTypes, Cond,
Context, ClausesInfo0, Status, no, PredInfo0),
Context, ClausesInfo0, Status, no, none, PredInfo0),
add_new_proc(PredInfo0, Arity, ArgModes, yes(Det), Context,
PredInfo, _),
@@ -800,7 +800,7 @@ preds_add_implicit(PredicateTable0,
Cond = true,
clauses_info_init(Arity, ClausesInfo),
pred_info_init(ModuleName, PredName, Arity, TVarSet, Types, Cond,
Context, ClausesInfo, local, no, PredInfo),
Context, ClausesInfo, local, no, none, PredInfo),
unqualify_name(PredName, PName), % ignore any module qualifier
(
\+ predicate_table_search_m_n_a(PredicateTable0,
@@ -912,6 +912,19 @@ module_add_clause(ModuleInfo0, ClauseVarSet, PredName, Args, Body, Context,
( { pred_info_is_imported(PredInfo0) } ->
{ module_info_incr_errors(ModuleInfo1, ModuleInfo) },
clause_for_imported_pred_error(PredName, Arity, Context)
;
{ pred_info_get_goal_type(PredInfo0, pragmas) }
->
{ module_info_incr_errors(ModuleInfo1, ModuleInfo) },
prog_out__write_context(Context),
io__write_string("Error: clause for pred `"),
io__write_string(PName),
io__write_string("/"),
io__write_int(Arity),
io__write_string("'\n"),
prog_out__write_context(Context),
io__write_string(
" with pragma(c_code, ...) declaration preceding.\n")
;
{
pred_info_clauses_info(PredInfo0, Clauses0),
@@ -919,7 +932,8 @@ module_add_clause(ModuleInfo0, ClauseVarSet, PredName, Args, Body, Context,
map__keys(Procs, ModeIds),
clauses_info_add_clause(Clauses0, ModeIds, ClauseVarSet, Args,
Body, Context, Goal, VarSet, Clauses, Warnings),
pred_info_set_clauses_info(PredInfo0, Clauses, PredInfo),
pred_info_set_clauses_info(PredInfo0, Clauses, PredInfo1),
pred_info_set_goal_type(PredInfo1, clauses, PredInfo),
map__set(Preds0, PredId, PredInfo, Preds),
predicate_table_set_preds(PredicateTable1, Preds,
PredicateTable),
@@ -1003,6 +1017,19 @@ module_add_pragma_c_code(PredName, PVars, VarSet, C_Code0, Context,
io__write_string("/"),
io__write_int(Arity),
io__write_string("'.\n")
;
{ pred_info_get_goal_type(PredInfo0, clauses) }
->
{ module_info_incr_errors(ModuleInfo1, ModuleInfo) },
prog_out__write_context(Context),
io__write_string("Error: pragma(c_code, ...) declaration "),
io__write_string("for pred `"),
io__write_string(PName),
io__write_string("/"),
io__write_int(Arity),
io__write_string("'\n"),
prog_out__write_context(Context),
io__write_string(" with clauses preceding.\n")
;
% add the pragma declaration to the proc_info for this procedure
{ pred_info_procedures(PredInfo0, Procs) },
@@ -1017,6 +1044,8 @@ module_add_pragma_c_code(PredName, PVars, VarSet, C_Code0, Context,
ProcId, VarSet, PVars, C_Code, Context,
Clauses, Goal) },
{ pred_info_set_clauses_info(PredInfo0, Clauses,
PredInfo1) },
{ pred_info_set_goal_type(PredInfo1, pragmas,
PredInfo) },
{ map__set(Preds0, PredId, PredInfo, Preds) },
{ predicate_table_set_preds(PredicateTable0, Preds,
@@ -1316,19 +1345,19 @@ warn_singletons_in_pragma_c_code(C_Code, Args, ArgNameMap,
io__stderr_stream(StdErr),
io__set_output_stream(StdErr, OldStream),
prog_out__write_context(Context),
io__write_string("In pragma(c_code, ...) for predicate `"),
hlds_out__write_pred_call_id(PredCallId),
io__write_string("':\n"),
prog_out__write_context(Context),
( { SingletonVars = [_] } ->
io__write_string(" Warning: variable `"),
io__write_string("Warning: variable `"),
write_string_list(SingletonVars),
io__write_string("' do not occur in C code.\n")
io__write_string("' does not occur in C code\n")
;
io__write_string(" Warning: variables `"),
io__write_string("Warning: variables `"),
write_string_list(SingletonVars),
io__write_string("' do not occur in C code.\n")
io__write_string("' do not occur in C code\n")
),
prog_out__write_context(Context),
io__write_string(" in pragma(c_code, ...) for predicate `"),
hlds_out__write_pred_call_id(PredCallId),
io__write_string("'.\n"),
io__set_output_stream(OldStream, _)
).
@@ -1357,6 +1386,8 @@ warn_singletons_in_pragma_c_code_2(C_CodeList, [Arg|Args], ArgNameMap,
%-----------------------------------------------------------------------------%
% c_code_to_name_list(Code, List) is true iff List is a list of the
% identifiers used in the C code in Code.
:- pred c_code_to_name_list(string, list(string)).
:- mode c_code_to_name_list(in, out) is det.
@@ -1372,6 +1403,7 @@ c_code_to_name_list_2(C_Code, List) :-
(
NameCharList = []
->
% no names left
List = []
;
c_code_to_name_list_2(TheRest, Names),
@@ -1390,6 +1422,8 @@ get_first_c_name([C|CodeChars], NameCharList, TheRest) :-
get_first_c_name_in_word(CodeChars, NameCharList0, TheRest),
NameCharList = [C|NameCharList0]
;
% strip off any characters in the C code which
% don't form part of an identifier.
get_first_c_name(CodeChars, NameCharList, TheRest)
).
@@ -1401,9 +1435,11 @@ get_first_c_name_in_word([C|CodeChars], NameCharList, TheRest) :-
(
char__is_alnum_or_underscore(C)
->
% There are more characters in the word
get_first_c_name_in_word(CodeChars, NameCharList0, TheRest),
NameCharList = [C|NameCharList0]
;
% The word is finished
NameCharList = [],
TheRest = CodeChars
).
@@ -1542,7 +1578,7 @@ clauses_info_add_clause(ClausesInfo0, ModeIds, CVarSet, Args, Body,
clauses_info_add_pragma_c_code(ClausesInfo0, PredId, ModeId, PVarSet, PVars,
C_Code, Context, ClausesInfo, HldsGoal) :-
ClausesInfo0 = clauses_info(VarSet0, VarTypes, HeadVars, _ClauseList),
ClausesInfo0 = clauses_info(VarSet0, VarTypes, HeadVars, ClauseList),
pragma_get_vars(PVars, Args0),
pragma_get_var_names(PVars, Names),
@@ -1554,7 +1590,8 @@ clauses_info_add_pragma_c_code(ClausesInfo0, PredId, ModeId, PVarSet, PVars,
% build the pragma_c_code
map__from_corresponding_lists(Args, Names, ArgNameMap),
goal_info_init(GoalInfo),
goal_info_init(GoalInfo0),
goal_info_set_context(GoalInfo0, Context, GoalInfo),
HldsGoal0 = pragma_c_code(C_Code, PredId, ModeId, Args, ArgNameMap) -
GoalInfo,
@@ -1565,7 +1602,8 @@ clauses_info_add_pragma_c_code(ClausesInfo0, PredId, ModeId, PVarSet, PVars,
implicitly_quantify_clause_body(HeadVars, HldsGoal1, VarSet2, Empty,
HldsGoal, VarSet, _, _Warnings),
NewClause = clause([ModeId], HldsGoal, Context),
ClausesInfo = clauses_info(VarSet, VarTypes, HeadVars, [NewClause]).
ClausesInfo = clauses_info(VarSet, VarTypes, HeadVars,
[NewClause|ClauseList]).
%-----------------------------------------------------------------------------