3 * (C) Copyright Nov 23 1996, Edmond J. Breen.
5 * This code may be copied for personal, non-profit use only.
8 * This file contains the methods for dealing with EiC function
24 int EiC_gotMissingNames(func_t *f)
26 /* check to see that all parameters have a name */
29 if(!EiC_hasPrototype(f) || EiC_gettype(getFPty(f, 0)) == t_void)
32 for (i = 0; i < getFNp(f); ++i)
34 if(EiC_gettype(getFPty(f, i)) != t_var)
39 int EiC_IsVariadic(func_t *f)
41 return f ? getFNp(f) > 1 && EiC_gettype(getFPty(f,getFNp(f)-1)) ==
45 int EiC_hasPrototype(func_t *f)
48 return f ? EiC_gettype(getFPty(f, 0)) != t_var: 0;
51 void EiC_swapFPLists(func_t *f1, func_t *f2)
53 /* swap parameter lists */
54 parm_t *t = getFPtyList(f1);
55 setFPtyList(f1,getFPtyList(f2));
60 int EiC_HasHiddenParm(type_expr *ty)
62 int t = EiC_gettype(nextType(ty));
63 if(t == t_struct || t == t_union)
68 int EiC_sameFuncs(type_expr * t1, type_expr * t2)
70 /* check for compatible function types */
77 if(!f1 || !f2) /* watch for builtin types before prototype */
79 if(EiC_hasPrototype(f1) && EiC_hasPrototype(f2)) {
80 if (getFNp(f1) != getFNp(f2))
82 for (i = 0; i < getFNp(f1); ++i)
83 if(!EiC_sametypes(getFPty(f1,i),getFPty(f2,i)))
85 } else if(EiC_IsVariadic(f1) || EiC_IsVariadic(f2))
87 else { /* now check parameters are not convertable */
88 if(EiC_hasPrototype(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:
98 /* check function return types */
99 return EiC_sametypes(t1->nxt, t2->nxt);
103 void EiC_make_func(token_t * e1)
105 /* if (EiC_gettype(e1->type) == t_array ||
106 EiC_gettype(e1->type) == t_funcdec) {
107 EiC_error("Illegal type construction");
110 e1->Type = EiC_addtype(t_funcdec, e1->Type);
111 setInf(e1->Type,xcalloc(1, sizeof(func_t)));
114 void EiC_adjustParam(type_expr **type)
116 int t = EiC_gettype(*type);
118 *type = EiC_addtype(t_pointer, *type);
119 else if(t == t_array)
120 EiC_exchtype(t_pointer,*type);
124 static void newParam(func_t *f,char *name, type_expr *type)
127 setFPtyList(f,xrealloc(getFPtyList(f),
128 sizeof(parm_t) * (getFNp(f) +1)));
132 setFPname(f,getFNp(f), EiC_strsave(name));
134 setFPname(f,getFNp(f), NULL);
136 setFPcomm(f,getFNp(f),NULL);
137 setFPval(f,getFNp(f),NULL);
139 setFPty(f,getFNp(f),EiC_copytype(type));
143 void EiC_add_func_parm(func_t * f, type_expr ** type, char * name)
147 /* must handle storage class, as yet not done. */
148 if ((t = EiC_gettype(*type)) != t_funcdec && t != t_array)
151 newParam(f,name,*type);
154 * Because, parameters will be removed
155 * from the lookup table the function types stored
156 * need to be as complete as possible.
158 t1 = getFPty(f,getFNp(f));
169 EiC_adjustParam(&getFPty(f,getFNp(f)));
170 setFNp(f,getFNp(f)+1);
173 void EiC_add_func_str(func_t * f, char *s)
176 f->strs = (char **) xcalloc(1, sizeof(char *));
178 f->strs = (char **) xrealloc(f->strs,
179 sizeof(char *) * (f->sn + 1));
185 void EiC_add_func_static(func_t * f, int n)
188 f->st = (int *) xcalloc(1, sizeof(int));
190 f->st = (int *) xrealloc(f->st,
191 sizeof(int) * (f->stn + 1));
196 void EiC_add_func_initialiser(func_t * f, void *s)
199 f->inzs = (void **) xcalloc(1, sizeof(void *));
201 f->inzs = (void **) xrealloc(f->inzs,
202 sizeof(void *) * (f->ni + 1));
207 void add_param_initialiser(func_t *f)
210 setFPval(f,f->ni-1,f->inzs[f->ni-1]);
212 EiC_error("Error in with parameter initialisation");
217 void EiC_freeFuncComments(func_t *f)
226 static void freeCallBack(func_t *f)
229 void EiC_freecode(code_t * code);
230 EiC_freecode(getFcallBack(f));
231 xfree(getFcallBack(f));
232 setFcallBack(f,NULL);
236 void EiC_free_func_inf(func_t * f)
242 for (i = 0; i < getFNp(f); i++) {
243 EiC_freetype(getFPty(f,i));
245 xfree(getFPname(f,i));
248 xfree(getFPtyList(f));
251 for (i = 0; i < f->sn; ++i)
257 for (i = 0; i < f->ni; ++i)
263 for (i = 0; i < f->stn; ++i)
264 xfree(EiC_ENV->AR[f->st[i]].v.p.p);
271 EiC_freeFuncComments(f);
275 void EiC_markFunc(type_expr * type, int mark)
277 func_t * f = EiC_getInf(type);
281 /* mark paramaters */
283 xmark(getFPtyList(f),mark);
284 for(i=0;i<getFNp(f);i++) {
285 EiC_marktype(getFPty(f,i),mark);
287 xmark(getFPname(f,i),mark);
289 xmark(getFPcomm(f,i),mark);
296 xmark(f->strs[i],mark);
299 /* mark initializers */
303 xmark(f->inzs[i],mark);
305 /* mark static arrays */
308 for(i=0;i<f->stn;i++)
309 xmark(EiC_ENV->AR[f->st[i]].v.p.p,mark);
311 if(getFcallBack(f)) {
312 code_t *code = getFcallBack(f);
314 xmark(code->inst,mark);
318 xmark(getFComm(f),mark);
322 void add_func_comment(char *s)
324 extern token_t *EiC_RETURNEXPR;
325 func_t * f = EiC_getInf(EiC_RETURNEXPR->Type);
329 void (*EiC_saveComment(void)) (char * s)
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;
343 void savecode(char * filename,
348 FILE *fp = fopen(filename,"wb");
349 for(i=0;i<CODE->nextinst;++i)
350 fwrite(&CODE->inst[i],sizeof(InsT_t),1,fp);