aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/eolian/legacy_generator.c
blob: d05122e5461251b94b2e84305921540437d95fe7 (plain) (blame)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <Eina.h>
#include <string.h>

#include "Eolian.h"

#include "legacy_generator.h"
#include "docs_generator.h"
#include "common_funcs.h"

static _eolian_class_vars class_env;

static const char
tmpl_eapi_funcdef[] = "EAPI @#type_return%s(@#params)@#flags;\n";

/*@#CLASS_CHECK(obj) @#check_ret;\n\*/
static const char
tmpl_eapi_body[] ="\
\n\
EAPI @#ret_type\n\
@#eapi_func(@#full_params)\n\
{\n\
   @#ret_type ret;\n\
   eo_do(@#eo_obj, ret = @#eo_func(@#eo_obj%s@#eo_params));\n\
   return ret;\n\
}\n\
";
static const char
tmpl_eapi_body_void[] ="\
\n\
EAPI void\n\
@#eapi_func(@#full_params)\n\
{\n\
   eo_do(@#eo_obj, @#eo_func(@#eo_obj%s@#eo_params));\n\
}\n\
";

static void
_eapi_decl_func_generate(const Eolian_Class *class, const Eolian_Function *funcid, Eolian_Function_Type ftype, Eina_Strbuf *buf)
{
   _eolian_class_func_vars func_env;
   const char *funcname = eolian_function_name_get(funcid);
   const Eolian_Type *rettypet = NULL;
   const char *rettype = NULL;
   Eina_Bool var_as_ret = EINA_FALSE;
   Eina_Bool add_star = EINA_FALSE;
   Eina_Bool is_prop = (ftype == EOLIAN_PROP_GET || ftype == EOLIAN_PROP_SET);
   Eina_Iterator *itr;
   void *data, *data2;
   Eina_Strbuf *flags = NULL;
   int leg_param_idx = 1; /* Index of the parameter inside the legacy function. It begins from 1 since obj is the first. */

   Eina_Strbuf *fbody = eina_strbuf_new();
   Eina_Strbuf *fparam = eina_strbuf_new();

   _class_func_env_create(class, funcname, ftype, &func_env);
   rettypet = eolian_function_return_type_get(funcid, ftype);
   if (ftype == EOLIAN_PROP_GET)
     {
        add_star = EINA_TRUE;
        if (!rettypet)
          {
             itr = eolian_property_values_get(funcid, ftype);
             /* We want to check if there is only one parameter */
             if (eina_iterator_next(itr, &data) && !eina_iterator_next(itr, &data2))
               {
                  rettypet = eolian_parameter_type_get((Eolian_Function_Parameter*)data);
                  var_as_ret = EINA_TRUE;
               }
             eina_iterator_free(itr);
          }
     }

   if (func_env.legacy_func[0] == '\0') goto end;

   Eina_Bool hasnewdocs = eolian_function_documentation_get(funcid, EOLIAN_UNRESOLVED) ||
                          eolian_function_documentation_get(funcid, ftype);
   if (hasnewdocs)
     {
        Eina_Strbuf *dbuf = docs_generate_function(funcid, ftype, 0, EINA_TRUE);
        eina_strbuf_append_char(fbody, '\n');
        eina_strbuf_append(fbody, eina_strbuf_string_get(dbuf));
        eina_strbuf_append_char(fbody, '\n');
        eina_strbuf_free(dbuf);
     }

   eina_strbuf_append_printf(fbody, tmpl_eapi_funcdef, func_env.legacy_func);

   if (!eolian_function_is_class(funcid))
     {
        if (ftype == EOLIAN_PROP_GET || eolian_function_object_is_const(funcid))
           eina_strbuf_append(fparam, "const ");
        eina_strbuf_append_printf(fparam, "%s *obj", class_env.full_classname);
     }

   itr = eolian_property_keys_get(funcid, ftype);
   EINA_ITERATOR_FOREACH(itr, data)
     {
        Eolian_Function_Parameter *param = data;
        const Eolian_Type *ptypet = eolian_parameter_type_get(param);
        const char *pname = eolian_parameter_name_get(param);
        const char *ptype = eolian_type_c_type_get(ptypet);
        leg_param_idx++;
        if (eina_strbuf_length_get(fparam)) eina_strbuf_append(fparam, ", ");
        eina_strbuf_append_printf(fparam, "%s %s", ptype, pname);
        eina_stringshare_del(ptype);
  
        if (eolian_parameter_is_nonull((Eolian_Function_Parameter*)data))
          {
             if (!flags)
               {
                  flags = eina_strbuf_new();
                  eina_strbuf_append_printf(flags, " EINA_ARG_NONNULL(%d", leg_param_idx);
               }
             else
                eina_strbuf_append_printf(flags, ", %d", leg_param_idx);
          }
     }
   eina_iterator_free(itr);
   if (!var_as_ret)
     {
       itr = is_prop ? eolian_property_values_get(funcid, ftype) : eolian_function_parameters_get(funcid);
       EINA_ITERATOR_FOREACH(itr, data)
         {
            Eolian_Function_Parameter *param = data;
            const Eolian_Type *ptypet = eolian_parameter_type_get(param);
            const char *pname = eolian_parameter_name_get(param);
            const char *ptype = eolian_type_c_type_get(ptypet);
            Eolian_Parameter_Dir pdir = eolian_parameter_direction_get(param);
            Eina_Bool had_star = !!strchr(ptype, '*');
            if (ftype == EOLIAN_UNRESOLVED || ftype == EOLIAN_METHOD) add_star = (pdir == EOLIAN_OUT_PARAM || pdir == EOLIAN_INOUT_PARAM);
            if (ftype == EOLIAN_PROP_GET) pdir = EOLIAN_OUT_PARAM;
            if (ftype == EOLIAN_PROP_SET) pdir = EOLIAN_IN_PARAM;
            leg_param_idx++;
            if (eina_strbuf_length_get(fparam)) eina_strbuf_append(fparam, ", ");
            eina_strbuf_append_printf(fparam, "%s%s%s%s",
                  ptype, had_star?"":" ", add_star?"*":"", pname);
            eina_stringshare_del(ptype);
            if (eolian_parameter_is_nonull((Eolian_Function_Parameter*)data))
              {
                 if (!flags)
                   {
                      flags = eina_strbuf_new();
                      eina_strbuf_append_printf(flags, " EINA_ARG_NONNULL(%d", leg_param_idx);
                   }
                 else
                    eina_strbuf_append_printf(flags, ", %d", leg_param_idx);
              }
         }
       eina_iterator_free(itr);
     }
   if (!eina_strbuf_length_get(fparam)) eina_strbuf_append(fparam, "void");
   if (flags) eina_strbuf_append_printf(flags, ")");

   if (rettypet) rettype = eolian_type_c_type_get(rettypet);

   eina_strbuf_replace_all(fbody, "@#params", eina_strbuf_string_get(fparam));
   eina_strbuf_reset(fparam);
   eina_strbuf_append_printf(fparam, "%s%s",
         rettype ? rettype : "void",
         rettype && strchr(rettype, '*')?"":" ");
   eina_strbuf_replace_all(fbody, "@#type_return", eina_strbuf_string_get(fparam));
   if (eolian_function_return_is_warn_unused(funcid, ftype))
     {
        Eina_Bool no_nonull = !flags;
        if (no_nonull) flags = eina_strbuf_new();
        eina_strbuf_prepend(flags, " EINA_WARN_UNUSED_RESULT");
     }
   if (flags)
      eina_strbuf_replace_all(fbody, "@#flags", eina_strbuf_string_get(flags));
   eina_strbuf_replace_all(fbody, "@#flags", (eolian_function_return_is_warn_unused(funcid, ftype)) ? " EINA_WARN_UNUSED_RESULT" : "");
   eina_strbuf_append(buf, eina_strbuf_string_get(fbody));

   if (rettype) eina_stringshare_del(rettype);

end:
   eina_strbuf_free(flags);
   eina_strbuf_free(fbody);
   eina_strbuf_free(fparam);
}

