source: git/Singular/ipid.cc @ bceefc

spielwiese
Last change on this file since bceefc was bceefc, checked in by Hans Schoenemann <hannes@…>, 10 years ago
chg: toward ring-indep. numbers
  • 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  if (root==NULL) return NULL;
253  idhdl h;
254  s=omStrDup(s);
255  // idhdl *save_root=root;
256  if (t==PACKAGE_CMD)
257  {
258    if (root!=&(basePack->idroot))
259    {
260      root=&(basePack->idroot);
261    }
262  }
263  // is it already defined in root ?
264  if ((h=(*root)->get(s,lev))!=NULL)
265  {
266    if (IDLEV(h)==lev)
267    {
268      if ((IDTYP(h) == t)||(t==DEF_CMD))
269      {
270        if ((IDTYP(h)==PACKAGE_CMD)
271        && (strcmp(s,"Top")==0))
272        {
273          goto errlabel;
274        }
275        if (BVERBOSE(V_REDEFINE))
276          Warn("redefining %s **",s);
277        if (s==IDID(h)) IDID(h)=NULL;
278        killhdl2(h,root,currRing);
279      }
280      else
281        goto errlabel;
282    }
283  }
284  // is it already defined in currRing->idroot ?
285  else if (search && (currRing!=NULL)&&((*root) != currRing->idroot))
286  {
287    if ((h=currRing->idroot->get(s,lev))!=NULL)
288    {
289      if (IDLEV(h)==lev)
290      {
291        if ((IDTYP(h) == t)||(t==DEF_CMD))
292        {
293          if (BVERBOSE(V_REDEFINE))
294            Warn("redefining %s **",s);
295          IDID(h)=NULL;
296          killhdl2(h,&currRing->idroot,currRing);
297        }
298        else
299          goto errlabel;
300      }
301    }
302  }
303  // is it already defined in idroot ?
304  else if (search && (*root != IDROOT))
305  {
306    if ((h=IDROOT->get(s,lev))!=NULL)
307    {
308      if (IDLEV(h)==lev)
309      {
310        if ((IDTYP(h) == t)||(t==DEF_CMD))
311        {
312          if (BVERBOSE(V_REDEFINE))
313            Warn("redefining `%s` **",s);
314          if (s==IDID(h)) IDID(h)=NULL;
315          killhdl2(h,&IDROOT,NULL);
316        }
317        else
318          goto errlabel;
319      }
320    }
321  }
322  *root = (*root)->set(s, lev, t, init);
323#ifndef SING_NDEBUG
324  checkall();
325#endif
326  return *root;
327
328  errlabel:
329    //Werror("identifier `%s` in use(lev h=%d,typ=%d,t=%d, curr=%d)",s,IDLEV(h),IDTYP(h),t,lev);
330    Werror("identifier `%s` in use",s);
331    //listall();
332    omFree((ADDRESS)s);
333    return NULL;
334}
335void killid(const char * id, idhdl * ih)
336{
337  if (id!=NULL)
338  {
339    idhdl h = (*ih)->get(id,myynest);
340
341    // id not found in global list, is it defined in current ring ?
342    if (h==NULL)
343    {
344      if ((currRing!=NULL) && (*ih != (currRing->idroot)))
345      {
346        h = currRing->idroot->get(id,myynest);
347        if (h!=NULL)
348        {
349          killhdl2(h,&(currRing->idroot),currRing);
350          return;
351        }
352      }
353      Werror("`%s` is not defined",id);
354      return;
355    }
356    killhdl2(h,ih,currRing);
357  }
358  else
359    Werror("kill what ?");
360}
361
362void killhdl(idhdl h, package proot)
363{
364  int t=IDTYP(h);
365  if (((BEGIN_RING<t) && (t<END_RING) && (t!=QRING_CMD))
366  || ((t==LIST_CMD) && (lRingDependend((lists)IDDATA(h)))))
367    killhdl2(h,&currRing->idroot,currRing);
368  else
369  {
370    if(t==PACKAGE_CMD)
371    {
372      killhdl2(h,&(basePack->idroot),NULL);
373    }
374    else
375    {
376      idhdl s=proot->idroot;
377      while ((s!=h) && (s!=NULL)) s=s->next;
378      if (s!=NULL)
379        killhdl2(h,&(proot->idroot),NULL);
380      else if (basePack!=proot)
381      {
382        idhdl s=basePack->idroot;
383        while ((s!=h) && (s!=NULL)) s=s->next;
384        if (s!=NULL)
385          killhdl2(h,&(basePack->idroot),currRing);
386        else
387          killhdl2(h,&(currRing->idroot),currRing);
388       }
389    }
390  }
391}
392
393void killhdl2(idhdl h, idhdl * ih, ring r)
394{
395  //printf("kill %s, id %x, typ %d lev: %d\n",IDID(h),(int)IDID(h),IDTYP(h),IDLEV(h));
396  idhdl hh;
397
398  if (h->attribute!=NULL)
399  {
400    //h->attribute->killAll(r); MEMORY LEAK!
401    h->attribute=NULL;
402  }
403  if (IDTYP(h) == PACKAGE_CMD)
404  {
405    if (strcmp(IDID(h),"Top")==0)
406    {
407      WarnS("can not kill `Top`");
408      return;
409    }
410    // any objects defined for this package ?
411    if ((IDPACKAGE(h)->ref<=0)  &&  (IDPACKAGE(h)->idroot!=NULL))
412    {
413      if (currPack==IDPACKAGE(h))
414      {
415        currPack=basePack;
416        currPackHdl=NULL;
417      }
418      idhdl * hd = &IDRING(h)->idroot;
419      idhdl  hdh = IDNEXT(*hd);
420      idhdl  temp;
421      while (hdh!=NULL)
422      {
423        temp = IDNEXT(hdh);
424        killhdl2(hdh,&(IDPACKAGE(h)->idroot),NULL);
425        hdh = temp;
426      }
427      killhdl2(*hd,hd,NULL);
428      if (IDPACKAGE(h)->libname!=NULL) omFree((ADDRESS)(IDPACKAGE(h)->libname));
429    }
430    paKill(IDPACKAGE(h));
431    if (currPackHdl==h) currPackHdl=packFindHdl(currPack);
432    iiCheckPack(currPack);
433  }
434  else if ((IDTYP(h)==RING_CMD)||(IDTYP(h)==QRING_CMD))
435    rKill(h);
436  else if (IDDATA(h)!=NULL)
437    s_internalDelete(IDTYP(h),IDDATA(h),r);
438  //  general  -------------------------------------------------------------
439  // now dechain it and delete idrec
440  if (IDID(h)!=NULL) // OB: ?????
441    omFree((ADDRESS)IDID(h));
442  IDID(h)=NULL;
443  IDDATA(h)=NULL;
444  if (h == (*ih))
445  {
446    // h is at the beginning of the list
447    *ih = IDNEXT(h) /* ==*ih */;
448  }
449  else if (ih!=NULL)
450  {
451    // h is somethere in the list:
452    hh = *ih;
453    loop
454    {
455      if (hh==NULL)
456      {
457        PrintS(">>?<< not found for kill\n");
458        return;
459      }
460      idhdl hhh = IDNEXT(hh);
461      if (hhh == h)
462      {
463        IDNEXT(hh) = IDNEXT(hhh);
464        break;
465      }
466      hh = hhh;
467    }
468  }
469  omFreeBin((ADDRESS)h, idrec_bin);
470}
471
472idhdl ggetid(const char *n, BOOLEAN /*local*/, idhdl *packhdl)
473{
474  idhdl h = IDROOT->get(n,myynest);
475  idhdl h2=NULL;
476  *packhdl = NULL;
477  if ((currRing!=NULL) && ((h==NULL)||(IDLEV(h)!=myynest)))
478  {
479    h2 = currRing->idroot->get(n,myynest);
480  }
481  if (h2==NULL) return h;
482  return h2;
483}
484
485idhdl ggetid(const char *n)
486{
487  idhdl h = IDROOT->get(n,myynest);
488  if ((h!=NULL)&&(IDLEV(h)==myynest)) return h;
489  idhdl h2=NULL;
490  if (currRing!=NULL)
491  {
492    h2 = currRing->idroot->get(n,myynest);
493  }
494  if (h2!=NULL) return h2;
495  if (h!=NULL) return h;
496  if (basePack!=currPack)
497    return basePack->idroot->get(n,myynest);
498  return NULL;
499}
500
501void ipListFlag(idhdl h)
502{
503  if (hasFlag(h,FLAG_STD)) PrintS(" (SB)");
504#ifdef HAVE_PLURAL
505  if (hasFlag(h,FLAG_TWOSTD)) PrintS(" (2SB)");
506#endif
507}
508
509lists ipNameList(idhdl root)
510{
511  idhdl h=root;
512  /* compute the length */
513  int l=0;
514  while (h!=NULL) { l++; h=IDNEXT(h); }
515  /* allocate list */
516  lists L=(lists)omAllocBin(slists_bin);
517  L->Init(l);
518  /* copy names */
519  h=root;
520  l=0;
521  while (h!=NULL)
522  {
523    /* list is initialized with 0 => no need to clear anything */
524    L->m[l].rtyp=STRING_CMD;
525    L->m[l].data=omStrDup(IDID(h));
526    l++;
527    h=IDNEXT(h);
528  }
529  return L;
530}
531
532lists ipNameListLev(idhdl root, int lev)
533{
534  idhdl h=root;
535  /* compute the length */
536  int l=0;
537  while (h!=NULL) { if (IDLEV(h)==lev) l++; h=IDNEXT(h); }
538  /* allocate list */
539  lists L=(lists)omAllocBin(slists_bin);
540  L->Init(l);
541  /* copy names */
542  h=root;
543  l=0;
544  while (h!=NULL)
545  {
546    if (IDLEV(h)==lev)
547    {
548      /* list is initialized with 0 => no need to clear anything */
549      L->m[l].rtyp=STRING_CMD;
550      L->m[l].data=omStrDup(IDID(h));
551      l++;
552    }
553    h=IDNEXT(h);
554  }
555  return L;
556}
557
558/*
559* move 'tomove' from root1 list to root2 list
560*/
561static int ipSwapId(idhdl tomove, idhdl &root1, idhdl &root2)
562{
563  idhdl h;
564  /* search 'tomove' in root2 : if found -> do nothing */
565  h=root2;
566  while ((h!=NULL) && (h!=tomove)) h=IDNEXT(h);
567  if (h!=NULL) return FALSE; /*okay */
568  /* search predecessor of h in root1, remove 'tomove' */
569  h=root1;
570  if (tomove==h)
571  {
572    root1=IDNEXT(h);
573  }
574  else
575  {
576    while ((h!=NULL) && (IDNEXT(h)!=tomove)) h=IDNEXT(h);
577    if (h==NULL) return TRUE; /* not in the list root1 -> do nothing */
578    IDNEXT(h)=IDNEXT(tomove);
579  }
580  /* add to root2 list */
581  IDNEXT(tomove)=root2;
582  root2=tomove;
583  return FALSE;
584}
585
586void  ipMoveId(idhdl tomove)
587{
588  if ((currRing!=NULL)&&(tomove!=NULL))
589  {
590    if (((QRING_CMD!=IDTYP(tomove)) && RingDependend(IDTYP(tomove)))
591    || ((IDTYP(tomove)==LIST_CMD) && (lRingDependend(IDLIST(tomove)))))
592    {
593      /*move 'tomove' to ring id's*/
594      if (ipSwapId(tomove,IDROOT,currRing->idroot))
595      ipSwapId(tomove,basePack->idroot,currRing->idroot);
596    }
597    else
598    {
599      /*move 'tomove' to global id's*/
600      ipSwapId(tomove,currRing->idroot,IDROOT);
601    }
602  }
603}
604
605const char * piProcinfo(procinfov pi, const char *request)
606{
607  if(pi == NULL) return "empty proc";
608  else if (strcmp(request, "libname")  == 0) return pi->libname;
609  else if (strcmp(request, "procname") == 0) return pi->procname;
610  else if (strcmp(request, "type")     == 0)
611  {
612    switch (pi->language)
613    {
614      case LANG_SINGULAR: return "singular"; break;
615      case LANG_C:        return "object";   break;
616      case LANG_NONE:     return "none";     break;
617      default:            return "unknow language";
618    }
619  }
620  else if (strcmp(request, "ref")      == 0)
621  {
622    char p[8];
623    sprintf(p, "%d", pi->ref);
624    return omStrDup(p);  // MEMORY-LEAK
625  }
626  return "??";
627}
628
629void piCleanUp(procinfov pi)
630{
631  (pi->ref)--;
632  if (pi->ref <= 0)
633  {
634    if (pi->libname != NULL) // OB: ????
635      omFree((ADDRESS)pi->libname);
636    if (pi->procname != NULL) // OB: ????
637      omFree((ADDRESS)pi->procname);
638
639    if( pi->language == LANG_SINGULAR)
640    {
641      if (pi->data.s.body != NULL) // OB: ????
642        omFree((ADDRESS)pi->data.s.body);
643    }
644    if( pi->language == LANG_C)
645    {
646    }
647    memset((void *) pi, 0, sizeof(procinfo));
648    pi->language=LANG_NONE;
649  }
650}
651
652BOOLEAN piKill(procinfov pi)
653{
654  Voice *p=currentVoice;
655  while (p!=NULL)
656  {
657    if (p->pi==pi && pi->ref <= 1)
658    {
659      Warn("`%s` in use, can not be killed",pi->procname);
660      return TRUE;
661    }
662    p=p->next;
663  }
664  piCleanUp(pi);
665  if (pi->ref <= 0)
666    omFreeBin((ADDRESS)pi,  procinfo_bin);
667  return FALSE;
668}
669
670void paCleanUp(package pack)
671{
672  (pack->ref)--;
673  if (pack->ref < 0)
674  {
675#ifndef HAVE_STATIC
676    if( pack->language == LANG_C)
677    {
678      Print("//dlclose(%s)\n",pack->libname);
679#ifdef HAVE_DYNAMIC_LOADING
680      dynl_close (pack->handle);
681#endif /* HAVE_DYNAMIC_LOADING */
682    }
683#endif /* HAVE_STATIC */
684    omFree((ADDRESS)pack->libname);
685    memset((void *) pack, 0, sizeof(sip_package));
686    pack->language=LANG_NONE;
687  }
688}
689
690void proclevel::push(char *n)
691{
692  //Print("push %s\n",n);
693  proclevel *p=(proclevel*)omAlloc0(sizeof(proclevel));
694  p->cRing=currRing;
695  p->cRingHdl=currRingHdl;
696  p->name=n;
697  p->cPackHdl=currPackHdl;
698  p->cPack=currPack;
699  p->next=this;
700  procstack=p;
701}
702void proclevel::pop()
703{
704  //Print("pop %s\n",name);
705  //if (currRing!=::currRing) PrintS("currRing wrong\n");;
706  //::currRing=this->currRing;
707  //if (r==NULL) Print("set ring to NULL at lev %d(%s)\n",myynest,name);
708  //::currRingHdl=this->currRingHdl;
709  //if((::currRingHdl==NULL)||(IDRING(::currRingHdl)!=(::currRing)))
710  //  ::currRingHdl=rFindHdl(::currRing,NULL,NULL);
711  //Print("restore pack=%s,1.obj=%s\n",IDID(currPackHdl),IDID(currPack->idroot));
712  currPackHdl=this->cPackHdl;
713  currPack=this->cPack;
714  iiCheckPack(currPack);
715  proclevel *p=this;
716  procstack=next;
717  omFreeSize(p,sizeof(proclevel));
718}
719
720idhdl packFindHdl(package r)
721{
722  idhdl h=basePack->idroot;
723  while (h!=NULL)
724  {
725    if ((IDTYP(h)==PACKAGE_CMD)
726        && (IDPACKAGE(h)==r))
727      return h;
728    h=IDNEXT(h);
729  }
730  return NULL;
731}
Note: See TracBrowser for help on using the repository browser.