Hash :
e4808e3a
Author :
Thomas de Grivel
Date :
2025-03-16T18:18:08
ikc3 --copy for pass by copy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/* kc3
* Copyright from 2022 to 2025 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "alloc.h"
#include "assert.h"
#include "struct_type.h"
#include "pstruct_type.h"
#include "sym.h"
#include "tag.h"
void pstruct_type_clean (p_struct_type *st)
{
struct_type_delete(*st);
*st = NULL;
}
p_struct_type * pstruct_type_init (p_struct_type *st,
const s_sym *module,
const s_list *spec)
{
s_struct_type *tmp;
if (! (tmp = struct_type_new(module, spec)))
return NULL;
*st = tmp;
return st;
}
p_struct_type * pstruct_type_init_cast (p_struct_type *st,
const s_sym * const *type,
s_tag *tag)
{
assert(st);
assert(tag);
switch (tag->type) {
case TAG_PSTRUCT_TYPE:
if (*type == &g_sym_StructType)
return pstruct_type_init_copy(st, &tag->data.pstruct_type);
default:
break;
}
err_write_1("struct_type_init_cast: cannot cast ");
err_write_1(tag_type_to_string(tag->type));
err_puts(" to StructType");
assert(! "struct_type_init_cast: cannot cast to StructType");
return NULL;
}
p_struct_type * pstruct_type_init_copy (p_struct_type *st,
p_struct_type *src)
{
s_struct_type *tmp;
if (env_global()->pass_by_copy)
tmp = struct_type_new_copy(*src);
else
tmp = struct_type_new_ref(*src);
if (! tmp)
return NULL;
*st = tmp;
return st;
}
p_struct_type * pstruct_type_init_clean (p_struct_type *st,
const s_struct_type *src,
const s_cfn *clean)
{
p_struct_type tmp = NULL;
assert(st);
assert(src);
assert(clean);
if (! (tmp = alloc(sizeof(s_struct_type))))
return NULL;
if (! struct_type_init_copy(tmp, src)) {
free(tmp);
return NULL;
}
tmp->clean = (f_clean) clean->ptr.f;
*st = tmp;
return st;
}