Annotation of OpenXM_contrib2/asir2000/parse/pvar.c, Revision 1.21
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.21 ! fujimoto 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/pvar.c,v 1.20 2015/08/06 10:01:53 fujimoto 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);
1.21 ! fujimoto 180: #if defined(__MINGW32__) || defined(__MINGW64__)
! 181: fflush(stderr);
! 182: #endif
1.14 noro 183: }
184: } else {
185: if ( CUR_MODULE ) {
186: c1 = getpvar(CUR_MODULE->pvs,str,1);
187: }
188: if ( c1 >= 0 ) goto CONFLICTION;
189: CPVS->va[c].attr = IS_LOCAL;
1.13 noro 190: }
1.7 noro 191: } else if ( CPVS != GPVS ) {
192: /* inside function */
1.12 noro 193: if ( CUR_MODULE ) {
194: /* search only */
195: c = getpvar(CPVS,str,1);
196: if ( c < 0 ) {
197: c = getpvar(CPVS,str,0);
198: created = 1;
199: } else
200: created = 0;
201: } else {
202: /* may be created */
203: c = getpvar(CPVS,str,0);
204: }
1.7 noro 205: switch ( CPVS->va[c].attr ) {
206: case IS_GLOBAL:
207: c1 = getpvar(GPVS,str,1); c = PVGLOBAL((unsigned int)c1);
208: break;
209: case IS_MGLOBAL:
210: c1 = getpvar(CUR_MODULE->pvs,str,1); c = PVMGLOBAL((unsigned int)c1);
211: break;
212: case IS_LOCAL:
213: default:
214: if ( CUR_MODULE &&
215: ((c1 = getpvar(CUR_MODULE->pvs,str,1)) >= 0) ) {
216: CPVS->va[c].attr = IS_MGLOBAL;
217: c = PVMGLOBAL((unsigned int)c1);
218: } else if ( getpvar(EPVS,str,1) >= 0 ) {
219: CPVS->va[c].attr = IS_GLOBAL;
220: c1 = getpvar(GPVS,str,1); c = PVGLOBAL((unsigned int)c1);
1.12 noro 221: } else if ( CUR_MODULE && created && (ldef == 0) ) {
222: /* not declared */
223: /* if ldef == 0, at least one local variables has been
224: declared */
225: fprintf(stderr,
226: "Warning: \"%s\", near line %d: undeclared local variable `%s'\n",
227: asir_infile->name,asir_infile->ln,str);
1.21 ! fujimoto 228: #if defined(__MINGW32__) || defined(__MINGW64__)
! 229: fflush(stderr);
! 230: #endif
1.7 noro 231: }
232: break;
1.1 noro 233: }
1.9 noro 234: } else if ( CUR_MODULE ) {
235: /* outside function, inside module */
236: if ( (c = getpvar(CUR_MODULE->pvs,str,1)) >= 0 )
237: c = PVMGLOBAL((unsigned int)c);
238: else if ( getpvar(EPVS,str,1) >= 0 ) {
239: c = getpvar(GPVS,str,1);
240: c = PVGLOBAL((unsigned int)c);
241: } else {
242: /* not declared as static or extern */
1.17 noro 243: buf = ALLOCA(strlen("undeclared variable"+strlen(str)+10));
244: sprintf(buf,"undeclared variable `%s'",str);
245: yyerror(buf);
1.9 noro 246: }
247: } else {
248: /* outside function, outside module */
249: c = getpvar(GPVS,str,0);
1.1 noro 250: }
251: return c;
1.13 noro 252:
253: CONFLICTION:
1.17 noro 254: buf = ALLOCA(strlen("conflicting declarations for "+strlen(str)+10));
255: sprintf(buf,"conflicting declarations for `%s'",str);
256: yyerror(buf);
1.1 noro 257: }
258:
259: extern FUNC parse_targetf;
260:
1.6 noro 261: int searchpvar(char *str)
1.1 noro 262: {
263: VS pvs;
1.7 noro 264: MODULE mod;
265: int c;
1.1 noro 266:
267: if ( parse_targetf && parse_targetf->id != A_USR )
1.7 noro 268: c = -1;
269: else if ( parse_targetf ) {
270: pvs = parse_targetf->f.usrf->pvs;
271: mod = parse_targetf->f.usrf->module;
272: if ( (c = getpvar(pvs,str,1)) >= 0 ) {
273: switch ( pvs->va[c].attr ) {
274: case IS_GLOBAL:
275: c = getpvar(GPVS,str,1);
276: c = PVGLOBAL((unsigned int)c);
277: break;
278: case IS_MGLOBAL:
279: c = getpvar(mod->pvs,str,1);
280: c = PVMGLOBAL((unsigned int)c);
281: break;
282: default:
283: break;
284: }
285: } else if ( mod && (c = getpvar(mod->pvs,str,1)) >= 0 )
286: c = PVMGLOBAL((unsigned int)c);
287: else if ( (c = getpvar(GPVS,str,1)) >= 0 )
288: c = PVGLOBAL((unsigned int)c);
289: else
290: c = -1;
291: }
292: return c;
1.1 noro 293: }
294:
1.6 noro 295: int getpvar(VS pvs,char *str,int searchonly)
1.1 noro 296: {
297: struct oPV *va;
298: PV v;
299: int i;
300:
301: for ( va = pvs->va, i = 0; i < (int)pvs->n; i++ )
302: if ( va[i].name && !strcmp(va[i].name,str) )
303: return i;
304: if ( searchonly )
305: return -1;
306: if ( pvs->asize == pvs->n )
307: reallocarray((char **)&pvs->va,(int *)&pvs->asize,(int *)&pvs->n,(int)sizeof(struct oPV));
308: v = &pvs->va[pvs->n];
309: NAME(v) = (char *)CALLOC(strlen(str)+1,sizeof(char));
310: strcpy(NAME(v),str); v->priv = 0;
1.7 noro 311: v->attr= IS_LOCAL; v->type = -1; i = pvs->n; pvs->n++;
1.1 noro 312: return i;
313: }
314:
1.20 fujimoto 315: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.1 noro 316: #define PCLOSE _pclose
317: #else
318: #define PCLOSE pclose
319: #endif
320:
321: void closecurrentinput()
322: {
323: if ( asir_infile && !asir_infile->fp )
324: return;
325:
1.20 fujimoto 326: #if defined(VISUAL) || defined(__MINGW32__) || defined(__MINGW64__)
1.1 noro 327: fclose(asir_infile->fp);
328: unlink(asir_infile->tname);
329: #else
1.16 noro 330: if ( asir_infile->fp != stdin )
331: PCLOSE(asir_infile->fp);
1.1 noro 332: #endif
333: asir_infile = NEXT(asir_infile);
334: }
335:
336: void resetpvs()
337: {
1.16 noro 338: if ( asir_infile && !NEXT(asir_infile) ) {
1.8 noro 339: PVSS = 0; CPVS = GPVS; MPVS = 0; CUR_MODULE = 0; nextbp = 0;
1.12 noro 340: gdef = mgdef = ldef = 0;
1.1 noro 341: if ( EPVS->va ) {
342: bzero((char *)EPVS->va,(int)(EPVS->asize*sizeof(struct oPV))); EPVS->n = 0;
343: }
344: }
345: }
346:
347: static NODE saved_PVSS;
348: static VS saved_CPVS;
349: static int saved_nextbp, saved_nextbplevel;
350:
351: void savepvs()
352: {
353: saved_PVSS = PVSS;
354: saved_CPVS = CPVS;
355: saved_nextbp = nextbp;
356: saved_nextbplevel = nextbplevel;
357: }
358:
359: void restorepvs()
360: {
361: PVSS = saved_PVSS;
362: CPVS = saved_CPVS;
363: nextbp = saved_nextbp;
364: nextbplevel = saved_nextbplevel;
365: }
366:
1.6 noro 367: void storeans(pointer p)
1.1 noro 368: {
369: if ( APVS->asize == APVS->n )
370: reallocarray((char **)&APVS->va,(int *)&APVS->asize,(int *)&APVS->n,(int)sizeof(struct oPV));
371: APVS->va[APVS->n++].priv = p;
372: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>