source: git/Singular/ipid.cc @ 411e002

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