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