source: git/Singular/ipid.cc @ 8c982c3

fieker-DuValspielwiese
Last change on this file since 8c982c3 was 8c982c3, checked in by Hans Schoenemann <hannes@…>, 11 years ago
fix: cannot use class in C files
  • 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));
50omBin char_ptr_bin =  omGetSpecBin(sizeof(char*));
51
52coeffs coeffs_BIGINT;
53
54FILE   *feFilePending; /*temp. storage for grammar.y */
55
56proclevel *procstack=NULL;
57#define TEST
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    case INTVEC_CMD:
137    case INTMAT_CMD:
138      return (void *)new intvec();
139    case BIGINTMAT_CMD:
140      return (void *)new bigintmat();
141    case NUMBER_CMD:
142      return (void *) nInit(0);
143    case BIGINT_CMD:
144      return (void *) n_Init(0, coeffs_BIGINT);
145    case IDEAL_CMD:
146    case MODUL_CMD:
147    case MATRIX_CMD:
148      return (void*) idInit(1,1);
149    case MAP_CMD:
150    {
151      map m = (map)idInit(1,1);
152      m->preimage = omStrDup(IDID(currRingHdl));
153      return (void *)m;
154    }
155    case STRING_CMD:
156      return (void *)omAlloc0(1);
157    case LIST_CMD:
158    {
159      lists l=(lists)omAllocBin(slists_bin);
160      l->Init();
161      return (void*)l;
162    }
163    //the types with the standard init: set the struct to zero
164    case LINK_CMD:
165      return (void*) omAlloc0Bin(sip_link_bin);
166    case RING_CMD:
167      return (void*) omAlloc0Bin(sip_sring_bin);
168    case PACKAGE_CMD:
169      return (void*) omAlloc0Bin(sip_package_bin);
170    case PROC_CMD:
171      return (void *) omAlloc0Bin(procinfo_bin);
172    case RESOLUTION_CMD:
173      return  (void *)omAlloc0(sizeof(ssyStrategy));
174    //other types: without init (int,script,poly,def,package)
175    case INT_CMD:
176    case DEF_CMD:
177    case POLY_CMD:
178    case VECTOR_CMD:
179    case QRING_CMD:
180       return (void*)0L;
181    default:
182      {
183        if (t>MAX_TOK)
184        {
185#ifdef BLACKBOX_DEVEL
186          Print("bb-type %d\n",t);
187#endif
188          blackbox *bb=getBlackboxStuff(t);
189          if (bb!=NULL)
190             return (void *)bb->blackbox_Init(bb);
191        }
192        else
193          Werror("unknown type %d",t);
194        break;
195      }
196  }
197  return (void *)0L;
198}
199idhdl idrec::set(const char * s, int level, int t, BOOLEAN init)
200{
201  //printf("define %s, %x, level: %d, typ: %d\n", s,s,level,t);
202  idhdl h = (idrec *)omAlloc0Bin(idrec_bin);
203  IDID(h)   = s;
204  IDTYP(h)  = t;
205  IDLEV(h)  = level;
206  IDNEXT(h) = this;
207  BOOLEAN at_start=(this==IDROOT);
208  h->id_i=iiS2I(s);
209  if (init)
210  {
211    if ((t==IDEAL_CMD)||(t==MODUL_CMD))
212      IDFLAG(h) = Sy_bit(FLAG_STD);
213    IDSTRING(h)=(char *)idrecDataInit(t);
214    // additional settings:--------------------------------------
215#if 0
216    // this leads to a memory leak
217    if (t == QRING_CMD)
218    {
219      // IDRING(h)=rCopy(currRing);
220      /* QRING_CMD is ring dep => currRing !=NULL */
221    }
222    else
223#endif
224    if (t == PROC_CMD)
225    {
226      IDPROC(h)->language=LANG_NONE;
227    }
228    else if (t == PACKAGE_CMD)
229    {
230      IDPACKAGE(h)->language=LANG_NONE;
231      IDPACKAGE(h)->loaded = FALSE;
232    }
233  }
234  // --------------------------------------------------------
235  if (at_start)
236    IDNEXT(h) = IDROOT;
237  return  h;
238}
239
240char * idrec::String(BOOLEAN typed)
241{
242  sleftv tmp;
243  memset(&tmp,0,sizeof(sleftv));
244  tmp.rtyp=IDTYP(this);
245  tmp.data=IDDATA(this);
246  tmp.name=IDID(this);
247  return tmp.String(NULL, typed);
248}
249
250idhdl enterid(const char * s, int lev, int t, idhdl* root, BOOLEAN init, BOOLEAN search)
251{
252  if (s==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 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
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.