summaryrefslogtreecommitdiff
path: root/src/bin/eolian/impl_generator.c
blob: f33854f4b65300bdb58818d5f924df500757b7e1 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <Eina.h>
#include <string.h>

#include "Eolian.h"
#include "impl_generator.h"
#include "common_funcs.h"

static Eina_Bool
_params_generate(Eolian_Function foo, Eolian_Function_Type ftype, Eina_Bool var_as_ret, Eina_Strbuf *params)
{
   const Eina_List *itr;
   Eolian_Function_Parameter param;
   eina_strbuf_reset(params);
   EINA_LIST_FOREACH(eolian_property_keys_list_get(foo), itr, param)
     {
        const char *pname;
        const char *ptype;
        eolian_parameter_information_get(param, NULL, &ptype, &pname, NULL);
        Eina_Bool had_star = !!strchr(ptype, '*');
        Eina_Bool is_const = eolian_parameter_const_attribute_get(param, ftype == EOLIAN_PROP_GET);
        if (eina_strbuf_length_get(params)) eina_strbuf_append(params, ", ");
        eina_strbuf_append_printf(params, "%s%s%s%s",
              is_const?"const ":"", ptype,
              had_star?"":" ",
              pname);
     }
   if (!var_as_ret)
     {
        Eina_Bool add_star = (ftype == EOLIAN_PROP_GET);
        EINA_LIST_FOREACH(eolian_parameters_list_get(foo), itr, param)
          {
             const char *pname;
             const char *ptype;
             Eolian_Parameter_Dir pdir;
             eolian_parameter_information_get(param, &pdir, &ptype, &pname, NULL);
             Eina_Bool is_const = eolian_parameter_const_attribute_get(param, ftype == EOLIAN_PROP_GET);
             Eina_Bool had_star = !!strchr(ptype, '*');
             if (ftype == EOLIAN_UNRESOLVED || ftype == EOLIAN_METHOD) add_star = (pdir == EOLIAN_OUT_PARAM);
             if (eina_strbuf_length_get(params)) eina_strbuf_append(params, ", ");
             eina_strbuf_append_printf(params, "%s%s%s%s%s",
                   is_const?"const ":"",
                   ptype, had_star?"":" ", add_star?"*":"", pname);
          }
     }
   return EINA_TRUE;
}

static Eina_Bool
_function_exists(const char* func_name, Eina_Strbuf *buffer)
{
   const char *ptr = eina_strbuf_string_get(buffer);
   int func_len = strlen(func_name);
   while ((ptr = strstr(ptr, func_name)) != NULL)
     {
        switch (*(ptr - 1))
          {
           case '\n': case ' ':
                {
                   switch (*(ptr + func_len))
                     {
                      case ' ': case '(':
                         return EINA_TRUE;
                     }
                }
           default:
              ptr++; /* so strstr doesn't fall again on func_name */
          }
     }
   return EINA_FALSE;
}

/* Check if the type is used in the file, not if it is a typedef... */
static Eina_Bool
_type_exists(const char* type_name, Eina_Strbuf *buffer)
{
   const char *ptr = eina_strbuf_string_get(buffer);
   int type_len = strlen(type_name);
   while ((ptr = strstr(ptr, type_name)) != NULL)
     {
        switch (*(ptr - 1))
          {
           case '\n': case ' ': case ',':
                {
                   switch (*(ptr + type_len))
                     {
                      case '\n': case ' ': case ',': case ';':
                         return EINA_TRUE;
                     }
                }
           default:
              ptr++; /* so strstr doesn't fall again on type_name */
          }
     }
   return EINA_FALSE;
}

static Eina_Bool
_prototype_generate(Eolian_Function foo, Eolian_Function_Type ftype, Eina_Strbuf *data_type_buf, char *impl_name, Eina_Strbuf *buffer)
{
   Eina_Bool var_as_ret = EINA_FALSE, ret_const = EINA_FALSE;
   Eina_Strbuf *params = NULL;
   char func_name[100];

   if (!impl_name && eolian_function_is_virtual_pure(foo, ftype)) return EINA_TRUE;

   sprintf(func_name, "_%s_%s%s",
         impl_name?impl_name:lowclass, eolian_function_name_get(foo),
         ftype == EOLIAN_PROP_GET?"_get": (ftype == EOLIAN_PROP_SET?"_set":""));

   if (_function_exists(func_name, buffer)) return EINA_TRUE;

   printf("Generation of function %s\n", func_name);
   const char *rettype = eolian_function_return_type_get(foo, ftype);
   if (ftype == EOLIAN_PROP_GET && !rettype)
     {
        const Eina_List *l = eolian_parameters_list_get(foo);
        if (eina_list_count(l) == 1)
          {
             Eolian_Function_Parameter param = eina_list_data_get(l);
             eolian_parameter_information_get(param, NULL, &rettype, NULL, NULL);
             var_as_ret = EINA_TRUE;
             ret_const = eolian_parameter_const_attribute_get(param, EINA_TRUE);
          }
     }

   params = eina_strbuf_new();
   _params_generate(foo, ftype, var_as_ret, params);
   if (eina_strbuf_length_get(params))
      eina_strbuf_prepend_printf(params, ", ");

   eina_strbuf_append_printf(buffer,
         "EOLIAN static %s%s\n%s(%sEo *obj, %s *pd%s%s)\n{\n\n}\n\n",
         ret_const?"const ":"", !rettype?"void":rettype,
         func_name,
         eolian_function_object_is_const(foo)?"const ":"",
         !eina_strbuf_length_get(data_type_buf) ? "void" : eina_strbuf_string_get(data_type_buf),
         !eina_strbuf_length_get(data_type_buf) ? " EINA_UNUSED" : "",
         eina_strbuf_string_get(params)
         );

   eina_strbuf_free(params);
   return EINA_TRUE;
}

