source: git/Singular/ipid.cc @ 207314f

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