summaryrefslogtreecommitdiff
path: root/src/bin/eolian/types_generator.c
blob: d18a0df6d6086be063fdbff8258a9aa043765ce3 (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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

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

#include "Eolian.h"
#include "types_generator.h"
#include "common_funcs.h"

static char *
_concat_name(const Eolian_Type *tp)
{
   const char *name;
   char *str = NULL;
   Eina_Strbuf *buf = eina_strbuf_new();
   Eina_Iterator *itr = eolian_type_namespaces_get(tp);
   EINA_ITERATOR_FOREACH(itr, name)
      if (name) eina_strbuf_append_printf(buf, "%s_", name);
   eina_iterator_free(itr);
   name = eolian_type_name_get(tp);
   if (name) eina_strbuf_append_printf(buf, "%s", name);
   if (eina_strbuf_length_get(buf))
     {
        char *tmp = str = eina_strbuf_string_steal(buf);
        *tmp = toupper(*tmp);
        while (*tmp) if (*tmp++ == '_' && *tmp) *tmp = toupper(*tmp);
     }
   eina_strbuf_free(buf);
   return str;
}

static void
_desc_generate(const char *desc, Eina_Strbuf *buf)
{
   if (desc)
     {
        eina_strbuf_append(buf, "/**\n");
        eina_strbuf_append(buf, desc);
        eina_strbuf_replace_all(buf, "\n", "\n * ");
        eina_strbuf_append(buf, "\n */\n");
        eina_strbuf_replace_all(buf, " * \n", " *\n"); /* Remove trailing whitespaces */
     }
}

static Eina_Strbuf *
_type_generate(const Eolian_Type *tp, Eina_Bool full)
{
   Eina_Strbuf *buf = eina_strbuf_new();
   _desc_generate(eolian_type_description_get(tp), buf);
   Eolian_Type_Type tp_type = eolian_type_type_get(tp);
   switch(tp_type)
     {
      case EOLIAN_TYPE_ALIAS:
           {
              char *name = _concat_name(tp);
              Eina_Stringshare *c_type = eolian_type_c_type_named_get(
                  eolian_type_base_type_get(tp), name);
              eina_strbuf_append_printf(buf, "typedef %s", c_type);
              eina_stringshare_del(c_type);
              free(name);
              break;
           }
      case EOLIAN_TYPE_STRUCT:
      case EOLIAN_TYPE_STRUCT_OPAQUE:
           {
              const Eolian_Struct_Type_Field *member;
              char *name = _concat_name(tp);
              if (tp_type == EOLIAN_TYPE_STRUCT_OPAQUE || !full)
                {
                   eina_strbuf_append_printf(buf, "typedef struct _%s %s", name, name);
                   free(name);
                   break;
                }
              eina_strbuf_append_printf(buf, "typedef struct _%s\n{\n", name);
              Eina_Iterator *members = eolian_type_struct_fields_get(tp);
              EINA_ITERATOR_FOREACH(members, member)
                {
                   const char *desc = eolian_type_struct_field_description_get(member);
                   const Eolian_Type *type = eolian_type_struct_field_type_get(member);
                   Eina_Stringshare *c_type = eolian_type_c_type_get(type);
                   eina_strbuf_append_printf(buf, "  %s%s%s;",
                         c_type, strchr(c_type, '*')?"":" ",
                         eolian_type_struct_field_name_get(member));
                   if (desc) eina_strbuf_append_printf(buf, " /** %s */", desc);
                   eina_strbuf_append(buf, "\n");
                }
              eina_iterator_free(members);
              eina_strbuf_append_printf(buf, "} %s", name);
              free(name);
              break;
           }
      case EOLIAN_TYPE_ENUM:
           {
              const Eolian_Enum_Type_Field *member;
              char *name;
              if (!full)
                break;
              name = _concat_name(tp);
              char *pre = NULL;
              eina_strbuf_append_printf(buf, "typedef enum\n{\n");
              if (eolian_type_enum_legacy_prefix_get(tp))
                pre = strdup(eolian_type_enum_legacy_prefix_get(tp));
              else
                pre = strdup(name);
              eina_str_toupper(&pre);
              Eina_Iterator *members = eolian_type_enum_fields_get(tp);
              Eina_Bool next = eina_iterator_next(members, (void**)&member);
              Eina_Strbuf *membuf = eina_strbuf_new();
              while (next)
                {
                   const char *desc = eolian_type_enum_field_description_get(member);
                   const Eolian_Expression *value = eolian_type_enum_field_value_get(member, EINA_FALSE);
                   char *memb_u = strdup(eolian_type_enum_field_name_get(member));
                   eina_str_toupper(&memb_u);
                   eina_strbuf_reset(membuf);
                   eina_strbuf_append(membuf, pre);
                   eina_strbuf_append_char(membuf, '_');
                   eina_strbuf_append(membuf, memb_u);
                   free(memb_u);
                   if (!value)
                     eina_strbuf_append_printf(buf, "  %s", eina_strbuf_string_get(membuf));
                   else
                     {
                        Eolian_Value val = eolian_expression_eval(value, EOLIAN_MASK_INT);
                        const char *lit = eolian_expression_value_to_literal(&val);
                        eina_strbuf_append_printf(buf, "  %s = %s", eina_strbuf_string_get(membuf), lit);
                        const char *exp = eolian_expression_serialize(value);
                        if (exp && strcmp(lit, exp))
                          {
                             eina_strbuf_append_printf(buf, " /* %s */", exp);
                             eina_stringshare_del(exp);
                          }
                        eina_stringshare_del(lit);
                     }
                   next = eina_iterator_next(members, (void**)&member);
                   if (next)
                     eina_strbuf_append(buf, ",");
                   if (desc) eina_strbuf_append_printf(buf, " /** %s */", desc);
                   eina_strbuf_append(buf, "\n");
                }
              eina_strbuf_append_printf(buf, "} %s", name);
              eina_strbuf_free(membuf);
              free(name);
              free(pre);
              eina_iterator_free(members);
              break;
           }
      default:
           {
              eina_strbuf_reset(buf);
           }
     }
   return buf;
}

Eina_Bool
types_header_generate(const char *eo_filename, Eina_Strbuf *buf, Eina_Bool full)
{
   const Eolian_Declaration *decl;

   Eina_Iterator *itr = eolian_declarations_get_by_file(eo_filename);
   EINA_ITERATOR_FOREACH(itr, decl)
     {
        Eolian_Declaration_Type dt = eolian_declaration_type_get(decl);
        if (dt != EOLIAN_DECL_ALIAS &&
            dt != EOLIAN_DECL_STRUCT &&
            dt != EOLIAN_DECL_ENUM)
          continue;

        if (dt == EOLIAN_DECL_ENUM && !full)
          continue;

        const Eolian_Type *tp = eolian_declaration_data_type_get(decl);
        if (!tp || eolian_type_is_extern(tp))
          continue;

        Eina_Strbuf *tbuf = _type_generate(tp, full);
        if (tbuf)
          {
             eina_strbuf_append(buf, eina_strbuf_string_get(tbuf));
             eina_strbuf_append(buf, ";\n\n");
             eina_strbuf_free(tbuf);
          }
     }
   eina_iterator_free(itr);

   return EINA_TRUE;
}

Eina_Bool
types_class_typedef_generate(const char *eo_filename, Eina_Strbuf *buf)
{
   const Eolian_Class *class = eolian_class_get_by_file(eo_filename);
   if (!class)
     return EINA_FALSE;

   static _eolian_class_vars class_env;
   _class_env_create(class, NULL, &class_env);

   eina_strbuf_append_printf(buf, "typedef Eo %s;\n", class_env.full_classname);
   return EINA_TRUE;
}