Update the changelog
[opencv] / apps / Hawk / CVEiCL / EiC / src / func.c
1 /* func.c
2  *
3  *      (C) Copyright Nov 23 1996, Edmond J. Breen.
4  *                 ALL RIGHTS RESERVED.
5  * This code may be copied for personal, non-profit use only.
6  *
7  *
8  * This file contains the methods for dealing with EiC function
9  * house keeping.
10  */
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15
16 #include "global.h"
17 #include "typemod.h"
18 #include "func.h"
19 #include "xalloc.h"
20 #include "error.h"
21 #include "symbol.h"
22 #include "cdecl.h"
23
24 int EiC_gotMissingNames(func_t *f)
25 {
26     /* check to see that all parameters have a name */
27     int i;
28
29     if(!EiC_hasPrototype(f) || EiC_gettype(getFPty(f, 0)) == t_void)
30         return 0;
31
32     for (i = 0; i < getFNp(f); ++i)
33         if(!getFPname(f,i)) 
34             if(EiC_gettype(getFPty(f, i)) != t_var)
35                return i+1;
36     return 0;
37 }
38
39 int EiC_IsVariadic(func_t *f)
40 {
41     return f ? getFNp(f) > 1 && EiC_gettype(getFPty(f,getFNp(f)-1)) ==
42         t_var : 0;
43 }
44
45 int EiC_hasPrototype(func_t *f)
46 {
47     
48     return f ? EiC_gettype(getFPty(f, 0)) != t_var: 0;
49 }
50
51 void EiC_swapFPLists(func_t *f1, func_t *f2)
52 {
53     /* swap parameter lists */
54     parm_t *t = getFPtyList(f1);
55     setFPtyList(f1,getFPtyList(f2));
56     setFPtyList(f2,t);
57 }
58
59
60 int EiC_HasHiddenParm(type_expr *ty)
61 {
62     int t = EiC_gettype(nextType(ty));
63     if(t == t_struct || t == t_union)
64         return 1;
65     return 0;
66 }
67
68 int EiC_sameFuncs(type_expr * t1, type_expr * t2)
69 {
70     /* check for compatible function types */
71
72     func_t * f1, *f2;
73     int i;
74
75     f1 = EiC_getInf(t1);
76     f2 = EiC_getInf(t2);
77     if(!f1 || !f2)   /* watch for builtin types before prototype */
78         return 0;
79     if(EiC_hasPrototype(f1) && EiC_hasPrototype(f2)) {
80         if (getFNp(f1) != getFNp(f2))
81             return 0;
82         for (i = 0; i < getFNp(f1); ++i)
83             if(!EiC_sametypes(getFPty(f1,i),getFPty(f2,i)))
84                 return 0;
85     } else if(EiC_IsVariadic(f1) || EiC_IsVariadic(f2))
86         return 0;
87     else { /* now check parameters are not convertable */
88         if(EiC_hasPrototype(f2))
89             f1 = f2;
90         for (i = 0; i < getFNp(f1); ++i)
91             switch(EiC_gettype(getFPty(f1,i))) {
92               case t_char: case t_uchar:
93               case t_short: case t_ushort:
94               case t_float:
95                 return 0;
96             }
97     }
98     /* check function return types */
99     return EiC_sametypes(t1->nxt, t2->nxt);
100 }
101
102
103 void EiC_make_func(token_t * e1)
104 {
105     /*    if (EiC_gettype(e1->type) == t_array ||
106         EiC_gettype(e1->type) == t_funcdec) {
107             EiC_error("Illegal type construction");
108             }
109             */
110     e1->Type = EiC_addtype(t_funcdec, e1->Type);
111     setInf(e1->Type,xcalloc(1, sizeof(func_t)));
112 }
113
114 void EiC_adjustParam(type_expr **type)
115 {
116     int t = EiC_gettype(*type);
117     if(t == t_funcdec) 
118       *type = EiC_addtype(t_pointer, *type);
119     else if(t == t_array)
120       EiC_exchtype(t_pointer,*type);
121 }
122
123
124 static void newParam(func_t *f,char *name, type_expr *type)
125 {
126
127     setFPtyList(f,xrealloc(getFPtyList(f),
128                            sizeof(parm_t) * (getFNp(f) +1)));
129     
130     
131     if(name)
132         setFPname(f,getFNp(f), EiC_strsave(name));
133     else
134         setFPname(f,getFNp(f), NULL);
135
136     setFPcomm(f,getFNp(f),NULL);
137     setFPval(f,getFNp(f),NULL);
138     
139     setFPty(f,getFNp(f),EiC_copytype(type));
140 }
141
142
143 void EiC_add_func_parm(func_t * f, type_expr ** type, char * name)
144 {
145     int t;
146     type_expr *t1, *t2;
147     /* must handle storage class, as yet not done. */
148     if ((t = EiC_gettype(*type)) != t_funcdec && t != t_array)
149         t = 0;
150
151     newParam(f,name,*type);
152
153     /*
154      * Because, parameters will be removed
155      * from the lookup table the function types stored
156      * need to be as complete as possible.
157      */
158     t1 = getFPty(f,getFNp(f));
159     t2 = *type;
160     while (t2) {
161         if (!t2->alias) {
162             t2->alias = 1;
163             t1->alias = 0;
164         }
165         t2 = nextType(t2);
166         t1 = nextType(t1);
167     }
168
169     EiC_adjustParam(&getFPty(f,getFNp(f)));
170     setFNp(f,getFNp(f)+1);
171 }
172
173 void EiC_add_func_str(func_t * f, char *s)
174 {
175     if (!f->sn)
176         f->strs = (char **) xcalloc(1, sizeof(char *));
177     else
178         f->strs = (char **) xrealloc(f->strs,
179                                      sizeof(char *) * (f->sn + 1));
180     f->strs[f->sn] = s;
181     f->sn++;
182 }
183
184
185 void EiC_add_func_static(func_t * f, int n)
186 {
187     if (!f->stn)
188         f->st = (int *) xcalloc(1, sizeof(int));
189     else
190         f->st = (int *) xrealloc(f->st,
191                                      sizeof(int) * (f->stn + 1));
192     f->st[f->stn] = n;
193     f->stn++;
194 }
195
196 void EiC_add_func_initialiser(func_t * f, void *s)
197 {
198     if (!f->ni)
199         f->inzs = (void **) xcalloc(1, sizeof(void *));
200     else
201         f->inzs = (void **) xrealloc(f->inzs,
202                                      sizeof(void *) * (f->ni + 1));
203     f->inzs[f->ni] = s;
204     f->ni++;
205 }
206
207 void add_param_initialiser(func_t *f)
208 {
209     if (f->ni) 
210         setFPval(f,f->ni-1,f->inzs[f->ni-1]);
211     else
212         EiC_error("Error in with parameter initialisation");
213 }
214
215
216
217 void EiC_freeFuncComments(func_t *f)
218 {
219
220     if(getFComm(f)) {
221         xfree(getFComm(f));
222         setFComm(f,NULL);
223     }
224 }                   
225
226 static void freeCallBack(func_t *f)
227 {                   
228     if(f->callBack) {
229         void EiC_freecode(code_t * code);
230         EiC_freecode(getFcallBack(f));
231         xfree(getFcallBack(f));
232         setFcallBack(f,NULL);
233     }
234 }
235
236 void EiC_free_func_inf(func_t * f)
237 {
238     int i;
239     if (!f)
240         return;
241     if (getFNp(f)) {
242         for (i = 0; i < getFNp(f); i++) {
243             EiC_freetype(getFPty(f,i));
244             if(getFPname(f,i))
245                 xfree(getFPname(f,i));
246        }
247             
248         xfree(getFPtyList(f));
249     }
250     if (f->sn) {
251         for (i = 0; i < f->sn; ++i)
252             xfree(f->strs[i]);
253         xfree(f->strs);
254         f->sn = 0;
255     }
256     if (f->ni) {
257         for (i = 0; i < f->ni; ++i)
258             xfree(f->inzs[i]);
259         xfree(f->inzs);
260         f->ni= 0;
261     }
262     if (f->stn) {
263         for (i = 0; i < f->stn; ++i)
264             xfree(EiC_ENV->AR[f->st[i]].v.p.p);
265         xfree(f->st);
266         f->stn = 0;
267     }
268
269     freeCallBack(f);
270
271     EiC_freeFuncComments(f);
272     xfree(f);
273 }
274
275 void EiC_markFunc(type_expr * type, int mark)
276 {
277     func_t * f = EiC_getInf(type);
278     int i;
279     
280     xmark(f,mark);
281     /* mark paramaters */
282     if(getFNp(f)) {
283         xmark(getFPtyList(f),mark);
284         for(i=0;i<getFNp(f);i++) {
285             EiC_marktype(getFPty(f,i),mark);
286             if(getFPname(f,i))
287                 xmark(getFPname(f,i),mark);
288             if(getFPcomm(f,i))
289                 xmark(getFPcomm(f,i),mark);
290         }
291     }
292     /* mark strings */
293     if(f->sn) {
294         xmark(f->strs,mark);
295         for(i=0;i<f->sn;i++) 
296             xmark(f->strs[i],mark);
297                 
298     }
299     /* mark initializers */
300     if(f->ni) {
301         xmark(f->inzs,mark);
302         for(i=0;i<f->ni;i++)
303             xmark(f->inzs[i],mark);
304     }
305      /* mark static arrays */
306     if(f->stn) {
307         xmark(f->st,mark);
308         for(i=0;i<f->stn;i++)
309             xmark(EiC_ENV->AR[f->st[i]].v.p.p,mark);
310     }
311     if(getFcallBack(f)) {
312         code_t *code = getFcallBack(f);
313         xmark(code,mark);
314         xmark(code->inst,mark);
315     }
316
317     if(getFComm(f))
318         xmark(getFComm(f),mark);
319
320 }
321
322 void add_func_comment(char *s)
323 {
324     extern token_t *EiC_RETURNEXPR;
325     func_t * f = EiC_getInf(EiC_RETURNEXPR->Type);
326     setFComm(f,s);
327 }
328
329 void (*EiC_saveComment(void)) (char * s)
330 {
331     if(EiC_INFUNC) {
332          extern token_t *EiC_RETURNEXPR;
333          func_t * f = EiC_getInf(EiC_RETURNEXPR->Type);
334          if(f && !getFComm(f))
335              return add_func_comment;
336     }   
337     return NULL;
338 }
339
340
341 #if 0
342
343 void savecode(char * filename,
344               func_t *func)
345 {
346
347     int i;
348     FILE *fp = fopen(filename,"wb");
349     for(i=0;i<CODE->nextinst;++i) 
350         fwrite(&CODE->inst[i],sizeof(InsT_t),1,fp);
351         
352 #endif
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368