2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
2016-07-31 10:30:46 +01:00
|
|
|
*************************************************************** f***********
|
2001-04-09 20:54:03 +01:00
|
|
|
* *
|
|
|
|
File: modules.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: module support *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
2016-01-31 10:17:47 +00:00
|
|
|
#ifdef SCCSLookupSystemModule
|
2014-11-25 12:03:48 +00:00
|
|
|
static char SccsId[] = "%W% %G%";
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2016-03-29 01:55:12 +01:00
|
|
|
#include "Yatom.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2015-11-11 16:46:30 +00:00
|
|
|
static Int current_module(USES_REGS1);
|
|
|
|
static Int current_module1(USES_REGS1);
|
2013-11-13 10:38:20 +00:00
|
|
|
static ModEntry *LookupModule(Term a);
|
2015-12-15 09:28:43 +00:00
|
|
|
static ModEntry *LookupSystemModule(Term a);
|
2016-02-22 13:01:09 +00:00
|
|
|
static ModEntry *GetModuleEntry(Atom at USES_REGS);
|
2016-01-31 10:17:47 +00:00
|
|
|
static ModEntry *FetchModuleEntry(Atom at);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2016-03-03 23:16:40 +00:00
|
|
|
/**
|
2016-01-31 10:17:47 +00:00
|
|
|
* initialize module data-structure
|
2016-03-03 23:16:40 +00:00
|
|
|
*
|
2016-01-31 10:17:47 +00:00
|
|
|
* @param to parent module (CurrentModule)
|
|
|
|
* @param ae module name.
|
2016-03-03 23:16:40 +00:00
|
|
|
*
|
2016-01-31 10:17:47 +00:00
|
|
|
* @return a new module structure
|
2016-03-29 01:55:12 +01:00
|
|
|
*/ /** */
|
|
|
|
static ModEntry *initMod(AtomEntry *toname, AtomEntry *ae) {
|
|
|
|
CACHE_REGS
|
|
|
|
ModEntry *n, *parent;
|
|
|
|
|
|
|
|
if (toname == NULL)
|
|
|
|
parent = NULL;
|
|
|
|
else {
|
|
|
|
parent = FetchModuleEntry(toname);
|
|
|
|
}
|
|
|
|
n = (ModEntry *)Yap_AllocAtomSpace(sizeof(*n));
|
|
|
|
INIT_RWLOCK(n->ModRWLock);
|
|
|
|
n->KindOfPE = ModProperty;
|
|
|
|
n->PredForME = NULL;
|
|
|
|
n->NextME = CurrentModules;
|
|
|
|
CurrentModules = n;
|
|
|
|
n->AtomOfME = ae;
|
|
|
|
n->OwnerFile = Yap_ConsultingFile(PASS_REGS1);
|
|
|
|
AddPropToAtom(ae, (PropEntry *)n);
|
|
|
|
Yap_setModuleFlags(n, parent);
|
|
|
|
return n;
|
2016-01-31 10:17:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-03 23:16:40 +00:00
|
|
|
/**
|
2016-01-31 10:17:47 +00:00
|
|
|
* get predicate entry for ap/arity; create it if neccessary
|
2016-03-03 23:16:40 +00:00
|
|
|
*
|
|
|
|
* @param[in] at
|
|
|
|
*
|
2016-01-31 10:17:47 +00:00
|
|
|
* @return module descriptorxs
|
|
|
|
*/
|
2016-03-29 01:55:12 +01:00
|
|
|
static ModEntry *GetModuleEntry(Atom at USES_REGS) {
|
2007-04-10 23:13:21 +01:00
|
|
|
Prop p0;
|
2016-03-04 00:09:33 +00:00
|
|
|
AtomEntry *ae = RepAtom(at);
|
2016-03-03 23:16:40 +00:00
|
|
|
|
2007-04-19 00:01:16 +01:00
|
|
|
READ_LOCK(ae->ARWLock);
|
2007-04-10 23:13:21 +01:00
|
|
|
p0 = ae->PropsOfAE;
|
|
|
|
while (p0) {
|
|
|
|
ModEntry *me = RepModProp(p0);
|
2014-11-25 12:03:48 +00:00
|
|
|
if (me->KindOfPE == ModProperty) {
|
2007-04-19 00:01:16 +01:00
|
|
|
READ_UNLOCK(ae->ARWLock);
|
2007-04-10 23:13:21 +01:00
|
|
|
return me;
|
|
|
|
}
|
|
|
|
p0 = me->NextOfPE;
|
|
|
|
}
|
2007-04-19 00:01:16 +01:00
|
|
|
READ_UNLOCK(ae->ARWLock);
|
2016-03-03 23:16:40 +00:00
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
return initMod(
|
|
|
|
(CurrentModule == PROLOG_MODULE ? NULL : AtomOfTerm(CurrentModule)), at);
|
2015-06-19 01:30:13 +01:00
|
|
|
}
|
|
|
|
|
2016-01-31 10:17:47 +00:00
|
|
|
/** get entry for ap/arity; assumes one is there. */
|
2016-03-29 01:55:12 +01:00
|
|
|
static ModEntry *FetchModuleEntry(Atom at) {
|
2007-04-10 23:13:21 +01:00
|
|
|
Prop p0;
|
|
|
|
AtomEntry *ae = RepAtom(at);
|
|
|
|
|
2016-01-31 10:17:47 +00:00
|
|
|
READ_LOCK(ae->ARWLock);
|
2007-04-10 23:13:21 +01:00
|
|
|
p0 = ae->PropsOfAE;
|
|
|
|
while (p0) {
|
2016-01-31 10:17:47 +00:00
|
|
|
ModEntry *me = RepModProp(p0);
|
|
|
|
if (me->KindOfPE == ModProperty) {
|
|
|
|
READ_UNLOCK(ae->ARWLock);
|
|
|
|
return me;
|
2015-09-21 23:05:36 +01:00
|
|
|
}
|
2016-01-31 10:17:47 +00:00
|
|
|
p0 = me->NextOfPE;
|
2013-11-13 10:38:20 +00:00
|
|
|
}
|
2016-01-31 10:17:47 +00:00
|
|
|
READ_UNLOCK(ae->ARWLock);
|
|
|
|
return NULL;
|
2007-04-10 23:13:21 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2016-01-03 02:06:09 +00:00
|
|
|
} else if (m && m->flags & UNKNOWN_FAST_FAIL) {
|
|
|
|
return TermFastFail;
|
2015-06-19 01:30:13 +01:00
|
|
|
} else {
|
|
|
|
return TermFail;
|
2014-11-25 12:03:48 +00:00
|
|
|
}
|
2014-10-10 10:00:27 +01:00
|
|
|
}
|
2007-04-10 23:13:21 +01:00
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
bool Yap_getUnknown(Term mod) {
|
|
|
|
ModEntry *m = LookupModule(mod);
|
|
|
|
return Yap_getUnknownModule(m);
|
2015-12-15 09:28:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
bool Yap_CharacterEscapes(Term mt) {
|
|
|
|
CACHE_REGS
|
|
|
|
if (mt == PROLOG_MODULE)
|
|
|
|
mt = TermProlog;
|
2016-02-22 13:01:09 +00:00
|
|
|
return GetModuleEntry(AtomOfTerm(mt) PASS_REGS)->flags & M_CHARESCAPE;
|
2015-06-19 01:30:13 +01:00
|
|
|
}
|
|
|
|
|
2014-11-25 12:03:48 +00:00
|
|
|
#define ByteAdr(X) ((char *)&(X))
|
|
|
|
Term Yap_Module_Name(PredEntry *ap) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2016-02-14 04:12:31 +00:00
|
|
|
|
2016-01-31 10:17:47 +00:00
|
|
|
if (!ap)
|
|
|
|
return TermUser;
|
2001-04-09 20:54:03 +01:00
|
|
|
if (!ap->ModuleOfPred)
|
2016-01-31 10:17:47 +00:00
|
|
|
/* If the system predicate is a meta-call I should return the
|
2001-04-09 20:54:03 +01:00
|
|
|
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.
|
|
|
|
*/
|
2016-01-31 10:17:47 +00:00
|
|
|
return TermProlog;
|
2001-10-30 16:42:05 +00:00
|
|
|
else {
|
2016-01-31 10:17:47 +00:00
|
|
|
return ap->ModuleOfPred;
|
2001-10-30 16:42:05 +00:00
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
static ModEntry *LookupSystemModule(Term a) {
|
2016-01-03 02:06:09 +00:00
|
|
|
CACHE_REGS
|
2015-12-15 09:28:43 +00:00
|
|
|
Atom at;
|
|
|
|
ModEntry *me;
|
2016-03-03 23:16:40 +00:00
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
/* prolog module */
|
|
|
|
if (a == 0) {
|
2016-01-31 10:17:47 +00:00
|
|
|
a = TermProlog;
|
2015-12-15 09:28:43 +00:00
|
|
|
}
|
|
|
|
at = AtomOfTerm(a);
|
2016-02-22 13:01:09 +00:00
|
|
|
me = GetModuleEntry(at PASS_REGS);
|
2016-01-31 10:17:47 +00:00
|
|
|
if (!me)
|
|
|
|
return NULL;
|
2015-12-15 09:28:43 +00:00
|
|
|
me->flags |= M_SYSTEM;
|
2016-03-29 01:55:12 +01:00
|
|
|
me->OwnerFile = Yap_ConsultingFile(PASS_REGS1);
|
2016-01-31 10:17:47 +00:00
|
|
|
return me;
|
|
|
|
}
|
2015-12-15 09:28:43 +00:00
|
|
|
|
2014-11-25 12:03:48 +00:00
|
|
|
static ModEntry *LookupModule(Term a) {
|
2016-02-22 13:01:09 +00:00
|
|
|
CACHE_REGS
|
2007-04-10 23:13:21 +01:00
|
|
|
Atom at;
|
2007-04-19 00:01:16 +01:00
|
|
|
ModEntry *me;
|
2003-08-27 14:37:10 +01:00
|
|
|
|
2004-02-13 23:53:20 +00:00
|
|
|
/* prolog module */
|
2014-04-23 21:39:32 +01:00
|
|
|
if (a == 0) {
|
2016-02-22 13:01:09 +00:00
|
|
|
return GetModuleEntry(AtomProlog PASS_REGS);
|
2014-04-23 21:39:32 +01:00
|
|
|
}
|
2007-04-10 23:13:21 +01:00
|
|
|
at = AtomOfTerm(a);
|
2016-02-22 13:01:09 +00:00
|
|
|
me = GetModuleEntry(at PASS_REGS);
|
2007-04-19 00:01:16 +01:00
|
|
|
return me;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
bool Yap_isSystemModule(Term a) {
|
|
|
|
ModEntry *me = LookupModule(a);
|
2016-03-29 01:55:12 +01:00
|
|
|
return me != NULL && me->flags & M_SYSTEM;
|
2015-12-15 09:28:43 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 12:03:48 +00:00
|
|
|
Term Yap_Module(Term tmod) {
|
2007-04-10 23:13:21 +01:00
|
|
|
LookupModule(tmod);
|
|
|
|
return tmod;
|
2004-08-11 17:14:55 +01:00
|
|
|
}
|
|
|
|
|
2014-11-25 12:03:48 +00:00
|
|
|
ModEntry *Yap_GetModuleEntry(Term mod) {
|
2013-11-13 10:38:20 +00:00
|
|
|
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);
|
|
|
|
;
|
2013-11-13 10:38:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 12:03:48 +00:00
|
|
|
struct pred_entry *Yap_ModulePred(Term mod) {
|
2007-04-10 23:13:21 +01:00
|
|
|
ModEntry *me;
|
|
|
|
if (!(me = LookupModule(mod)))
|
|
|
|
return NULL;
|
|
|
|
return me->PredForME;
|
2004-02-12 12:37:12 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 12:03:48 +00:00
|
|
|
void Yap_NewModulePred(Term mod, struct pred_entry *ap) {
|
2007-04-10 23:13:21 +01:00
|
|
|
ModEntry *me;
|
|
|
|
|
|
|
|
if (!(me = LookupModule(mod)))
|
|
|
|
return;
|
2007-04-19 00:01:16 +01:00
|
|
|
WRITE_LOCK(me->ModRWLock);
|
2007-04-10 23:13:21 +01:00
|
|
|
ap->NextPredOfModule = me->PredForME;
|
|
|
|
me->PredForME = ap;
|
2007-04-19 00:01:16 +01:00
|
|
|
WRITE_UNLOCK(me->ModRWLock);
|
2002-11-11 17:38:10 +00:00
|
|
|
}
|
2002-11-12 00:33:48 +00:00
|
|
|
|
2014-11-25 12:03:48 +00:00
|
|
|
static Int
|
2016-01-31 10:17:47 +00:00
|
|
|
current_module(USES_REGS1) { /* $current_module(Old,N) */
|
2014-11-25 12:03:48 +00:00
|
|
|
Term t;
|
|
|
|
|
2004-02-12 12:37:12 +00:00
|
|
|
if (CurrentModule) {
|
2014-11-25 12:03:48 +00:00
|
|
|
if (!Yap_unify_constant(ARG1, CurrentModule))
|
2004-02-12 12:37:12 +00:00
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
if (!Yap_unify_constant(ARG1, TermProlog))
|
|
|
|
return FALSE;
|
|
|
|
}
|
2001-04-09 20:54:03 +01:00
|
|
|
t = Deref(ARG2);
|
|
|
|
if (IsVarTerm(t) || !IsAtomTerm(t))
|
2004-02-12 12:37:12 +00:00
|
|
|
return FALSE;
|
|
|
|
if (t == TermProlog) {
|
2004-05-13 21:54:58 +01:00
|
|
|
CurrentModule = PROLOG_MODULE;
|
2004-02-12 12:37:12 +00:00
|
|
|
} else {
|
2015-09-21 23:05:36 +01:00
|
|
|
// make it very clear that t inherits from cm.
|
|
|
|
LookupModule(t);
|
2004-02-12 12:37:12 +00:00
|
|
|
CurrentModule = t;
|
|
|
|
}
|
2013-11-13 10:38:20 +00:00
|
|
|
LOCAL_SourceModule = CurrentModule;
|
2007-04-19 00:01:16 +01:00
|
|
|
return TRUE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2016-01-31 10:17:47 +00:00
|
|
|
static Int change_module(USES_REGS1) { /* $change_module(N) */
|
2004-02-12 12:37:12 +00:00
|
|
|
Term mod = Deref(ARG1);
|
|
|
|
LookupModule(mod);
|
2001-11-15 00:01:43 +00:00
|
|
|
CurrentModule = mod;
|
2016-02-28 19:32:55 +00:00
|
|
|
LOCAL_SourceModule = mod;
|
2004-02-12 12:37:12 +00:00
|
|
|
return TRUE;
|
2001-10-30 16:42:05 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 09:28:43 +00:00
|
|
|
static Int 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 cont_current_module(USES_REGS1) {
|
|
|
|
ModEntry *imod = AddressOfTerm(EXTRA_CBACK_ARG(1, 1)), *next;
|
2007-04-10 23:13:21 +01:00
|
|
|
Term t = MkAtomTerm(imod->AtomOfME);
|
|
|
|
next = imod->NextME;
|
|
|
|
|
|
|
|
/* ARG1 is unbound */
|
2014-11-25 12:03:48 +00:00
|
|
|
Yap_unify(ARG1, t);
|
2007-04-10 23:13:21 +01:00
|
|
|
if (!next)
|
|
|
|
cut_succeed();
|
2014-11-25 12:03:48 +00:00
|
|
|
EXTRA_CBACK_ARG(1, 1) = MkAddressTerm(next);
|
2007-04-10 23:13:21 +01:00
|
|
|
return TRUE;
|
2002-09-17 01:14:23 +01:00
|
|
|
}
|
|
|
|
|
2014-11-25 12:03:48 +00:00
|
|
|
static Int init_current_module(
|
|
|
|
USES_REGS1) { /* current_module(?ModuleName) */
|
2007-04-10 23:13:21 +01:00
|
|
|
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");
|
2007-04-10 23:13:21 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2007-11-08 15:52:15 +00:00
|
|
|
if (FetchModuleEntry(AtomOfTerm(t)) != NULL)
|
|
|
|
cut_succeed();
|
|
|
|
cut_fail();
|
2007-04-10 23:13:21 +01:00
|
|
|
}
|
2014-11-25 12:03:48 +00:00
|
|
|
EXTRA_CBACK_ARG(1, 1) = MkIntegerTerm((Int)CurrentModules);
|
|
|
|
return cont_current_module(PASS_REGS1);
|
2002-09-17 01:14:23 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-03-29 01:55:12 +01:00
|
|
|
/* current_module(?ModuleName) */
|
2014-11-25 12:03:48 +00:00
|
|
|
Term t1 = Deref(ARG1), tmod = CurrentModule, t3;
|
|
|
|
if (tmod == PROLOG_MODULE) {
|
|
|
|
tmod = TermProlog;
|
|
|
|
}
|
|
|
|
t3 = Yap_YapStripModule(t1, &tmod);
|
|
|
|
if (!t3) {
|
2015-03-04 09:35:47 +00:00
|
|
|
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();
|
|
|
|
}
|
2016-03-29 01:55:12 +01:00
|
|
|
if (!Yap_unify(ARG2, tmod) || !Yap_unify(ARG3, t3)) {
|
|
|
|
cut_fail();
|
|
|
|
}
|
|
|
|
// make sure we keep the binding
|
2014-11-25 12:03:48 +00:00
|
|
|
B->cp_tr = TR;
|
|
|
|
B->cp_h = HR;
|
|
|
|
EXTRA_CBACK_ARG(3, 1) = MkAddressTerm(CurrentModules);
|
|
|
|
return cont_ground_module(PASS_REGS1);
|
|
|
|
}
|
|
|
|
|
2016-03-03 23:16:40 +00:00
|
|
|
/**
|
2016-01-03 02:06:09 +00:00
|
|
|
* @pred system_module( + _Mod_)
|
2016-03-03 23:16:40 +00:00
|
|
|
*
|
|
|
|
* @param module
|
|
|
|
*
|
|
|
|
* @return
|
2015-12-15 09:28:43 +00:00
|
|
|
*/
|
2016-03-29 01:55:12 +01:00
|
|
|
static Int is_system_module(USES_REGS1) {
|
2015-12-15 09:28:43 +00:00
|
|
|
Term t;
|
2016-03-29 01:55:12 +01:00
|
|
|
if (IsVarTerm(t = Deref(ARG1))) {
|
2015-12-15 09:28:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM, t, "load_files/2");
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-29 01:55:12 +01:00
|
|
|
return Yap_isSystemModule(t);
|
2015-12-15 09:28:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 01:55:12 +01:00
|
|
|
static Int new_system_module(USES_REGS1) {
|
2015-12-15 09:28:43 +00:00
|
|
|
ModEntry *me;
|
|
|
|
Term t;
|
2016-03-29 01:55:12 +01:00
|
|
|
if (IsVarTerm(t = Deref(ARG1))) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, NULL);
|
2015-12-15 09:28:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM, t, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-29 01:55:12 +01:00
|
|
|
if ((me = LookupSystemModule(t)))
|
|
|
|
me->OwnerFile = Yap_ConsultingFile(PASS_REGS1);
|
2015-12-15 09:28:43 +00:00
|
|
|
return me != NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-11 16:46:30 +00:00
|
|
|
static Int strip_module(USES_REGS1) {
|
2014-02-09 10:46:43 +00:00
|
|
|
Term t1 = Deref(ARG1), tmod = CurrentModule;
|
2008-02-15 12:41:33 +00:00
|
|
|
if (tmod == PROLOG_MODULE) {
|
|
|
|
tmod = TermProlog;
|
|
|
|
}
|
2014-11-25 12:03:48 +00:00
|
|
|
t1 = Yap_StripModule(t1, &tmod);
|
2014-02-09 10:46:43 +00:00
|
|
|
if (!t1) {
|
2014-11-25 12:03:48 +00:00
|
|
|
Yap_Error(TYPE_ERROR_CALLABLE, t1, "trying to obtain module");
|
2014-02-09 10:46:43 +00:00
|
|
|
return FALSE;
|
2008-02-12 17:03:59 +00:00
|
|
|
}
|
2014-11-25 12:03:48 +00:00
|
|
|
return Yap_unify(ARG3, t1) && Yap_unify(ARG2, tmod);
|
2008-02-12 17:03:59 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2016-07-31 10:30:46 +01:00
|
|
|
if (modp) {
|
2014-07-16 17:56:09 +01:00
|
|
|
tmod = *modp;
|
2016-07-31 10:30:46 +01:00
|
|
|
if (tmod == PROLOG_MODULE) {
|
|
|
|
*modp = tmod = TermProlog;
|
|
|
|
}
|
|
|
|
} else {
|
2014-07-16 17:56:09 +01:00
|
|
|
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)) {
|
2016-06-02 10:52:06 +01:00
|
|
|
if (modp)
|
|
|
|
*modp = tmod;
|
|
|
|
return t;
|
2014-07-16 17:56:09 +01:00
|
|
|
}
|
|
|
|
t = ArgOfTerm(2, t);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
if (modp)
|
|
|
|
*modp = tmod;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
return 0L;
|
|
|
|
}
|
|
|
|
|
2015-11-11 16:46:30 +00:00
|
|
|
static Int 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);
|
2016-06-02 10:52:06 +01:00
|
|
|
if (!t1 || (!IsVarTerm(tmod) && !IsAtomTerm(tmod))) {
|
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
|
|
|
}
|
|
|
|
|
2015-11-11 16:46:30 +00:00
|
|
|
static Int context_module(USES_REGS1) {
|
2008-03-13 14:38:02 +00:00
|
|
|
yamop *parentcp = P;
|
|
|
|
CELL *yenv;
|
|
|
|
PredEntry *ap = EnvPreg(parentcp);
|
2014-11-25 12:03:48 +00:00
|
|
|
if (ap->ModuleOfPred && !(ap->PredFlags & MetaPredFlag))
|
2008-03-13 14:38:02 +00:00
|
|
|
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))
|
2008-03-13 14:38:02 +00:00
|
|
|
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);
|
2008-03-13 14:38:02 +00:00
|
|
|
return Yap_unify(ARG1, CurrentModule);
|
|
|
|
}
|
|
|
|
|
2016-03-03 23:16:40 +00:00
|
|
|
/**
|
2015-12-15 09:28:43 +00:00
|
|
|
* @pred source_module(-Mod)
|
2016-03-03 23:16:40 +00:00
|
|
|
*
|
|
|
|
* @param Mod is the current text source module.
|
|
|
|
*
|
2015-12-15 09:28:43 +00:00
|
|
|
* : _Mod_ is the current read-in or source module.
|
|
|
|
*/
|
|
|
|
static Int source_module(USES_REGS1) {
|
|
|
|
if (LOCAL_SourceModule == PROLOG_MODULE) {
|
|
|
|
return Yap_unify(ARG1, TermProlog);
|
|
|
|
}
|
|
|
|
return Yap_unify(ARG1, LOCAL_SourceModule);
|
|
|
|
}
|
|
|
|
|
2016-04-28 15:01:16 +01:00
|
|
|
/**
|
2016-07-31 10:30:46 +01:00
|
|
|
* @pred source_module(-Mod)
|
|
|
|
*
|
|
|
|
* @param Mod is the current text source module.
|
|
|
|
*
|
|
|
|
* : _Mod_ is the current read-in or source module.
|
|
|
|
*/
|
|
|
|
static Int current_source_module(USES_REGS1) {
|
|
|
|
Term t;
|
|
|
|
if (LOCAL_SourceModule == PROLOG_MODULE) {
|
|
|
|
LOCAL_SourceModule = TermProlog;
|
|
|
|
}
|
|
|
|
if (!Yap_unify(ARG1, LOCAL_SourceModule)) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if (IsVarTerm(t = Deref(ARG2))) {
|
|
|
|
Yap_Error(INSTANTIATION_ERROR, t, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!IsAtomTerm(t)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM, t, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOCAL_SourceModule = t;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @pred $copy_operators(+Mode, +ModTarget)
|
2016-04-28 15:01:16 +01:00
|
|
|
*
|
|
|
|
* Copy all operators in ModSource to ModTarget
|
|
|
|
*
|
|
|
|
* : _Mod_ is the current read-in or source module.
|
|
|
|
*/
|
|
|
|
static Int copy_operators(USES_REGS1) {
|
2016-07-31 10:30:46 +01:00
|
|
|
ModEntry *me = LookupModule(Deref(ARG1));
|
|
|
|
if (!me)
|
2016-04-28 15:01:16 +01:00
|
|
|
return true;
|
2016-07-31 10:30:46 +01:00
|
|
|
ModEntry *she = LookupModule(Deref(ARG2));
|
|
|
|
if (!she)
|
2016-04-28 15:01:16 +01:00
|
|
|
return true;
|
|
|
|
OpEntry *op = me->OpForME;
|
|
|
|
while (op) {
|
|
|
|
if (!Yap_dup_op(op, she)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
op = op->NextForME;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-25 12:03:48 +00:00
|
|
|
Term Yap_StripModule(Term t, Term *modp) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2016-03-29 01:55:12 +01:00
|
|
|
Term tmod;
|
2008-08-07 21:51:23 +01:00
|
|
|
|
2013-11-25 10:25:56 +00:00
|
|
|
if (modp)
|
|
|
|
tmod = *modp;
|
2014-02-09 10:46:43 +00:00
|
|
|
else {
|
2013-11-25 10:25:56 +00:00
|
|
|
tmod = CurrentModule;
|
2014-02-09 10:46:43 +00:00
|
|
|
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)) {
|
2014-02-09 10:46:43 +00:00
|
|
|
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);
|
2008-08-07 21:51:23 +01:00
|
|
|
if (fun == FunctorModule) {
|
2014-11-25 12:03:48 +00:00
|
|
|
Term t1 = ArgOfTerm(1, t);
|
|
|
|
if (IsVarTerm(t1)) {
|
|
|
|
*modp = tmod;
|
|
|
|
return t;
|
2008-08-07 21:51:23 +01:00
|
|
|
}
|
2014-02-09 10:46:43 +00:00
|
|
|
tmod = t1;
|
2014-11-25 12:03:48 +00:00
|
|
|
if (!IsVarTerm(tmod) && !IsAtomTerm(tmod)) {
|
|
|
|
return 0L;
|
2008-08-07 21:51:23 +01:00
|
|
|
}
|
|
|
|
t = ArgOfTerm(2, t);
|
|
|
|
goto restart;
|
|
|
|
}
|
2013-11-25 10:25:56 +00:00
|
|
|
if (modp)
|
|
|
|
*modp = tmod;
|
2008-08-07 21:51:23 +01:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
return 0L;
|
|
|
|
}
|
|
|
|
|
2014-11-25 12:03:48 +00:00
|
|
|
void Yap_InitModulesC(void) {
|
2015-11-11 16:46:30 +00:00
|
|
|
Yap_InitCPred("$current_module", 2, current_module,
|
2014-11-25 12:03:48 +00:00
|
|
|
SafePredFlag | SyncPredFlag);
|
2015-11-11 16:46:30 +00:00
|
|
|
Yap_InitCPred("$current_module", 1, current_module1,
|
2014-11-25 12:03:48 +00:00
|
|
|
SafePredFlag | SyncPredFlag);
|
2015-11-11 16:46:30 +00:00
|
|
|
Yap_InitCPred("$change_module", 1, change_module,
|
2014-11-25 12:03:48 +00:00
|
|
|
SafePredFlag | SyncPredFlag);
|
2015-11-11 16:46:30 +00:00
|
|
|
Yap_InitCPred("strip_module", 3, strip_module, SafePredFlag | SyncPredFlag);
|
2015-12-15 09:28:43 +00:00
|
|
|
Yap_InitCPred("source_module", 1, source_module, SafePredFlag | SyncPredFlag);
|
2016-07-31 10:30:46 +01:00
|
|
|
Yap_InitCPred("current_source_module", 2, current_source_module,
|
|
|
|
SafePredFlag | SyncPredFlag);
|
2015-11-11 16:46:30 +00:00
|
|
|
Yap_InitCPred("$yap_strip_module", 3, yap_strip_module,
|
2014-11-25 12:03:48 +00:00
|
|
|
SafePredFlag | SyncPredFlag);
|
2015-11-11 16:46:30 +00:00
|
|
|
Yap_InitCPred("context_module", 1, context_module, 0);
|
2016-07-31 10:30:46 +01:00
|
|
|
Yap_InitCPred("$is_system_module", 1, is_system_module, SafePredFlag);
|
|
|
|
Yap_InitCPred("$copy_operators", 2, copy_operators, 0);
|
2016-01-03 02:06:09 +00:00
|
|
|
Yap_InitCPred("new_system_module", 1, new_system_module, SafePredFlag);
|
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);
|
2004-02-06 02:26:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 12:03:48 +00:00
|
|
|
void Yap_InitModules(void) {
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2015-12-15 09:28:43 +00:00
|
|
|
LookupSystemModule(MkAtomTerm(AtomProlog));
|
2014-11-25 12:03:48 +00:00
|
|
|
LOCAL_SourceModule = MkAtomTerm(AtomProlog);
|
2007-04-10 23:13:21 +01:00
|
|
|
LookupModule(USER_MODULE);
|
|
|
|
LookupModule(IDB_MODULE);
|
|
|
|
LookupModule(ATTRIBUTES_MODULE);
|
2015-12-15 09:28:43 +00:00
|
|
|
LookupSystemModule(CHARSIO_MODULE);
|
|
|
|
LookupSystemModule(TERMS_MODULE);
|
|
|
|
LookupSystemModule(SYSTEM_MODULE);
|
|
|
|
LookupSystemModule(READUTIL_MODULE);
|
|
|
|
LookupSystemModule(HACKS_MODULE);
|
2008-02-12 17:03:59 +00:00
|
|
|
LookupModule(ARG_MODULE);
|
2015-12-15 09:28:43 +00:00
|
|
|
LookupSystemModule(GLOBALS_MODULE);
|
|
|
|
LookupSystemModule(DBLOAD_MODULE);
|
|
|
|
LookupSystemModule(RANGE_MODULE);
|
2004-02-12 12:37:12 +00:00
|
|
|
CurrentModule = PROLOG_MODULE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|