static void
_eapi_func_generate(const Eolian_Class *class, const Eolian_Function *funcid, Eolian_Function_Type ftype, Eina_Strbuf *buf)
{
   _eolian_class_func_vars func_env;
   char tmpstr[0xFF];
   Eina_Bool var_as_ret = EINA_FALSE;
   const Eolian_Type *rettypet = NULL;
   const char *rettype = NULL;
   const char *retname = NULL;
   Eina_Bool add_star = EINA_FALSE;
   Eina_Bool ret_is_void = EINA_FALSE;
   Eina_Bool is_prop = (ftype == EOLIAN_PROP_GET || ftype == EOLIAN_PROP_SET);

   Eina_Strbuf *fbody = eina_strbuf_new();
   Eina_Strbuf *fparam = eina_strbuf_new();
   Eina_Strbuf *eoparam = eina_strbuf_new();

   Eina_Iterator *itr;
   void *data, *data2;

   _class_func_env_create(class, eolian_function_name_get(funcid), ftype, &func_env);
   rettypet = eolian_function_return_type_get(funcid, ftype);
   if (rettypet) rettype = eolian_type_c_type_get(rettypet);
   if (rettype && !strcmp(rettype, "void")) ret_is_void = EINA_TRUE;
   retname = "ret";
   if (ftype == EOLIAN_PROP_GET)
     {
        add_star = EINA_TRUE;
        if (!rettypet)
          {
             itr = eolian_property_values_get(funcid, ftype);
             /* We want to check if there is only one parameter */
             if (eina_iterator_next(itr, &data) && !eina_iterator_next(itr, &data2))
               {
                  Eolian_Function_Parameter *param = data;
                  rettypet = eolian_parameter_type_get(param);
                  retname = eolian_parameter_name_get(param);
                  var_as_ret = EINA_TRUE;
               }
             eina_iterator_free(itr);
          }
     }

   if (func_env.legacy_func[0] == '\0') goto end;

   if (!rettype && rettypet) rettype = eolian_type_c_type_get(rettypet);

   Eina_Bool has_params = EINA_FALSE;

   itr = eolian_property_keys_get(funcid, ftype);
   has_params |= (eina_iterator_next(itr, &data));
   eina_iterator_free(itr);

   if (!has_params && !var_as_ret)
     {
        itr = is_prop ? eolian_property_values_get(funcid, ftype) : eolian_function_parameters_get(funcid);
        has_params |= (eina_iterator_next(itr, &data));
        eina_iterator_free(itr);
     }

   if (rettype && (!ret_is_void))
     eina_strbuf_append_printf(fbody, tmpl_eapi_body, has_params?", ":"");
   else
     eina_strbuf_append_printf(fbody, tmpl_eapi_body_void, has_params?", ":"");

   if (!eolian_function_is_class(funcid))
     {
        if (ftype == EOLIAN_PROP_GET || eolian_function_object_is_const(funcid))
           eina_strbuf_append(fparam, "const ");
        eina_strbuf_append_printf(fparam, "%s *obj", class_env.full_classname);
        char cbuf[256];
        snprintf(cbuf, sizeof(cbuf), "(%s *)obj", class_env.full_classname);
        eina_strbuf_replace_all(fbody, "@#eo_obj", cbuf);
     }
   else
     {
        Eina_Strbuf *class_buf = eina_strbuf_new();
        _template_fill(class_buf, "@#CLASS_@#CLASSTYPE", class, NULL, NULL, EINA_TRUE);
        eina_strbuf_replace_all(fbody, "@#eo_obj", eina_strbuf_string_get(class_buf));
        eina_strbuf_free(class_buf);
     }
   eina_strbuf_replace_all(fbody, "@#eapi_func", func_env.legacy_func);
   eina_strbuf_replace_all(fbody, "@#eo_func", func_env.lower_eo_func);

   tmpstr[0] = '\0';

   itr = eolian_property_keys_get(funcid, ftype);
   EINA_ITERATOR_FOREACH(itr, data)
     {
        Eolian_Function_Parameter *param = data;
        const Eolian_Type *ptypet = eolian_parameter_type_get(param);
        const char *pname = eolian_parameter_name_get(param);
        const char *ptype = eolian_type_c_type_get(ptypet);
        if (eina_strbuf_length_get(fparam)) eina_strbuf_append(fparam, ", ");
        eina_strbuf_append_printf(fparam, "%s %s", ptype, pname);
        eina_stringshare_del(ptype);
        if (eina_strbuf_length_get(eoparam)) eina_strbuf_append(eoparam, ", ");
        eina_strbuf_append_printf(eoparam, "%s", pname);
     }
   eina_iterator_free(itr);
   if (!var_as_ret)
   {
      itr = is_prop ? eolian_property_values_get(funcid, ftype) : eolian_function_parameters_get(funcid);
      EINA_ITERATOR_FOREACH(itr, data)
        {
            Eolian_Function_Parameter *param = data;
            const Eolian_Type *ptypet = eolian_parameter_type_get(param);
            const char *pname = eolian_parameter_name_get(param);
            const char *ptype = eolian_type_c_type_get(ptypet);
            Eolian_Parameter_Dir pdir = eolian_parameter_direction_get(param);
            Eina_Bool had_star = !!strchr(ptype, '*');
            if (ftype == EOLIAN_UNRESOLVED || ftype == EOLIAN_METHOD) add_star = (pdir == EOLIAN_OUT_PARAM || pdir == EOLIAN_INOUT_PARAM);
            if (eina_strbuf_length_get(fparam)) eina_strbuf_append(fparam, ", ");
            eina_strbuf_append_printf(fparam, "%s%s%s%s",
                  ptype, had_star?"":" ", add_star?"*":"", pname);
            eina_stringshare_del(ptype);
            if (eina_strbuf_length_get(eoparam)) eina_strbuf_append(eoparam, ", ");
            eina_strbuf_append_printf(eoparam, "%s", pname);
        }
      eina_iterator_free(itr);
   }
   if (!eina_strbuf_length_get(fparam)) eina_strbuf_append(fparam, "void");

   if (rettype && (!ret_is_void))
     {
        char tmp_ret_str[0xFF];
        sprintf (tmp_ret_str, "%s", rettype);
             const Eolian_Expression *default_ret_val =
                eolian_function_return_default_value_get(funcid, ftype);
             const char *val_str = NULL;
             if (default_ret_val)
               {
                  Eolian_Value val = eolian_expression_eval_type
                    (default_ret_val, rettypet);
                  if (val.type)
                    val_str = eolian_expression_value_to_literal(&val);
               }
             Eina_Bool had_star = !!strchr(rettype, '*');
             sprintf (tmpstr, "   %s%s%s = %s;\n",
                   rettype, had_star?"":" ", retname,
                   val_str?val_str:"0");

             eina_strbuf_replace_all(fbody, "@#ret_type", tmp_ret_str);
             eina_strbuf_replace_all(fbody, "@#ret_init_val", tmpstr);
     }

   eina_strbuf_replace_all(fbody, "@#full_params", eina_strbuf_string_get(fparam));
   eina_strbuf_replace_all(fbody, "@#eo_params", eina_strbuf_string_get(eoparam));

   eina_strbuf_replace_all(fbody, "@#ret_val", (rettype && !ret_is_void) ? retname : "");

   eina_strbuf_append(buf, eina_strbuf_string_get(fbody));

   if (rettype) eina_stringshare_del(rettype);

end:
   eina_strbuf_free(fbody);
   eina_strbuf_free(fparam);
   eina_strbuf_free(eoparam);
}

