summaryrefslogtreecommitdiff
path: root/src/bin/eolian_cxx/convert.cc
blob: 4d03c8f775919dee903539c4ef597deff975baf7 (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

#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cassert>
#include <cstddef>

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Eina.hh>
#include <Eolian.h>

#include "eo_types.hh"
#include "eo_validate.hh"

#include "safe_strings.hh"
#include "convert_comments.hh"
#include "eolian_wrappers.hh"

namespace eolian_cxx {

extern efl::eina::log_domain domain;
typedef std::map<efl::eolian::eo_event, bool> event_map;

void
add_ancestor_recursive(const char* klass_name, std::set<std::string>& ancestor)
{
   if (!klass_name)
     return;

   Eolian_Class const* klass = ::eolian_class_get_by_name(klass_name);
   if (!klass)
     {
        std::cerr << "Error: could not get eolian class name `" << klass_name << "'" << std::endl;
        return;
     }

   ancestor.insert(class_format_cxx(safe_lower(klass_name)));

   Eina_Iterator* inheritances = ::eolian_class_inherits_get(klass);
   void* curr = 0;

   EINA_ITERATOR_FOREACH(inheritances, curr)
     {
        add_ancestor_recursive(static_cast<const char*>(curr), ancestor);
     }
   eina_iterator_free(inheritances);
}

void
add_events_recursive(event_map& events, Eolian_Class const& klass, std::set<std::string>& ancestors)
{
   for (efl::eolian::eo_event const& e : event_list(klass))
     {
        auto it = events.find(e);
        if (it == events.end())
          events[e] = true;
        else
          it->second = false;
     }

   Eina_Iterator* inheritances = ::eolian_class_inherits_get(&klass);
   void* curr = 0;

   EINA_ITERATOR_FOREACH(inheritances, curr)
     {
        const char* ancestor_name = static_cast<const char*>(curr);
        if (!ancestor_name || ancestors.find(ancestor_name) != ancestors.end())
          continue;

        Eolian_Class const* ancestor_klass = ::eolian_class_get_by_name(ancestor_name);
        if (!ancestor_klass)
          {
             std::cerr << "Error: could not get eolian class name `" << ancestor_name << "'" << std::endl;
             continue;
          }
        ancestors.insert(ancestor_name);
        add_events_recursive(events, *ancestor_klass, ancestors);
     }

   eina_iterator_free(inheritances);
}

static efl::eolian::parameters_container_type
_convert_eolian_parameters(Eina_Iterator *parameters,
                          Eolian_Function_Type func_type)
{
   if (parameters == NULL) return {};
   assert(func_type != EOLIAN_PROPERTY);

   void *curr;
   efl::eolian::parameters_container_type list;
   EINA_ITERATOR_FOREACH(parameters, curr)
     {
        const Eolian_Function_Parameter *id =
          (static_cast<const Eolian_Function_Parameter*>(curr));
        list.push_back
          ({
             parameter_type(*id, func_type),
             parameter_name(*id)
          });
     }
   eina_iterator_free(parameters);
   return list;
}

static efl::eolian::parameters_container_type
_convert_eolian_parameters(Eolian_Function const& func, getter_t func_type)
{
   return _convert_eolian_parameters
     (::eolian_function_parameters_get(&func), func_type.value);
}

static efl::eolian::parameters_container_type
_convert_eolian_parameters(Eina_Iterator *parameters, getter_t func_type)
{
   return _convert_eolian_parameters(parameters, func_type.value);
}

static efl::eolian::parameters_container_type
_convert_eolian_parameters(Eina_Iterator *parameters, setter_t func_type)
{
   return _convert_eolian_parameters(parameters, func_type.value);
}

static efl::eolian::parameters_container_type
_convert_eolian_parameters(Eolian_Function const& func)
{
   assert(function_op_type(func) != EOLIAN_PROPERTY);
   return _convert_eolian_parameters
     (::eolian_function_parameters_get(&func), function_op_type(func));
}

static efl::eolian::eo_function
_convert_property_set_to_function(Eolian_Class const& klass,
                                  Eolian_Function const& prop_)
{
   efl::eolian::eo_function set_ =
     {
       efl::eolian::eo_function::regular_,
       function_scope(prop_),
       function_name(prop_) + "_set",
       function_impl(prop_) + "_set",
       function_return_type(prop_, eolian_cxx::setter),
       _convert_eolian_parameters(::eolian_function_parameters_get(&prop_),
                                  eolian_cxx::setter),
       convert_comments_function(klass, prop_, eolian_cxx::setter)
     };
   efl::eolian::parameters_container_type keys =
     _convert_eolian_parameters(::eolian_property_keys_get(&prop_),
                                eolian_cxx::setter);
   if (!keys.empty())
     {
        keys.reserve(keys.size() + set_.params.size());
        keys.insert(keys.end(), set_.params.begin(),
                    set_.params.end());
        set_.params = keys;
     }
   return set_;
}

static efl::eolian::eo_function
_convert_property_get_to_function(Eolian_Class const& klass,
                                  Eolian_Function const& prop_)
{
   efl::eolian::eo_function get_ =
     {
       efl::eolian::eo_function::regular_,
       function_scope(prop_),
       function_name(prop_) + "_get",
       function_impl(prop_) + "_get",
       function_return_type(prop_, eolian_cxx::getter),
       _convert_eolian_parameters(prop_, eolian_cxx::getter),
       convert_comments_function(klass, prop_, eolian_cxx::getter)
     };

   // if the getter has a single parameter and a void return
   // it is transformed into a getter with no parameters
   // that actually returns what would be the first argument.
   if (get_.params.size() == 1 && efl::eolian::type_is_void(get_.ret) &&
       !function_return_is_explicit_void(prop_, eolian_cxx::getter))
     {
        get_.ret = get_.params[0].type;
        get_.params.clear();
     }
   else // otherwise just create the described getter
     {
        std::transform
          (get_.params.begin(), get_.params.end(), get_.params.begin(),
           [](efl::eolian::eo_parameter const& param)
           {
              efl::eolian::eolian_type getter_param_type =
                type_to_native(param.type);
              getter_param_type.native += "*";
              return efl::eolian::eo_parameter
                { { getter_param_type }, param.name };
           });
     }
   efl::eolian::parameters_container_type keys =
     _convert_eolian_parameters(::eolian_property_keys_get(&prop_),
                                eolian_cxx::getter);
   if (!keys.empty())
     {
        keys.reserve(keys.size() + get_.params.size());
        keys.insert(keys.end(), get_.params.begin(),
                    get_.params.end());
        get_.params = keys;
     }
   return get_;
}

static efl::eolian::eo_function
_convert_function(Eolian_Class const& klass, Eolian_Function const& func)
{
   return {
     function_type(func),
     function_scope(func),
     function_name(func),
     function_impl(func),
     function_return_type(func),
     _convert_eolian_parameters(func),
     convert_comments_function(klass, func, eolian_cxx::method)
   };
}


void
convert_eolian_inheritances(efl::eolian::eo_class& cls, Eolian_Class const& klass)
{
   Eina_Iterator *inheritances =
     ::eolian_class_inherits_get(&klass);
   void *curr;

   std::set<std::string> ancestors;

   EINA_ITERATOR_FOREACH(inheritances, curr)
     {
        const char* klass_name = static_cast<const char*>(curr);
        cls.parents.push_back(class_format_cxx(safe_lower(klass_name)));
        add_ancestor_recursive(klass_name, ancestors);
     }
   eina_iterator_free(inheritances);

   cls.ancestors.assign(ancestors.begin(), ancestors.end());
}

void
convert_eolian_events(efl::eolian::eo_class& cls, Eolian_Class const& klass)
{
   efl::eolian::events_container_type events = event_list(klass);
   cls.own_events.reserve(cls.own_events.size() + events.size());
   cls.own_events.insert(cls.own_events.end(), events.begin(), events.end());

   event_map concrete_events;
   std::set<std::string> ancestors;

   add_events_recursive(concrete_events, klass, ancestors);

   for (auto const& e : events)
     {
        concrete_events[e] = true;
     }

   for (auto const& pair : concrete_events)
     {
        if (pair.second)
          cls.concrete_events.push_back(pair.first);
     }
}

efl::eolian::eo_class
convert_eolian_class_new(Eolian_Class const& klass)
{
   efl::eolian::eo_class cls;
   cls.type = class_type(klass);
   cls.name = class_name(klass);
   cls.name_space = class_namespace_full(klass);
   cls.eo_name = class_eo_name(klass);
   cls.comment = convert_comments_class(klass);
   return cls;
}

void
convert_eolian_functions(efl::eolian::eo_class& cls, Eolian_Class const& klass)
{
   for(efl::eina::iterator<const Eolian_Function> first ( ::eolian_class_functions_get(&klass, EOLIAN_METHOD))
    , last; first != last; ++first)
     {
        Eolian_Function const& func = *first;

        if (function_is_visible(func, function_op_type(func)) &&
            !function_is_constructor(klass, func))
          {
             cls.functions.push_back(_convert_function(klass, func));
          }
     }
   if (class_eo_name(klass) != "EO_BASE_CLASS")
     for(efl::eina::iterator<const Eolian_Constructor> first ( ::eolian_class_constructors_get(&klass))
      , last; first != last; ++first)
       {
          Eolian_Constructor const& ctor = *first;
          Eolian_Function const& func = *(::eolian_constructor_function_get(&ctor));

          efl::eolian::eo_function f;
          if (::eolian_function_type_get(&func) != EOLIAN_PROPERTY)
            f = _convert_function(klass, func);
          else
            f = _convert_property_set_to_function(klass, func);


          (::eolian_constructor_is_optional(&ctor) ?
            cls.optional_constructors :
            cls.constructors
          ).push_back({
             function_name(func),
             f.impl,
             f.params,
             f.comment
          });
       }

   cls.all_constructors = cls.constructors;
   cls.all_constructors.insert(cls.all_constructors.end(),
    cls.optional_constructors.begin(), cls.optional_constructors.end());

   for(efl::eina::iterator<const Eolian_Function> first ( ::eolian_class_functions_get(&klass, EOLIAN_PROPERTY))
    , last; first != last; ++first)
     {
        Eolian_Function const& func = *first;
        Eolian_Function_Type t = ::eolian_function_type_get(&func);

        if (!function_is_visible(func, t))
          continue;

        if(t == EOLIAN_PROP_GET)
          {
             cls.functions.push_back
               (_convert_property_get_to_function(klass, func));
          }
        else if(t == EOLIAN_PROP_SET)
          {
             cls.functions.push_back
               (_convert_property_set_to_function(klass, func));
          }
        else if(t == EOLIAN_PROPERTY)
          {
             cls.functions.push_back
               (_convert_property_get_to_function(klass, func));
             cls.functions.push_back
               (_convert_property_set_to_function(klass, func));
          }
       else
          {
             std::cerr << "Error: Inconsistent type for Eolian function \'" << ::eolian_function_name_get(&func) << "\'." << std::endl;
             throw std::runtime_error("Invalid Eolian function type");
          }
     }
}

efl::eolian::eo_class
convert_eolian_class(const Eolian_Class& klass)
{
   efl::eolian::eo_class cls(eolian_cxx::convert_eolian_class_new(klass));
   eolian_cxx::convert_eolian_inheritances(cls, klass);
   eolian_cxx::convert_eolian_functions(cls, klass);
   eolian_cxx::convert_eolian_events(cls, klass);
   efl::eolian::eo_class_validate(cls);
   return cls;
}

} // namespace eolian_cxx {