source: git/Singular/ipid.cc @ 3e7db4

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