Eina_Bool
legacy_header_generate(const Eolian_Class *class, Eina_Strbuf *buf)
{
   _class_env_create(class, NULL, &class_env);

   const Eolian_Documentation *doc = eolian_class_documentation_get(class);
   if (doc)
     {
        Eina_Strbuf *cdoc = docs_generate_full(doc, eolian_class_full_name_get(class), 0, EINA_TRUE);
        if (cdoc)
          {
             eina_strbuf_append(buf, eina_strbuf_string_get(cdoc));
             eina_strbuf_append_char(buf, '\n');
             eina_strbuf_free(cdoc);
          }
     }

   Eina_Iterator *itr = eolian_class_implements_get(class);
   if (itr)
     {
        const Eolian_Implement *impl;
        EINA_ITERATOR_FOREACH(itr, impl)
          {
             if (eolian_implement_class_get(impl) != class)
               continue;
             Eolian_Function_Type ftype = EOLIAN_UNRESOLVED;
             const Eolian_Function *fid = eolian_implement_function_get(impl, &ftype);
             switch (ftype)
               {
                case EOLIAN_PROP_GET: case EOLIAN_PROP_SET:
                  _eapi_decl_func_generate(class, fid, ftype, buf);
                  break;
                case EOLIAN_PROPERTY:
                  _eapi_decl_func_generate(class, fid, EOLIAN_PROP_SET, buf);
                  _eapi_decl_func_generate(class, fid, EOLIAN_PROP_GET, buf);
                  break;
                default:
                  _eapi_decl_func_generate(class, fid, EOLIAN_METHOD, buf);
                  break;
               }
          }
        eina_iterator_free(itr);
     }
   return EINA_TRUE;
}

