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

Annotation of OpenXM_contrib2/asir2000/parse/pvar.c, Revision 1.19

1.2       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
1.3       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2       noro       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.19    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/parse/pvar.c,v 1.18 2006/06/21 09:46:06 noro Exp $
1.2       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
                     52:
                     53: NODE PVSS;
1.10      noro       54: extern char *CUR_FUNC;
1.12      noro       55: int gdef,mgdef,ldef;
                     56:
1.1       noro       57:
1.10      noro       58: void mkpvs(char *fname)
1.6       noro       59: {
1.1       noro       60:        VS pvs;
1.17      noro       61:        char *fullname,*buf;
1.12      noro       62:        FUNC f;
1.1       noro       63:
1.12      noro       64:        if ( CUR_MODULE ) {
                     65:                /* function must be declared in advance */
                     66:                searchf(CUR_MODULE->usrf_list,fname,&f);
                     67:                if ( !f ) {
1.17      noro       68:                        buf = ALLOCA(strlen("undeclared function "+strlen(fname)+10));
                     69:                        sprintf(buf,"undeclared function `%s'",fname);
                     70:                        yyerror(buf);
1.19    ! noro       71:                        error("cannot continue to read inputs");
1.12      noro       72:                }
                     73:        }
1.1       noro       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;
1.10      noro       79:
                     80:        /* XXX */
1.11      noro       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;
1.1       noro       88: }
                     89:
1.6       noro       90: void pushpvs(FUNC f)
1.1       noro       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:
1.7       noro      127: #define IS_LOCAL 0
                    128: #define IS_GLOBAL 1
                    129: #define IS_MGLOBAL 2
1.15      noro      130: #define IS_PATTERN 3
1.7       noro      131:
1.10      noro      132:
1.7       noro      133: unsigned int makepvar(char *str)
1.1       noro      134: {
1.12      noro      135:        int c,c1,created;
1.17      noro      136:        char *buf;
1.1       noro      137:
1.15      noro      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 */
1.9       noro      147:                c = getpvar(CPVS,str,0);
1.1       noro      148:                getpvar(EPVS,str,0);
1.10      noro      149:                if ( CUR_MODULE ) {
                    150:                        c1 = getpvar(CUR_MODULE->pvs,str,1);
1.13      noro      151:                        if ( c1 >= 0 ) goto CONFLICTION;
1.10      noro      152:                }
1.1       noro      153:                if ( CPVS != GPVS ) {
1.7       noro      154:                        /* inside function : we add the name to the global list */
1.1       noro      155:                        getpvar(GPVS,str,0);
1.7       noro      156:                        CPVS->va[c].attr = IS_GLOBAL;
                    157:                }
                    158:        } else if ( mgdef ) {
1.9       noro      159:                c = getpvar(CPVS,str,0);
1.7       noro      160:                getpvar(CUR_MODULE->pvs,str,0);
1.10      noro      161:                c1 = getpvar(EPVS,str,1);
1.13      noro      162:                if ( c1 >= 0 ) goto CONFLICTION;
1.7       noro      163:                if ( CPVS != GPVS ) {
                    164:                        /* inside function */
                    165:                        CPVS->va[c].attr = IS_MGLOBAL;
                    166:                }
1.12      noro      167:        } else if ( ldef > 0 ) {
                    168:                /* if ldef > 0, then local variables are being declared */
                    169:                c = getpvar(CPVS,str,0);
1.13      noro      170:                c1 = getpvar(EPVS,str,1);
1.14      noro      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;
1.13      noro      187:                }
1.7       noro      188:        } else if ( CPVS != GPVS ) {
                    189:                /* inside function */
1.12      noro      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:                }
1.7       noro      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);
1.12      noro      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);
1.7       noro      225:                                }
                    226:                                break;
1.1       noro      227:                }
1.9       noro      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.17      noro      237:                        buf = ALLOCA(strlen("undeclared variable"+strlen(str)+10));
                    238:                        sprintf(buf,"undeclared variable `%s'",str);
                    239:                        yyerror(buf);
1.9       noro      240:                }
                    241:        } else {
                    242:                /* outside function, outside module */
                    243:                c = getpvar(GPVS,str,0);
1.1       noro      244:        }
                    245:        return c;
1.13      noro      246:
                    247: CONFLICTION:
1.17      noro      248:        buf = ALLOCA(strlen("conflicting declarations for "+strlen(str)+10));
                    249:        sprintf(buf,"conflicting declarations for `%s'",str);
                    250:        yyerror(buf);
1.1       noro      251: }
                    252:
                    253: extern FUNC parse_targetf;
                    254:
1.6       noro      255: int searchpvar(char *str)
1.1       noro      256: {
                    257:        VS pvs;
1.7       noro      258:        MODULE mod;
                    259:        int c;
1.1       noro      260:
                    261:        if ( parse_targetf && parse_targetf->id != A_USR )
1.7       noro      262:                c = -1;
                    263:        else if ( parse_targetf ) {
                    264:                pvs = parse_targetf->f.usrf->pvs;
                    265:                mod = parse_targetf->f.usrf->module;
                    266:                if ( (c = getpvar(pvs,str,1)) >= 0 ) {
                    267:                        switch ( pvs->va[c].attr ) {
                    268:                                case IS_GLOBAL:
                    269:                                        c = getpvar(GPVS,str,1);
                    270:                                        c = PVGLOBAL((unsigned int)c);
                    271:                                        break;
                    272:                                case IS_MGLOBAL:
                    273:                                        c = getpvar(mod->pvs,str,1);
                    274:                                        c = PVMGLOBAL((unsigned int)c);
                    275:                                        break;
                    276:                                default:
                    277:                                        break;
                    278:                        }
                    279:                } else if ( mod && (c = getpvar(mod->pvs,str,1)) >= 0 )
                    280:                        c = PVMGLOBAL((unsigned int)c);
                    281:                else if ( (c = getpvar(GPVS,str,1)) >= 0 )
                    282:                        c = PVGLOBAL((unsigned int)c);
                    283:                else
                    284:                        c = -1;
                    285:        }
                    286:        return c;
1.1       noro      287: }
                    288:
1.6       noro      289: int getpvar(VS pvs,char *str,int searchonly)
1.1       noro      290: {
                    291:        struct oPV *va;
                    292:        PV v;
                    293:        int i;
                    294:
                    295:        for ( va = pvs->va, i = 0; i < (int)pvs->n; i++ )
                    296:                if ( va[i].name && !strcmp(va[i].name,str) )
                    297:                        return i;
                    298:        if ( searchonly )
                    299:                return -1;
                    300:        if ( pvs->asize == pvs->n )
                    301:                reallocarray((char **)&pvs->va,(int *)&pvs->asize,(int *)&pvs->n,(int)sizeof(struct oPV));
                    302:        v = &pvs->va[pvs->n];
                    303:        NAME(v) = (char *)CALLOC(strlen(str)+1,sizeof(char));
                    304:        strcpy(NAME(v),str); v->priv = 0;
1.7       noro      305:        v->attr= IS_LOCAL; v->type = -1; i = pvs->n; pvs->n++;
1.1       noro      306:        return i;
                    307: }
                    308:
                    309: #if defined(VISUAL)
                    310: #define PCLOSE _pclose
                    311: #else
                    312: #define PCLOSE pclose
                    313: #endif
                    314:
                    315: void closecurrentinput()
                    316: {
                    317:        if ( asir_infile && !asir_infile->fp )
                    318:                return;
                    319:
1.5       noro      320: #if defined(VISUAL)
1.1       noro      321:        fclose(asir_infile->fp);
                    322:        unlink(asir_infile->tname);
                    323: #else
1.16      noro      324:        if ( asir_infile->fp != stdin )
                    325:                PCLOSE(asir_infile->fp);
1.1       noro      326: #endif
                    327:        asir_infile = NEXT(asir_infile);
                    328: }
                    329:
                    330: void resetpvs()
                    331: {
1.16      noro      332:        if ( asir_infile && !NEXT(asir_infile) ) {
1.8       noro      333:                PVSS = 0; CPVS = GPVS; MPVS = 0; CUR_MODULE = 0; nextbp = 0;
1.12      noro      334:                gdef = mgdef = ldef = 0;
1.1       noro      335:                if ( EPVS->va ) {
                    336:                        bzero((char *)EPVS->va,(int)(EPVS->asize*sizeof(struct oPV))); EPVS->n = 0;
                    337:                }
                    338:        }
                    339: }
                    340:
                    341: static NODE saved_PVSS;
                    342: static VS saved_CPVS;
                    343: static int saved_nextbp, saved_nextbplevel;
                    344:
                    345: void savepvs()
                    346: {
                    347:        saved_PVSS = PVSS;
                    348:        saved_CPVS = CPVS;
                    349:        saved_nextbp = nextbp;
                    350:        saved_nextbplevel = nextbplevel;
                    351: }
                    352:
                    353: void restorepvs()
                    354: {
                    355:        PVSS = saved_PVSS;
                    356:        CPVS = saved_CPVS;
                    357:        nextbp = saved_nextbp;
                    358:        nextbplevel = saved_nextbplevel;
                    359: }
                    360:
1.6       noro      361: void storeans(pointer p)
1.1       noro      362: {
                    363:        if ( APVS->asize == APVS->n )
                    364:                reallocarray((char **)&APVS->va,(int *)&APVS->asize,(int *)&APVS->n,(int)sizeof(struct oPV));
                    365:        APVS->va[APVS->n++].priv = p;
                    366: }

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