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 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
File: modules.c *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: module support *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2013-11-13 10:38:20 +00:00
|
|
|
#include "pl-shared.h"
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
static Int p_current_module( USES_REGS1 );
|
|
|
|
static Int p_current_module1( USES_REGS1 );
|
2013-11-13 10:38:20 +00:00
|
|
|
static ModEntry *LookupModule(Term a);
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2013-11-13 10:38:20 +00:00
|
|
|
unsigned int
|
|
|
|
getUnknownModule(ModEntry * m) {
|
|
|
|
return m->flags & UNKNOWN_MASK;
|
|
|
|
}
|
2007-04-10 23:13:21 +01:00
|
|
|
|
|
|
|
inline static ModEntry *
|
|
|
|
FetchModuleEntry(Atom at)
|
|
|
|
/* get predicate entry for ap/arity; create it if neccessary. */
|
|
|
|
{
|
|
|
|
Prop p0;
|
|
|
|
AtomEntry *ae = RepAtom(at);
|
|
|
|
|
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);
|
|
|
|
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);
|
2007-04-10 23:13:21 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static ModEntry *
|
|
|
|
GetModuleEntry(Atom at)
|
|
|
|
/* get predicate entry for ap/arity; create it if neccessary. */
|
|
|
|
{
|
|
|
|
Prop p0;
|
|
|
|
AtomEntry *ae = RepAtom(at);
|
|
|
|
ModEntry *new;
|
|
|
|
|
2013-11-13 10:38:20 +00:00
|
|
|
|
2007-04-10 23:13:21 +01:00
|
|
|
p0 = ae->PropsOfAE;
|
|
|
|
while (p0) {
|
|
|
|
ModEntry *me = RepModProp(p0);
|
|
|
|
if ( me->KindOfPE == ModProperty
|
|
|
|
) {
|
|
|
|
return me;
|
|
|
|
}
|
|
|
|
p0 = me->NextOfPE;
|
|
|
|
}
|
2013-11-13 10:38:20 +00:00
|
|
|
{
|
|
|
|
CACHE_REGS
|
|
|
|
new = (ModEntry *) Yap_AllocAtomSpace(sizeof(*new));
|
|
|
|
INIT_RWLOCK(new->ModRWLock);
|
|
|
|
new->KindOfPE = ModProperty;
|
|
|
|
new->PredForME = NULL;
|
|
|
|
new->NextME = CurrentModules;
|
|
|
|
CurrentModules = new;
|
|
|
|
new->AtomOfME = ae;
|
|
|
|
if (at == AtomProlog)
|
|
|
|
new->flags = UNKNOWN_FAIL|M_SYSTEM|M_CHARESCAPE;
|
|
|
|
else
|
|
|
|
new->flags = LookupModule(LOCAL_SourceModule)->flags;
|
|
|
|
AddPropToAtom(ae, (PropEntry *)new);
|
|
|
|
}
|
2007-04-10 23:13:21 +01:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#define ByteAdr(X) ((char *) &(X))
|
|
|
|
Term
|
2004-02-12 12:37:12 +00:00
|
|
|
Yap_Module_Name(PredEntry *ap)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2004-02-12 12:37:12 +00:00
|
|
|
Term mod;
|
2001-04-09 20:54:03 +01:00
|
|
|
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.
|
|
|
|
*/
|
2004-02-12 12:37:12 +00:00
|
|
|
mod = CurrentModule;
|
2001-10-30 16:42:05 +00:00
|
|
|
else {
|
2004-02-12 12:37:12 +00:00
|
|
|
mod = ap->ModuleOfPred;
|
2001-10-30 16:42:05 +00:00
|
|
|
}
|
2004-02-12 12:37:12 +00:00
|
|
|
if (mod) return mod;
|
|
|
|
return TermProlog;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2007-04-10 23:13:21 +01:00
|
|
|
static ModEntry *
|
2013-11-13 10:38:20 +00:00
|
|
|
LookupModule(Term a )
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
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 */
|
|
|
|
if (a == 0)
|
2013-11-13 10:38:20 +00:00
|
|
|
return GetModuleEntry(AtomProlog);
|
2007-04-10 23:13:21 +01:00
|
|
|
at = AtomOfTerm(a);
|
2007-04-19 00:01:16 +01:00
|
|
|
me = GetModuleEntry(at);
|
|
|
|
return me;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2004-08-11 17:14:55 +01: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
|
|
|
}
|
|
|
|
|
2013-11-13 10:38:20 +00:00
|
|
|
ModEntry *
|
|
|
|
Yap_GetModuleEntry(Term mod)
|
|
|
|
{
|
|
|
|
ModEntry *me;
|
|
|
|
if (!(me = LookupModule(mod)))
|
|
|
|
return NULL;
|
|
|
|
return me;
|
|
|
|
}
|
|
|
|
|
|
|
|
Term
|
|
|
|
Yap_GetModuleFromEntry(ModEntry *me)
|
|
|
|
{
|
|
|
|
return MkAtomTerm(me->AtomOfME);;
|
|
|
|
}
|
|
|
|
|
2004-02-12 12:37:12 +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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Yap_NewModulePred(Term mod, struct pred_entry *ap)
|
2002-11-11 17:38:10 +00:00
|
|
|
{
|
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
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_current_module( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{ /* $current_module(Old,New) */
|
|
|
|
Term t;
|
|
|
|
|
2004-02-12 12:37:12 +00:00
|
|
|
if (CurrentModule) {
|
|
|
|
if(!Yap_unify_constant(ARG1, CurrentModule))
|
|
|
|
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 {
|
|
|
|
CurrentModule = t;
|
|
|
|
LookupModule(CurrentModule);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_current_module1( USES_REGS1 )
|
2001-04-09 20:54:03 +01:00
|
|
|
{ /* $current_module(Old) */
|
2004-02-12 12:37:12 +00:00
|
|
|
if (CurrentModule)
|
|
|
|
return Yap_unify_constant(ARG1, CurrentModule);
|
|
|
|
return Yap_unify_constant(ARG1, TermProlog);
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|
|
|
|
|
2001-06-06 20:10:51 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_change_module( USES_REGS1 )
|
2001-06-06 20:10:51 +01:00
|
|
|
{ /* $change_module(New) */
|
2004-02-12 12:37:12 +00:00
|
|
|
Term mod = Deref(ARG1);
|
|
|
|
LookupModule(mod);
|
2001-11-15 00:01:43 +00:00
|
|
|
CurrentModule = mod;
|
2013-11-13 10:38:20 +00:00
|
|
|
LOCAL_SourceModule = mod;
|
2004-02-12 12:37:12 +00:00
|
|
|
return TRUE;
|
2001-10-30 16:42:05 +00:00
|
|
|
}
|
|
|
|
|
2002-09-17 01:14:23 +01:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
cont_current_module( USES_REGS1 )
|
2002-09-17 01:14:23 +01:00
|
|
|
{
|
2007-04-10 23:13:21 +01:00
|
|
|
ModEntry *imod = (ModEntry *)IntegerOfTerm(EXTRA_CBACK_ARG(1,1)), *next;
|
|
|
|
Term t = MkAtomTerm(imod->AtomOfME);
|
|
|
|
next = imod->NextME;
|
|
|
|
|
|
|
|
/* ARG1 is unbound */
|
|
|
|
Yap_unify(ARG1,t);
|
|
|
|
if (!next)
|
|
|
|
cut_succeed();
|
|
|
|
EXTRA_CBACK_ARG(1,1) = MkIntegerTerm((Int)next);
|
|
|
|
return TRUE;
|
2002-09-17 01:14:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
init_current_module( USES_REGS1 )
|
2002-09-17 01:14:23 +01:00
|
|
|
{ /* current_module(?ModuleName) */
|
2007-04-10 23:13:21 +01:00
|
|
|
Term t = Deref(ARG1);
|
|
|
|
if (!IsVarTerm(t)) {
|
|
|
|
if (!IsAtomTerm(t)) {
|
|
|
|
Yap_Error(TYPE_ERROR_ATOM,t,"module name must be an atom");
|
|
|
|
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
|
|
|
}
|
|
|
|
EXTRA_CBACK_ARG(1,1) = MkIntegerTerm((Int)CurrentModules);
|
2011-03-07 16:02:55 +00:00
|
|
|
return cont_current_module( PASS_REGS1 );
|
2002-09-17 01:14:23 +01:00
|
|
|
}
|
|
|
|
|
2008-02-12 17:03:59 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_strip_module( USES_REGS1 )
|
2008-02-12 17:03:59 +00:00
|
|
|
{
|
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-02-09 10:46:43 +00:00
|
|
|
t1 = Yap_StripModule( t1, &tmod );
|
|
|
|
if (!t1) {
|
|
|
|
Yap_Error(TYPE_ERROR_CALLABLE,ARG1,"trying to obtain module");
|
|
|
|
return FALSE;
|
2008-02-12 17:03:59 +00:00
|
|
|
}
|
|
|
|
return Yap_unify(ARG3, t1) &&
|
|
|
|
Yap_unify(ARG2, tmod);
|
|
|
|
}
|
|
|
|
|
2008-03-13 14:38:02 +00:00
|
|
|
static Int
|
2011-03-07 16:02:55 +00:00
|
|
|
p_context_module( USES_REGS1 )
|
2008-03-13 14:38:02 +00:00
|
|
|
{
|
|
|
|
yamop *parentcp = P;
|
|
|
|
CELL *yenv;
|
|
|
|
PredEntry *ap = EnvPreg(parentcp);
|
|
|
|
if (ap->ModuleOfPred &&
|
|
|
|
!(ap->PredFlags & MetaPredFlag))
|
|
|
|
return Yap_unify(ARG1, ap->ModuleOfPred);
|
|
|
|
parentcp = CP;
|
|
|
|
yenv = ENV;
|
|
|
|
do {
|
|
|
|
ap = EnvPreg(parentcp);
|
|
|
|
if (ap->ModuleOfPred &&
|
|
|
|
!(ap->PredFlags & MetaPredFlag))
|
|
|
|
return Yap_unify(ARG1, ap->ModuleOfPred);
|
|
|
|
parentcp = (yamop *)yenv[E_CP];
|
|
|
|
yenv = (CELL *)yenv[E_E];
|
|
|
|
} while(yenv);
|
|
|
|
return Yap_unify(ARG1, CurrentModule);
|
|
|
|
}
|
|
|
|
|
2008-08-07 21:51:23 +01:00
|
|
|
Term
|
|
|
|
Yap_StripModule(Term t, Term *modp)
|
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2008-08-07 21:51:23 +01:00
|
|
|
Term tmod;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2008-08-07 21:51:23 +01: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 {
|
2008-08-07 21:51:23 +01:00
|
|
|
Functor fun = FunctorOfTerm(t);
|
|
|
|
if (fun == FunctorModule) {
|
2014-02-09 10:46:43 +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;
|
|
|
|
if (!IsVarTerm(tmod) && !IsAtomTerm(tmod) ) {
|
2008-08-07 21:51:23 +01:00
|
|
|
return 0L;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-02-06 02:26:23 +00:00
|
|
|
void
|
|
|
|
Yap_InitModulesC(void)
|
|
|
|
{
|
2012-10-19 18:10:48 +01:00
|
|
|
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);
|
2008-03-13 14:38:02 +00:00
|
|
|
Yap_InitCPred("context_module", 1, p_context_module, 0);
|
2004-02-06 02:26:23 +00:00
|
|
|
Yap_InitCPredBack("$all_current_modules", 1, 1, init_current_module, cont_current_module,
|
2012-10-19 18:10:48 +01:00
|
|
|
SafePredFlag|SyncPredFlag);
|
2004-02-06 02:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_InitModules(void)
|
2001-04-09 20:54:03 +01:00
|
|
|
{
|
2011-03-07 16:02:55 +00:00
|
|
|
CACHE_REGS
|
2008-12-23 01:53:52 +00:00
|
|
|
LookupModule(MkAtomTerm(AtomProlog));
|
2007-04-10 23:13:21 +01:00
|
|
|
LookupModule(USER_MODULE);
|
|
|
|
LookupModule(IDB_MODULE);
|
|
|
|
LookupModule(ATTRIBUTES_MODULE);
|
|
|
|
LookupModule(CHARSIO_MODULE);
|
|
|
|
LookupModule(TERMS_MODULE);
|
|
|
|
LookupModule(SYSTEM_MODULE);
|
|
|
|
LookupModule(READUTIL_MODULE);
|
|
|
|
LookupModule(HACKS_MODULE);
|
2008-02-12 17:03:59 +00:00
|
|
|
LookupModule(ARG_MODULE);
|
2007-04-10 23:13:21 +01:00
|
|
|
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);
|
2004-02-12 12:37:12 +00:00
|
|
|
CurrentModule = PROLOG_MODULE;
|
2001-04-09 20:54:03 +01:00
|
|
|
}
|