Eina_Bool
legacy_source_generate(const Eolian_Class *class, Eina_Strbuf *buf)
{
   Eina_Bool ret = EINA_FALSE;
   Eina_Iterator *itr;

   _class_env_create(class, NULL, &class_env);

   Eina_Strbuf *tmpbuf = eina_strbuf_new();
   Eina_Strbuf *str_bodyf = eina_strbuf_new();

   if ((itr = eolian_class_implements_get(class)))
     {
        const Eolian_Implement *impl;
        EINA_ITERATOR_FOREACH(itr, impl)
          {
             if (eolian_implement_class_get(impl) != class)
               continue;
             Eolian_Function_Type ftype = EOLIAN_UNRESOLVED;
             const Eolian_Function *fid = eolian_implement_function_get(impl, &ftype);
             switch (ftype)
               {
                case EOLIAN_PROP_GET: case EOLIAN_PROP_SET:
                  _eapi_func_generate(class, fid, ftype, str_bodyf);
                  break;
                case EOLIAN_PROPERTY:
                  _eapi_func_generate(class, fid, EOLIAN_PROP_SET, str_bodyf);
                  _eapi_func_generate(class, fid, EOLIAN_PROP_GET, str_bodyf);
                  break;
                default:
                  _eapi_func_generate(class, fid, EOLIAN_METHOD, str_bodyf);
                  break;
               }
          }
        eina_iterator_free(itr);
     }

   eina_strbuf_append(buf, eina_strbuf_string_get(str_bodyf));

   ret = EINA_TRUE;
   eina_strbuf_free(tmpbuf);
   eina_strbuf_free(str_bodyf);

   return ret;
}