From af7cc32343293f3ae5f4aa1102ad781e40413dbe Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Thu, 15 Sep 2016 16:56:43 +0200 Subject: [PATCH] eolian gen2: full type generator --- src/bin/eolian2/main.c | 7 ++-- src/bin/eolian2/main.h | 2 +- src/bin/eolian2/types.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 101 insertions(+), 7 deletions(-) diff --git a/src/bin/eolian2/main.c b/src/bin/eolian2/main.c index 88790b2..731f1b1 100644 --- a/src/bin/eolian2/main.c +++ b/src/bin/eolian2/main.c @@ -202,12 +202,11 @@ _read_file(const char *fname) return eina_strbuf_manage_new_length(cont, fs); } -char *eo_gen_class_full_name_get(const Eolian_Class *cl) +char *eo_gen_c_full_name_get(const char *nm) { - Eina_Stringshare *cln = eolian_class_full_name_get(cl); - if (!cln) + if (!nm) return NULL; - char *buf = strdup(cln); + char *buf = strdup(nm); if (!buf) return NULL; for (char *p = strchr(buf, '.'); p; p = strchr(p, '.')) diff --git a/src/bin/eolian2/main.h b/src/bin/eolian2/main.h index 985e806..b88ca6b 100644 --- a/src/bin/eolian2/main.h +++ b/src/bin/eolian2/main.h @@ -31,6 +31,6 @@ extern int _eolian_gen_log_dom; #endif #define CRIT(...) EINA_LOG_DOM_CRIT(_eolian_gen_log_dom, __VA_ARGS__) -char *eo_gen_class_full_name_get(const Eolian_Class *cl); +char *eo_gen_c_full_name_get(const char *nm); #endif diff --git a/src/bin/eolian2/types.c b/src/bin/eolian2/types.c index a2970ce..5b59733 100644 --- a/src/bin/eolian2/types.c +++ b/src/bin/eolian2/types.c @@ -3,7 +3,102 @@ static Eina_Strbuf * _type_generate(const Eolian_Typedecl *tp, Eina_Bool full, Eina_Bool legacy) { - return NULL; + Eolian_Typedecl_Type tpt = eolian_typedecl_type_get(tp); + Eina_Strbuf *buf = eina_strbuf_new(); + switch (tpt) + { + case EOLIAN_TYPEDECL_ALIAS: + { + Eina_Stringshare *tn = eolian_typedecl_c_type_get(tp); + eina_strbuf_append(buf, tn); + eina_stringshare_del(tn); + break; + } + case EOLIAN_TYPEDECL_STRUCT: + case EOLIAN_TYPEDECL_STRUCT_OPAQUE: + { + const Eolian_Struct_Type_Field *memb; + char *fn = eo_gen_c_full_name_get(eolian_typedecl_full_name_get(tp)); + if (tpt == EOLIAN_TYPEDECL_STRUCT_OPAQUE || !full) + { + eina_strbuf_append_printf(buf, "typedf struct _%s %s", fn, fn); + free(fn); + break; + } + eina_strbuf_append_printf(buf, "typedef struct _%s\n{\n", fn); + Eina_Iterator *membs = eolian_typedecl_struct_fields_get(tp); + EINA_ITERATOR_FOREACH(membs, memb) + { + const Eolian_Type *mtp = eolian_typedecl_struct_field_type_get(memb); + Eina_Stringshare *ct = NULL; + if (eolian_type_type_get(mtp) == EOLIAN_TYPE_STATIC_ARRAY) + { + ct = eolian_type_c_type_get(eolian_type_base_type_get(mtp)); + eina_strbuf_append_printf(buf, " %s%s%s[%zu];", + ct, strchr(ct, '*') ? "" : " ", + eolian_typedecl_struct_field_name_get(memb), + eolian_type_array_size_get(mtp)); + } + else + { + ct = eolian_type_c_type_get(mtp); + eina_strbuf_append_printf(buf, " %s%s%s;", + ct, strchr(ct, '*') ? "" : " ", + eolian_typedecl_struct_field_name_get(memb)); + } + eina_stringshare_del(ct); + eina_strbuf_append(buf, "\n"); + } + eina_iterator_free(membs); + eina_strbuf_append_printf(buf, "} %s", fn); + free(fn); + break; + } + case EOLIAN_TYPEDECL_ENUM: + { + const Eolian_Enum_Type_Field *memb; + if (!full) + break; + eina_strbuf_append_printf(buf, "typedef enum\n{\n"); + Eina_Iterator *membs = eolian_typedecl_enum_fields_get(tp); + Eina_Bool next = eina_iterator_next(membs, (void **)&memb); + while (next) + { + const Eolian_Expression *vale = + eolian_typedecl_enum_field_value_get(memb, EINA_FALSE); + Eina_Stringshare *membn = + eolian_typedecl_enum_field_c_name_get(memb); + if (!vale) + eina_strbuf_append_printf(buf, " %s", membn); + else + { + Eolian_Value val = + eolian_expression_eval(vale, EOLIAN_MASK_INT); + const char *lit = eolian_expression_value_to_literal(&val); + eina_strbuf_append_printf(buf, " %s = %s", membn, lit); + const char *exp = eolian_expression_serialize(vale); + if (exp && strcmp(lit, exp)) + eina_strbuf_append_printf(buf, " /* %s */", exp); + eina_stringshare_del(exp); + eina_stringshare_del(lit); + } + eina_stringshare_del(membn); + next = eina_iterator_next(membs, (void **)&memb); + if (next) + eina_strbuf_append(buf, ","); + eina_strbuf_append(buf, "\n"); + } + eina_iterator_free(membs); + char *fn = eo_gen_c_full_name_get(eolian_typedecl_full_name_get(tp)); + eina_strbuf_append_printf(buf, "} %s", fn); + free(fn); + break; + } + default: + eina_strbuf_reset(buf); + break; + } + return buf; } void eo_gen_types_header_gen(const char *eof, Eina_Strbuf *buf, @@ -48,7 +143,7 @@ Eina_Strbuf *eo_gen_class_typedef_gen(const char *eof) const Eolian_Class *cl = eolian_class_get_by_file(eof); if (!cl) return NULL; - char *clfn = eo_gen_class_full_name_get(cl); + char *clfn = eo_gen_c_full_name_get(eolian_class_full_name_get(cl)); if (!clfn) return NULL; Eina_Strbuf *ret = eina_strbuf_new(); -- 2.7.4