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