source: git/Singular/ipid.cc @ d5add65

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