mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-17 10:23:46 +00:00
Estimated hours taken: 0.1 library/require.m: Add missing `#include' of "mercury_trace.h", needed for MR_trace_report().
100 lines
2.7 KiB
Mathematica
100 lines
2.7 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% Copyright (C) 1993-1998 The University of Melbourne.
|
|
% This file may only be copied under the terms of the GNU Library General
|
|
% Public License - see the file COPYING.LIB in the Mercury distribution.
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- module require.
|
|
|
|
% Main author: fjh.
|
|
% Stability: medium to high.
|
|
|
|
% This module provides features similar to <assert.h> in C.
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
:- interface.
|
|
|
|
:- pred error(string).
|
|
:- mode error(in) is erroneous.
|
|
|
|
% error(Message).
|
|
% Abort with error message.
|
|
|
|
|
|
:- pred require(pred, string).
|
|
:- mode require((pred) is semidet, in) is det.
|
|
|
|
% require(Goal, Message).
|
|
% Call goal, and abort with error message if Goal fails.
|
|
% This is not as useful as you might imagine, since it requires
|
|
% that the goal not produce any output variables. In
|
|
% most circumstances you should use an explicit if-then-else
|
|
% with a call to error/1 in the "else".
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
:- implementation.
|
|
|
|
require(Goal, Message) :-
|
|
( call(Goal) ->
|
|
true
|
|
;
|
|
error(Message),
|
|
fail
|
|
).
|
|
|
|
%-----------------------------------------------------------------------------%
|
|
|
|
/* error/1, from require.m */
|
|
|
|
:- pragma c_header_code("
|
|
#include <stdio.h>
|
|
#include ""mercury_trace.h""
|
|
#include ""mercury_stack_trace.h""
|
|
").
|
|
|
|
% Hopefully error/1 won't be called often (!), so no point inlining it.
|
|
:- pragma no_inline(error/1).
|
|
|
|
error(Message) :-
|
|
error_internal(Message).
|
|
|
|
:- pred error_internal(string::in) is erroneous.
|
|
|
|
% We define error using handwritten code in error_internal because we
|
|
% need complete control over it if we want to call MR_dump_stack. In
|
|
% particular we don't want to have to explicitly tell MR_dump_stack whether
|
|
% a stack frame was generated by its caller. The easiest way to do
|
|
% this is to make sure it wasn't.
|
|
|
|
:- external(error_internal/1).
|
|
|
|
:- pragma c_code("
|
|
|
|
Define_extern_entry(mercury__require__error_internal_1_0);
|
|
|
|
MR_MAKE_STACK_LAYOUT_ENTRY(mercury__require__error_internal_1_0);
|
|
|
|
BEGIN_MODULE(require_module_internal)
|
|
init_entry(mercury__require__error_internal_1_0);
|
|
BEGIN_CODE
|
|
|
|
/* code for predicate 'error_internal'/1 in mode 0 */
|
|
Define_entry(mercury__require__error_internal_1_0);
|
|
{
|
|
String Message;
|
|
Message = (String) r1;
|
|
|
|
fflush(stdout);
|
|
fprintf(stderr, ""Software error: %s\\n"", Message);
|
|
MR_trace_report(stderr);
|
|
MR_dump_stack(MR_succip, MR_sp, MR_curfr);
|
|
exit(1);
|
|
}
|
|
END_MODULE
|
|
").
|
|
|
|
:- end_module require.
|
|
|
|
/*---------------------------------------------------------------------------*/
|