summaryrefslogtreecommitdiff
path: root/src/lib/eolian_cxx/eo_types.hh
blob: 843c2fab557192e462b6169432ebeb9fdeec218b (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

#ifndef EOLIAN_CXX_EO_TYPES_HH
#define EOLIAN_CXX_EO_TYPES_HH

#include <algorithm>
#include <string>
#include <vector>
#include <cassert>

namespace efl { namespace eolian {

struct eo_constructor;
struct eo_parameter;
struct eo_function;
struct eo_event;

typedef std::vector<std::string> ancestors_container_type;
typedef std::vector<std::string> includes_container_type;
typedef std::vector<eo_constructor> constructors_container_type;
typedef std::vector<eo_function> functions_container_type;
typedef std::vector<eo_parameter> parameters_container_type;
typedef std::vector<eo_event> events_container_type;


enum class eolian_scope
  {
     public_, protected_, private_
  };

struct eolian_type
{
   enum category_type
   {
     unknown_, simple_, complex_, callback_
   };

   eolian_type()
     : native("")
     , category(unknown_)
     , is_const(false)
     , is_own(false)
     , is_class(false)
     , binding_requires_optional(false)
     , binding()
     , includes()
   {}

   eolian_type(std::string native_,
               category_type category_,
               bool is_const_,
               bool is_own_,
               bool is_class_,
               bool binding_requires_optional_,
               std::string binding_,
               includes_container_type includes_)
     : native(native_)
     , category(category_)
     , is_const(is_const_)
     , is_own(is_own_)
     , is_class(is_class_)
     , binding_requires_optional(binding_requires_optional_)
     , binding(binding_)
     , includes(includes_)
   {
      assert(!native.empty());
      assert(category != unknown_);
   }

   eolian_type(std::string native_,
               category_type category_,
               includes_container_type const& includes_)
     : eolian_type(native_, category_, false, false, false, false, "", includes_)
   {
      assert(category == callback_);
   }

   std::string native;
   category_type category;
   bool is_const;
   bool is_own;
   bool is_class;
   bool binding_requires_optional;
   std::string binding;
   includes_container_type includes;
};

typedef std::vector<eolian_type> eolian_type_container;

struct eolian_type_instance
{
  eolian_type_instance()
    : is_out(false)
    , is_optional(false)
    , parts()
  {}

  eolian_type_instance(std::initializer_list<eolian_type> il,
                       bool is_out_ = false,
                       bool is_optional_ = false)
    : is_out(is_out_)
    , is_optional(is_optional_)
    , parts(il)
  {}

  explicit eolian_type_instance(std::size_t size)
    : is_out(false)
    , is_optional(false)
    , parts(size)
  {}

  bool empty() const { return parts.empty(); }
  std::size_t size() const { return parts.size(); }

  eolian_type& front() { return parts.front(); }
  eolian_type const& front() const { return parts.front(); }

  bool is_out;
  bool is_optional;
  eolian_type_container parts;
};

const efl::eolian::eolian_type
void_type { "void", efl::eolian::eolian_type::simple_, false, false, false, false, "", {} };

inline bool
type_is_void(eolian_type_instance const& type)
{
   return type.empty() || type.front().native.compare("void") == 0;
}

inline bool
type_is_binding(eolian_type const& type)
{
   return !type.binding.empty();
}

inline bool
type_is_binding(eolian_type_instance const& type)
{
   assert(!type.empty());
   return type_is_binding(type.front());
}

inline bool
type_is_out(eolian_type_instance const& type)
{
   return type.is_out;
}

inline bool
type_is_class(eolian_type const& type)
{
   return type.is_class;
}

inline bool
type_is_class(eolian_type_instance const& type)
{
   assert(!type.empty());
   return type_is_class(type.front());
}

inline bool
type_binding_requires_optional(eolian_type const& type)
{
   return type.binding_requires_optional;
}

inline bool
type_binding_requires_optional(eolian_type_instance const& type)
{
   assert(!type.empty());
   return type_binding_requires_optional(type.front());
}

inline bool
type_is_optional(eolian_type_instance const& type)
{
   return type.is_optional;
}

inline eolian_type
type_to_native(eolian_type const& type)
{
   eolian_type native(type);
   native.binding.clear();
   native.category = eolian_type::simple_;
   native.is_class = false;
   return native;
}

inline eolian_type
type_to_native(eolian_type_instance const& type_ins)
{
   assert(!type_ins.empty());
   return type_to_native(type_ins.front());
}

inline std::string
type_to_native_str(eolian_type_instance const& type_ins)
{
   return type_to_native(type_ins).native;
}

inline bool
type_is_complex(eolian_type const& type)
{
   return type.category == eolian_type::complex_;
}

inline bool
type_is_complex(eolian_type_instance const& type_ins)
{
   assert(!type_ins.empty());
   return type_is_complex(type_ins.front());
}

template <typename T>
inline bool
type_is_callback(T const&);

template <>
inline bool
type_is_callback(eolian_type const& type)
{
   return type.category == eolian_type::callback_;
}

template <>
inline bool
type_is_callback(eolian_type_instance const& type_ins)
{
   return type_is_callback(type_ins.front());
}

struct eo_generator_options
{
   std::string header_decl_file_name;
   std::string header_impl_file_name;
   includes_container_type cxx_headers;
   includes_container_type c_headers;
};

struct eo_class
{
   enum eo_class_type
     {
       regular_, regular_noninst_, interface_, mixin_
     };

   eo_class_type type;
   std::string name;
   std::string eo_name;
   ancestors_container_type parents;
   ancestors_container_type ancestors;
   constructors_container_type constructors;
   constructors_container_type optional_constructors;
   constructors_container_type all_constructors;
   functions_container_type functions;
   events_container_type own_events;
   events_container_type concrete_events;
   std::string comment;
   std::string name_space;
};

struct eo_parameter
{
   eolian_type_instance type;
   std::string name;
};

struct eo_constructor
{
   std::string name;
   std::string impl;
   parameters_container_type params;
   std::string comment;
};

struct eo_function
{
   enum eo_function_type
     {
       regular_, class_
     };
   eo_function_type type;
   eolian_scope scope;
   std::string name;
   std::string impl;
   eolian_type_instance ret;
   parameters_container_type params;
   std::string comment;
};

struct eo_event
{
   eo_event() : scope(eolian_scope::public_) {}
  
   eolian_scope scope;
   std::string name;
   std::string eo_name;
   //parameters_container_type params; // XXX desirable.
   std::string comment;

   bool operator<(eo_event const& other) const { return name < other.name; }
};


inline bool
function_is_void(eo_function const& func)
{
   return func.ret.empty() || func.ret.front().native.compare("void") == 0;
}

inline bool
function_is_static(eo_function const& func)
{
   return func.type == eo_function::class_;
}

inline unsigned int
parameters_count_callbacks(parameters_container_type const& parameters)
{
   unsigned int r = 0u;
   for (auto first = parameters.begin(), last = parameters.end()
          ; first != last ; ++first)
     if(type_is_callback(first->type) && first + 1 != last)
       ++r;
   return r;
}

inline parameters_container_type::const_iterator
parameters_find_callback(parameters_container_type const& parameters)
{
   for (auto it = parameters.cbegin(), last = parameters.cend();
        it != last; ++it)
     {
        if (type_is_callback((*it).type) && it + 1 != last)
          return it;
     }
   return parameters.cend();
}

} } // namespace efl { namespace eolian {

#endif // EFL_EOLIAN_CXX_EO_TYPES_HH