summaryrefslogtreecommitdiff
path: root/src/bin/eolian2/headers.c
blob: 69dc94f3ae7e81781c47ab058e80a531473c33a5 (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
#include "main.h"

static const char *
_cl_type_str_get(const Eolian_Class *cl, Eina_Bool uc)
{
   switch (eolian_class_type_get(cl))
     {
      case EOLIAN_CLASS_REGULAR:
      case EOLIAN_CLASS_ABSTRACT:
        return uc ? "CLASS" : "class";
      case EOLIAN_CLASS_MIXIN:
        return uc ? "MIXIN" : "mixin";
      case EOLIAN_CLASS_INTERFACE:
        return uc ? "INTERFACE" : "interface";
      default:
        return NULL;
     }
}

static const char *
_get_add_star(Eolian_Function_Type ftype, Eolian_Parameter_Dir pdir)
{
   if (ftype == EOLIAN_PROP_GET)
     return "*";
   if ((pdir == EOLIAN_OUT_PARAM) || (pdir == EOLIAN_INOUT_PARAM))
     return "*";
   return "";
}

static void
_gen_func(const Eolian_Function *fid, Eolian_Function_Type ftype,
          Eina_Strbuf *buf, char *cnameu, Eina_Bool legacy)
{
   Eina_Stringshare *fcn = eolian_function_full_c_name_get(fid, ftype, legacy);
   if (!fcn)
     return;

   Eina_Bool var_as_ret = EINA_FALSE;
   const Eolian_Type *rtp = eolian_function_return_type_get(fid, ftype);
   if (ftype == EOLIAN_PROP_GET && !rtp)
     {
        void *d1, *d2;
        Eina_Iterator *itr = eolian_property_values_get(fid, ftype);
        if (eina_iterator_next(itr, &d1) && !eina_iterator_next(itr, &d2))
          {
             rtp = eolian_parameter_type_get((Eolian_Function_Parameter *)d1);
             var_as_ret = EINA_TRUE;
          }
        eina_iterator_free(itr);
     }

   Eolian_Object_Scope fsc = eolian_function_scope_get(fid, ftype);

   if (eolian_function_is_beta(fid))
     eina_strbuf_append_printf(buf, "#ifdef %s_BETA\n", cnameu);
   if (fsc == EOLIAN_SCOPE_PROTECTED)
     eina_strbuf_append_printf(buf, "#ifdef %s_PROTECTED\n", cnameu);

   eina_strbuf_append(buf, "EOAPI ");
   if (rtp)
     {
        Eina_Stringshare *rtps = eolian_type_c_type_get(rtp);
        eina_strbuf_append(buf, rtps);
        if (rtps[strlen(rtps) - 1] != '*')
          eina_strbuf_append_char(buf, ' ');
        eina_stringshare_del(rtps);
     }
   else
     eina_strbuf_append(buf, "void ");

   eina_strbuf_append(buf, fcn);
   eina_stringshare_del(fcn);

   eina_strbuf_append_char(buf, '(');
   if ((ftype == EOLIAN_PROP_GET) || eolian_function_object_is_const(fid)
       || eolian_function_is_class(fid))
     {
        eina_strbuf_append(buf, "const ");
     }
   eina_strbuf_append(buf, "Eo *obj");

   {
      Eolian_Function_Parameter *pr = NULL;
      Eina_Iterator *itr = eolian_property_keys_get(fid, ftype);
      EINA_ITERATOR_FOREACH(itr, pr)
        {
           const Eolian_Type *prt = eolian_parameter_type_get(pr);
           const char *prn = eolian_parameter_name_get(pr);
           Eina_Stringshare *prtn = eolian_type_c_type_get(prt);
           eina_strbuf_append_printf(buf, ", %s %s", prtn, prn);
           eina_stringshare_del(prtn);
        }
      eina_iterator_free(itr);
   }

   if (!var_as_ret)
     {
        Eina_Iterator *itr = NULL;
        if (ftype == EOLIAN_PROP_GET || ftype == EOLIAN_PROP_SET)
          itr = eolian_property_values_get(fid, ftype);
        else
          itr = eolian_function_parameters_get(fid);

        Eolian_Function_Parameter *pr = NULL;
        EINA_ITERATOR_FOREACH(itr, pr)
          {
             const Eolian_Type *prt = eolian_parameter_type_get(pr);
             const char *prn = eolian_parameter_name_get(pr);
             Eina_Stringshare *prtn = eolian_type_c_type_get(prt);

             eina_strbuf_append(buf, ", ");
             eina_strbuf_append(buf, prtn);
             if (!strchr(prtn, '*'))
               eina_strbuf_append_char(buf, ' ');
             eina_strbuf_append(buf,
                 _get_add_star(ftype, eolian_parameter_direction_get(pr)));
             eina_strbuf_append(buf, prn);
             eina_stringshare_del(prtn);
          }

        eina_iterator_free(itr);
     }

   eina_strbuf_append(buf, ");\n");

   if (eolian_function_is_beta(fid))
     eina_strbuf_append_printf(buf, "#endif\n");
   if (fsc == EOLIAN_SCOPE_PROTECTED)
     eina_strbuf_append_printf(buf, "#endif\n");
}

void
eo_gen_header_gen(const Eolian_Class *cl, Eina_Strbuf *buf, Eina_Bool legacy)
{
   if (!cl)
     return;

   char *cname = NULL, *cnameu = NULL, *cnamel = NULL;

   cname = eo_gen_c_full_name_get(eolian_class_full_name_get(cl));
   if (!cname)
     goto end;

   cnameu = strdup(cname);
   if (!cnameu)
     goto end;
   eina_str_toupper(&cnameu);

   cnamel = strdup(cname);
   if (!cnamel)
     goto end;
   eina_str_tolower(&cnamel);

   /* class definition */

   eina_strbuf_append_printf(buf, "#define %s_%s %s_%s_get()\n\n",
                             cnameu, _cl_type_str_get(cl, EINA_TRUE),
                             cnamel, _cl_type_str_get(cl, EINA_FALSE));

   eina_strbuf_append_printf(buf, "EWAPI const Efl_Class *%s_%s_get(void);\n\n",
                             cnamel, _cl_type_str_get(cl, EINA_FALSE));

   /* method section */
   {
      Eina_Iterator *itr = eolian_class_implements_get(cl);
      if (!itr)
        goto events;

      const Eolian_Implement *imp;
      EINA_ITERATOR_FOREACH(itr, imp)
        {
           if (eolian_implement_class_get(imp) != cl)
             continue;
           Eolian_Function_Type ftype = EOLIAN_UNRESOLVED;
           const Eolian_Function *fid = eolian_implement_function_get(imp, &ftype);
           switch (ftype)
             {
              case EOLIAN_PROP_GET:
              case EOLIAN_PROP_SET:
                _gen_func(fid, ftype, buf, cnameu, legacy);
                break;
              case EOLIAN_PROPERTY:
                _gen_func(fid, EOLIAN_PROP_SET, buf, cnameu, legacy);
                _gen_func(fid, EOLIAN_PROP_GET, buf, cnameu, legacy);
                break;
              default:
                _gen_func(fid, EOLIAN_UNRESOLVED, buf, cnameu, legacy);
             }
        }
      eina_iterator_free(itr);
   }

events:
   /* event section */
   {
      Eina_Iterator *itr = eolian_class_events_get(cl);
      Eolian_Event *ev;
      EINA_ITERATOR_FOREACH(itr, ev)
        {
           Eina_Stringshare *evn = eolian_event_c_name_get(ev);
           Eolian_Object_Scope evs = eolian_event_scope_get(ev);

           if (evs == EOLIAN_SCOPE_PRIVATE)
             continue;

           if (eolian_event_is_beta(ev))
             {
                eina_strbuf_append_printf(buf, "\n#ifdef %s_BETA\n", cnameu);
             }
           if (evs == EOLIAN_SCOPE_PROTECTED)
             {
                if (!eolian_event_is_beta(ev))
                  eina_strbuf_append_char(buf, '\n');
                eina_strbuf_append_printf(buf, "#ifdef %s_PROTECTED\n", cnameu);
             }

           if (!eolian_event_is_beta(ev) && evs == EOLIAN_SCOPE_PUBLIC)
             eina_strbuf_append_char(buf, '\n');

           eina_strbuf_append_printf(buf, "EOAPI extern const "
                                     "Efl_Event_Description _%s;\n", evn);
           eina_strbuf_append_printf(buf, "#define %s (&(_%s))\n", evn, evn);

           if (evs == EOLIAN_SCOPE_PROTECTED)
             eina_strbuf_append(buf, "#endif\n");
           if (eolian_event_is_beta(ev))
             eina_strbuf_append(buf, "#endif\n");

           eina_stringshare_del(evn);
        }
      eina_iterator_free(itr);
   }

end:
   free(cname);
   free(cnameu);
   free(cnamel);
}