[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.13

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.13    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/parse/pvar.c,v 1.12 2003/05/20 06:15:01 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.11      noro       61:        char *fullname;
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 ) {
                     68:                        fprintf(stderr,"\"%s\", near line %d: undeclared function `%s'",
                     69:                                asir_infile->name,asir_infile->ln,fname);
                     70:                        error("");
                     71:                }
                     72:        }
1.1       noro       73:        pvs = (VS)MALLOC(sizeof(struct oVS));
                     74:        pvs->va = (struct oPV *)MALLOC(DEFSIZE*sizeof(struct oPV));
                     75:        pvs->n = 0;
                     76:        pvs->asize=DEFSIZE;
                     77:        CPVS = pvs;
1.10      noro       78:
                     79:        /* XXX */
1.11      noro       80:        if ( CUR_MODULE ) {
                     81:                fullname =
                     82:                        (char *)MALLOC_ATOMIC(strlen(CUR_MODULE->name)+strlen(fname)+1);
                     83:                sprintf(fullname,"%s.%s",CUR_MODULE->name,fname);
                     84:                CUR_FUNC = fullname;
                     85:        } else
                     86:                CUR_FUNC = fname;
1.1       noro       87: }
                     88:
1.6       noro       89: void pushpvs(FUNC f)
1.1       noro       90: {
                     91:        VS pvs;
                     92:        NODE node;
                     93:        int level;
                     94:        extern int evalstatline;
                     95:
                     96:        pvs = f->f.usrf->pvs;
                     97:        if ( PVSS ) {
                     98:                ((VS)BDY(PVSS))->at = evalstatline;
                     99:                level = ((VS)BDY(PVSS))->level+1;
                    100:        } else
                    101:                level = 1;
                    102:        MKNODE(node,pvs,PVSS);
                    103:        PVSS = node;
                    104:        CPVS = (VS)MALLOC(sizeof(struct oVS)); BDY(PVSS) = (pointer)CPVS;
                    105:        CPVS->usrf = f;
                    106:        CPVS->n = CPVS->asize = pvs->n;
                    107:        CPVS->level = level;
                    108:        if ( CPVS->n ) {
                    109:                CPVS->va = (struct oPV *)MALLOC(CPVS->n*sizeof(struct oPV));
                    110:                bcopy((char *)pvs->va,(char *)CPVS->va,(int)(pvs->n*sizeof(struct oPV)));
                    111:        }
                    112:        if ( nextbp )
                    113:                nextbplevel++;
                    114: }
                    115:
                    116: void poppvs() {
                    117:        PVSS = NEXT(PVSS);
                    118:        if ( PVSS )
                    119:                CPVS = (VS)BDY(PVSS);
                    120:        else
                    121:                CPVS = GPVS;
                    122:        if ( nextbp )
                    123:                nextbplevel--;
                    124: }
                    125:
1.7       noro      126: #define IS_LOCAL 0
                    127: #define IS_GLOBAL 1
                    128: #define IS_MGLOBAL 2
                    129:
