source: git/Singular/ipid.cc @ f24b9c

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