[BACK]Return to pvar.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2018 / parse

Annotation of OpenXM_contrib2/asir2018/parse/pvar.c, Revision 1.3

1.1       noro        1: /*
                      2:  * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
                      3:  * All rights reserved.
                      4:  *
                      5:  * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
                      6:  * non-exclusive and royalty-free license to use, copy, modify and
                      7:  * redistribute, solely for non-commercial and non-profit purposes, the
                      8:  * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
                      9:  * conditions of this Agreement. For the avoidance of doubt, you acquire
                     10:  * only a limited right to use the SOFTWARE hereunder, and FLL or any
                     11:  * third party developer retains all rights, including but not limited to
                     12:  * copyrights, in and to the SOFTWARE.
                     13:  *
                     14:  * (1) FLL does not grant you a license in any way for commercial
                     15:  * purposes. You may use the SOFTWARE only for non-commercial and
                     16:  * non-profit purposes only, such as academic, research and internal
                     17:  * business use.
                     18:  * (2) The SOFTWARE is protected by the Copyright Law of Japan and
                     19:  * international copyright treaties. If you make copies of the SOFTWARE,
                     20:  * with or without modification, as permitted hereunder, you shall affix
                     21:  * to all such copies of the SOFTWARE the above copyright notice.
                     22:  * (3) An explicit reference to this SOFTWARE and its copyright owner
                     23:  * shall be made on your publication or presentation in any form of the
                     24:  * results obtained by use of the SOFTWARE.
                     25:  * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
                     26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
                     27:  * for such modification or the source code of the modified part of the
                     28:  * SOFTWARE.
                     29:  *
                     30:  * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
                     31:  * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
                     32:  * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
                     33:  * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
                     34:  * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
                     35:  * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
                     36:  * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
                     37:  * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
                     38:  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
                     39:  * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
                     40:  * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
                     41:  * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
                     42:  * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
                     44:  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
                     45:  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
                     46:  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
                     47:  *
1.3     ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2018/parse/pvar.c,v 1.2 2020/09/27 04:35:05 noro Exp $
1.1       noro       49: */
                     50: #include "ca.h"
                     51: #include "parse.h"
                     52:
                     53: NODE PVSS;
                     54: extern char *CUR_FUNC;
                     55: int gdef,mgdef,ldef;
                     56:
                     57:
                     58: void mkpvs(char *fname)
                     59: {
                     60:   VS pvs;
                     61:   char *fullname,*buf;
                     62:   FUNC f;
                     63:
                     64:   if ( CUR_MODULE ) {
                     65:     /* function must be declared in advance */
                     66:     searchf(CUR_MODULE->usrf_list,fname,&f);
                     67:     if ( !f ) {
1.2       noro       68:       buf = ALLOCA(strlen("undeclared function ")+strlen(fname)+10);
1.1       noro       69:       sprintf(buf,"undeclared function `%s'",fname);
                     70:       yyerror(buf);
                     71:       error("cannot continue to read inputs");
                     72:     }
                     73:   }
                     74:   pvs = (VS)MALLOC(sizeof(struct oVS));
                     75:   pvs->va = (struct oPV *)MALLOC(DEFSIZE*sizeof(struct oPV));
                     76:   pvs->n = 0;
                     77:   pvs->asize=DEFSIZE;
                     78:   CPVS = pvs;
                     79:
                     80:   /* XXX */
                     81:   if ( CUR_MODULE ) {
                     82:     fullname =
                     83:       (char *)MALLOC_ATOMIC(strlen(CUR_MODULE->name)+strlen(fname)+1);
                     84:     sprintf(fullname,"%s.%s",CUR_MODULE->name,fname);
                     85:     CUR_FUNC = fullname;
                     86:   } else
                     87:     CUR_FUNC = fname;
                     88: }
                     89:
                     90: void pushpvs(FUNC f)
                     91: {
                     92:   VS pvs;
                     93:   NODE node;
                     94:   int level;
                     95:   extern int evalstatline;
                     96:
                     97:   pvs = f->f.usrf->pvs;
                     98:   if ( PVSS ) {
                     99:     ((VS)BDY(PVSS))->at = evalstatline;
                    100:     level = ((VS)BDY(PVSS))->level+1;
                    101:   } else
                    102:     level = 1;
                    103:   MKNODE(node,pvs,PVSS);
                    104:   PVSS = node;
                    105:   CPVS = (VS)MALLOC(sizeof(struct oVS)); BDY(PVSS) = (pointer)CPVS;
                    106:   CPVS->usrf = f;
                    107:   CPVS->n = CPVS->asize = pvs->n;
                    108:   CPVS->level = level;
                    109:   if ( CPVS->n ) {
                    110:     CPVS->va = (struct oPV *)MALLOC(CPVS->n*sizeof(struct oPV));
                    111:     bcopy((char *)pvs->va,(char *)CPVS->va,(int)(pvs->n*sizeof(struct oPV)));
                    112:   }
                    113:   if ( nextbp )
                    114:     nextbplevel++;
                    115: }
                    116:
                    117: void poppvs() {
                    118:   PVSS = NEXT(PVSS);
                    119:   if ( PVSS )
                    120:     CPVS = (VS)BDY(PVSS);
                    121:   else
                    122:     CPVS = GPVS;
                    123:   if ( nextbp )
                    124:     nextbplevel--;
                    125: }
                    126:
                    127: #define IS_LOCAL 0
                    128: #define IS_GLOBAL 1
                    129: #define IS_MGLOBAL 2
                    130: #define IS_PATTERN 3
                    131:
                    132:
                    133: unsigned int makepvar(char *str)
                    134: {
                    135:   int c,c1,created;
                    136:   char *buf;
                    137:
                    138:   if ( str[0] == '_' ) {
                    139:     /* pattern variable */
                    140:     c1 = getpvar(PPVS,str,0);
                    141:     c = PVPATTERN((unsigned int)c1);
                    142:     PPVS->va[c1].attr = IS_PATTERN;
                    143:   } else if ( gdef ) {
                    144:     /* EPVS : global list of the current file */
                    145:     /* add to the local variable list */
                    146:     /* also add to the external variable list */
                    147:     c = getpvar(CPVS,str,0);
                    148:     getpvar(EPVS,str,0);
                    149:     if ( CUR_MODULE ) {
                    150:       c1 = getpvar(CUR_MODULE->pvs,str,1);
                    151:       if ( c1 >= 0 ) goto CONFLICTION;
                    152:     }
                    153:     if ( CPVS != GPVS ) {
                    154:       /* inside function : we add the name to the global list */
                    155:       getpvar(GPVS,str,0);
                    156:       CPVS->va[c].attr = IS_GLOBAL;
                    157:     }
                    158:   } else if ( mgdef ) {
                    159:     c = getpvar(CPVS,str,0);
                    160:     getpvar(CUR_MODULE->pvs,str,0);
                    161:     c1 = getpvar(EPVS,str,1);
                    162:     if ( c1 >= 0 ) goto CONFLICTION;
                    163:     if ( CPVS != GPVS ) {
                    164:       /* inside function */
                    165:       CPVS->va[c].attr = IS_MGLOBAL;
                    166:     }
                    167:   } else if ( ldef > 0 ) {
                    168:     /* if ldef > 0, then local variables are being declared */
                    169:     c = getpvar(CPVS,str,0);
                    170:     c1 = getpvar(EPVS,str,1);
                    171:     if ( c1 >= 0 ) {
                    172:       if ( CUR_MODULE )
                    173:         goto CONFLICTION;
                    174:       else {
                    175:         fprintf(stderr,"Warning: \"%s\", near line %d: conflicting declarations for `%s'\n",
                    176:           asir_infile->name,asir_infile->ln,str);
                    177:         fprintf(stderr,"         `%s' is bound to the global variable\n",str);
                    178:         CPVS->va[c].attr = IS_GLOBAL;
                    179:         c1 = getpvar(GPVS,str,1); c = PVGLOBAL((unsigned int)c1);
                    180:       }
                    181:     } else {
                    182:       if ( CUR_MODULE ) {
                    183:         c1 = getpvar(CUR_MODULE->pvs,str,1);
                    184:       }
                    185:       if ( c1 >= 0 ) goto CONFLICTION;
                    186:       CPVS->va[c].attr = IS_LOCAL;
                    187:     }
                    188:   } else if ( CPVS != GPVS ) {
                    189:     /* inside function */
                    190:     if ( CUR_MODULE ) {
                    191:       /* search only */
                    192:       c = getpvar(CPVS,str,1);
                    193:       if ( c < 0 ) {
                    194:         c = getpvar(CPVS,str,0);
                    195:         created = 1;
                    196:       } else
                    197:         created = 0;
                    198:     } else {
                    199:       /* may be created */
                    200:       c = getpvar(CPVS,str,0);
                    201:     }
                    202:     switch ( CPVS->va[c].attr ) {
                    203:       case IS_GLOBAL:
                    204:         c1 = getpvar(GPVS,str,1); c = PVGLOBAL((unsigned int)c1);
                    205:         break;
                    206:       case IS_MGLOBAL:
                    207:         c1 = getpvar(CUR_MODULE->pvs,str,1); c = PVMGLOBAL((unsigned int)c1);
                    208:         break;
                    209:       case IS_LOCAL:
                    210:       default:
                    211:         if ( CUR_MODULE &&
                    212:           ((c1 = getpvar(CUR_MODULE->pvs,str,1)) >= 0) ) {
                    213:           CPVS->va[c].attr = IS_MGLOBAL;
                    214:           c = PVMGLOBAL((unsigned int)c1);
                    215:         } else if ( getpvar(EPVS,str,1) >= 0 ) {
                    216:           CPVS->va[c].attr = IS_GLOBAL;
                    217:           c1 = getpvar(GPVS,str,1); c = PVGLOBAL((unsigned int)c1);
                    218:         } else if ( CUR_MODULE && created && (ldef == 0) ) {
                    219:           /* not declared */
                    220:           /* if ldef == 0, at least one local variables has been
                    221:              declared */
                    222:           fprintf(stderr,
                    223:             "Warning: \"%s\", near line %d: undeclared local variable `%s'\n",
                    224:             asir_infile->name,asir_infile->ln,str);
                    225:         }
                    226:         break;
                    227:     }
                    228:   } else if ( CUR_MODULE ) {
                    229:     /* outside function, inside module */
                    230:     if ( (c = getpvar(CUR_MODULE->pvs,str,1)) >= 0 )
                    231:       c = PVMGLOBAL((unsigned int)c);
                    232:     else if ( getpvar(EPVS,str,1) >= 0 ) {
                    233:       c = getpvar(GPVS,str,1);
                    234:       c = PVGLOBAL((unsigned int)c);
                    235:     } else {
                    236:       /* not declared as static or extern */
1.3     ! noro      237:       buf = ALLOCA(strlen("undeclared variable")+strlen(str)+10);
1.1       noro      238:       sprintf(buf,"undeclared variable `%s'",str);
                    239:       yyerror(buf);
                    240:     }
                    241:   } else {
                    242:     /* outside function, outside module */
                    243:     c = getpvar(GPVS,str,0);
                    244:   }
                    245:   return c;
                    246:
                    247: CONFLICTION:
1.3     ! noro      248:   buf = ALLOCA(strlen("conflicting declarations for ")+strlen(str)+10);
1.1       noro      249:   sprintf(buf,"conflicting declarations for `%s'",str);
                    250:   yyerror(buf);
1.3     ! noro      251:   return 0;
1.1       noro      252: }
                    253:
                    254: extern FUNC parse_targetf;
                    255:
                    256: int searchpvar(char *str)
                    257: {
                    258:   VS pvs;
                    259:   MODULE mod;
                    260:   int c;
                    261:
                    262:   if ( parse_targetf && parse_targetf->id != A_USR )
                    263:     c = -1;
                    264:   else if ( parse_targetf ) {
                    265:     pvs = parse_targetf->f.usrf->pvs;
                    266:     mod = parse_targetf->f.usrf->module;
                    267:     if ( (c = getpvar(pvs,str,1)) >= 0 ) {
                    268:       switch ( pvs->va[c].attr ) {
                    269:         case IS_GLOBAL:
                    270:           c = getpvar(GPVS,str,1);
                    271:           c = PVGLOBAL((unsigned int)c);
                    272:           break;
                    273:         case IS_MGLOBAL:
                    274:           c = getpvar(mod->pvs,str,1);
                    275:           c = PVMGLOBAL((unsigned int)c);
                    276:           break;
                    277:         default:
                    278:           break;
                    279:       }
                    280:     } else if ( mod && (c = getpvar(mod->pvs,str,1)) >= 0 )
                    281:       c = PVMGLOBAL((unsigned int)c);
                    282:     else if ( (c = getpvar(GPVS,str,1)) >= 0 )
                    283:       c = PVGLOBAL((unsigned int)c);
                    284:     else
                    285:       c = -1;
                    286:   }
                    287:   return c;
                    288: }
                    289:
                    290: int getpvar(VS pvs,char *str,int searchonly)
                    291: {
                    292:   struct oPV *va;
                    293:   PV v;
                    294:   int i;
                    295:
                    296:   for ( va = pvs->va, i = 0; i < (int)pvs->n; i++ )
                    297:     if ( va[i].name && !strcmp(va[i].name,str) )
                    298:       return i;
                    299:   if ( searchonly )
                    300:     return -1;
                    301:   if ( pvs->asize == pvs->n )
                    302:     asir_reallocarray((char **)&pvs->va,(int *)&pvs->asize,(int *)&pvs->n,(int)sizeof(struct oPV));
                    303:   v = &pvs->va[pvs->n];
                    304:   NAME(v) = (char *)CALLOC(strlen(str)+1,sizeof(char));
                    305:   strcpy(NAME(v),str); v->priv = 0;
                    306:   v->attr= IS_LOCAL; v->type = -1; i = pvs->n; pvs->n++;
                    307:   return i;
                    308: }
                    309:
                    310: #if defined(VISUAL) || defined(__MINGW32__)
                    311: #define PCLOSE _pclose
                    312: #else
                    313: #define PCLOSE pclose
                    314: #endif
                    315:
                    316: void closecurrentinput()
                    317: {
                    318:   if ( asir_infile && !asir_infile->fp )
                    319:     return;
                    320:
                    321: #if defined(VISUAL) || defined(__MINGW32__)
                    322:   fclose(asir_infile->fp);
                    323:   unlink(asir_infile->tname);
                    324: #else
                    325:   if ( asir_infile->fp != stdin )
                    326:     PCLOSE(asir_infile->fp);
                    327: #endif
                    328:   asir_infile = NEXT(asir_infile);
                    329: }
                    330:
                    331: void resetpvs()
                    332: {
                    333:   if ( asir_infile && !NEXT(asir_infile) ) {
                    334:     PVSS = 0; CPVS = GPVS; MPVS = 0; CUR_MODULE = 0; nextbp = 0;
                    335:     gdef = mgdef = ldef = 0;
                    336:     if ( EPVS->va ) {
                    337:       bzero((char *)EPVS->va,(int)(EPVS->asize*sizeof(struct oPV))); EPVS->n = 0;
                    338:     }
                    339:   }
                    340: }
                    341:
                    342: static NODE saved_PVSS;
                    343: static VS saved_CPVS;
                    344: static int saved_nextbp, saved_nextbplevel;
                    345:
                    346: void savepvs()
                    347: {
                    348:   saved_PVSS = PVSS;
                    349:   saved_CPVS = CPVS;
                    350:   saved_nextbp = nextbp;
                    351:   saved_nextbplevel = nextbplevel;
                    352: }
                    353:
                    354: void restorepvs()
                    355: {
                    356:   PVSS = saved_PVSS;
                    357:   CPVS = saved_CPVS;
                    358:   nextbp = saved_nextbp;
                    359:   nextbplevel = saved_nextbplevel;
                    360: }
                    361:
                    362: void storeans(pointer p)
                    363: {
                    364:   if ( APVS->asize == APVS->n )
                    365:     asir_reallocarray((char **)&APVS->va,(int *)&APVS->asize,(int *)&APVS->n,(int)sizeof(struct oPV));
                    366:   APVS->va[APVS->n++].priv = p;
                    367: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>