Annotation of OpenXM_contrib2/asir2000/parse/pvar.c, Revision 1.17
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.17 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/pvar.c,v 1.16 2006/02/08 02:11:19 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.12 noro 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
1.15 noro 129: #define IS_PATTERN 3
1.7 noro 130:
1.10 noro 131:
1.7 noro 132: unsigned int makepvar(char *str)
1.1 noro 133: {
1.12 noro 134: int c,c1,created;
1.17 ! noro 135: char *buf;
1.1 noro 136:
1.15 noro 137: if ( str[0] == '_' ) {
138: /* pattern variable */
139: c1 = getpvar(PPVS,str,0);
140: c = PVPATTERN((unsigned int)c1);
141: PPVS->va[c1].attr = IS_PATTERN;
142: } else if ( gdef ) {
143: /* EPVS : global list of the current file */
144: /* add to the local variable list */
145: /* also add to the external variable list */
1.9 noro 146: c = getpvar(CPVS,str,0);
1.1 noro 147: getpvar(EPVS,str,0);
1.10 noro 148: if ( CUR_MODULE ) {
149: c1 = getpvar(CUR_MODULE->pvs,str,1);
1.13 noro 150: if ( c1 >= 0 ) goto CONFLICTION;
1.10 noro 151: }
1.1 noro 152: if ( CPVS != GPVS ) {
1.7 noro 153: /* inside function : we add the name to the global list */
1.1 noro 154: getpvar(GPVS,str,0);
1.7 noro 155: CPVS->va[c].attr = IS_GLOBAL;
156: }
157: } else if ( mgdef ) {
1.9 noro 158: c = getpvar(CPVS,str,0);
1.7 noro 159: getpvar(CUR_MODULE->pvs,str,0);
1.10 noro 160: c1 = getpvar(EPVS,str,1);
1.13 noro 161: if ( c1 >= 0 ) goto CONFLICTION;
1.7 noro 162: if ( CPVS != GPVS ) {
163: /* inside function */
164: CPVS->va[c].attr = IS_MGLOBAL;
165: }
1.12 noro 166: } else if ( ldef > 0 ) {
167: /* if ldef > 0, then local variables are being declared */
168: c = getpvar(CPVS,str,0);
1.13 noro 169: c1 = getpvar(EPVS,str,1);
1.14 noro 170: if ( c1 >= 0 ) {
171: if ( CUR_MODULE )
172: goto CONFLICTION;
173: else {
174: fprintf(stderr,"Warning: \"%s\", near line %d: conflicting declarations for `%s'\n",
175: asir_infile->name,asir_infile->ln,str);
176: fprintf(stderr," `%s' is bound to the global variable\n",str);
177: CPVS->va[c].attr = IS_GLOBAL;
178: c1 = getpvar(GPVS,str,1); c = PVGLOBAL((unsigned int)c1);
179: }
180: } else {
181: if ( CUR_MODULE ) {
182: c1 = getpvar(CUR_MODULE->pvs,str,1);
183: }
184: if ( c1 >= 0 ) goto CONFLICTION;
185: CPVS->va[c].attr = IS_LOCAL;
1.13 noro 186: }
1.7 noro 187: } else if ( CPVS != GPVS ) {
188: /* inside function */
1.12 noro 189: if ( CUR_MODULE ) {
190: /* search only */
191: c = getpvar(CPVS,str,1);
192: if ( c < 0 ) {
193: c = getpvar(CPVS,str,0);
194: created = 1;
195: } else
196: created = 0;
197: } else {
198: /* may be created */
199: c = getpvar(CPVS,str,0);
200: }
1.7 noro 201: switch ( CPVS->va[c].attr ) {
202: case IS_GLOBAL:
203: c1 = getpvar(GPVS,str,1); c = PVGLOBAL((unsigned int)c1);
204: break;
205: case IS_MGLOBAL:
206: c1 = getpvar(CUR_MODULE->pvs,str,1); c = PVMGLOBAL((unsigned int)c1);
207: break;
208: case IS_LOCAL:
209: default:
210: if ( CUR_MODULE &&
211: ((c1 = getpvar(CUR_MODULE->pvs,str,1)) >= 0) ) {
212: CPVS->va[c].attr = IS_MGLOBAL;
213: c = PVMGLOBAL((unsigned int)c1);
214: } else if ( getpvar(EPVS,str,1) >= 0 ) {
215: CPVS->va[c].attr = IS_GLOBAL;
216: c1 = getpvar(GPVS,str,1); c = PVGLOBAL((unsigned int)c1);
1.12 noro 217: } else if ( CUR_MODULE && created && (ldef == 0) ) {
218: /* not declared */
219: /* if ldef == 0, at least one local variables has been
220: declared */
221: fprintf(stderr,
222: "Warning: \"%s\", near line %d: undeclared local variable `%s'\n",
223: asir_infile->name,asir_infile->ln,str);
1.7 noro 224: }
225: break;
1.1 noro 226: }
1.9 noro 227: } else if ( CUR_MODULE ) {
228: /* outside function, inside module */
229: if ( (c = getpvar(CUR_MODULE->pvs,str,1)) >= 0 )
230: c = PVMGLOBAL((unsigned int)c);
231: else if ( getpvar(EPVS,str,1) >= 0 ) {
232: c = getpvar(GPVS,str,1);
233: c = PVGLOBAL((unsigned int)c);
234: } else {
235: /* not declared as static or extern */
1.17 ! noro 236: buf = ALLOCA(strlen("undeclared variable"+strlen(str)+10));
! 237: sprintf(buf,"undeclared variable `%s'",str);
! 238: yyerror(buf);
1.9 noro 239: }
240: } else {
241: /* outside function, outside module */
242: c = getpvar(GPVS,str,0);
1.1 noro 243: }
244: return c;
1.13 noro 245:
246: CONFLICTION:
1.17 ! noro 247: buf = ALLOCA(strlen("conflicting declarations for "+strlen(str)+10));
! 248: sprintf(buf,"conflicting declarations for `%s'",str);
! 249: yyerror(buf);
1.1 noro 250: }
251:
252: extern FUNC parse_targetf;
253:
1.6 noro 254: int searchpvar(char *str)
1.1 noro 255: {
256: VS pvs;
1.7 noro 257: MODULE mod;
258: int c;
1.1 noro 259:
260: if ( parse_targetf && parse_targetf->id != A_USR )
1.7 noro 261: c = -1;
262: else if ( parse_targetf ) {
263: pvs = parse_targetf->f.usrf->pvs;
264: mod = parse_targetf->f.usrf->module;
265: if ( (c = getpvar(pvs,str,1)) >= 0 ) {
266: switch ( pvs->va[c].attr ) {
267: case IS_GLOBAL:
268: c = getpvar(GPVS,str,1);
269: c = PVGLOBAL((unsigned int)c);
270: break;
271: case IS_MGLOBAL:
272: c = getpvar(mod->pvs,str,1);
273: c = PVMGLOBAL((unsigned int)c);
274: break;
275: default:
276: break;
277: }
278: } else if ( mod && (c = getpvar(mod->pvs,str,1)) >= 0 )
279: c = PVMGLOBAL((unsigned int)c);
280: else if ( (c = getpvar(GPVS,str,1)) >= 0 )
281: c = PVGLOBAL((unsigned int)c);
282: else
283: c = -1;
284: }
285: return c;
1.1 noro 286: }
287:
1.6 noro 288: int getpvar(VS pvs,char *str,int searchonly)
1.1 noro 289: {
290: struct oPV *va;
291: PV v;
292: int i;
293:
294: for ( va = pvs->va, i = 0; i < (int)pvs->n; i++ )
295: if ( va[i].name && !strcmp(va[i].name,str) )
296: return i;
297: if ( searchonly )
298: return -1;
299: if ( pvs->asize == pvs->n )
300: reallocarray((char **)&pvs->va,(int *)&pvs->asize,(int *)&pvs->n,(int)sizeof(struct oPV));
301: v = &pvs->va[pvs->n];
302: NAME(v) = (char *)CALLOC(strlen(str)+1,sizeof(char));
303: strcpy(NAME(v),str); v->priv = 0;
1.7 noro 304: v->attr= IS_LOCAL; v->type = -1; i = pvs->n; pvs->n++;
1.1 noro 305: return i;
306: }
307:
308: #if defined(VISUAL)
309: #define PCLOSE _pclose
310: #else
311: #define PCLOSE pclose
312: #endif
313:
314: void closecurrentinput()
315: {
316: if ( asir_infile && !asir_infile->fp )
317: return;
318:
1.5 noro 319: #if defined(VISUAL)
1.1 noro 320: fclose(asir_infile->fp);
321: unlink(asir_infile->tname);
322: #else
1.16 noro 323: if ( asir_infile->fp != stdin )
324: PCLOSE(asir_infile->fp);
1.1 noro 325: #endif
326: asir_infile = NEXT(asir_infile);
327: resetpvs();
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>