source: git/Singular/ipid.cc @ fe9ee1

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