summaryrefslogtreecommitdiff
path: root/src/lib/eolian_cxx/grammar/inheritance_base_generator.hh
blob: 82678496ee83227a234dea1ebd224f4bb2031faa (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
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

#ifndef EOLIAN_CXX_STD_INHERITANCE_GENERATOR_HH
#define EOLIAN_CXX_STD_INHERITANCE_GENERATOR_HH

#include <iosfwd>
#include <cassert>
#include <algorithm>

#include "eo_types.hh"
#include "tab.hh"
#include "parameters_generator.hh"
#include "eo_class_functions_generator.hh"

namespace efl { namespace eolian { namespace grammar {

inline std::string
_ns_as_prefix(eo_class const& cls)
{
   // XXX Use eolian_cxx::find_replace() instead.
   std::string s = cls.name_space;
   std::string::size_type found = s.find("::");
   while (found != std::string::npos)
     {
        s.replace(found, 1, "_");
        found = s.find("::");
     }
   return s;
}

struct inheritance_operation
{
   eo_class const& _cls;
   functions_container_type::size_type _idx;
   inheritance_operation(eo_class const& cls, functions_container_type::size_type idx)
     : _cls(cls), _idx(idx)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, inheritance_operation const& x)
{
   assert(x._idx < x._cls.functions.size());
   eo_function const& func = x._cls.functions[x._idx];
   out << tab(1)
       << "ops[" << x._idx << "].func = reinterpret_cast<void*>(& ::"
       << _ns_as_prefix(x._cls) << "_"
       << x._cls.name << "_" << func.name << "_wrapper<T>);" << endl
       << tab(1) << "ops[" << x._idx << "].api_func = reinterpret_cast<void*>(& ::"
       << func.impl << ");" << endl
       << tab(1) << "ops[" << x._idx << "].op = EO_OP_OVERRIDE;" << endl
       << tab(1) << "ops[" << x._idx << "].op_type = EO_OP_TYPE_REGULAR;" << endl // XXX class ops
       << tab(1) << "ops[" << x._idx << "].doc = NULL;" << endl
       << endl;
   return out;
}

struct inheritance_operations_description
{
   eo_class const& _cls;
   inheritance_operations_description(eo_class const& cls)
     : _cls(cls)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, inheritance_operations_description const& x)
{
   std::string s;

   out << "template <typename T>"
       << endl << "int initialize_operation_description(::efl::eo::detail::tag<"
       << full_name(x._cls) << ">" << endl
       << tab(11)
       << ", Eo_Op_Description* ops)" << endl
       << "{" << endl
       << tab(1) << "(void)ops;" << endl;
   functions_container_type::size_type n_ops = x._cls.functions.size();
   for (functions_container_type::size_type i=0; i < n_ops; ++i)
     {
        out << inheritance_operation(x._cls, i);
     }

   for (std::string const& parent : x._cls.parents)
     {
        out << tab(1)
            << "initialize_operation_description<T>(::efl::eo::detail::tag<::"
            << parent << ">(), &ops[" << x._cls.functions.size() << s << "]);" << endl;

        s += " + operation_description_class_size<::" + parent + ">::value";
     }

   out << tab(1) << "return 0;" << endl
       << "}" << endl;

   return out;
}

struct inheritance_wrappers
{
   eo_class const& _cls;
   inheritance_wrappers(eo_class const& cls) : _cls(cls) {}
};

inline std::ostream&
operator<<(std::ostream& out, inheritance_wrappers const& x)
{
   functions_container_type::const_iterator it,
     first = x._cls.functions.begin(),
     last = x._cls.functions.end();
   for (it = first; it != last; ++it)
     {
        eo_function const& func = *it;
        out << "template <typename T>" << endl
            << reinterpret_type(func.ret) << " "
            << _ns_as_prefix(x._cls) << "_"
            << x._cls.name << "_" << func.name
            << "_wrapper(Eo* objid EINA_UNUSED, "
            << "::efl::eo::detail::Inherit_Private_Data* self"
            << (func.params.size() ? ", " : "")
            << parameters_c_declaration(func.params)
            << ")" << endl
            << "{" << endl;

        if (!function_is_void(func))
          out << tab(1) << reinterpret_type(func.ret) << " _tmp_ret{};" << endl;

        out << tab(1)
            << "try" << endl
            << tab(2) << "{" << endl
            << tab(3)
            << (!function_is_void(func) ? "_tmp_ret = ": "")
            << "static_cast<T*>(self->this_)->"
            << func.name << "(" << parameters_cxx_list(func.params) << ");" << endl
            << tab(2) << "}" << endl
            << tab(1) << "catch (...)" << endl
            << tab(2) << "{" << endl
            << tab(3) << "eina_error_set( ::efl::eina::unknown_error() );" << endl
            << tab(2) << "}" << endl;

        if (!function_is_void(func))
          out << tab(1) << "return _tmp_ret;" << endl;

        out << "}" << endl << endl;
     }
   return out;
}

struct inheritance_base_operations_size
{
   eo_class const& _cls;
   inheritance_base_operations_size(eo_class const& cls)
     : _cls(cls)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, inheritance_base_operations_size const& x)
{
   out << "template<>"
       << endl << "struct operation_description_class_size< "
       << full_name(x._cls) << " >" << endl
       << "{" << endl
       << tab(1) << "static const int value = "
       << x._cls.functions.size();

   for (std::string const& parent : x._cls.parents)
     {
        out << " + operation_description_class_size<::" << parent << " >::value";
     }

   out << ";" << endl
       << "};" << endl
       << endl;

   return out;
}


struct inheritance_base_operations_extensions
{
   eo_class const& _cls;
   inheritance_base_operations_extensions(eo_class const& cls)
     : _cls(cls)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, inheritance_base_operations_extensions const& x)
{
   eo_class const& cls = x._cls;
   ancestors_container_type::const_iterator it, first = cls.parents.begin();
   ancestors_container_type::const_iterator last = cls.parents.end();

   for (it = first; it != last; ++it)
     {
        out << endl
            << tab(3) << (it == first ? ": " : ", ")
            << "virtual operations< ::" << *it
            << " >::template type<T>";
     }

   return out << endl;
}

struct inheritance_base_operations_function
{
   eo_class const& _cls;
   eo_function const& _func;
   inheritance_base_operations_function(eo_class const& cls, eo_function const& func)
     : _cls(cls) , _func(func)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, inheritance_base_operations_function const& x)
{
   eo_function const& func = x._func;
   bool is_void = function_is_void(func);

   if (parameters_count_callbacks(func.params))
     out << template_parameters_declaration(func.params, 2) << tab(2);
   else
     out << tab(2) << "virtual ";

   out << reinterpret_type(func.ret) << " "
       << func.name << "("
       << parameters_declaration(func.params) << ")" << endl
       << tab(2) << "{" << endl;

   if (!is_void)
     out << tab(3) << func.ret.front().native << " _tmp_ret = {};"  << endl;

   out << callbacks_heap_alloc("dynamic_cast<T*>(this)->_eo_ptr()", func.params, 3)
       << endl;

   out << tab(3)
       << "eo_do_super(dynamic_cast<T*>(this)->_eo_ptr()," << endl
       << tab(5) << "dynamic_cast<T*>(this)->_eo_class()," << endl
       << tab(5) << function_call(func) << ");" << endl;

   if (!is_void)
     out << tab(4) << "return " << to_cxx(func.ret, "_tmp_ret") << ";" << endl;

   return out << tab(2) << "}" << endl << endl;
}

struct inheritance_base_operations
{
   eo_class const& _cls;
   inheritance_base_operations(eo_class const& cls) : _cls(cls) {}
};

inline std::ostream&
operator<<(std::ostream& out, inheritance_base_operations const& x)
{
   out << "template<>" << endl
       << "struct operations< "
       << full_name(x._cls) << " >" << endl
       << "{" << endl
       << tab(1) << "template <typename T>" << endl
       << tab(1) << "struct type" << inheritance_base_operations_extensions(x._cls)
       << tab(1) << "{" << endl;
   functions_container_type::const_iterator it,
     first = x._cls.functions.begin(),
     last = x._cls.functions.end();
   for (it = first; it != last; ++it)
     {
        out << inheritance_base_operations_function(x._cls, *it);
     }
   out << tab(1) << "};" << endl
       << "};" << endl << endl;
   return out;
}

struct inheritance_call_constructor_arguments
{
   parameters_container_type const& _params;
   inheritance_call_constructor_arguments(parameters_container_type const& params)
     : _params(params)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, inheritance_call_constructor_arguments const& x)
{
   parameters_container_type::size_type i, n = x._params.size();
   for (i=0; i<n; i++)
     {
        if(i!=0) out << ", ";
        out << "::efl::eolian::to_c(args.get<" << i << ">())";
     }
   return out;
}

struct inheritance_call_constructors
{
   eo_class const& _cls;
   inheritance_call_constructors(eo_class const& cls) : _cls(cls) {}
};

inline std::ostream&
operator<<(std::ostream& out, inheritance_call_constructors const& x)
{
   constructors_container_type::const_iterator it,
     first = x._cls.constructors.begin(),
     last = x._cls.constructors.end();
   for (it = first; it != last; ++it)
     {
        eo_constructor const& ctor = *it;
        out << "inline void" << endl
            << "call_constructor(::efl::eo::detail::tag< "
            << full_name(x._cls) << " >" << endl
            << tab(5) << ", Eo* eo, Eo_Class const* cls EINA_UNUSED," << endl
            << tab(5) << "::efl::eo::detail::args_class<"
            << full_name(x._cls)
            << ", ::std::tuple<"
            << parameters_types(ctor.params)
            << "> > const& args)" << endl
            << "{" << endl
            << tab(1) << "(void)args;" << endl
            << tab(1)
            << "eo_do_super(eo, cls, ::" << ctor.name
            << "(" << inheritance_call_constructor_arguments(ctor.params)
            << "));" << endl
            << "}" << endl << endl;
     }

   out << "inline void" << endl
       << "call_constructor(::efl::eo::detail::tag< "
       << full_name(x._cls) << " >" << endl
       << tab(5) << ", Eo* eo, Eo_Class const* cls EINA_UNUSED," << endl
       << tab(5) << "::efl::eo::detail::args_class<"
       << full_name(x._cls)
       << ", ::std::tuple<::efl::eo::parent_type> > const& args)" << endl
       << "{" << endl
       << tab(1) << "eo_do(eo, ::eo_parent_set(args.get<0>()._eo_raw));" << endl
       << "}" << endl << endl;

   return out;
}

struct inheritance_eo_class_getter
{
   eo_class const& _cls;
   inheritance_eo_class_getter(eo_class const& cls)
     : _cls(cls)
   {}
};

inline std::ostream&
operator<<(std::ostream& out, inheritance_eo_class_getter const& x)
{
   out << "inline Eo_Class const* get_eo_class(tag<"
       << full_name(x._cls) << ">)" << endl
       << "{" << endl
       << tab(1) << "return (" << x._cls.eo_name << ");" << endl
       << "}" << endl << endl;
   return out;
}

inline void
eo_inheritance_detail_generator(std::ostream& out, eo_class const& cls)
{
   if(cls.eo_name != "EO_BASE_CLASS")
     out << inheritance_wrappers(cls)
         << "namespace efl { namespace eo { namespace detail {" << endl << endl
         << inheritance_base_operations(cls) << endl
         << inheritance_base_operations_size(cls)
         << inheritance_operations_description(cls)
         << inheritance_call_constructors(cls)
         << inheritance_eo_class_getter(cls)
         <<  "} } }" << endl;
}

} } } // namespace efl { namespace eolian { namespace grammar {

#endif // EOLIAN_CXX_STD_INHERITANCE_GENERATOR_HH