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/C/modules.c

436 lines
10 KiB
C
Raw Normal View History

/*************************************************************************
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
File: modules.c *
* Last rev: *
* mods: *
* comments: module support *
* *
*************************************************************************/
#ifdef SCCS
2014-11-25 12:03:48 +00:00
static char SccsId[] = "%W% %G%";
#endif
#include "Yap.h"
#include "Yatom.h"
#include "YapHeap.h"
2014-11-25 12:03:48 +00:00
static Int p_current_module(USES_REGS1);
static Int p_current_module1(USES_REGS1);
static ModEntry *LookupModule(Term a);
2015-04-21 23:09:43 +01:00
static ModEntry *FetchModuleEntry(Atom at)
/* get predicate entry for ap/arity; create it if neccessary. */
{
Prop p0;
AtomEntry *ae = RepAtom(at);
READ_LOCK(ae->ARWLock);
p0 = ae->PropsOfAE;
while (p0) {
ModEntry *me = RepModProp(p0);
2014-11-25 12:03:48 +00:00
if (me->KindOfPE == ModProperty) {
READ_UNLOCK(ae->ARWLock);
return me;
}
p0 = me->NextOfPE;
}
READ_UNLOCK(ae->ARWLock);
return NULL;
2015-06-19 01:30:13 +01:00
}
inline static ModEntry *GetModuleEntry(Atom at)
2015-09-21 23:05:36 +01:00
/* Get predicate entry for ap/arity; create it if necessary. */
{
Prop p0;
AtomEntry *ae = RepAtom(at);
2015-09-21 23:05:36 +01:00
ModEntry *new, *oat;
p0 = ae->PropsOfAE;
while (p0) {
2015-04-21 23:09:43 +01:00
if (p0->KindOfPE == ModProperty) {
return RepModProp(p0);
}
2015-04-21 23:09:43 +01:00
p0 = p0->NextOfPE;
}
{
CACHE_REGS
2014-11-25 12:03:48 +00:00
new = (ModEntry *)Yap_AllocAtomSpace(sizeof(*new));
INIT_RWLOCK(new->ModRWLock);
new->KindOfPE = ModProperty;
new->PredForME = NULL;
new->NextME = CurrentModules;
CurrentModules = new;
new->AtomOfME = ae;
AddPropToAtom(ae, (PropEntry *)new);
2015-09-21 23:05:36 +01:00
if (CurrentModule == 0L || (oat = GetModuleEntry(AtomOfTerm(CurrentModule))) == new) {
Yap_setModuleFlags(new, NULL);
} else {
Yap_setModuleFlags(new, oat);
}
}
return new;
}
2015-06-19 01:30:13 +01:00
Term Yap_getUnknownModule(ModEntry *m) {
if (m && m->flags & UNKNOWN_ERROR) {
return TermError;
} else if (m && m->flags & UNKNOWN_WARNING) {
return TermWarning;
} else {
return TermFail;
2014-11-25 12:03:48 +00:00
}
}
2015-06-19 01:30:13 +01:00
bool Yap_CharacterEscapes(Term mt) {
if (mt == PROLOG_MODULE) mt = TermProlog;
return GetModuleEntry(AtomOfTerm(mt))->flags & M_CHARESCAPE;
}
2014-11-25 12:03:48 +00:00
#define ByteAdr(X) ((char *)&(X))
Term Yap_Module_Name(PredEntry *ap) {
CACHE_REGS
Term mod;
if (!ap->ModuleOfPred)
/* If the system predicate is a metacall I should return the
module for the metacall, which I will suppose has to be
reachable from the current module anyway.
So I will return the current module in case the system
predicate is a meta-call. Otherwise it will still work.
*/
2014-11-25 12:03:48 +00:00
mod = CurrentModule;
else {
mod = ap->ModuleOfPred;
}
2014-11-25 12:03:48 +00:00
if (mod)
return mod;
return TermProlog;
2015-09-21 23:05:36 +01:00
}
2014-11-25 12:03:48 +00:00
static ModEntry *LookupModule(Term a) {
Atom at;
ModEntry *me;
/* prolog module */
if (a == 0) {
2014-11-25 12:03:48 +00:00
return GetModuleEntry(AtomProlog);
}
at = AtomOfTerm(a);
me = GetModuleEntry(at);
return me;
}
2014-11-25 12:03:48 +00:00
Term Yap_Module(Term tmod) {
LookupModule(tmod);
return tmod;
}
2014-11-25 12:03:48 +00:00
ModEntry *Yap_GetModuleEntry(Term mod) {
ModEntry *me;
if (!(me = LookupModule(mod)))
return NULL;
return me;
}
2014-11-25 12:03:48 +00:00
Term Yap_GetModuleFromEntry(ModEntry *me) {
return MkAtomTerm(me->AtomOfME);
;
}
2014-11-25 12:03:48 +00:00
struct pred_entry *Yap_ModulePred(Term mod) {
ModEntry *me;
if (!(me = LookupModule(mod)))
return NULL;
return me->PredForME;
}
2014-11-25 12:03:48 +00:00
void Yap_NewModulePred(Term mod, struct pred_entry *ap) {
ModEntry *me;
if (!(me = LookupModule(mod)))
return;
WRITE_LOCK(me->ModRWLock);
ap->NextPredOfModule = me->PredForME;
me->PredForME = ap;
WRITE_UNLOCK(me->ModRWLock);
}
2014-11-25 12:03:48 +00:00
static Int
p_current_module(USES_REGS1) { /* $current_module(Old,New) */
Term t;
if (CurrentModule) {
2014-11-25 12:03:48 +00:00
if (!Yap_unify_constant(ARG1, CurrentModule))
return FALSE;
} else {
if (!Yap_unify_constant(ARG1, TermProlog))
return FALSE;
}
t = Deref(ARG2);
if (IsVarTerm(t) || !IsAtomTerm(t))
return FALSE;
if (t == TermProlog) {
CurrentModule = PROLOG_MODULE;
} else {
2015-09-21 23:05:36 +01:00
// make it very clear that t inherits from cm.
LookupModule(t);
CurrentModule = t;
}
LOCAL_SourceModule = CurrentModule;
return TRUE;
}
2014-11-25 12:03:48 +00:00
static Int p_current_module1(USES_REGS1) { /* $current_module(Old)
*/
if (CurrentModule)
return Yap_unify_constant(ARG1, CurrentModule);
return Yap_unify_constant(ARG1, TermProlog);
}
2014-11-25 12:03:48 +00:00
static Int p_change_module(USES_REGS1) { /* $change_module(New) */
Term mod = Deref(ARG1);
LookupModule(mod);
CurrentModule = mod;
LOCAL_SourceModule = mod;
return TRUE;
}
2014-11-25 12:03:48 +00:00
static Int cont_current_module(USES_REGS1) {
ModEntry *imod = AddressOfTerm(EXTRA_CBACK_ARG(1, 1)), *next;
Term t = MkAtomTerm(imod->AtomOfME);
next = imod->NextME;
/* ARG1 is unbound */
2014-11-25 12:03:48 +00:00
Yap_unify(ARG1, t);
if (!next)
cut_succeed();
2014-11-25 12:03:48 +00:00
EXTRA_CBACK_ARG(1, 1) = MkAddressTerm(next);
return TRUE;
}
2014-11-25 12:03:48 +00:00
static Int init_current_module(
USES_REGS1) { /* current_module(?ModuleName) */
Term t = Deref(ARG1);
if (!IsVarTerm(t)) {
if (!IsAtomTerm(t)) {
2014-11-25 12:03:48 +00:00
Yap_Error(TYPE_ERROR_ATOM, t, "module name must be an atom");
return FALSE;
}
if (FetchModuleEntry(AtomOfTerm(t)) != NULL)
cut_succeed();
cut_fail();
}
2014-11-25 12:03:48 +00:00
EXTRA_CBACK_ARG(1, 1) = MkIntegerTerm((Int)CurrentModules);
return cont_current_module(PASS_REGS1);
}
2014-11-25 12:03:48 +00:00
static Int cont_ground_module(USES_REGS1) {
ModEntry *imod = AddressOfTerm(EXTRA_CBACK_ARG(3, 1)), *next;
Term t2 = MkAtomTerm(imod->AtomOfME);
next = imod->NextME;
/* ARG2 is unbound */
if (!next)
cut_succeed();
EXTRA_CBACK_ARG(3, 1) = MkAddressTerm(next);
return Yap_unify(ARG2, t2);
}
static Int init_ground_module(USES_REGS1) {
/* current_module(?ModuleName) */
Term t1 = Deref(ARG1), tmod = CurrentModule, t3;
if (tmod == PROLOG_MODULE) {
tmod = TermProlog;
}
t3 = Yap_YapStripModule(t1, &tmod);
if (!t3) {
Yap_Error(TYPE_ERROR_CALLABLE, t3, "trying to obtain module");
2014-11-25 12:03:48 +00:00
return FALSE;
}
if (!IsVarTerm(tmod)) {
if (!IsAtomTerm(tmod)) {
Yap_Error(TYPE_ERROR_ATOM, tmod, "module name must be an atom");
cut_fail();
}
if (FetchModuleEntry(AtomOfTerm(tmod)) != NULL && Yap_unify(tmod, ARG2) &&
Yap_unify(t3, ARG3)) {
cut_succeed();
}
cut_fail();
}
if (!Yap_unify(ARG2, tmod) ||
!Yap_unify(ARG3, t3) ) {
cut_fail();
}
// make sure we keep the binding
B->cp_tr = TR;
B->cp_h = HR;
EXTRA_CBACK_ARG(3, 1) = MkAddressTerm(CurrentModules);
return cont_ground_module(PASS_REGS1);
}
static Int p_strip_module(USES_REGS1) {
Term t1 = Deref(ARG1), tmod = CurrentModule;
if (tmod == PROLOG_MODULE) {
tmod = TermProlog;
}
2014-11-25 12:03:48 +00:00
t1 = Yap_StripModule(t1, &tmod);
if (!t1) {
2014-11-25 12:03:48 +00:00
Yap_Error(TYPE_ERROR_CALLABLE, t1, "trying to obtain module");
return FALSE;
}
2014-11-25 12:03:48 +00:00
return Yap_unify(ARG3, t1) && Yap_unify(ARG2, tmod);
}
2015-04-21 23:09:43 +01:00
Term Yap_YapStripModule(Term t, Term *modp) {
2014-07-16 17:56:09 +01:00
CACHE_REGS
Term tmod;
if (modp)
tmod = *modp;
else {
tmod = CurrentModule;
if (tmod == PROLOG_MODULE) {
tmod = TermProlog;
}
}
2014-11-25 12:03:48 +00:00
restart:
2014-07-16 17:56:09 +01:00
if (IsVarTerm(t) || !IsApplTerm(t)) {
if (modp)
*modp = tmod;
return t;
} else {
2014-11-25 12:03:48 +00:00
Functor fun = FunctorOfTerm(t);
2014-07-16 17:56:09 +01:00
if (fun == FunctorModule) {
2014-11-25 12:03:48 +00:00
Term t1 = ArgOfTerm(1, t);
2014-07-16 17:56:09 +01:00
tmod = t1;
2014-11-25 12:03:48 +00:00
if (!IsVarTerm(tmod) && !IsAtomTerm(tmod)) {
return 0L;
2014-07-16 17:56:09 +01:00
}
t = ArgOfTerm(2, t);
goto restart;
}
if (modp)
*modp = tmod;
return t;
}
return 0L;
}
2014-11-25 12:03:48 +00:00
static Int p_yap_strip_module(USES_REGS1) {
2014-07-16 17:56:09 +01:00
Term t1 = Deref(ARG1), tmod = CurrentModule;
if (tmod == PROLOG_MODULE) {
tmod = TermProlog;
}
2014-11-25 12:03:48 +00:00
t1 = Yap_YapStripModule(t1, &tmod);
2014-07-16 17:56:09 +01:00
if (!t1) {
2014-10-20 09:20:56 +01:00
Yap_Error(TYPE_ERROR_CALLABLE, t1, "trying to obtain module");
2014-07-16 17:56:09 +01:00
return FALSE;
}
2014-11-25 12:03:48 +00:00
return Yap_unify(ARG3, t1) && Yap_unify(ARG2, tmod);
2014-07-16 17:56:09 +01:00
}
2014-11-25 12:03:48 +00:00
static Int p_context_module(USES_REGS1) {
yamop *parentcp = P;
CELL *yenv;
PredEntry *ap = EnvPreg(parentcp);
2014-11-25 12:03:48 +00:00
if (ap->ModuleOfPred && !(ap->PredFlags & MetaPredFlag))
return Yap_unify(ARG1, ap->ModuleOfPred);
parentcp = CP;
yenv = ENV;
do {
ap = EnvPreg(parentcp);
2014-11-25 12:03:48 +00:00
if (ap->ModuleOfPred && !(ap->PredFlags & MetaPredFlag))
return Yap_unify(ARG1, ap->ModuleOfPred);
parentcp = (yamop *)yenv[E_CP];
yenv = (CELL *)yenv[E_E];
2014-11-25 12:03:48 +00:00
} while (yenv);
return Yap_unify(ARG1, CurrentModule);
}
2014-11-25 12:03:48 +00:00
Term Yap_StripModule(Term t, Term *modp) {
CACHE_REGS
Term tmod;
if (modp)
tmod = *modp;
else {
tmod = CurrentModule;
if (tmod == PROLOG_MODULE) {
tmod = TermProlog;
}
}
2014-11-25 12:03:48 +00:00
restart:
2014-02-09 18:07:17 +00:00
if (IsVarTerm(t) || !IsApplTerm(t)) {
if (modp)
*modp = tmod;
return t;
2014-02-09 18:07:17 +00:00
} else {
2014-11-25 12:03:48 +00:00
Functor fun = FunctorOfTerm(t);
if (fun == FunctorModule) {
2014-11-25 12:03:48 +00:00
Term t1 = ArgOfTerm(1, t);
if (IsVarTerm(t1)) {
*modp = tmod;
return t;
}
tmod = t1;
2014-11-25 12:03:48 +00:00
if (!IsVarTerm(tmod) && !IsAtomTerm(tmod)) {
return 0L;
}
t = ArgOfTerm(2, t);
goto restart;
}
if (modp)
*modp = tmod;
return t;
}
return 0L;
}
2014-11-25 12:03:48 +00:00
void Yap_InitModulesC(void) {
Yap_InitCPred("$current_module", 2, p_current_module,
SafePredFlag | SyncPredFlag);
Yap_InitCPred("$current_module", 1, p_current_module1,
SafePredFlag | SyncPredFlag);
Yap_InitCPred("$change_module", 1, p_change_module,
SafePredFlag | SyncPredFlag);
Yap_InitCPred("strip_module", 3, p_strip_module, SafePredFlag | SyncPredFlag);
Yap_InitCPred("$yap_strip_module", 3, p_yap_strip_module,
SafePredFlag | SyncPredFlag);
Yap_InitCPred("context_module", 1, p_context_module, 0);
2014-11-25 12:03:48 +00:00
Yap_InitCPredBack("$all_current_modules", 1, 1, init_current_module,
cont_current_module, SafePredFlag | SyncPredFlag);
Yap_InitCPredBack("$ground_module", 3, 1, init_ground_module,
cont_ground_module, SafePredFlag | SyncPredFlag);
}
2014-11-25 12:03:48 +00:00
void Yap_InitModules(void) {
CACHE_REGS
LookupModule(MkAtomTerm(AtomProlog));
2014-11-25 12:03:48 +00:00
LOCAL_SourceModule = MkAtomTerm(AtomProlog);
LookupModule(USER_MODULE);
LookupModule(IDB_MODULE);
LookupModule(ATTRIBUTES_MODULE);
LookupModule(CHARSIO_MODULE);
LookupModule(TERMS_MODULE);
LookupModule(SYSTEM_MODULE);
LookupModule(READUTIL_MODULE);
LookupModule(HACKS_MODULE);
LookupModule(ARG_MODULE);
LookupModule(GLOBALS_MODULE);
2011-04-30 01:16:40 +01:00
LookupModule(DBLOAD_MODULE);
2012-10-23 14:55:44 +01:00
LookupModule(RANGE_MODULE);
CurrentModule = PROLOG_MODULE;
}