Eina_Bool
impl_source_generate(const Eolian_Class class, Eina_Strbuf *buffer)
{
   Eina_Bool ret = EINA_FALSE;
   Eina_Strbuf *data_type_buf = eina_strbuf_new();
   const Eina_List *itr_funcs;
   Eolian_Function foo;
   Eina_Strbuf *begin = eina_strbuf_new();
   const char *class_name = eolian_class_name_get(class);

   _class_func_names_fill(class, NULL, NULL);

   if (!_type_exists("EFL_BETA_API_SUPPORT", buffer))
     {
        printf("Generation of EFL_BETA_API_SUPPORT\n");
        eina_strbuf_append_printf(begin, "#define EFL_BETA_API_SUPPORT\n");
     }

   if (!_type_exists("<Eo.h>", buffer))
     {
        printf("Generation of #include <Eo.h> and \"%s.eo.h\"\n", lowclass);
        eina_strbuf_append_printf(begin, "#include <Eo.h>\n#include \"%s.eo.h\"\n\n", lowclass);
     }

   /* Little calculation of the prefix of the data */
   const char *data_type = eolian_class_data_type_get(class);
   if (data_type)
     {
        if (strcmp(data_type, "null"))
           eina_strbuf_append_printf(data_type_buf, "%s", data_type);
     }
   else
      eina_strbuf_append_printf(data_type_buf, "%s_Data", class_name);

   /* Definition of the structure */
   const char *data_type_str = eina_strbuf_string_get(data_type_buf);
   if (!_type_exists(data_type_str, buffer) && 0 != eina_strbuf_length_get(data_type_buf))
     {
        printf("Generation of type %s\n", data_type_str);
        eina_strbuf_append_printf(begin, "typedef struct\n{\n\n} %s;\n\n", data_type_str);
     }

   if (eina_strbuf_length_get(begin))
      eina_strbuf_prepend_printf(buffer, "%s", eina_strbuf_string_get(begin));
   eina_strbuf_free(begin);

   /* Properties */
   EINA_LIST_FOREACH(eolian_class_functions_list_get(class, EOLIAN_PROPERTY), itr_funcs, foo)
     {
        const Eolian_Function_Type ftype = eolian_function_type_get(foo);
        if (ftype == EOLIAN_PROP_SET || ftype == EOLIAN_PROPERTY)
           _prototype_generate(foo, EOLIAN_PROP_SET, data_type_buf, NULL, buffer);

        if (ftype == EOLIAN_PROP_GET || ftype == EOLIAN_PROPERTY)
           _prototype_generate(foo, EOLIAN_PROP_GET, data_type_buf, NULL, buffer);
     }

   /* Methods */
   EINA_LIST_FOREACH(eolian_class_functions_list_get(class, EOLIAN_METHOD), itr_funcs, foo)
     {
        _prototype_generate(foo, EOLIAN_METHOD, data_type_buf, NULL, buffer);
     }

   /* Custom constructors */
   EINA_LIST_FOREACH(eolian_class_functions_list_get(class, EOLIAN_CTOR), itr_funcs, foo)
     {
        _prototype_generate(foo, EOLIAN_CTOR, data_type_buf, NULL, buffer);
     }

   if (eolian_class_implements_list_get(class))
     {
        Eolian_Implement impl_desc;
        EINA_LIST_FOREACH(eolian_class_implements_list_get(class), itr_funcs, impl_desc)
          {
             const char *func_name;
             const char *impl_classname;
             Eolian_Function_Type ftype;

             eolian_implement_information_get(impl_desc, &impl_classname, &func_name, &ftype);
             Eolian_Class impl_class = eolian_class_find_by_name(impl_classname);

             _class_func_names_fill(impl_class, NULL, NULL);

             char implname[0xFF];
             char *tmp = implname;
             sprintf(implname, "%s_%s", class_name, impl_classname);
             eina_str_tolower(&tmp);

             foo = eolian_class_function_find_by_name(impl_class, func_name, ftype);
             if (!foo)
               {
                  ERR ("Failed to generate implementation of %s:%s - missing form super class", impl_classname, func_name);
                  goto end;
               }

             switch (ftype)
               {
                case EOLIAN_PROP_SET: case EOLIAN_PROP_GET:
                   _prototype_generate(foo, ftype, data_type_buf, implname, buffer);
                   break;
                default:
                   _prototype_generate(foo, eolian_function_type_get(foo), data_type_buf, implname, buffer);
                   break;
               }
          }
     }

   _class_func_names_fill(class, NULL, NULL);
   if (eolian_class_ctor_enable_get(class))
     {
        char func_name[100];
        sprintf(func_name, "_%s_class_constructor", lowclass);
        if (!_function_exists(func_name, buffer))
          {
             printf("Generation of function %s\n", func_name);
             eina_strbuf_append_printf(buffer,
                   "EOLIAN static void\n_%s_class_constructor(Eo_Class *klass)\n{\n\n}\n\n",
                   lowclass);
          }
     }

   if (eolian_class_dtor_enable_get(class))
     {
        char func_name[100];
        sprintf(func_name, "_%s_class_destructor", lowclass);
        if (!_function_exists(func_name, buffer))
          {
             printf("Generation of function %s\n", func_name);
             eina_strbuf_append_printf(buffer, "EOLIAN static void\n_%s_class_destructor(Eo_Class *klass)\n{\n\n}\n\n",
                   lowclass);
          }
     }

   ret = EINA_TRUE;
end:
   eina_strbuf_free(data_type_buf);
   return ret;
}