summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Kolesa <d.kolesa@samsung.com>2014-08-12 17:24:24 +0100
committerDaniel Kolesa <d.kolesa@samsung.com>2014-08-21 09:26:04 +0100
commit732e3f417e07e2edb5f55822bcf4d4d67a5b30b1 (patch)
tree5cd0eac017cecf7bd413b9aa076e2fa79f5b4b7b
parentc81356e2ecf06fc76f28897e8c1679b2d81f9cf7 (diff)
eolian: new API: eolian_type_enum_legacy_prefix_set + generator changes
This change adds a new API to retrieve the legacy prefix for enum members and also adds support for generating enums into the C generator. Besides that, it prevents generation of entire struct contents when the .eo file declares a named struct inside of a typedef - as the struct itself gets generated later on.
-rw-r--r--src/bin/eolian/types_generator.c90
-rw-r--r--src/lib/eolian/Eolian.h11
-rw-r--r--src/lib/eolian/database_type_api.c8
-rw-r--r--src/tests/eolian/data/struct_ref.c5
4 files changed, 102 insertions, 12 deletions
diff --git a/src/bin/eolian/types_generator.c b/src/bin/eolian/types_generator.c
index e770fe3cb8..9da055816f 100644
--- a/src/bin/eolian/types_generator.c
+++ b/src/bin/eolian/types_generator.c
@@ -37,7 +37,7 @@ _desc_generate(const char *desc, Eina_Strbuf *buf)
37} 37}
38 38
39static Eina_Strbuf * 39static Eina_Strbuf *
40_type_generate(const Eolian_Type *tp) 40_type_generate(const Eolian_Type *tp, Eina_Bool in_typedef)
41{ 41{
42 Eina_Strbuf *buf = eina_strbuf_new(); 42 Eina_Strbuf *buf = eina_strbuf_new();
43 _desc_generate(eolian_type_description_get(tp), buf); 43 _desc_generate(eolian_type_description_get(tp), buf);
@@ -48,10 +48,10 @@ _type_generate(const Eolian_Type *tp)
48 { 48 {
49 const Eolian_Type *base_tp = eolian_type_base_type_get(tp); 49 const Eolian_Type *base_tp = eolian_type_base_type_get(tp);
50 Eolian_Type_Type base_tp_type = eolian_type_type_get(base_tp); 50 Eolian_Type_Type base_tp_type = eolian_type_type_get(base_tp);
51 if (base_tp_type == EOLIAN_TYPE_STRUCT) 51 if (base_tp_type == EOLIAN_TYPE_STRUCT || base_tp_type == EOLIAN_TYPE_ENUM)
52 { 52 {
53 const char *name = eolian_type_name_get(tp); 53 const char *name = eolian_type_name_get(tp);
54 Eina_Strbuf *struct_buf = _type_generate(base_tp); 54 Eina_Strbuf *struct_buf = _type_generate(base_tp, EINA_TRUE);
55 eina_strbuf_append_printf(buf, "typedef %s%s%s", 55 eina_strbuf_append_printf(buf, "typedef %s%s%s",
56 eina_strbuf_string_get(struct_buf), 56 eina_strbuf_string_get(struct_buf),
57 name?" ":"", name?name:""); 57 name?" ":"", name?name:"");
@@ -72,6 +72,12 @@ _type_generate(const Eolian_Type *tp)
72 { 72 {
73 const char *member_name; 73 const char *member_name;
74 char *name = _concat_name(tp); 74 char *name = _concat_name(tp);
75 if (in_typedef && name)
76 {
77 eina_strbuf_append_printf(buf, "struct %s", name);
78 free(name);
79 break;
80 }
75 eina_strbuf_append_printf(buf, "struct%s%s {\n", name?" ":"", name?name:""); 81 eina_strbuf_append_printf(buf, "struct%s%s {\n", name?" ":"", name?name:"");
76 free(name); 82 free(name);
77 Eina_Iterator *members = eolian_type_struct_field_names_get(tp); 83 Eina_Iterator *members = eolian_type_struct_field_names_get(tp);
@@ -90,6 +96,62 @@ _type_generate(const Eolian_Type *tp)
90 eina_strbuf_append(buf, "}"); 96 eina_strbuf_append(buf, "}");
91 break; 97 break;
92 } 98 }
99 case EOLIAN_TYPE_ENUM:
100 {
101 const char *member_name;
102 char *name = _concat_name(tp);
103 if (in_typedef)
104 {
105 eina_strbuf_append_printf(buf, "enum %s", name);
106 free(name);
107 break;
108 }
109 char *pre = NULL;
110 eina_strbuf_append_printf(buf, "enum %s {\n", name);
111 if (eolian_type_enum_legacy_prefix_get(tp))
112 {
113 pre = strdup(eolian_type_enum_legacy_prefix_get(tp));
114 free(name);
115 }
116 else
117 pre = name;
118 eina_str_toupper(&pre);
119 Eina_Iterator *members = eolian_type_enum_field_names_get(tp);
120 Eina_Bool next = eina_iterator_next(members, (void**)&member_name);
121 Eina_Strbuf *membuf = eina_strbuf_new();
122 while (next)
123 {
124 const char *desc = eolian_type_enum_field_description_get(tp, member_name);
125 const Eolian_Expression *member = eolian_type_enum_field_get(tp, member_name);
126 char *memb_u = strdup(member_name);
127 eina_str_toupper(&memb_u);
128 eina_strbuf_reset(membuf);
129 eina_strbuf_append(membuf, pre);
130 eina_strbuf_append_char(membuf, '_');
131 eina_strbuf_append(membuf, memb_u);
132 free(memb_u);
133 if (!member)
134 eina_strbuf_append_printf(buf, " %s", eina_strbuf_string_get(membuf));
135 else
136 {
137 Eina_Value *v = NULL;
138 Eolian_Expression_Type et = eolian_expression_eval(member, EOLIAN_MASK_INT, &v);
139 const char *lit = eolian_expression_value_to_literal(v, et);
140 eina_strbuf_append_printf(buf, " %s = %s", eina_strbuf_string_get(membuf), lit);
141 eina_stringshare_del(lit);
142 }
143 next = eina_iterator_next(members, (void**)&member_name);
144 if (next)
145 eina_strbuf_append(buf, ",");
146 if (desc) eina_strbuf_append_printf(buf, " /** %s */", desc);
147 eina_strbuf_append(buf, "\n");
148 }
149 eina_strbuf_free(membuf);
150 free(pre);
151 eina_iterator_free(members);
152 eina_strbuf_append(buf, "}");
153 break;
154 }
93 default: 155 default:
94 { 156 {
95 eina_strbuf_reset(buf); 157 eina_strbuf_reset(buf);
@@ -107,10 +169,7 @@ types_header_generate(const char *eo_filename, Eina_Strbuf *buf)
107 Eina_Iterator *itr = eolian_type_aliases_get_by_file(eo_filename); 169 Eina_Iterator *itr = eolian_type_aliases_get_by_file(eo_filename);
108 EINA_ITERATOR_FOREACH(itr, tp) 170 EINA_ITERATOR_FOREACH(itr, tp)
109 { 171 {
110 /* avoid for the time being */ 172 Eina_Strbuf *type_buf = _type_generate(tp, EINA_TRUE);
111 if (eolian_type_type_get(eolian_type_base_type_get(tp)) == EOLIAN_TYPE_ENUM)
112 continue;
113 Eina_Strbuf *type_buf = _type_generate(tp);
114 if (type_buf) 173 if (type_buf)
115 { 174 {
116 eina_strbuf_append(buf, eina_strbuf_string_get(type_buf)); 175 eina_strbuf_append(buf, eina_strbuf_string_get(type_buf));
@@ -124,7 +183,21 @@ types_header_generate(const char *eo_filename, Eina_Strbuf *buf)
124 itr = eolian_type_structs_get_by_file(eo_filename); 183 itr = eolian_type_structs_get_by_file(eo_filename);
125 EINA_ITERATOR_FOREACH(itr, tp) 184 EINA_ITERATOR_FOREACH(itr, tp)
126 { 185 {
127 Eina_Strbuf *type_buf = _type_generate(tp); 186 Eina_Strbuf *type_buf = _type_generate(tp, EINA_FALSE);
187 if (type_buf)
188 {
189 eina_strbuf_append(buf, eina_strbuf_string_get(type_buf));
190 eina_strbuf_append(buf, ";\n\n");
191 eina_strbuf_free(type_buf);
192 }
193 }
194 eina_iterator_free(itr);
195
196 /* Generation of enums */
197 itr = eolian_type_enums_get_by_file(eo_filename);
198 EINA_ITERATOR_FOREACH(itr, tp)
199 {
200 Eina_Strbuf *type_buf = _type_generate(tp, EINA_FALSE);
128 if (type_buf) 201 if (type_buf)
129 { 202 {
130 eina_strbuf_append(buf, eina_strbuf_string_get(type_buf)); 203 eina_strbuf_append(buf, eina_strbuf_string_get(type_buf));
@@ -133,6 +206,7 @@ types_header_generate(const char *eo_filename, Eina_Strbuf *buf)
133 } 206 }
134 } 207 }
135 eina_iterator_free(itr); 208 eina_iterator_free(itr);
209
136 return EINA_TRUE; 210 return EINA_TRUE;
137} 211}
138 212
diff --git a/src/lib/eolian/Eolian.h b/src/lib/eolian/Eolian.h
index ff2166516f..5dd39253cb 100644
--- a/src/lib/eolian/Eolian.h
+++ b/src/lib/eolian/Eolian.h
@@ -1086,6 +1086,17 @@ EAPI const Eolian_Expression *eolian_type_enum_field_get(const Eolian_Type *tp,
1086EAPI Eina_Stringshare *eolian_type_enum_field_description_get(const Eolian_Type *tp, const char *field); 1086EAPI Eina_Stringshare *eolian_type_enum_field_description_get(const Eolian_Type *tp, const char *field);
1087 1087
1088/* 1088/*
1089 * @brief Get the legacy prefix of enum field names. When not specified,
1090 * enum name is used.
1091 *
1092 * @param[in] tp the type.
1093 * @return the legacy prefix or NULL.
1094 *
1095 * @ingroup Eolian
1096 */
1097EAPI Eina_Stringshare *eolian_type_enum_legacy_prefix_get(const Eolian_Type *tp);
1098
1099/*
1089 * @brief Get the description of a struct/alias type. 1100 * @brief Get the description of a struct/alias type.
1090 * 1101 *
1091 * @param[in] tp the type. 1102 * @param[in] tp the type.
diff --git a/src/lib/eolian/database_type_api.c b/src/lib/eolian/database_type_api.c
index 225d73ad68..9bc9da1ebe 100644
--- a/src/lib/eolian/database_type_api.c
+++ b/src/lib/eolian/database_type_api.c
@@ -173,6 +173,14 @@ eolian_type_enum_field_description_get(const Eolian_Type *tp, const char *field)
173} 173}
174 174
175EAPI Eina_Stringshare * 175EAPI Eina_Stringshare *
176eolian_type_enum_legacy_prefix_get(const Eolian_Type *tp)
177{
178 EINA_SAFETY_ON_NULL_RETURN_VAL(tp, NULL);
179 EINA_SAFETY_ON_FALSE_RETURN_VAL(tp->type == EOLIAN_TYPE_ENUM, NULL);
180 return tp->legacy;
181}
182
183EAPI Eina_Stringshare *
176eolian_type_description_get(const Eolian_Type *tp) 184eolian_type_description_get(const Eolian_Type *tp)
177{ 185{
178 Eolian_Type_Type tpp; 186 Eolian_Type_Type tpp;
diff --git a/src/tests/eolian/data/struct_ref.c b/src/tests/eolian/data/struct_ref.c
index f5d633dc98..326de88a81 100644
--- a/src/tests/eolian/data/struct_ref.c
+++ b/src/tests/eolian/data/struct_ref.c
@@ -11,10 +11,7 @@ typedef Eo Struct;
11#ifndef _STRUCT_EO_TYPES 11#ifndef _STRUCT_EO_TYPES
12#define _STRUCT_EO_TYPES 12#define _STRUCT_EO_TYPES
13 13
14typedef struct _Foo { 14typedef struct _Foo Foo;
15 int field;
16 float another;
17} Foo;
18 15
19typedef struct { 16typedef struct {
20 Foo a; 17 Foo a;