1.10      noro      130:
1.7       noro      131: unsigned int makepvar(char *str)
1.1       noro      132: {
1.12      noro      133:        int c,c1,created;
1.1       noro      134:
1.7       noro      135:        /* EPVS : global list of the current file */
                    136:        /* add to the local variable list */
1.1       noro      137:        if ( gdef ) {
1.7       noro      138:                /* add to the external variable list */
1.9       noro      139:                c = getpvar(CPVS,str,0);
1.1       noro      140:                getpvar(EPVS,str,0);
1.10      noro      141:                if ( CUR_MODULE ) {
                    142:                        c1 = getpvar(CUR_MODULE->pvs,str,1);
1.13    ! noro      143:                        if ( c1 >= 0 ) goto CONFLICTION;
1.10      noro      144:                }
1.1       noro      145:                if ( CPVS != GPVS ) {
1.7       noro      146:                        /* inside function : we add the name to the global list */
1.1       noro      147:                        getpvar(GPVS,str,0);
1.7       noro      148:                        CPVS->va[c].attr = IS_GLOBAL;
                    149:                }
                    150:        } else if ( mgdef ) {
1.9       noro      151:                c = getpvar(CPVS,str,0);
1.7       noro      152:                getpvar(CUR_MODULE->pvs,str,0);
1.10      noro      153:                c1 = getpvar(EPVS,str,1);
1.13    ! noro      154:                if ( c1 >= 0 ) goto CONFLICTION;
1.7       noro      155:                if ( CPVS != GPVS ) {
                    156:                        /* inside function */
                    157:                        CPVS->va[c].attr = IS_MGLOBAL;
                    158:                }
1.12      noro      159:        } else if ( ldef > 0 ) {
                    160:                /* if ldef > 0, then local variables are being declared */
                    161:                c = getpvar(CPVS,str,0);
1.13    ! noro      162:                c1 = getpvar(EPVS,str,1);
        !           163:                if ( c1 >= 0 ) goto CONFLICTION;
        !           164:                if ( CUR_MODULE ) {
        !           165:                        c1 = getpvar(CUR_MODULE->pvs,str,1);
        !           166:                }
        !           167:                if ( c1 >= 0 ) goto CONFLICTION;
1.12      noro      168:                CPVS->va[c].attr = IS_LOCAL;
1.7       noro      169:        } else if ( CPVS != GPVS ) {
                    170:                /* inside function */
1.12      noro      171:                if ( CUR_MODULE ) {
                    172:                        /* search only */
                    173:                        c = getpvar(CPVS,str,1);
                    174:                        if ( c < 0 ) {
                    175:                                c = getpvar(CPVS,str,0);
                    176:                                created = 1;
                    177:                        } else
                    178:                                created = 0;
                    179:                } else {
                    180:                        /* may be created */
                    181:                        c = getpvar(CPVS,str,0);
                    182:                }
1.7       noro      183:                switch ( CPVS->va[c].attr ) {
                    184:                        case IS_GLOBAL:
                    185:                                c1 = getpvar(GPVS,str,1); c = PVGLOBAL((unsigned int)c1);
                    186:                                break;
                    187:                        case IS_MGLOBAL:
                    188:                                c1 = getpvar(CUR_MODULE->pvs,str,1); c = PVMGLOBAL((unsigned int)c1);
                    189:                                break;
                    190:                        case IS_LOCAL:
                    191:                        default:
                    192:                                if ( CUR_MODULE &&
                    193:                                        ((c1 = getpvar(CUR_MODULE->pvs,str,1)) >= 0) ) {
                    194:                                        CPVS->va[c].attr = IS_MGLOBAL;
                    195:                                        c = PVMGLOBAL((unsigned int)c1);
                    196:                                } else if ( getpvar(EPVS,str,1) >= 0 ) {
                    197:                                        CPVS->va[c].attr = IS_GLOBAL;
                    198:                                        c1 = getpvar(GPVS,str,1); c = PVGLOBAL((unsigned int)c1);
1.12      noro      199:                                } else if ( CUR_MODULE && created && (ldef == 0) ) {
                    200:                                        /* not declared */
                    201:                                        /* if ldef == 0, at least one local variables has been
                    202:                                           declared */
                    203:                                        fprintf(stderr,
                    204:                                                "Warning: \"%s\", near line %d: undeclared local variable `%s'\n",
                    205:                                                asir_infile->name,asir_infile->ln,str);
1.7       noro      206:                                }
                    207:                                break;
1.1       noro      208:                }
1.9       noro      209:        } else if ( CUR_MODULE ) {
                    210:                /* outside function, inside module */
                    211:                if ( (c = getpvar(CUR_MODULE->pvs,str,1)) >= 0 )
                    212:                        c = PVMGLOBAL((unsigned int)c);
                    213:                else if ( getpvar(EPVS,str,1) >= 0 ) {
                    214:                        c = getpvar(GPVS,str,1);
                    215:                        c = PVGLOBAL((unsigned int)c);
                    216:                } else {
                    217:                        /* not declared as static or extern */
1.10      noro      218:                        fprintf(stderr,"\"%s\", near line %d: undeclared variable `%s'",
                    219:                                asir_infile->name,asir_infile->ln,str);
                    220:                        error("");
1.9       noro      221:                }
                    222:        } else {
                    223:                /* outside function, outside module */
                    224:                c = getpvar(GPVS,str,0);
1.1       noro      225:        }
                    226:        return c;
1.13    ! noro      227:
        !           228: CONFLICTION:
        !           229:        fprintf(stderr,"\"%s\", near line %d: conflicting declarations for `%s'",
        !           230:                asir_infile->name,asir_infile->ln,str);
        !           231:        error("");
1.1       noro      232: }
                    233:
                    234: extern FUNC parse_targetf;
                    235:
1.6       noro      236: int searchpvar(char *str)
1.1       noro      237: {
                    238:        VS pvs;
1.7       noro      239:        MODULE mod;
                    240:        int c;
1.1       noro      241:
                    242:        if ( parse_targetf && parse_targetf->id != A_USR )
1.7       noro      243:                c = -1;
                    244:        else if ( parse_targetf ) {
                    245:                pvs = parse_targetf->f.usrf->pvs;
                    246:                mod = parse_targetf->f.usrf->module;
                    247:                if ( (c = getpvar(pvs,str,1)) >= 0 ) {
                    248:                        switch ( pvs->va[c].attr ) {
                    249:                                case IS_GLOBAL:
                    250:                                        c = getpvar(GPVS,str,1);
                    251:                                        c = PVGLOBAL((unsigned int)c);
                    252:                                        break;
                    253:                                case IS_MGLOBAL:
                    254:                                        c = getpvar(mod->pvs,str,1);
                    255:                                        c = PVMGLOBAL((unsigned int)c);
                    256:                                        break;
                    257:                                default:
                    258:                                        break;
                    259:                        }
                    260:                } else if ( mod && (c = getpvar(mod->pvs,str,1)) >= 0 )
                    261:                        c = PVMGLOBAL((unsigned int)c);
                    262:                else if ( (c = getpvar(GPVS,str,1)) >= 0 )
                    263:                        c = PVGLOBAL((unsigned int)c);
                    264:                else
                    265:                        c = -1;
                    266:        }
                    267:        return c;
1.1       noro      268: }
                    269:
1.6       noro      270: int getpvar(VS pvs,char *str,int searchonly)
1.1       noro      271: {
                    272:        struct oPV *va;
                    273:        PV v;
                    274:        int i;
                    275:
                    276:        for ( va = pvs->va, i = 0; i < (int)pvs->n; i++ )
                    277:                if ( va[i].name && !strcmp(va[i].name,str) )
                    278:                        return i;
                    279:        if ( searchonly )
                    280:                return -1;
                    281:        if ( pvs->asize == pvs->n )
                    282:                reallocarray((char **)&pvs->va,(int *)&pvs->asize,(int *)&pvs->n,(int)sizeof(struct oPV));
                    283:        v = &pvs->va[pvs->n];
                    284:        NAME(v) = (char *)CALLOC(strlen(str)+1,sizeof(char));
                    285:        strcpy(NAME(v),str); v->priv = 0;
1.7       noro      286:        v->attr= IS_LOCAL; v->type = -1; i = pvs->n; pvs->n++;
1.1       noro      287:        return i;
                    288: }
                    289:
                    290: #if defined(VISUAL)
                    291: #define PCLOSE _pclose
                    292: #else
                    293: #define PCLOSE pclose
                    294: #endif
                    295:
                    296: void closecurrentinput()
                    297: {
                    298:        if ( asir_infile && !asir_infile->fp )
                    299:                return;
                    300:
1.5       noro      301: #if defined(VISUAL)
1.1       noro      302:        fclose(asir_infile->fp);
                    303:        unlink(asir_infile->tname);
                    304: #else
                    305:        PCLOSE(asir_infile->fp);
                    306: #endif
                    307:        asir_infile = NEXT(asir_infile);
                    308:        resetpvs();
                    309: }
                    310:
                    311: void resetpvs()
                    312: {
                    313:        if ( !NEXT(asir_infile) ) {
1.8       noro      314:                PVSS = 0; CPVS = GPVS; MPVS = 0; CUR_MODULE = 0; nextbp = 0;
1.12      noro      315:                gdef = mgdef = ldef = 0;
1.1       noro      316:                if ( EPVS->va ) {
                    317:                        bzero((char *)EPVS->va,(int)(EPVS->asize*sizeof(struct oPV))); EPVS->n = 0;
                    318:                }
                    319:        }
                    320: }
                    321:
                    322: static NODE saved_PVSS;
                    323: static VS saved_CPVS;
                    324: static int saved_nextbp, saved_nextbplevel;
                    325:
                    326: void savepvs()
                    327: {
                    328:        saved_PVSS = PVSS;
                    329:        saved_CPVS = CPVS;
                    330:        saved_nextbp = nextbp;
                    331:        saved_nextbplevel = nextbplevel;
                    332: }
                    333:
                    334: void restorepvs()
                    335: {
                    336:        PVSS = saved_PVSS;
                    337:        CPVS = saved_CPVS;
                    338:        nextbp = saved_nextbp;
                    339:        nextbplevel = saved_nextbplevel;
                    340: }
                    341:
1.6       noro      342: void storeans(pointer p)
1.1       noro      343: {
                    344:        if ( APVS->asize == APVS->n )
                    345:                reallocarray((char **)&APVS->va,(int *)&APVS->asize,(int *)&APVS->n,(int)sizeof(struct oPV));
                    346:        APVS->va[APVS->n++].priv = p;
                    347: }

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