source: git/Singular/ipid.cc @ a71a00

spielwiese
Last change on this file since a71a00 was c227a2f, checked in by Hans Schoenemann <hannes@…>, 13 years ago
attribute handling git-svn-id: file:///usr/local/Singular/svn/trunk@14260 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.0 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 level)
79{
80  assume(s!=NULL);
81  assume((level>=0) && (level<=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==level))
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==level) 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 level, int t, BOOLEAN init)
186{
187  //printf("define %s, %x, level: %d, typ: %d\n", s,s,level,t);
188  idhdl h = (idrec *)omAlloc0Bin(idrec_bin);
189  IDID(h)   = s;
190  IDTYP(h)  = t;
191  IDLEV(h)  = level;
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->killAll(r); MEMORY LEAK!
375    h->attribute=NULL;
376  }
377  if (IDTYP(h) == PACKAGE_CMD)
378  {
379    if (strcmp(IDID(h),"Top")==0)
380    {
381      WarnS("can not kill `Top`");
382      return;
383    }
384    // any objects defined for this package ?
385    if ((IDPACKAGE(h)->ref<=0)  &&  (IDPACKAGE(h)->idroot!=NULL))
386    {
387      if (currPack==IDPACKAGE(h))
388      {
389        currPack=basePack;
390        currPackHdl=NULL;
391      }
392      idhdl * hd = &IDRING(h)->idroot;
393      idhdl  hdh = IDNEXT(*hd);
394      idhdl  temp;
395      while (hdh!=NULL)
396      {
397        temp = IDNEXT(hdh);
398        killhdl2(hdh,&(IDPACKAGE(h)->idroot),NULL);
399        hdh = temp;
400      }
401      killhdl2(*hd,hd,NULL);
402      if (IDPACKAGE(h)->libname!=NULL) omFree((ADDRESS)(IDPACKAGE(h)->libname));
403    }
404    paKill(IDPACKAGE(h));
405    if (currPackHdl==h) currPackHdl=packFindHdl(currPack);
406    iiCheckPack(currPack);
407  }
408  else if ((IDTYP(h)==RING_CMD)||(IDTYP(h)==QRING_CMD))
409    rKill(h);
410  else
411    s_internalDelete(IDTYP(h),IDDATA(h),r);
412  //  general  -------------------------------------------------------------
413  // now dechain it and delete idrec
414  if (IDID(h)!=NULL) // OB: ?????
415    omFree((ADDRESS)IDID(h));
416  IDID(h)=NULL;
417  IDDATA(h)=NULL;
418  if (h == (*ih))
419  {
420    // h is at the beginning of the list
421    *ih = IDNEXT(h) /* ==*ih */;
422  }
423  else if (ih!=NULL)
424  {
425    // h is somethere in the list:
426    hh = *ih;
427    loop
428    {
429      if (hh==NULL)
430      {
431        PrintS(">>?<< not found for kill\n");
432        return;
433      }
434      idhdl hhh = IDNEXT(hh);
435      if (hhh == h)
436      {
437        IDNEXT(hh) = IDNEXT(hhh);
438        break;
439      }
440      hh = hhh;
441    }
442  }
443  omFreeBin((ADDRESS)h, idrec_bin);
444}
445
446idhdl ggetid(const char *n, BOOLEAN local, idhdl *packhdl)
447{
448  idhdl h = IDROOT->get(n,myynest);
449  idhdl h2=NULL;
450  *packhdl = NULL;
451  if ((currRing!=NULL) && ((h==NULL)||(IDLEV(h)!=myynest)))
452  {
453    h2 = currRing->idroot->get(n,myynest);
454  }
455  if (h2==NULL) return h;
456  return h2;
457}
458
459idhdl ggetid(const char *n)
460{
461  idhdl h = IDROOT->get(n,myynest);
462  if ((h!=NULL)&&(IDLEV(h)==myynest)) return h;
463  idhdl h2=NULL;
464  if (currRing!=NULL)
465  {
466    h2 = currRing->idroot->get(n,myynest);
467  }
468  if (h2!=NULL) return h2;
469  if (h!=NULL) return h;
470  if (basePack!=currPack)
471    return basePack->idroot->get(n,myynest);
472  return NULL;
473}
474
475void ipListFlag(idhdl h)
476{
477  if (hasFlag(h,FLAG_STD)) PrintS(" (SB)");
478#ifdef HAVE_PLURAL
479  if (hasFlag(h,FLAG_TWOSTD)) PrintS(" (2SB)");
480#endif
481}
482
483lists ipNameList(idhdl root)
484{
485  idhdl h=root;
486  /* compute the length */
487  int l=0;
488  while (h!=NULL) { l++; h=IDNEXT(h); }
489  /* allocate list */
490  lists L=(lists)omAllocBin(slists_bin);
491  L->Init(l);
492  /* copy names */
493  h=root;
494  l=0;
495  while (h!=NULL)
496  {
497    /* list is initialized with 0 => no need to clear anything */
498    L->m[l].rtyp=STRING_CMD;
499    L->m[l].data=omStrDup(IDID(h));
500    l++;
501    h=IDNEXT(h);
502  }
503  return L;
504}
505
506/*
507* move 'tomove' from root1 list to root2 list
508*/
509static int ipSwapId(idhdl tomove, idhdl &root1, idhdl &root2)
510{
511  idhdl h;
512  /* search 'tomove' in root2 : if found -> do nothing */
513  h=root2;
514  while ((h!=NULL) && (h!=tomove)) h=IDNEXT(h);
515  if (h!=NULL) return FALSE; /*okay */
516  /* search predecessor of h in root1, remove 'tomove' */
517  h=root1;
518  if (tomove==h)
519  {
520    root1=IDNEXT(h);
521  }
522  else
523  {
524    while ((h!=NULL) && (IDNEXT(h)!=tomove)) h=IDNEXT(h);
525    if (h==NULL) return TRUE; /* not in the list root1 -> do nothing */
526    IDNEXT(h)=IDNEXT(tomove);
527  }
528  /* add to root2 list */
529  IDNEXT(tomove)=root2;
530  root2=tomove;
531  return FALSE;
532}
533
534void  ipMoveId(idhdl tomove)
535{
536  if ((currRing!=NULL)&&(tomove!=NULL))
537  {
538    if (((QRING_CMD!=IDTYP(tomove)) && RingDependend(IDTYP(tomove)))
539    || ((IDTYP(tomove)==LIST_CMD) && (lRingDependend(IDLIST(tomove)))))
540    {
541      /*move 'tomove' to ring id's*/
542      if (ipSwapId(tomove,IDROOT,currRing->idroot))
543      ipSwapId(tomove,basePack->idroot,currRing->idroot);
544    }
545    else
546    {
547      /*move 'tomove' to global id's*/
548      ipSwapId(tomove,currRing->idroot,IDROOT);
549    }
550  }
551}
552
553const char * piProcinfo(procinfov pi, const char *request)
554{
555  if(pi == NULL) return "empty proc";
556  else if (strcmp(request, "libname")  == 0) return pi->libname;
557  else if (strcmp(request, "procname") == 0) return pi->procname;
558  else if (strcmp(request, "type")     == 0)
559  {
560    switch (pi->language)
561    {
562      case LANG_SINGULAR: return "singular"; break;
563      case LANG_C:        return "object";   break;
564      case LANG_NONE:     return "none";     break;
565      default:            return "unknow language";
566    }
567  }
568  else if (strcmp(request, "ref")      == 0)
569  {
570    char p[8];
571    sprintf(p, "%d", pi->ref);
572    return omStrDup(p);  // MEMORY-LEAK
573  }
574  return "??";
575}
576
577void piCleanUp(procinfov pi)
578{
579  (pi->ref)--;
580  if (pi->ref <= 0)
581  {
582    if (pi->libname != NULL) // OB: ????
583      omFree((ADDRESS)pi->libname);
584    if (pi->procname != NULL) // OB: ????
585      omFree((ADDRESS)pi->procname);
586
587    if( pi->language == LANG_SINGULAR)
588    {
589      if (pi->data.s.body != NULL) // OB: ????
590        omFree((ADDRESS)pi->data.s.body);
591    }
592    if( pi->language == LANG_C)
593    {
594    }
595    memset((void *) pi, 0, sizeof(procinfo));
596    pi->language=LANG_NONE;
597  }
598}
599
600BOOLEAN piKill(procinfov pi)
601{
602  Voice *p=currentVoice;
603  while (p!=NULL)
604  {
605    if (p->pi==pi && pi->ref <= 1)
606    {
607      Warn("`%s` in use, can not be killed",pi->procname);
608      return TRUE;
609    }
610    p=p->next;
611  }
612  piCleanUp(pi);
613  if (pi->ref <= 0)
614    omFreeBin((ADDRESS)pi,  procinfo_bin);
615  return FALSE;
616}
617
618void paCleanUp(package pack)
619{
620  (pack->ref)--;
621  if (pack->ref < 0)
622  {
623#ifndef HAVE_STATIC
624    if( pack->language == LANG_C)
625    {
626      Print("//dlclose(%s)\n",pack->libname);
627#ifdef HAVE_DYNAMIC_LOADING
628      dynl_close (pack->handle);
629#endif /* HAVE_DYNAMIC_LOADING */
630    }
631#endif /* HAVE_STATIC */
632    omfree((ADDRESS)pack->libname);
633    memset((void *) pack, 0, sizeof(sip_package));
634    pack->language=LANG_NONE;
635  }
636}
637
638void proclevel::push(char *n)
639{
640  //Print("push %s\n",n);
641  proclevel *p=(proclevel*)omAlloc0(sizeof(proclevel));
642  p->cRing=currRing;
643  p->cRingHdl=currRingHdl;
644  p->name=n;
645  p->cPackHdl=currPackHdl;
646  p->cPack=currPack;
647  p->next=this;
648  procstack=p;
649}
650void proclevel::pop()
651{
652  //Print("pop %s\n",name);
653  //if (currRing!=::currRing) PrintS("currRing wrong\n");;
654  //::currRing=this->currRing;
655  //if (r==NULL) Print("set ring to NULL at lev %d(%s)\n",myynest,name);
656  //::currRingHdl=this->currRingHdl;
657  //if((::currRingHdl==NULL)||(IDRING(::currRingHdl)!=(::currRing)))
658  //  ::currRingHdl=rFindHdl(::currRing,NULL,NULL);
659  //Print("restore pack=%s,1.obj=%s\n",IDID(currPackHdl),IDID(currPack->idroot));
660  currPackHdl=this->cPackHdl;
661  currPack=this->cPack;
662  iiCheckPack(currPack);
663  proclevel *p=this;
664  procstack=next;
665  omFreeSize(p,sizeof(proclevel));
666}
667
668idhdl packFindHdl(package r)
669{
670  idhdl h=basePack->idroot;
671  while (h!=NULL)
672  {
673    if ((IDTYP(h)==PACKAGE_CMD)
674        && (IDPACKAGE(h)==r))
675      return h;
676    h=IDNEXT(h);
677  }
678  return NULL;
679}
Note: See TracBrowser for help on using the repository browser.