This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/include/YapError.h

294 lines
12 KiB
C
Raw Permalink Normal View History

/*************************************************************************
2018-07-17 11:43:57 +01:00
* *
* YAP Prolog %W% %G% *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: YapError.h * mods:
** comments: error header file for YAP *
* version: $Id: Yap.h,v 1.38 2008-06-18 10:02:27 vsc Exp $ *
*************************************************************************/
2013-11-15 01:10:25 +00:00
#ifndef YAP_ERROR_H
#define YAP_ERROR_H 1
2015-09-25 10:57:26 +01:00
#define ECLASS(CL, A, B) CL,
#define E0(A, B) A,
#define E(A, B, C) A,
2018-04-17 17:47:40 +01:00
#define E1(A, B, C) A,
2015-09-25 10:57:26 +01:00
#define E2(A, B, C, D) A,
#define BEGIN_ERRORS() typedef enum {
#define END_ERRORS() \
} \
yap_error_number
#define BEGIN_ERROR_CLASSES() typedef enum {
#define END_ERROR_CLASSES() \
} \
yap_error_class_number
#include "YapErrors.h"
#define MAX_ERROR_MSG_SIZE 1024
2015-09-25 10:57:26 +01:00
2018-07-17 11:43:57 +01:00
extern void Yap_InitError__(const char *file, const char *function, int lineno,
yap_error_number e, YAP_Term g, ...);
2018-02-21 13:02:20 +00:00
2018-07-17 11:43:57 +01:00
extern struct yami *Yap_Error__(bool thrw, const char *file,
const char *function, int lineno,
yap_error_number err, YAP_Term wheret, ...);
2015-09-25 10:57:26 +01:00
2018-02-21 13:02:20 +00:00
extern void Yap_ThrowError__(const char *file, const char *function, int lineno,
2018-03-02 21:18:24 +00:00
yap_error_number err, YAP_Term wheret, ...)
2018-01-05 16:57:38 +00:00
#ifndef MSC_VER
__attribute__((noreturn))
#endif
;
#define Yap_NilError(id, ...) \
2019-01-21 01:11:42 +00:00
Yap_Error__(false, __FILE__, __FUNCTION__, __LINE__, id, TermNil, __VA_ARGS__)
2018-03-02 21:18:24 +00:00
#define Yap_InitError(id, ...) \
2018-02-21 13:02:20 +00:00
Yap_InitError__(__FILE__, __FUNCTION__, __LINE__, id, TermNil, __VA_ARGS__)
#define Yap_Error(id, inp, ...) \
2018-07-17 11:43:57 +01:00
Yap_Error__(false, __FILE__, __FUNCTION__, __LINE__, id, inp, __VA_ARGS__)
2018-01-05 16:57:38 +00:00
#define Yap_ThrowError(id, inp, ...) \
Yap_ThrowError__(__FILE__, __FUNCTION__, __LINE__, id, inp, __VA_ARGS__)
2015-09-29 23:43:02 +01:00
2016-02-11 14:20:16 +00:00
#ifdef YAP_TERM_H
/**
* make sure next argument is a bound instance of type
2016-02-11 14:20:16 +00:00
* atom.
*/
#define Yap_ensure_atom(T0, TF) \
{ if ( (TF = Yap_ensure_atom__(__FILE__, __FUNCTION__, __LINE__, T0 ) == 0L ) return false; \
}
2016-02-11 14:20:16 +00:00
2019-04-27 17:55:22 +01:00
//INLINE_ONLY
static Term Yap_ensure_atom__(const char *fu, const char *fi, int line,
2018-07-17 11:43:57 +01:00
Term in) {
2016-02-11 14:20:16 +00:00
Term t = Deref(in);
// Term Context = Deref(ARG2);
2016-02-11 14:20:16 +00:00
if (!IsVarTerm(t) && IsAtomTerm(t))
return t;
if (IsVarTerm(t)) {
2018-07-17 11:43:57 +01:00
Yap_Error__(false, fu, fi, line, INSTANTIATION_ERROR, t, NULL);
2016-02-11 14:20:16 +00:00
} else {
if (IsAtomTerm(t))
return t;
2018-07-17 11:43:57 +01:00
Yap_Error__(false, fu, fi, line, TYPE_ERROR_ATOM, t, NULL);
return 0L;
}
2016-02-11 14:20:16 +00:00
#endif
2015-09-25 10:57:26 +01:00
#define JMP_LOCAL_ERROR(v, LAB) \
if (H + 2 * (v) > ASP - 1024) { \
LOCAL_Error_TYPE = RESOURCE_ERROR_STACK; \
LOCAL_Error_Size = 2 * (v) * sizeof(CELL); \
goto LAB; \
}
#define LOCAL_ERROR(t, v) \
2015-09-25 10:57:26 +01:00
if (HR + (v) > ASP - 1024) { \
LOCAL_Error_TYPE = RESOURCE_ERROR_STACK; \
LOCAL_Error_Size = 2 * (v) * sizeof(CELL); \
return NULL; \
2013-12-04 23:01:30 +00:00
}
#define LOCAL_TERM_ERROR(t, v) \
2015-09-25 10:57:26 +01:00
if (HR + (v) > ASP - 1024) { \
LOCAL_Error_TYPE = RESOURCE_ERROR_STACK; \
LOCAL_Error_Size = 2 * (v) * sizeof(CELL); \
return 0L; \
}
2015-09-25 10:57:26 +01:00
#define AUX_ERROR(t, n, s, TYPE) \
if (s + (n + 1) > (TYPE *)AuxSp) { \
2018-07-17 11:43:57 +01:00
pop_text_stack(lvl); \
LOCAL_Error_TYPE = RESOURCE_ERROR_AUXILIARY_STACK; \
2015-09-25 10:57:26 +01:00
LOCAL_Error_Size = n * sizeof(TYPE); \
return NULL; \
}
2015-09-25 10:57:26 +01:00
#define AUX_TERM_ERROR(t, n, s, TYPE) \
if (s + (n + 1) > (TYPE *)AuxSp) { \
LOCAL_Error_TYPE = RESOURCE_ERROR_AUXILIARY_STACK; \
LOCAL_Error_Size = n * sizeof(TYPE); \
return 0L; \
}
2015-09-25 10:57:26 +01:00
#define JMP_AUX_ERROR(n, s, t, TYPE, LAB) \
if (s + (n + 1) > (TYPE *)AuxSp) { \
LOCAL_Error_TYPE = RESOURCE_ERROR_AUXILIARY_STACK; \
LOCAL_Error_Size = n * sizeof(TYPE); \
goto LAB; \
}
#define HEAP_ERROR(a, TYPE) \
if (a == NIL) { \
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP; \
LOCAL_Error_Size = n * sizeof(TYPE); \
return NULL; \
}
2016-08-15 20:53:20 +01:00
#define HEAP_TERM_ERROR(a, TYPE, n) \
2015-09-25 10:57:26 +01:00
if (a == NIL) { \
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP; \
LOCAL_Error_Size = n * sizeof(TYPE); \
return 0L; \
}
#define JMP_HEAP_ERROR(a, n, t, TYPE, LAB) \
if (a == NIL) { \
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP; \
LOCAL_Error_Size = n * sizeof(TYPE); \
goto LAB; \
}
/**
* Error stages since it was initially processed.
*
* Notice that some of the stages may be active simultaneouly.
*/
typedef enum yap_error_status {
/// where we like to be
YAP_NO_ERROR_STATUS = 0x0,
/// Prolog discovered the problem
YAP_ERROR_INITIATED_IN_PROLOG = 0x1,
/// The problem was found before the system can cope
YAP_ERROR_INITIATED_IN_BOOT = 0x2,
/// C-helper like must_ found out the problem
YAP_ERROR_INITIATED_IN_HELPER = 0x4,
/// C-builtin crashed
YAP_ERROR_INITIATED_IN_SYSTEM_C = 0x8,
/// user code crashed
YAP_ERROR_INITIATED_IN_USER_C = 0x10,
/// ok, we put a throw to be dispatched
YAP_THROW_THROWN = 0x20,
/// someone caught it
YAP_THROW_CAUGHT = 0x40,
/// error became an exception (usually SWIG bridge)
YAP_ERROR_EXPORTED_TO_CXX = 0x80,
/// handle error in Prolog
YAP_ERROR_BEING_PROCESSED_IN_PROLOG = 0x100
/// go back t
} yap_error_stage_t;
2017-11-29 13:47:57 +00:00
/// a Prolog goal that caused a bug
2016-10-20 04:44:59 +01:00
2017-12-10 11:42:51 +00:00
typedef struct yap_error_prolog_source {
2017-11-29 13:47:57 +00:00
intptr_t prologPredCl;
uintptr_t prologPredLine;
uintptr_t prologPredFirstLine;
uintptr_t prologPredLastLine;
2018-01-05 16:57:38 +00:00
const char *prologPredName;
2017-11-29 13:47:57 +00:00
uintptr_t prologPredArity;
2018-01-05 16:57:38 +00:00
const char *prologPredModule;
const char *prologPredFile;
2017-11-29 13:47:57 +00:00
void *errorGoal;
2016-10-20 04:44:59 +01:00
struct error_prolog_source *errorParent;
2017-12-10 11:42:51 +00:00
} yap_error_prolog_source_t;
2016-10-20 04:44:59 +01:00
/// all we need to know about an error/throw
2017-12-10 11:42:51 +00:00
typedef struct s_yap_error_descriptor {
2018-10-22 12:38:13 +01:00
/// error identifier
2018-04-02 14:49:45 +01:00
yap_error_number errorNo;
2018-10-22 12:38:13 +01:00
/// kind of error: derived from errorNo;
yap_error_class_number errorClass;
2018-10-22 12:38:13 +01:00
/// if non-NULL: goal who caused error;
2018-04-02 14:49:45 +01:00
const char *errorGoal;
2018-10-22 12:38:13 +01:00
/// errorNo as text
2018-01-05 16:57:38 +00:00
const char *errorAsText;
2018-10-22 12:38:13 +01:00
/// errorClass as text
2018-01-05 16:57:38 +00:00
const char *classAsText;
2018-10-22 12:38:13 +01:00
/// c-code that generated the error
/// C-line
2017-11-29 13:47:57 +00:00
intptr_t errorLine;
2018-10-22 12:38:13 +01:00
/// C-function
const char *errorFunction;
2018-10-22 12:38:13 +01:00
/// C-file
const char *errorFile;
2016-10-20 04:44:59 +01:00
// struct error_prolog_source *errorSource;
2018-10-22 12:38:13 +01:00
/// Prolog predicate that caused the error: name
2018-01-05 16:57:38 +00:00
const char *prologPredName;
2018-10-22 12:38:13 +01:00
/// Prolog predicate that caused the error:arity
2017-11-29 13:47:57 +00:00
uintptr_t prologPredArity;
2018-10-22 12:38:13 +01:00
/// Prolog predicate that caused the error:module
2018-01-05 16:57:38 +00:00
const char *prologPredModule;
2018-10-22 12:38:13 +01:00
/// Prolog predicate that caused the error:line
2018-01-05 16:57:38 +00:00
const char *prologPredFile;
2018-10-22 12:38:13 +01:00
/// line where error clause defined
uintptr_t prologPredLine;
/// syntax and other parsing errors
uintptr_t parserPos;
uintptr_t parserFirstPos;
uintptr_t parserLastPos;
uintptr_t parserLine;
uintptr_t parserFirstLine;
uintptr_t parserLastLine;
const char *parserTextA;
const char *parserTextB;
const char *parserFile;
/// reading a clause, or called from read?
bool parserReadingCode;
/// whether we are consulting
2017-11-29 13:47:57 +00:00
bool prologConsulting;
2018-07-17 11:43:57 +01:00
const char *culprit;
2018-10-28 10:10:40 +00:00
/// Prolog stack at the time
const char *prologStack;
2018-04-02 14:49:45 +01:00
YAP_Term errorRawTerm, rawExtraErrorTerm;
2018-10-22 12:38:13 +01:00
char *errorMsg;
size_t errorMsgLen;
2017-12-10 11:42:51 +00:00
struct s_yap_error_descriptor *top_error;
} yap_error_descriptor_t;
/// compatibility with existing code..
2016-10-20 04:44:59 +01:00
#define LOCAL_Error_TYPE LOCAL_ActiveError->errorNo
#define LOCAL_Error_File LOCAL_ActiveError->errorFile
#define LOCAL_Error_Function LOCAL_ActiveError->errorFunction
#define LOCAL_Error_Lineno LOCAL_ActiveError->errorLine
#define LOCAL_Error_Size LOCAL_ActiveError->errorMsgLen
2017-10-28 03:21:34 +01:00
#define LOCAL_RawTerm LOCAL_ActiveError->errorRawTerm
2016-10-20 04:44:59 +01:00
#define LOCAL_ErrorMessage LOCAL_ActiveError->errorMsg
2018-05-24 12:00:10 +01:00
extern void Yap_CatchError(void);
2018-05-13 12:59:17 +01:00
extern void Yap_ThrowExistingError(void);
2018-10-22 12:38:13 +01:00
extern YAP_Term Yap_MkFullError(void);
2018-07-17 11:43:57 +01:00
extern bool Yap_MkErrorRecord(
yap_error_descriptor_t * r, const char *file, const char *function,
int lineno, yap_error_number type, YAP_Term where, const char *msg);
extern yap_error_descriptor_t *Yap_pc_add_location(
yap_error_descriptor_t * t, void *pc0, void *b_ptr0, void *env0);
extern yap_error_descriptor_t *Yap_env_add_location(
yap_error_descriptor_t * t, void *cp0, void *b_ptr0, void *env0,
YAP_Int ignore_first);
2018-10-28 10:10:40 +00:00
extern const char *Yap_dump_stack(void);
2018-07-17 11:43:57 +01:00
extern yap_error_descriptor_t *Yap_prolog_add_culprit(yap_error_descriptor_t *
t);
extern yap_error_class_number Yap_errorClass(yap_error_number e);
extern const char *Yap_errorName(yap_error_number e);
extern const char *Yap_errorClassName(yap_error_class_number e);
2018-07-17 11:43:57 +01:00
extern bool Yap_pushErrorContext(bool pass,
yap_error_descriptor_t *new_error);
2018-04-16 14:54:53 +01:00
extern yap_error_descriptor_t *Yap_popErrorContext(bool oerr, bool pass);
2016-10-20 04:44:59 +01:00
2019-02-01 13:14:33 +00:00
#define must_be_variable(t) if (!IsVarTerm(t)) Yap_ThrowError(UNINSTANTIATION_ERROR, v, NULL)
2013-11-15 01:10:25 +00:00
#endif
2019-02-01 13:14:33 +00:00