From 4e927bbc1de06f4e17049327a6538af8bd73b5d4 Mon Sep 17 00:00:00 2001 From: Pantelis Antoniou Date: Wed, 1 Jan 2025 11:46:00 +0200 Subject: [PATCH] mutation gone Signed-off-by: Pantelis Antoniou --- include/libfyaml.h | 14 + src/reflection/fy-reflection-private.h | 1 + src/reflection/fy-reflection.c | 36 ++- src/tool/fy-tool.c | 385 +++++-------------------- 4 files changed, 110 insertions(+), 326 deletions(-) diff --git a/include/libfyaml.h b/include/libfyaml.h index afee9dc..71d9405 100644 --- a/include/libfyaml.h +++ b/include/libfyaml.h @@ -9901,6 +9901,20 @@ struct fy_document * fy_type_info_get_yaml_annotation(const struct fy_type_info *ti) FY_EXPORT; +/** + * fy_type_info_get_yaml_comment() - Get the yaml annotation of this type as string + * + * Retrieve a document containing the yaml keyword annotations of this type as a string + * + * @ti: The type info + * + * Returns: + * The yaml comment or NULL + */ +const char * +fy_type_info_get_yaml_comment(const struct fy_type_info *ti) + FY_EXPORT; + const char * fy_type_info_get_yaml_name(const struct fy_type_info *ti) FY_EXPORT; diff --git a/src/reflection/fy-reflection-private.h b/src/reflection/fy-reflection-private.h index 0d4b93a..c984b0e 100644 --- a/src/reflection/fy-reflection-private.h +++ b/src/reflection/fy-reflection-private.h @@ -239,6 +239,7 @@ struct fy_decl { struct fy_document *fyd_yaml; /* the YAML keyworded */ bool fyd_yaml_parsed; char *yaml_comment; + bool yaml_comment_generated; union { struct { diff --git a/src/reflection/fy-reflection.c b/src/reflection/fy-reflection.c index db1731d..53deb00 100644 --- a/src/reflection/fy-reflection.c +++ b/src/reflection/fy-reflection.c @@ -1312,25 +1312,20 @@ struct fy_document *fy_decl_get_yaml_annotation(struct fy_decl *decl) const char *fy_decl_get_yaml_comment(struct fy_decl *decl) { struct fy_document *fyd; - char *s, *e; if (!decl) return NULL; + if (decl->yaml_comment_generated) + return decl->yaml_comment; + if (!decl->yaml_comment) { fyd = fy_decl_get_yaml_annotation(decl); - if (fyd) { - decl->yaml_comment = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE); - if (decl->yaml_comment) { - /* trim newlines at the end */ - s = decl->yaml_comment; - e = s + strlen(s); - while (s < e && e[-1] == '\n') - *--e = '\0'; - } - } + if (fyd) + decl->yaml_comment = fy_emit_document_to_string(fyd, + FYECF_MODE_FLOW_ONELINE | FYECF_WIDTH_INF | FYECF_NO_ENDING_NEWLINE); } - + decl->yaml_comment_generated = true; return decl->yaml_comment; } @@ -3323,6 +3318,23 @@ struct fy_document *fy_type_info_get_yaml_annotation(const struct fy_type_info * return fy_decl_get_yaml_annotation(decl); } +const char *fy_type_info_get_yaml_comment(const struct fy_type_info *ti) +{ + struct fy_type_info_wrapper *tiw; + struct fy_decl *decl; + + tiw = fy_type_info_wrapper_from_info(ti); + if (!tiw) + return NULL; + + decl = tiw->decl; + if (!decl) + return NULL; + + return fy_decl_get_yaml_comment(decl); +} + + struct fy_document *fy_field_info_get_yaml_annotation(const struct fy_field_info *fi) { return fy_decl_get_yaml_annotation(fy_decl_from_field_info(fi)); diff --git a/src/tool/fy-tool.c b/src/tool/fy-tool.c index 0e3e1b3..7117a04 100644 --- a/src/tool/fy-tool.c +++ b/src/tool/fy-tool.c @@ -2089,11 +2089,6 @@ struct reflection_type_ops { int (*cmp)(struct reflection_type_data *rtd, const void *a, const void *b); }; -struct reflection_type { - const char *name; - const struct reflection_type_ops ops; -}; - /* instance datas */ #define STRUCT_ID_FIELD_PRESENT_INLINE 128 struct struct_instance_data { @@ -2164,7 +2159,7 @@ enum reflection_type_data_flags { RTDF_PTR_PURE = FY_BIT(1), /* is pointer, but pointer to pure data */ RTDF_SPECIALIZED = FY_BIT(2), /* type was specialized before */ RTDF_HAS_ANNOTATION = FY_BIT(3), /* type has a yaml annotation */ - RTDF_MUTATED = FY_BIT(4), /* type was mutated */ + RTDF_CLONED = FY_BIT(4), /* type was cloned */ RTDF_PURITY_MASK = (RTDF_UNPURE | RTDF_PTR_PURE), RTDF_SPECIALIZING = FY_BIT(5), /* in progress */ @@ -2176,10 +2171,10 @@ struct reflection_type_data { int idx; struct reflection_type_system *rts; const struct fy_type_info *ti; + const struct fy_field_info *fi; /* XXX */ struct reflection_type_data *rtd_source; /* XXX */ struct reflection_type_data *rtd_parent; /* XXX */ - void *parent_addr; /* XXX */ - const char *mutation_name; /* XXX */ + struct reflection_field_data *rfd; /* XXX */ const struct reflection_type_ops *ops; enum reflection_type_data_flags flags; /* flags of the type */ const char *flat_field; /* set to field which flattens us */ @@ -2188,7 +2183,7 @@ struct reflection_type_data { bool skip_unknown; /* allowed to skip unknown fields */ bool document; struct fy_document *yaml_annotation; /* the yaml annotation */ - char *yaml_annotation_str; /* the annotation as a string */ + const char *yaml_annotation_str; /* the annotation as a string */ struct fy_node *fyn_default; void *default_value; struct fy_node *fyn_fill; /* fill for constant arrays */ @@ -2366,7 +2361,13 @@ reflection_type_data_lookup_field_by_unsigned_enum_value(struct reflection_type_ static inline struct reflection_field_data * struct_field_data(struct reflection_type_data *rtd_parent, void *parent_addr) { - return rtd_parent && rtd_parent->ti->kind == FYTK_STRUCT ? parent_addr : NULL; + if (!rtd_parent) + return NULL; + + if (rtd_parent->ti->kind != FYTK_STRUCT && rtd_parent->ti->kind != FYTK_UNION) + return NULL; + + return parent_addr; } static inline bool @@ -3012,6 +3013,7 @@ const_array_setup_child(struct reflection_object *ro, struct reflection_object * item_size = rtd_dep->ti->size; data = ro_parent->data + item_size * idx; + fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); ret = reflection_object_setup(ro, ro_parent, (void *)(uintptr_t)idx, rtd_dep, fyp, fye, path, data, item_size); if (ret) @@ -3528,6 +3530,7 @@ static int struct_setup(struct reflection_object *ro, struct fy_parser *fyp, str if (!id->ro_flatten) goto err_out; + fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); rc = reflection_object_setup(id->ro_flatten, ro, id->rfd_flatten, id->rfd_flatten->rtd, fyp, fye, path, field_data, @@ -3743,8 +3746,10 @@ struct_setup_child(struct reflection_object *ro, struct reflection_object *ro_pa struct fy_document *fyd_complex_key; id = &ro_parent->id_struct; - if (id->ro_flatten) + if (id->ro_flatten) { + fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); return reflection_object_setup_child(ro, id->ro_flatten, fyp, fye, path); + } rtd = ro_parent->rtd; assert(rtd); @@ -3813,6 +3818,7 @@ struct_setup_child(struct reflection_object *ro, struct reflection_object *ro_pa field_data = &bitfield_data; } + fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); rc = reflection_object_setup(ro, ro_parent, rfd, rfd->rtd, fyp, fye, path, field_data, ti->size); if (rc) @@ -4526,6 +4532,7 @@ static int dyn_array_setup(struct reflection_object *ro, struct fy_parser *fyp, if (!ro->parent || !ro->parent_addr || ro->parent->rtd->ti->kind != FYTK_STRUCT || ro->rtd->ti->kind != FYTK_PTR) { fy_event_report(fyp, fye, FYEP_VALUE, FYET_ERROR, "%s:%d internal error", __FILE__, __LINE__); + fprintf(stderr, "!ro->parent=%d !ro->parent_addr=%d\n", !ro->parent, !ro->parent_addr); goto err_out; } @@ -4686,6 +4693,7 @@ dyn_array_setup_child(struct reflection_object *ro, struct reflection_object *ro data = *(void **)ro_parent->data + item_size * idx; + fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); rc = reflection_object_setup(ro, ro_parent, (void *)(uintptr_t)idx, rtd_dep, fyp, fye, path, data, item_size); if (rc) @@ -4802,14 +4810,13 @@ void dyn_array_dtor(struct reflection_type_data *rtd, void *data) if (rtd->counter) { /* this must have been mutated, and filled in */ assert(rtd->rtd_parent); - assert(rtd->parent_addr); + assert(rtd->rfd); + + rfd = rtd->rfd; rfd_counter = reflection_type_data_lookup_field(rtd->rtd_parent, rtd->counter); assert(rfd_counter); - /* the parent address is the field data */ - rfd = rtd->parent_addr; - /* find out where the parent structure address */ parent_data = data - rfd->fi->offset; @@ -5275,9 +5282,6 @@ void reflection_type_data_destroy(struct reflection_type_data *rtd) reflection_free(rts, rtd->enum_missing_value); } - if (rtd->yaml_annotation_str) - free(rtd->yaml_annotation_str); - if (rtd->rtd_dep && !rtd->rtd_dep_recursive) reflection_type_data_destroy(rtd->rtd_dep); @@ -5314,18 +5318,18 @@ void reflection_type_system_dump(struct reflection_type_system *rts) if (rtd == NULL) continue; printf("#%d:'%s' T#%d", rtd->idx, rtd->ti->fullname, fy_type_info_get_id(rtd->ti)); - printf(" %s%s%s%s%s", + printf("%s%s%s%s%s", (rtd->flags & RTDF_UNPURE) ? " UNPURE" : "", (rtd->flags & RTDF_PTR_PURE) ? " PTR_PURE" : "", (rtd->flags & RTDF_SPECIALIZED) ? " SPECIALIZED" : "", (rtd->flags & RTDF_HAS_ANNOTATION) ? " HAS_ANNOTATION" : "", - (rtd->flags & RTDF_MUTATED) ? " MUTATED" : ""); - if (rtd->flags & RTDF_MUTATED) - printf(" MUT='%s'", rtd->mutation_name); + (rtd->flags & RTDF_CLONED) ? " CLONED" : ""); if (rtd->rtd_dep) printf(" dep: #%d:'%s'", rtd->rtd_dep->idx, rtd->rtd_dep->ti->fullname); if (rtd->rtd_source) printf(" src: #%d:'%s'", rtd->rtd_source->idx, rtd->rtd_source->ti->fullname); + if (rtd->rtd_parent && rtd->rfd) + printf(" '%s'.%s", rtd->rtd_parent->ti->fullname, rtd->rfd->fi->name); if (rtd->yaml_annotation_str) printf(" %s", rtd->yaml_annotation_str); printf(" %d", rtd->refs); @@ -5364,13 +5368,11 @@ void reflection_type_system_destroy(struct reflection_type_system *rts) struct reflection_type_data * reflection_setup_type(struct reflection_type_system *rts, - const struct fy_type_info *ti, - const struct reflection_type_ops *ops); + const struct fy_type_info *ti); struct reflection_type_data * reflection_setup_type_lookup(struct reflection_type_system *rts, struct reflection_type_data *rtd_parent, - const struct fy_field_info *fi, const struct fy_type_info *ti, - const struct reflection_type_ops *ops) + const struct fy_field_info *fi, const struct fy_type_info *ti) { struct reflection_type_data *rtd; size_t i; @@ -5378,26 +5380,10 @@ reflection_setup_type_lookup(struct reflection_type_system *rts, struct reflecti if (!rts || !ti) return NULL; -#if 0 - { - struct fy_document *fyd; - char *yaml_str = NULL; - - fyd = fy_type_info_get_yaml_annotation(ti); - if (fyd) - yaml_str = fy_emit_document_to_string(fyd, FYECF_MODE_FLOW_ONELINE | FYECF_WIDTH_INF | FYECF_NO_ENDING_NEWLINE); - - fprintf(stderr, "%s: ti=%s - yaml: %s\n", __func__, ti->fullname, yaml_str ? yaml_str : "NULL"); - - if (yaml_str) - free(yaml_str); - } -#endif - rtd = NULL; for (i = 0; i < rts->rtd_count; i++) { rtd = rts->rtds[i]; - if (rtd->ti == ti) + if (rtd->ti == ti && rtd->rtd_parent == rtd_parent && rtd->fi == fi) break; rtd = NULL; } @@ -5407,8 +5393,7 @@ reflection_setup_type_lookup(struct reflection_type_system *rts, struct reflecti struct reflection_type_data * reflection_setup_type_recursive(struct reflection_type_system *rts, struct reflection_type_data *rtd_parent, - const struct fy_field_info *fi, const struct fy_type_info *ti, - const struct reflection_type_ops *ops) + const struct fy_field_info *fi, const struct fy_type_info *ti) { struct reflection_type_data *rtd; size_t i; @@ -5421,7 +5406,7 @@ reflection_setup_type_recursive(struct reflection_type_system *rts, struct refle rtd = rts->rtds_setup[i]; if (!rtd) continue; - if (rtd->ti == ti) + if (rtd->ti == ti && rtd->rtd_parent == rtd_parent && rtd->fi == fi) break; rtd = NULL; } @@ -5431,13 +5416,13 @@ reflection_setup_type_recursive(struct reflection_type_system *rts, struct refle struct reflection_type_data * reflection_setup_type_resolve(struct reflection_type_system *rts, struct reflection_type_data *rtd_parent, - const struct fy_field_info *fi, const struct fy_type_info *ti, - const struct reflection_type_ops *ops) + const struct fy_field_info *fi, const struct fy_type_info *ti) { struct reflection_type_data *rtd, *rtd2; size_t i; + int idx; - rtd = reflection_setup_type_recursive(rts, rtd_parent, fi, ti, ops); + rtd = reflection_setup_type_recursive(rts, rtd_parent, fi, ti); if (rtd) { fprintf(stderr, "%s: parent='%s' ti->fullname='%s' RECURSIVE dependent_type ('%s')\n", __func__, rtd_parent->ti->fullname, @@ -5452,14 +5437,25 @@ reflection_setup_type_resolve(struct reflection_type_system *rts, struct reflect } /* exact match? */ - rtd = reflection_setup_type_lookup(rts, rtd_parent, fi, ti, ops); + rtd = reflection_setup_type_lookup(rts, rtd_parent, fi, ti); if (rtd) return reflection_type_data_ref(rtd); - rtd = reflection_setup_type(rts, ti, ops); + rtd = reflection_setup_type(rts, ti); if (!rtd) return NULL; + rtd->fi = fi; + rtd->rtd_parent = rtd_parent; + if (rtd->fi) { + idx = fy_field_info_index(fi); + if (idx < 0) + return NULL; + assert((size_t)idx < rtd_parent->fields_count); + rtd->rfd = rtd_parent->fields[idx]; + } else + rtd->rfd = NULL; + return rtd; } @@ -5777,154 +5773,6 @@ reflection_type_data_setup_add(struct reflection_type_data *rtd) return 0; } -struct reflection_type_mutation { - const char *mutation_name; - struct reflection_type_data *rtd_parent; - void *parent_addr; - const struct reflection_type_ops *ops; - const char *flat_field; - const char *counter; - const char *key_field; - struct reflection_type_data *rtd_dep; -}; - -static inline void -reflection_type_mutation_reset(struct reflection_type_mutation *rtm) -{ - memset(rtm, 0, sizeof(*rtm)); -} - -struct reflection_type_data * -reflection_type_data_mutate(struct reflection_type_data *rtd_source, const struct reflection_type_mutation *rtm) -{ - struct reflection_type_system *rts; - struct reflection_type_data *rtd = NULL; - struct reflection_field_data *rfd = NULL, *rfd_src; - size_t i; - int rc; - - if (!rtd_source || !rtm || !rtm->mutation_name) - return NULL; - - rts = rtd_source->rts; - assert(rts); - - // fprintf(stderr, "%s: #%d %p (%s)\n", __func__, rtd_source->idx, rtd_source, rtm->mutation_name); - - /* first lookup if an already mutated type with the same characteristics exist */ - for (i = 0; i < rts->rtd_count; i++) { - rtd = rts->rtds[i]; - if (!rtd) - continue; - - /* skip source and all types with different source */ - if (rtd == rtd_source || rtd->rtd_source != rtd_source) - continue; - - /* hit? */ - if ((!rtm->ops || rtm->ops == rtd->ops) && - (!rtm->rtd_parent || rtm->rtd_parent == rtd->rtd_parent) && - (!rtm->parent_addr || rtm->parent_addr == rtd->parent_addr) && - (!rtm->flat_field || !strcmp(rtm->flat_field, rtd->flat_field)) && - (!rtm->counter || !strcmp(rtm->counter, rtd->counter)) && - (!rtm->key_field || !strcmp(rtm->key_field, rtd->key_field)) && - (!rtm->rtd_dep || rtm->rtd_dep == rtd->rtd_dep) && - (!strcmp(rtm->mutation_name, rtd->mutation_name))) { - - rtd = reflection_type_data_ref(rtd); - goto out; - } - } - - // fprintf(stderr, "new MUT! (from #%d)\n", rtd_source->idx); - - rtd = malloc(sizeof(*rtd)); - if (!rtd) - goto err_out; - - memset(rtd, 0, sizeof(*rtd)); - rtd->refs = 1; - - rtd->idx = -1; - rtd->rts = rtd_source->rts; - rtd->ti = rtd_source->ti; - rtd->flat_field = rtm->flat_field ? rtm->flat_field : rtd_source->flat_field; - rtd->counter = rtm->counter ? rtm->counter : rtd_source->counter; - rtd->key_field = rtm->key_field ? rtm->key_field : rtd_source->key_field; - rtd->rtd_parent = rtm->rtd_parent ? rtm->rtd_parent : rtd_source->rtd_parent; - rtd->parent_addr = rtm->parent_addr ? rtm->parent_addr : rtd_source->parent_addr; - rtd->ops = rtm->ops ? rtm->ops : &reflection_ops_table[rtd_source->ti->kind]; - rtd->rtd_dep = rtm->rtd_dep ? rtm->rtd_dep : rtd_source->rtd_dep; - rtd->rtd_dep = reflection_type_data_ref(rtd->rtd_dep); - - rtd->flags = rtd_source->flags & ~(RTDF_SPECIALIZING | RTDF_SPECIALIZED); - - rtd->fields_count = rtd_source->fields_count; - if (rtd->fields_count > 0) { - rtd->fields = malloc(sizeof(*rtd->fields)*rtd->fields_count); - if (!rtd->fields) - goto err_out; - - for (i = 0; i < rtd->fields_count; i++) { - - rfd_src = rtd_source->fields[i]; - - rfd = malloc(sizeof(*rfd)); - if (!rfd) - goto err_out; - - memset(rfd, 0, sizeof(*rfd)); - - rfd->idx = (int)i; - rfd->rtd = reflection_type_data_ref(rfd_src->rtd); - rfd->field_name = rfd_src->field_name; - rfd->fi = rfd_src->fi; - rfd->omit_if_null = rfd_src->omit_if_null; - rfd->omit_on_emit = rfd_src->omit_on_emit; - rfd->required = rfd_src->required; - rfd->is_counter = rfd_src->is_counter; - - rtd->fields[i] = rfd; - } - } - - if (rtm->ops) - rtd->ops = rtm->ops; - if (rtm->rtd_parent) - rtd->rtd_parent = rtm->rtd_parent; - if (rtm->parent_addr) - rtd->parent_addr = rtm->parent_addr; - if (rtm->flat_field) - rtd->flat_field = rtm->flat_field; - if (rtm->counter) - rtd->counter = rtm->counter; - if (rtm->key_field) - rtd->key_field = rtm->key_field; - - rtd->flags |= RTDF_MUTATED; - rtd->rtd_source = reflection_type_data_ref(rtd_source); - rtd->mutation_name = rtm->mutation_name; - - /* and copy annotations (and values) */ - rc = reflection_type_data_copy_annotations(rtd, rtd_source); - if (rc) - goto err_out; - - /* must be the final bit */ - rc = reflection_type_data_add(rtd); - if (rc) - goto err_out; -out: - - /* destroy the source (unref) if all OK */ - reflection_type_data_destroy(rtd_source); - return rtd; - -err_out: - reflection_type_data_destroy(rtd); - return NULL; -} - struct reflection_type_data * reflection_setup_type_specialize(struct reflection_type_data *rtd, struct reflection_type_data *rtd_parent, void *parent_addr, @@ -5932,8 +5780,6 @@ reflection_setup_type_specialize(struct reflection_type_data *rtd, { struct reflection_type_data *rtd_spec; struct reflection_field_data *rfd, *rfd_ref, *rfd_flatten, *rfd_key; - struct reflection_type_mutation rtm; - struct reflection_type_data *rtd_mut; struct fy_node *fyn_terminator; const struct reflection_type_ops *ops; const struct fy_type_info *rfd_ti; @@ -5970,42 +5816,17 @@ reflection_setup_type_specialize(struct reflection_type_data *rtd, break; case FYTK_PTR: fprintf(stderr, "\e[31m%s ptr %s\e[0m\n", __func__, rtd->ti->fullname); - /* only for char * */ assert(rtd->rtd_dep); assert(rtd->rtd_dep->ti); switch (rtd->rtd_dep->ti->kind) { case FYTK_CHAR: - - reflection_type_mutation_reset(&rtm); - rtm.mutation_name = "ptr_char"; - rtm.ops = &ptr_char_ops; - - rtd_mut = reflection_type_data_mutate(rtd, &rtm); - if (!rtd_mut) - goto err_out; -#if 1 - rtd_mut->flags |= RTDF_SPECIALIZING; - is_recursive = false; -#endif - - rtd = rtd_mut; - + /* char * -> text */ + rtd->ops = &ptr_char_ops; break; case FYTK_VOID: - reflection_type_mutation_reset(&rtm); - rtm.mutation_name = "ptr_doc"; - rtm.ops = &ptr_doc_ops; - - rtd_mut = reflection_type_data_mutate(rtd, &rtm); - if (!rtd_mut) - goto err_out; -#if 1 - rtd_mut->flags |= RTDF_SPECIALIZING; - is_recursive = false; -#endif - rtd = rtd_mut; - + /* void * -> doc */ + rtd->ops = &ptr_doc_ops; break; default: @@ -6018,20 +5839,7 @@ reflection_setup_type_specialize(struct reflection_type_data *rtd, /* only for char[] */ switch (rtd->ti->dependent_type->kind) { case FYTK_CHAR: - reflection_type_mutation_reset(&rtm); - rtm.mutation_name = "constarray_char"; - rtm.ops = &constarray_char_ops; - - /* this does not need a parent */ - rtd_mut = reflection_type_data_mutate(rtd, &rtm); - if (!rtd_mut) - goto err_out; -#if 1 - rtd_mut->flags |= RTDF_SPECIALIZING; - is_recursive = false; -#endif - rtd = rtd_mut; - + rtd->ops = &constarray_char_ops; break; default: @@ -6044,50 +5852,21 @@ reflection_setup_type_specialize(struct reflection_type_data *rtd, flatten_field = fy_type_info_get_yaml_string(rtd->ti, "flatten-field"); if (flatten_field) { - assert(!rtd->flat_field); - - // fprintf(stderr, ">>>> struct %s flatten-field=%s\n", rtd->ti->name, flatten_field); - rfd_flatten = reflection_type_data_lookup_field(rtd, flatten_field); if (!rfd_flatten) goto err_out; - reflection_type_mutation_reset(&rtm); - rtm.mutation_name = "flatten"; - rtm.flat_field = flatten_field; - - rtd_mut = reflection_type_data_mutate(rtd, &rtm); - if (!rtd_mut) - goto err_out; -#if 1 - rtd_mut->flags |= RTDF_SPECIALIZING; - is_recursive = false; -#endif - rtd = rtd_mut; + rtd->flat_field = flatten_field; } key_field = fy_type_info_get_yaml_string(rtd->ti, "key"); if (key_field) { - assert(!rtd->key_field); - - // fprintf(stderr, ">>>> struct %s key-field=%s\n", rtd->ti->name, key_field); rfd_key = reflection_type_data_lookup_field(rtd, key_field); if (!rfd_key) goto err_out; - reflection_type_mutation_reset(&rtm); - rtm.mutation_name = "key"; - rtm.key_field = key_field; - - rtd_mut = reflection_type_data_mutate(rtd, &rtm); - if (!rtd_mut) - goto err_out; -#if 1 - rtd_mut->flags |= RTDF_SPECIALIZING; - is_recursive = false; -#endif - rtd = rtd_mut; + rtd->key_field = key_field; } rtd->skip_unknown = fy_type_info_get_yaml_bool(rtd->ti, "skip-unknown"); @@ -6133,19 +5912,16 @@ reflection_setup_type_specialize(struct reflection_type_data *rtd, counter = fy_type_info_get_yaml_string(rfd_ti, "counter"); fyn_terminator = fy_type_info_get_yaml_node(rfd_ti, "terminator"); - if (rfd->rtd->rtd_dep && rfd->rtd->rtd_dep->ti->kind == FYTK_PTR) { - fprintf(stderr, "%s: DEP is PTR\n", __func__); - } - if (counter || fyn_terminator || (rfd->rtd->rtd_dep && rfd->rtd->rtd_dep->ti->kind == FYTK_PTR)) { - reflection_type_mutation_reset(&rtm); - rtm.mutation_name = "dyn_array"; - rtm.ops = &dyn_array_ops; + rfd->rtd->ops = &dyn_array_ops; if (counter) { rfd_ref = reflection_type_data_lookup_field(rtd, counter); - assert(rfd_ref); + if (!rfd_ref) { + fprintf(stderr, "dyn_array counter field '%s' does not exist\n", counter); + goto err_out; + } /* must be an integer */ if (!fy_type_kind_is_integer(rfd_ref->rtd->ti->kind)) { @@ -6159,25 +5935,9 @@ reflection_setup_type_specialize(struct reflection_type_data *rtd, goto err_out; } rfd_ref->is_counter = true; -#if 0 - fprintf(stderr, "%s: %s.%s counter=%s (%s)\n", __func__, - rtd->ti->name, rfd->fi->name, str, - rfd_ref ? rfd_ref->field_name : "N/A"); - fprintf(stderr, "rtd=%p rfd=%p rfd_ref=%p\n", rtd, rfd, rfd_ref); -#endif - rtm.counter = counter; - rtm.rtd_parent = rtd; - rtm.parent_addr = rfd; - } - rtd_mut = reflection_type_data_mutate(rfd->rtd, &rtm); - if (!rtd_mut) - goto err_out; -#if 1 - rtd_mut->flags |= RTDF_SPECIALIZING; - is_recursive = false; -#endif - rfd->rtd = rtd_mut; + rtd->counter = counter; + } } break; @@ -6237,12 +5997,9 @@ reflection_setup_type_specialize(struct reflection_type_data *rtd, } rtd->yaml_annotation = fy_type_info_get_yaml_annotation(rtd->ti); - if (rtd->yaml_annotation) { - - rtd->yaml_annotation_str = fy_emit_document_to_string(rtd->yaml_annotation, - FYECF_MODE_FLOW_ONELINE | FYECF_WIDTH_INF | FYECF_NO_ENDING_NEWLINE); - assert(rtd->yaml_annotation_str); + rtd->yaml_annotation_str = fy_type_info_get_yaml_comment(rtd->ti); + if (rtd->yaml_annotation) { rtd->flags |= RTDF_HAS_ANNOTATION; rc = reflection_type_data_generate_values(rtd, rtd, "default", &rtd->fyn_default, &rtd->default_value); @@ -6306,8 +6063,7 @@ reflection_setup_type_specialize(struct reflection_type_data *rtd, struct reflection_type_data * reflection_setup_type(struct reflection_type_system *rts, - const struct fy_type_info *ti, - const struct reflection_type_ops *ops) + const struct fy_type_info *ti) { struct reflection_type_data *rtd = NULL; struct reflection_field_data *rfd; @@ -6319,8 +6075,6 @@ reflection_setup_type(struct reflection_type_system *rts, assert(ti->fullname); fprintf(stderr, "%s: ti->fullname='%s'\n", __func__, ti->fullname); - if (!ops) - ops = &reflection_ops_table[ti->kind]; rtd = malloc(sizeof(*rtd)); if (!rtd) goto err_out; @@ -6332,7 +6086,7 @@ reflection_setup_type(struct reflection_type_system *rts, rtd->idx = -1; rtd->rts = rts; rtd->ti = ti; - rtd->ops = ops; + rtd->ops = &reflection_ops_table[ti->kind]; /* do fields */ rtd->fields_count = fy_type_kind_has_fields(ti->kind) ? ti->count : 0; @@ -6363,7 +6117,7 @@ reflection_setup_type(struct reflection_type_system *rts, if (ti->dependent_type) { fprintf(stderr, "%s: ti->fullname='%s' dependent_type: calling reflection_setup_type_resolve('%s')\n", __func__, ti->fullname, ti->dependent_type->fullname); - rtd->rtd_dep = reflection_setup_type_resolve(rts, rtd, NULL, ti->dependent_type, NULL); + rtd->rtd_dep = reflection_setup_type_resolve(rts, rtd, NULL, ti->dependent_type); if (!rtd->rtd_dep) goto err_out; } @@ -6373,7 +6127,7 @@ reflection_setup_type(struct reflection_type_system *rts, fprintf(stderr, "%s: ti->fullname='%s' field '%s': calling reflection_setup_type_resolve()\n", __func__, ti->fullname, rfd->fi->name); - rfd->rtd = reflection_setup_type_resolve(rts, rtd, rfd->fi, rfd->fi->type_info, NULL); + rfd->rtd = reflection_setup_type_resolve(rts, rtd, rfd->fi, rfd->fi->type_info); if (!rfd->rtd) goto err_out; } @@ -6502,7 +6256,7 @@ reflection_type_system_create(const struct reflection_type_system_config *cfg) fprintf(stderr, "%s: reflection dump after root\n", __func__); fy_reflection_dump(rts->cfg.rfl, false, false); - rts->rtd_root = reflection_setup_type(rts, ti_root, NULL); + rts->rtd_root = reflection_setup_type(rts, ti_root); if (!rts->rtd_root) goto err_out; @@ -6518,6 +6272,8 @@ reflection_type_system_create(const struct reflection_type_system_config *cfg) fprintf(stderr, "%s: after specialization\n", __func__); reflection_type_system_dump(rts); + fflush(stdout); + fflush(stderr); return rts; err_out: @@ -6694,6 +6450,7 @@ reflection_compose_process_event(struct fy_parser *fyp, struct fy_event *fye, st return FYCR_ERROR; ro_parent = fy_path_get_parent_user_data(path); + fprintf(stderr, "%s %s:%d ro_parent=%p\n", __func__, __FILE__, __LINE__, ro_parent); rc = !ro_parent ? reflection_object_setup(ro, NULL, NULL, rd->entry, fyp, fye, path, rd->data, rd->data_size) : reflection_object_setup_child(ro, ro_parent, fyp, fye, path);