summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Kolesa <d.kolesa@samsung.com>2019-01-30 11:33:58 +0100
committerDaniel Kolesa <d.kolesa@samsung.com>2019-02-28 01:09:02 +0100
commitce372ee53476cade560261bd764bbab14916fd80 (patch)
tree670674a3fb674e230d0f72ac89d4b3ac7641bd4b
parent4cbc010d8fefe25a390a4d4cfe5bdcbe77e9ff73 (diff)
eolian_gen: support for iterative freeing of struct fields
-rw-r--r--src/bin/eolian/sources.c97
1 files changed, 57 insertions, 40 deletions
diff --git a/src/bin/eolian/sources.c b/src/bin/eolian/sources.c
index e9da5b294d..a0f67c3ba0 100644
--- a/src/bin/eolian/sources.c
+++ b/src/bin/eolian/sources.c
@@ -151,9 +151,9 @@ _append_defval(Eina_Strbuf *buf, const Eolian_Expression *exp, const Eolian_Type
151} 151}
152 152
153static void 153static void
154_write_free_call(Eina_Strbuf *buf, const char *ffunc, const Eina_Strbuf *param, const char *indent) 154_write_free_call(Eina_Strbuf *buf, const char *ffunc, const Eina_Strbuf *param, const char *indent, const char *eindent)
155{ 155{
156 eina_strbuf_append_printf(buf," %s%s(", indent, ffunc); 156 eina_strbuf_append_printf(buf," %s%s%s(", indent, eindent, ffunc);
157 eina_strbuf_append_buffer(buf, param); 157 eina_strbuf_append_buffer(buf, param);
158 eina_strbuf_append(buf, ");\n"); 158 eina_strbuf_append(buf, ");\n");
159} 159}
@@ -195,22 +195,22 @@ _get_free_func(const Eolian_Type *type)
195} 195}
196 196
197static void 197static void
198_generate_normal_free(Eina_Strbuf **buf, const Eolian_Type *type, const Eina_Strbuf *param, const char *indent) 198_generate_normal_free(Eina_Strbuf **buf, const Eolian_Type *type, const Eina_Strbuf *param, const char *indent, const char *eindent)
199{ 199{
200 const Eolian_Type *btype = eolian_type_aliased_base_get(type); 200 const Eolian_Type *btype = eolian_type_aliased_base_get(type);
201 Eolian_Type_Builtin_Type t = eolian_type_builtin_type_get(btype); 201 Eolian_Type_Builtin_Type t = eolian_type_builtin_type_get(btype);
202 switch (t) 202 switch (t)
203 { 203 {
204 case EOLIAN_TYPE_BUILTIN_FUTURE: 204 case EOLIAN_TYPE_BUILTIN_FUTURE:
205 _write_free_call(*buf, "(void)", param, indent); break; 205 _write_free_call(*buf, "(void)", param, indent, eindent); break;
206 case EOLIAN_TYPE_BUILTIN_HASH: 206 case EOLIAN_TYPE_BUILTIN_HASH:
207 eina_strbuf_append_printf(*buf," eina_hash_free_cb_set("); 207 eina_strbuf_append_printf(*buf," eina_hash_free_cb_set(");
208 eina_strbuf_append_buffer(*buf, param); 208 eina_strbuf_append_buffer(*buf, param);
209 eina_strbuf_append(*buf, ",NULL);\n"); 209 eina_strbuf_append(*buf, ",NULL);\n");
210 _write_free_call(*buf, "eina_hash_free", param, indent); 210 _write_free_call(*buf, "eina_hash_free", param, indent, eindent);
211 break; 211 break;
212 case EOLIAN_TYPE_BUILTIN_INLIST: 212 case EOLIAN_TYPE_BUILTIN_INLIST:
213 _write_free_call(*buf, "(void)", param, indent); break; 213 _write_free_call(*buf, "(void)", param, indent, eindent); break;
214 case EOLIAN_TYPE_BUILTIN_ACCESSOR: 214 case EOLIAN_TYPE_BUILTIN_ACCESSOR:
215 case EOLIAN_TYPE_BUILTIN_ARRAY: 215 case EOLIAN_TYPE_BUILTIN_ARRAY:
216 case EOLIAN_TYPE_BUILTIN_ITERATOR: 216 case EOLIAN_TYPE_BUILTIN_ITERATOR:
@@ -228,18 +228,18 @@ _generate_normal_free(Eina_Strbuf **buf, const Eolian_Type *type, const Eina_Str
228 eolian_type_short_name_get(type)); 228 eolian_type_short_name_get(type));
229 break; 229 break;
230 } 230 }
231 _write_free_call(*buf, ffunc, param, indent); 231 _write_free_call(*buf, ffunc, param, indent, eindent);
232 break; 232 break;
233 } 233 }
234 } 234 }
235} 235}
236 236
237static void 237static void
238_generate_loop_content(Eina_Strbuf **buf, const Eolian_Type *inner_type, const Eina_Strbuf *iter_param) 238_generate_loop_content(Eina_Strbuf **buf, const Eolian_Type *inner_type, const Eina_Strbuf *iter_param, const char *indent)
239{ 239{
240 eina_strbuf_append(*buf, " {\n"); 240 eina_strbuf_append_printf(*buf, " %s{\n", indent);
241 _generate_normal_free(buf, inner_type, iter_param, " "); 241 _generate_normal_free(buf, inner_type, iter_param, indent, " ");
242 eina_strbuf_append(*buf, " }\n"); 242 eina_strbuf_append_printf(*buf, " %s}\n", indent);
243} 243}
244 244
245static const Eolian_Typedecl * 245static const Eolian_Typedecl *
@@ -265,11 +265,16 @@ _have_struct_owned_fields(const Eolian_Type *tp)
265 return NULL; 265 return NULL;
266} 266}
267 267
268static void _generate_iterative_free(Eina_Strbuf **buf, const Eolian_Type *type,
269 const Eolian_Type *inner_type,
270 const char *pname, Eina_Strbuf *param,
271 const char *indent);
272
268static void 273static void
269_generate_inarray_loop(Eina_Strbuf **buf, const Eolian_Typedecl *tdecl, 274_generate_inarray_loop(Eina_Strbuf **buf, const Eolian_Typedecl *tdecl,
270 const Eina_Strbuf *iter_param) 275 const Eina_Strbuf *iter_param, const char *indent)
271{ 276{
272 eina_strbuf_append(*buf, " {\n"); 277 eina_strbuf_append_printf(*buf, " %s{\n", indent);
273 Eina_Iterator *itr = eolian_typedecl_struct_fields_get(tdecl); 278 Eina_Iterator *itr = eolian_typedecl_struct_fields_get(tdecl);
274 const Eolian_Struct_Type_Field *sf; 279 const Eolian_Struct_Type_Field *sf;
275 Eina_Strbuf *fldbuf = eina_strbuf_new(); 280 Eina_Strbuf *fldbuf = eina_strbuf_new();
@@ -278,11 +283,20 @@ _generate_inarray_loop(Eina_Strbuf **buf, const Eolian_Typedecl *tdecl,
278 const Eolian_Type *ftp = eolian_typedecl_struct_field_type_get(sf); 283 const Eolian_Type *ftp = eolian_typedecl_struct_field_type_get(sf);
279 if (!eolian_type_is_owned(ftp)) 284 if (!eolian_type_is_owned(ftp))
280 continue; 285 continue;
286 const char *fldn = eolian_typedecl_struct_field_name_get(sf);
281 eina_strbuf_reset(fldbuf); 287 eina_strbuf_reset(fldbuf);
282 eina_strbuf_append_buffer(fldbuf, iter_param); 288 eina_strbuf_append_buffer(fldbuf, iter_param);
283 eina_strbuf_append(fldbuf, "->"); 289 eina_strbuf_append(fldbuf, "->");
284 eina_strbuf_append(fldbuf, eolian_typedecl_struct_field_name_get(sf)); 290 eina_strbuf_append(fldbuf, fldn);
285 _generate_normal_free(buf, ftp, fldbuf, " "); 291 const Eolian_Type *btp = eolian_type_aliased_base_get(ftp);
292 const Eolian_Type *itp = eolian_type_base_type_get(btp);;
293 /* inarray loop is always at one level at most so do not respect
294 * extra indent
295 */
296 if (itp && eolian_type_is_owned(itp))
297 _generate_iterative_free(buf, btp, itp, fldn, fldbuf, " ");
298 else
299 _generate_normal_free(buf, ftp, fldbuf, " ", "");
286 } 300 }
287 eina_strbuf_free(fldbuf); 301 eina_strbuf_free(fldbuf);
288 eina_iterator_free(itr); 302 eina_iterator_free(itr);
@@ -290,7 +304,9 @@ _generate_inarray_loop(Eina_Strbuf **buf, const Eolian_Typedecl *tdecl,
290} 304}
291 305
292static void 306static void
293_generate_iterative_free(Eina_Strbuf **buf, const Eolian_Type *type, const Eolian_Type *inner_type, Eolian_Function_Parameter *parameter, Eina_Strbuf *param) 307_generate_iterative_free(Eina_Strbuf **buf, const Eolian_Type *type,
308 const Eolian_Type *inner_type, const char *pname,
309 Eina_Strbuf *param, const char *indent)
294{ 310{
295 Eina_Strbuf *iterator_header, *iter_param; 311 Eina_Strbuf *iterator_header, *iter_param;
296 312
@@ -299,10 +315,10 @@ _generate_iterative_free(Eina_Strbuf **buf, const Eolian_Type *type, const Eolia
299 315
300 Eolian_Type_Builtin_Type t = eolian_type_builtin_type_get(type); 316 Eolian_Type_Builtin_Type t = eolian_type_builtin_type_get(type);
301 317
302 eina_strbuf_append_printf(iter_param, "%s_iter", eolian_parameter_name_get(parameter)); 318 eina_strbuf_append_printf(iter_param, "%s_iter", pname);
303 319
304 //generate the field definition 320 //generate the field definition
305 eina_strbuf_append_printf(*buf, " %s", eolian_type_c_type_get(inner_type, EOLIAN_C_TYPE_DEFAULT)); 321 eina_strbuf_append_printf(*buf, " %s%s", indent, eolian_type_c_type_get(inner_type, EOLIAN_C_TYPE_DEFAULT));
306 if(t == EOLIAN_TYPE_BUILTIN_INARRAY 322 if(t == EOLIAN_TYPE_BUILTIN_INARRAY
307 || t == EOLIAN_TYPE_BUILTIN_INLIST) 323 || t == EOLIAN_TYPE_BUILTIN_INLIST)
308 { 324 {
@@ -314,77 +330,77 @@ _generate_iterative_free(Eina_Strbuf **buf, const Eolian_Type *type, const Eolia
314 330
315 if (t == EOLIAN_TYPE_BUILTIN_LIST) 331 if (t == EOLIAN_TYPE_BUILTIN_LIST)
316 { 332 {
317 eina_strbuf_append_printf(*buf, " EINA_LIST_FREE("); 333 eina_strbuf_append_printf(*buf, " %sEINA_LIST_FREE(", indent);
318 eina_strbuf_append_buffer(*buf, param); 334 eina_strbuf_append_buffer(*buf, param);
319 eina_strbuf_append_char(*buf, ','); 335 eina_strbuf_append_char(*buf, ',');
320 eina_strbuf_append_buffer(*buf, iter_param); 336 eina_strbuf_append_buffer(*buf, iter_param);
321 eina_strbuf_append(*buf, ")\n"); 337 eina_strbuf_append(*buf, ")\n");
322 _generate_loop_content(buf, inner_type, iter_param); 338 _generate_loop_content(buf, inner_type, iter_param, indent);
323 } 339 }
324 else if (t == EOLIAN_TYPE_BUILTIN_INARRAY) 340 else if (t == EOLIAN_TYPE_BUILTIN_INARRAY)
325 { 341 {
326 const Eolian_Typedecl *ts = _have_struct_owned_fields(inner_type); 342 const Eolian_Typedecl *ts = _have_struct_owned_fields(inner_type);
327 if (ts) 343 if (ts)
328 { 344 {
329 eina_strbuf_append_printf(*buf, " EINA_INARRAY_FOREACH("); 345 eina_strbuf_append_printf(*buf, " %sEINA_INARRAY_FOREACH(", indent);
330 eina_strbuf_append_buffer(*buf, param); 346 eina_strbuf_append_buffer(*buf, param);
331 eina_strbuf_append_char(*buf, ','); 347 eina_strbuf_append_char(*buf, ',');
332 eina_strbuf_append_buffer(*buf, iter_param); 348 eina_strbuf_append_buffer(*buf, iter_param);
333 eina_strbuf_append(*buf, ")\n"); 349 eina_strbuf_append(*buf, ")\n");
334 _generate_inarray_loop(buf, ts, iter_param); 350 _generate_inarray_loop(buf, ts, iter_param, indent);
335 } 351 }
336 _write_free_call(*buf, "eina_inarray_free", param, ""); 352 _write_free_call(*buf, "eina_inarray_free", param, indent, "");
337 } 353 }
338 else if (t == EOLIAN_TYPE_BUILTIN_INLIST) 354 else if (t == EOLIAN_TYPE_BUILTIN_INLIST)
339 { 355 {
340 eina_strbuf_append_printf(*buf, " EINA_INLIST_FREE("); 356 eina_strbuf_append_printf(*buf, " %sEINA_INLIST_FREE(", indent);
341 eina_strbuf_append_buffer(*buf, param); 357 eina_strbuf_append_buffer(*buf, param);
342 eina_strbuf_append_char(*buf, ','); 358 eina_strbuf_append_char(*buf, ',');
343 eina_strbuf_append_buffer(*buf, iter_param); 359 eina_strbuf_append_buffer(*buf, iter_param);
344 eina_strbuf_append(*buf, ")\n"); 360 eina_strbuf_append(*buf, ")\n");
345 eina_strbuf_append(*buf, " {\n"); 361 eina_strbuf_append(*buf, " {\n");
346 _write_free_call(*buf, eolian_type_free_func_get(type), iter_param, " "); 362 _write_free_call(*buf, eolian_type_free_func_get(type), iter_param, indent, " ");
347 eina_strbuf_append(*buf, " }\n"); 363 eina_strbuf_append(*buf, " }\n");
348 } 364 }
349 else if (t == EOLIAN_TYPE_BUILTIN_ITERATOR) 365 else if (t == EOLIAN_TYPE_BUILTIN_ITERATOR)
350 { 366 {
351 eina_strbuf_append_printf(*buf, " EINA_ITERATOR_FOREACH("); 367 eina_strbuf_append_printf(*buf, " %sEINA_ITERATOR_FOREACH(", indent);
352 eina_strbuf_append_buffer(*buf, param); 368 eina_strbuf_append_buffer(*buf, param);
353 eina_strbuf_append_char(*buf, ','); 369 eina_strbuf_append_char(*buf, ',');
354 eina_strbuf_append_buffer(*buf, iter_param); 370 eina_strbuf_append_buffer(*buf, iter_param);
355 eina_strbuf_append(*buf, ")\n"); 371 eina_strbuf_append(*buf, ")\n");
356 _generate_loop_content(buf, inner_type, iter_param); 372 _generate_loop_content(buf, inner_type, iter_param, indent);
357 _write_free_call(*buf, "eina_iterator_free", param, ""); 373 _write_free_call(*buf, "eina_iterator_free", param, indent, "");
358 } 374 }
359 else if (t == EOLIAN_TYPE_BUILTIN_ACCESSOR) 375 else if (t == EOLIAN_TYPE_BUILTIN_ACCESSOR)
360 { 376 {
361 eina_strbuf_append_printf(*buf, " unsigned int %s_i = 0;\n", eolian_parameter_name_get(parameter)); 377 eina_strbuf_append_printf(*buf, " %sunsigned int %s_i = 0;\n", pname, indent);
362 eina_strbuf_append_printf(*buf, " EINA_ACCESSOR_FOREACH("); 378 eina_strbuf_append_printf(*buf, " %sEINA_ACCESSOR_FOREACH(", indent);
363 eina_strbuf_append_buffer(*buf, param); 379 eina_strbuf_append_buffer(*buf, param);
364 eina_strbuf_append_printf(*buf, ",%s_i,", eolian_parameter_name_get(parameter)); 380 eina_strbuf_append_printf(*buf, ",%s_i,", pname);
365 eina_strbuf_append_buffer(*buf, iter_param); 381 eina_strbuf_append_buffer(*buf, iter_param);
366 eina_strbuf_append(*buf, ")\n"); 382 eina_strbuf_append(*buf, ")\n");
367 _generate_loop_content(buf, inner_type, iter_param); 383 _generate_loop_content(buf, inner_type, iter_param, indent);
368 _write_free_call(*buf, "eina_accessor_free", param, ""); 384 _write_free_call(*buf, "eina_accessor_free", param, indent, "");
369 } 385 }
370 else if (t == EOLIAN_TYPE_BUILTIN_HASH) 386 else if (t == EOLIAN_TYPE_BUILTIN_HASH)
371 { 387 {
372 eina_strbuf_append_printf(*buf," eina_hash_free_cb_set("); 388 eina_strbuf_append_printf(*buf," %seina_hash_free_cb_set(", indent);
373 eina_strbuf_append_buffer(*buf, param); 389 eina_strbuf_append_buffer(*buf, param);
374 eina_strbuf_append_printf(*buf, ",%s);\n",_get_free_func(type)); 390 eina_strbuf_append_printf(*buf, ",%s);\n",_get_free_func(type));
375 eina_strbuf_append_printf(*buf," eina_hash_free("); 391 eina_strbuf_append_printf(*buf," %seina_hash_free(", indent);
376 eina_strbuf_append_buffer(*buf, param); 392 eina_strbuf_append_buffer(*buf, param);
377 eina_strbuf_append(*buf, ");\n"); 393 eina_strbuf_append(*buf, ");\n");
378 } 394 }
379 else if (t == EOLIAN_TYPE_BUILTIN_ARRAY) 395 else if (t == EOLIAN_TYPE_BUILTIN_ARRAY)
380 { 396 {
381 eina_strbuf_append_printf(*buf, " while(("); 397 eina_strbuf_append_printf(*buf, " %swhile((", indent);
382 eina_strbuf_append_buffer(*buf, iter_param); 398 eina_strbuf_append_buffer(*buf, iter_param);
383 eina_strbuf_append_printf(*buf, " = eina_array_pop("); 399 eina_strbuf_append_printf(*buf, " = eina_array_pop(");
384 eina_strbuf_append_buffer(*buf, param); 400 eina_strbuf_append_buffer(*buf, param);
385 eina_strbuf_append_printf(*buf, ")))\n"); 401 eina_strbuf_append_printf(*buf, ")))\n");
386 _generate_loop_content(buf, inner_type, iter_param); 402 _generate_loop_content(buf, inner_type, iter_param, indent);
387 eina_strbuf_append_printf(*buf, " eina_array_free("); 403 eina_strbuf_append_printf(*buf, " %seina_array_free(", indent);
388 eina_strbuf_append_buffer(*buf, param); 404 eina_strbuf_append_buffer(*buf, param);
389 eina_strbuf_append_printf(*buf, ");\n"); 405 eina_strbuf_append_printf(*buf, ");\n");
390 } 406 }
@@ -429,11 +445,12 @@ _gen_function_param_fallback(Eina_Iterator *itr, Eina_Strbuf *fallback_free_owne
429 //check if we might want to free or handle the children 445 //check if we might want to free or handle the children
430 if (!inner_type || !eolian_type_is_owned(inner_type)) 446 if (!inner_type || !eolian_type_is_owned(inner_type))
431 { 447 {
432 _generate_normal_free(&fallback_free_ownership, btype, param_call, ""); 448 _generate_normal_free(&fallback_free_ownership, btype, param_call, "", "");
433 } 449 }
434 else if (inner_type && eolian_type_is_owned(inner_type)) 450 else if (inner_type && eolian_type_is_owned(inner_type))
435 { 451 {
436 _generate_iterative_free(&fallback_free_ownership, btype, inner_type, pr, param_call); 452 const char *pn = eolian_parameter_name_get(pr);
453 _generate_iterative_free(&fallback_free_ownership, btype, inner_type, pn, param_call, "");
437 } 454 }
438 } 455 }
439 eina_iterator_free(itr); 456 eina_iterator_free(itr);