source: git/Singular/ipid.cc @ f5a3a23

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