source: git/Singular/ipid.cc @ 810491

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