source: git/Singular/ipid.cc @ 929fae

spielwiese
Last change on this file since 929fae was 929fae, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
FIX: currRing / currQuotient removed from Singular/ipid since they are needed for kernel!
  • Property mode set to 100644
File size: 15.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5
6/*
7* ABSTRACT: identfier handling
8*/
9
10#include <string.h>
11
12#include <kernel/mod2.h>
13#include <Singular/static.h>
14#include <omalloc/omalloc.h>
15#include <Singular/tok.h>
16#include <misc/options.h>
17#include <Singular/ipshell.h>
18#include <misc/intvec.h>
19#include <kernel/febase.h>
20#include <coeffs/numbers.h>
21#include <kernel/longrat.h>
22#include <kernel/polys.h>
23#include <polys/monomials/ring.h>
24#include <kernel/ideals.h>
25#include <polys/matpol.h>
26#include <Singular/lists.h>
27#include <Singular/attrib.h>
28#include <Singular/silink.h>
29#include <kernel/syz.h>
30#include <Singular/ipid.h>
31#include <Singular/blackbox.h>
32
33#ifdef HAVE_DYNAMIC_LOADING
34#include <polys/mod_raw.h>
35#endif /* HAVE_DYNAMIC_LOADING */
36
37omBin sip_command_bin = omGetSpecBin(sizeof(sip_command));
38omBin sip_package_bin = omGetSpecBin(sizeof(sip_package));
39//omBin ip_package_bin = omGetSpecBin(sizeof(ip_package));
40omBin idrec_bin = omGetSpecBin(sizeof(idrec));
41
42coeffs coeffs_BIGINT;
43
44FILE   *feFilePending; /*temp. storage for grammar.y */
45
46proclevel *procstack=NULL;
47#define TEST
48//idhdl idroot = NULL;
49
50idhdl currPackHdl = NULL;
51idhdl basePackHdl = NULL;
52package currPack =NULL;
53package basePack =NULL;
54idhdl currRingHdl = NULL;
55const char* iiNoName="_";
56
57void paCleanUp(package pack);
58
59/*0 implementation*/
60
61int iiS2I(const char *s)
62{
63  int i;
64  i=s[0];
65  if (s[1]!='\0')
66  {
67    i=(i<<8)+s[1];
68    if (s[2]!='\0')
69    {
70      i=(i<<8)+s[2];
71      if (s[3]!='\0')
72      {
73        i=(i<<8)+s[3];
74      }
75    }
76  }
77  return i;
78}
79
80idhdl idrec::get(const char * s, int level)
81{
82  assume(s!=NULL);
83  assume((level>=0) && (level<=1000)); //not really, but if it isnt in that bounds..
84  idhdl h = this;
85  idhdl found=NULL;
86  int l;
87  const char *id_;
88  int i=iiS2I(s);
89  int less4=(i < (1<<24));
90  while (h!=NULL)
91  {
92    omCheckAddr((ADDRESS)IDID(h));
93    l=IDLEV(h);
94    if ((l==0)||(l==level))
95    {
96      if (i==h->id_i)
97      {
98        id_=IDID(h);
99        if (less4 || (0 == strcmp(s+4,id_+4)))
100        {
101          if (l==level) return h;
102          found=h;
103        }
104      }
105    }
106    h = IDNEXT(h);
107  }
108  return found;
109}
110
111//idrec::~idrec()
112//{
113//  if (id!=NULL)
114//  {
115//    omFree((ADDRESS)id);
116//    id=NULL;
117//  }
118//  /* much more !! */
119//}
120
121void *idrecDataInit(int t)
122{
123  switch (t)
124  {
125    //the type with init routines:
126    case INTVEC_CMD:
127    case INTMAT_CMD:
128      return (void *)new intvec();
129    case NUMBER_CMD:
130      return (void *) nInit(0);
131    case BIGINT_CMD:
132      return (void *) n_Init(0, coeffs_BIGINT);
133    case IDEAL_CMD:
134    case MODUL_CMD:
135    case MATRIX_CMD:
136      return (void*) idInit(1,1);
137    case MAP_CMD:
138    {
139      map m = (map)idInit(1,1);
140      m->preimage = omStrDup(IDID(currRingHdl));
141      return (void *)m;
142    }
143    case STRING_CMD:
144      return (void *)omAlloc0(1);
145    case LIST_CMD:
146    {
147      lists l=(lists)omAllocBin(slists_bin);
148      l->Init();
149      return (void*)l;
150    }
151    //the types with the standard init: set the struct to zero
152    case LINK_CMD:
153      return (void*) omAlloc0Bin(sip_link_bin);
154    case RING_CMD:
155      return (void*) omAlloc0Bin(sip_sring_bin);
156    case PACKAGE_CMD:
157      return (void*) omAlloc0Bin(sip_package_bin);
158    case PROC_CMD:
159      return (void *) omAlloc0Bin(procinfo_bin);
160    case RESOLUTION_CMD:
161      return  (void *)omAlloc0(sizeof(ssyStrategy));
162    //other types: without init (int,script,poly,def,package)
163    case INT_CMD:
164    case DEF_CMD:
165    case POLY_CMD:
166    case VECTOR_CMD:
167    case QRING_CMD:
168       return (void*)0L;
169    default:
170      {
171        if (t>MAX_TOK)
172        {
173#ifdef BLACKBOX_DEVEL
174          Print("bb-type %d\n",t);
175#endif
176          blackbox *bb=getBlackboxStuff(t);
177          if (bb!=NULL)
178             return (void *)bb->blackbox_Init(bb);
179        }
180        else
181          Werror("unknown type %d",t);
182        break;
183      }
184  }
185  return (void *)0L;
186}
187idhdl idrec::set(const char * s, int level, int t, BOOLEAN init)
188{
189  //printf("define %s, %x, level: %d, typ: %d\n", s,s,level,t);
190  idhdl h = (idrec *)omAlloc0Bin(idrec_bin);
191  IDID(h)   = s;
192  IDTYP(h)  = t;
193  IDLEV(h)  = level;
194  IDNEXT(h) = this;
195  h->id_i=iiS2I(s);
196  if (init)
197  {
198    if ((t==IDEAL_CMD)||(t==MODUL_CMD))
199      IDFLAG(h) = Sy_bit(FLAG_STD);
200    IDSTRING(h)=(char *)idrecDataInit(t);
201    // additional settings:--------------------------------------
202#if 0
203    // this leads to a memory leak
204    if (t == QRING_CMD)
205    {
206      // IDRING(h)=rCopy(currRing);
207      /* QRING_CMD is ring dep => currRing !=NULL */
208    }
209    else
210#endif
211    if (t == PROC_CMD)
212    {
213      IDPROC(h)->language=LANG_NONE;
214    }
215    else if (t == PACKAGE_CMD)
216    {
217      IDPACKAGE(h)->language=LANG_NONE;
218      IDPACKAGE(h)->loaded = FALSE;
219    }
220  }
221  // --------------------------------------------------------
222  return  h;
223}
224
225char * idrec::String()
226{
227  sleftv tmp;
228  memset(&tmp,0,sizeof(sleftv));
229  tmp.rtyp=IDTYP(this);
230  tmp.data=IDDATA(this);
231  tmp.name=IDID(this);
232  return tmp.String();
233}
234
235idhdl enterid(const char * s, int lev, int t, idhdl* root, BOOLEAN init, BOOLEAN search)
236{
237  if (s==NULL) return NULL;
238  idhdl h;
239  s=omStrDup(s);
240  // is it already defined in root ?
241  if ((h=(*root)->get(s,lev))!=NULL)
242  {
243    if (IDLEV(h)==lev)
244    {
245      if ((IDTYP(h) == t)||(t==DEF_CMD))
246      {
247        if ((IDTYP(h)==PACKAGE_CMD)
248        && (strcmp(s,"Top")==0))
249        {
250          goto errlabel;
251        }
252        if (BVERBOSE(V_REDEFINE))
253          Warn("redefining %s **",s);
254        if (s==IDID(h)) IDID(h)=NULL;
255        killhdl2(h,root,currRing);
256      }
257      else
258        goto errlabel;
259    }
260  }
261  // is it already defined in currRing->idroot ?
262  else if (search && (currRing!=NULL)&&((*root) != currRing->idroot))
263  {
264    if ((h=currRing->idroot->get(s,lev))!=NULL)
265    {
266      if (IDLEV(h)==lev)
267      {
268        if ((IDTYP(h) == t)||(t==DEF_CMD))
269        {
270          if (BVERBOSE(V_REDEFINE))
271            Warn("redefining %s **",s);
272          IDID(h)=NULL;
273          killhdl2(h,&currRing->idroot,currRing);
274        }
275        else
276          goto errlabel;
277      }
278    }
279  }
280  // is it already defined in idroot ?
281  else if (search && (*root != IDROOT))
282  {
283    if ((h=IDROOT->get(s,lev))!=NULL)
284    {
285      if (IDLEV(h)==lev)
286      {
287        if ((IDTYP(h) == t)||(t==DEF_CMD))
288        {
289          if (BVERBOSE(V_REDEFINE))
290            Warn("redefining `%s` **",s);
291          if (s==IDID(h)) IDID(h)=NULL;
292          killhdl2(h,&IDROOT,NULL);
293        }
294        else
295          goto errlabel;
296      }
297    }
298  }
299  *root = (*root)->set(s, lev, t, init);
300#ifndef NDEBUG
301  checkall();
302#endif
303  return *root;
304
305  errlabel:
306    //Werror("identifier `%s` in use(lev h=%d,typ=%d,t=%d, curr=%d)",s,IDLEV(h),IDTYP(h),t,lev);
307    Werror("identifier `%s` in use",s);
308    //listall();
309    omFree((ADDRESS)s);
310    return NULL;
311}
312void killid(const char * id, idhdl * ih)
313{
314  if (id!=NULL)
315  {
316    idhdl h = (*ih)->get(id,myynest);
317
318    // id not found in global list, is it defined in current ring ?
319    if (h==NULL)
320    {
321      if ((currRing!=NULL) && (*ih != (currRing->idroot)))
322      {
323        h = currRing->idroot->get(id,myynest);
324        if (h!=NULL)
325        {
326          killhdl2(h,&(currRing->idroot),currRing);
327          return;
328        }
329      }
330      Werror("`%s` is not defined",id);
331      return;
332    }
333    killhdl2(h,ih,currRing);
334  }
335  else
336    Werror("kill what ?");
337}
338
339void killhdl(idhdl h, package proot)
340{
341  int t=IDTYP(h);
342  if (((BEGIN_RING<t) && (t<END_RING) && (t!=QRING_CMD))
343  || ((t==LIST_CMD) && (lRingDependend((lists)IDDATA(h)))))
344    killhdl2(h,&currRing->idroot,currRing);
345  else
346  {
347    if(t==PACKAGE_CMD)
348    {
349      killhdl2(h,&(basePack->idroot),NULL);
350    }
351    else
352    {
353      idhdl s=proot->idroot;
354      while ((s!=h) && (s!=NULL)) s=s->next;
355      if (s!=NULL)
356        killhdl2(h,&(proot->idroot),NULL);
357      else if (basePack!=proot)
358      {
359        idhdl s=basePack->idroot;
360        while ((s!=h) && (s!=NULL)) s=s->next;
361        if (s!=NULL)
362          killhdl2(h,&(basePack->idroot),currRing);
363        else
364          killhdl2(h,&(currRing->idroot),currRing);
365       }
366    }
367  }
368}
369
370void killhdl2(idhdl h, idhdl * ih, ring r)
371{
372  //printf("kill %s, id %x, typ %d lev: %d\n",IDID(h),(int)IDID(h),IDTYP(h),IDLEV(h));
373  idhdl hh;
374
375  if (h->attribute!=NULL)
376  {
377    //h->attribute->killAll(r); MEMORY LEAK!
378    h->attribute=NULL;
379  }
380  if (IDTYP(h) == PACKAGE_CMD)
381  {
382    if (strcmp(IDID(h),"Top")==0)
383    {
384      WarnS("can not kill `Top`");
385      return;
386    }
387    // any objects defined for this package ?
388    if ((IDPACKAGE(h)->ref<=0)  &&  (IDPACKAGE(h)->idroot!=NULL))
389    {
390      if (currPack==IDPACKAGE(h))
391      {
392        currPack=basePack;
393        currPackHdl=NULL;
394      }
395      idhdl * hd = &IDRING(h)->idroot;
396      idhdl  hdh = IDNEXT(*hd);
397      idhdl  temp;
398      while (hdh!=NULL)
399      {
400        temp = IDNEXT(hdh);
401        killhdl2(hdh,&(IDPACKAGE(h)->idroot),NULL);
402        hdh = temp;
403      }
404      killhdl2(*hd,hd,NULL);
405      if (IDPACKAGE(h)->libname!=NULL) omFree((ADDRESS)(IDPACKAGE(h)->libname));
406    }
407    paKill(IDPACKAGE(h));
408    if (currPackHdl==h) currPackHdl=packFindHdl(currPack);
409    iiCheckPack(currPack);
410  }
411  else if ((IDTYP(h)==RING_CMD)||(IDTYP(h)==QRING_CMD))
412    rKill(h);
413  else
414    s_internalDelete(IDTYP(h),IDDATA(h),r);
415  //  general  -------------------------------------------------------------
416  // now dechain it and delete idrec
417  if (IDID(h)!=NULL) // OB: ?????
418    omFree((ADDRESS)IDID(h));
419  IDID(h)=NULL;
420  IDDATA(h)=NULL;
421  if (h == (*ih))
422  {
423    // h is at the beginning of the list
424    *ih = IDNEXT(h) /* ==*ih */;
425  }
426  else if (ih!=NULL)
427  {
428    // h is somethere in the list:
429    hh = *ih;
430    loop
431    {
432      if (hh==NULL)
433      {
434        PrintS(">>?<< not found for kill\n");
435        return;
436      }
437      idhdl hhh = IDNEXT(hh);
438      if (hhh == h)
439      {
440        IDNEXT(hh) = IDNEXT(hhh);
441        break;
442      }
443      hh = hhh;
444    }
445  }
446  omFreeBin((ADDRESS)h, idrec_bin);
447}
448
449idhdl ggetid(const char *n, BOOLEAN local, idhdl *packhdl)
450{
451  idhdl h = IDROOT->get(n,myynest);
452  idhdl h2=NULL;
453  *packhdl = NULL;
454  if ((currRing!=NULL) && ((h==NULL)||(IDLEV(h)!=myynest)))
455  {
456    h2 = currRing->idroot->get(n,myynest);
457  }
458  if (h2==NULL) return h;
459  return h2;
460}
461
462idhdl ggetid(const char *n)
463{
464  idhdl h = IDROOT->get(n,myynest);
465  if ((h!=NULL)&&(IDLEV(h)==myynest)) return h;
466  idhdl h2=NULL;
467  if (currRing!=NULL)
468  {
469    h2 = currRing->idroot->get(n,myynest);
470  }
471  if (h2!=NULL) return h2;
472  if (h!=NULL) return h;
473  if (basePack!=currPack)
474    return basePack->idroot->get(n,myynest);
475  return NULL;
476}
477
478void ipListFlag(idhdl h)
479{
480  if (hasFlag(h,FLAG_STD)) PrintS(" (SB)");
481#ifdef HAVE_PLURAL
482  if (hasFlag(h,FLAG_TWOSTD)) PrintS(" (2SB)");
483#endif
484}
485
486lists ipNameList(idhdl root)
487{
488  idhdl h=root;
489  /* compute the length */
490  int l=0;
491  while (h!=NULL) { l++; h=IDNEXT(h); }
492  /* allocate list */
493  lists L=(lists)omAllocBin(slists_bin);
494  L->Init(l);
495  /* copy names */
496  h=root;
497  l=0;
498  while (h!=NULL)
499  {
500    /* list is initialized with 0 => no need to clear anything */
501    L->m[l].rtyp=STRING_CMD;
502    L->m[l].data=omStrDup(IDID(h));
503    l++;
504    h=IDNEXT(h);
505  }
506  return L;
507}
508
509/*
510* move 'tomove' from root1 list to root2 list
511*/
512static int ipSwapId(idhdl tomove, idhdl &root1, idhdl &root2)
513{
514  idhdl h;
515  /* search 'tomove' in root2 : if found -> do nothing */
516  h=root2;
517  while ((h!=NULL) && (h!=tomove)) h=IDNEXT(h);
518  if (h!=NULL) return FALSE; /*okay */
519  /* search predecessor of h in root1, remove 'tomove' */
520  h=root1;
521  if (tomove==h)
522  {
523    root1=IDNEXT(h);
524  }
525  else
526  {
527    while ((h!=NULL) && (IDNEXT(h)!=tomove)) h=IDNEXT(h);
528    if (h==NULL) return TRUE; /* not in the list root1 -> do nothing */
529    IDNEXT(h)=IDNEXT(tomove);
530  }
531  /* add to root2 list */
532  IDNEXT(tomove)=root2;
533  root2=tomove;
534  return FALSE;
535}
536
537void  ipMoveId(idhdl tomove)
538{
539  if ((currRing!=NULL)&&(tomove!=NULL))
540  {
541    if (((QRING_CMD!=IDTYP(tomove)) && RingDependend(IDTYP(tomove)))
542    || ((IDTYP(tomove)==LIST_CMD) && (lRingDependend(IDLIST(tomove)))))
543    {
544      /*move 'tomove' to ring id's*/
545      if (ipSwapId(tomove,IDROOT,currRing->idroot))
546      ipSwapId(tomove,basePack->idroot,currRing->idroot);
547    }
548    else
549    {
550      /*move 'tomove' to global id's*/
551      ipSwapId(tomove,currRing->idroot,IDROOT);
552    }
553  }
554}
555
556const char * piProcinfo(procinfov pi, const char *request)
557{
558  if(pi == NULL) return "empty proc";
559  else if (strcmp(request, "libname")  == 0) return pi->libname;
560  else if (strcmp(request, "procname") == 0) return pi->procname;
561  else if (strcmp(request, "type")     == 0)
562  {
563    switch (pi->language)
564    {
565      case LANG_SINGULAR: return "singular"; break;
566      case LANG_C:        return "object";   break;
567      case LANG_NONE:     return "none";     break;
568      default:            return "unknow language";
569    }
570  }
571  else if (strcmp(request, "ref")      == 0)
572  {
573    char p[8];
574    sprintf(p, "%d", pi->ref);
575    return omStrDup(p);  // MEMORY-LEAK
576  }
577  return "??";
578}
579
580void piCleanUp(procinfov pi)
581{
582  (pi->ref)--;
583  if (pi->ref <= 0)
584  {
585    if (pi->libname != NULL) // OB: ????
586      omFree((ADDRESS)pi->libname);
587    if (pi->procname != NULL) // OB: ????
588      omFree((ADDRESS)pi->procname);
589
590    if( pi->language == LANG_SINGULAR)
591    {
592      if (pi->data.s.body != NULL) // OB: ????
593        omFree((ADDRESS)pi->data.s.body);
594    }
595    if( pi->language == LANG_C)
596    {
597    }
598    memset((void *) pi, 0, sizeof(procinfo));
599    pi->language=LANG_NONE;
600  }
601}
602
603BOOLEAN piKill(procinfov pi)
604{
605  Voice *p=currentVoice;
606  while (p!=NULL)
607  {
608    if (p->pi==pi && pi->ref <= 1)
609    {
610      Warn("`%s` in use, can not be killed",pi->procname);
611      return TRUE;
612    }
613    p=p->next;
614  }
615  piCleanUp(pi);
616  if (pi->ref <= 0)
617    omFreeBin((ADDRESS)pi,  procinfo_bin);
618  return FALSE;
619}
620
621void paCleanUp(package pack)
622{
623  (pack->ref)--;
624  if (pack->ref < 0)
625  {
626#ifndef HAVE_STATIC
627    if( pack->language == LANG_C)
628    {
629      Print("//dlclose(%s)\n",pack->libname);
630#ifdef HAVE_DYNAMIC_LOADING
631      dynl_close (pack->handle);
632#endif /* HAVE_DYNAMIC_LOADING */
633    }
634#endif /* HAVE_STATIC */
635    omfree((ADDRESS)pack->libname);
636    memset((void *) pack, 0, sizeof(sip_package));
637    pack->language=LANG_NONE;
638  }
639}
640
641void proclevel::push(char *n)
642{
643  //Print("push %s\n",n);
644  proclevel *p=(proclevel*)omAlloc0(sizeof(proclevel));
645  p->cRing=currRing;
646  p->cRingHdl=currRingHdl;
647  p->name=n;
648  p->cPackHdl=currPackHdl;
649  p->cPack=currPack;
650  p->next=this;
651  procstack=p;
652}
653void proclevel::pop()
654{
655  //Print("pop %s\n",name);
656  //if (currRing!=::currRing) PrintS("currRing wrong\n");;
657  //::currRing=this->currRing;
658  //if (r==NULL) Print("set ring to NULL at lev %d(%s)\n",myynest,name);
659  //::currRingHdl=this->currRingHdl;
660  //if((::currRingHdl==NULL)||(IDRING(::currRingHdl)!=(::currRing)))
661  //  ::currRingHdl=rFindHdl(::currRing,NULL,NULL);
662  //Print("restore pack=%s,1.obj=%s\n",IDID(currPackHdl),IDID(currPack->idroot));
663  currPackHdl=this->cPackHdl;
664  currPack=this->cPack;
665  iiCheckPack(currPack);
666  proclevel *p=this;
667  procstack=next;
668  omFreeSize(p,sizeof(proclevel));
669}
670
671idhdl packFindHdl(package r)
672{
673  idhdl h=basePack->idroot;
674  while (h!=NULL)
675  {
676    if ((IDTYP(h)==PACKAGE_CMD)
677        && (IDPACKAGE(h)==r))
678      return h;
679    h=IDNEXT(h);
680  }
681  return NULL;
682}
Note: See TracBrowser for help on using the repository browser.