summaryrefslogtreecommitdiff
path: root/src/bin/eolian_mono/eolian/mono/helpers.hh
blob: 31f85924b84fce90ce99bf44c028c36234d0153d (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
 * Copyright 2019 by its authors. See AUTHORS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef EOLIAN_MONO_HELPERS_HH
#define EOLIAN_MONO_HELPERS_HH

#include "grammar/klass_def.hpp"
#include "grammar/context.hpp"
#include "blacklist.hh"
#include "generation_contexts.hh"
#include "name_helpers.hh"

namespace eolian_mono {

namespace helpers {

/* General helpers, not related directly with generating strings (those go in the name_helpers.hh). */

namespace attributes = efl::eolian::grammar::attributes;

inline bool need_struct_conversion(attributes::regular_type_def const* regular)
{
   return regular && regular->is_struct() && !blacklist::is_struct_blacklisted(*regular);
}

inline bool need_struct_conversion(attributes::parameter_def const& param, attributes::regular_type_def const* regular)
{
   if (param.direction == attributes::parameter_direction::in && param.type.has_own)
     return false;

   return need_struct_conversion(regular);
}

inline bool need_struct_conversion_in_return(attributes::type_def const& ret_type, attributes::parameter_direction const& direction)
{
   auto regular = efl::eina::get<attributes::regular_type_def>(&ret_type.original_type);

   if (!regular->is_struct())
     return false;

   if (regular->is_struct() && (direction == attributes::parameter_direction::out || direction == attributes::parameter_direction::unknown))
     return false;

   if (ret_type.has_own)
     return false;

   return true;
}

inline bool need_pointer_conversion(attributes::regular_type_def const* regular)
{
   if (!regular)
     return false;

   if (regular->is_enum()
       || (regular->is_struct() && name_helpers::type_full_eolian_name(*regular) != "Eina.Binbuf")
      )
     return true;

   std::set<std::string> const types {
     "bool", "char"
     , "byte" , "short" , "int" , "long" , "llong" , "int8" , "int16" , "int32" , "int64" , "ssize"
     , "ubyte", "ushort", "uint", "ulong", "ullong", "uint8", "uint16", "uint32", "uint64", "size"
     , "ptrdiff"
     , "float", "double"
   };
   if (types.find(regular->base_type) != types.end())
     return true;

   return false;
}

// While klass_def has immediate_inherits, we need a way to get all interfaces inherited by an interface
// either directly or through another interface.
std::set<attributes::klass_name, attributes::compare_klass_name_by_name> interface_inherits(attributes::klass_def const& cls)
{
   std::set<attributes::klass_name, attributes::compare_klass_name_by_name> inherits;

   std::function<void(attributes::klass_name const&)> inherit_algo =
       [&] (attributes::klass_name const& klass)
       {
          // TODO we could somehow cache klass_def instantiations
          attributes::klass_def c(get_klass(klass, cls.unit), cls.unit);
          for(auto&& inherit : c.immediate_inherits)
            {
               switch(inherit.type)
                 {
                 case attributes::class_type::mixin:
                 case attributes::class_type::interface_:
                   inherits.insert(inherit);
                   inherit_algo(inherit);
                   break;
                 case attributes::class_type::regular:
                 case attributes::class_type::abstract_:
                   inherit_algo(inherit);
                 default:
                   break;
                 }
            }
       };

   inherit_algo(get_klass_name(cls));


   return inherits;
}

// Returns the set of interfaces implemented by this type that haven't been implemented
// by a regular parent class.
template<typename Context>
std::set<attributes::klass_name, attributes::compare_klass_name_by_name> non_implemented_interfaces(attributes::klass_def const& cls, Context const& context)
{
   auto options = efl::eolian::grammar::context_find_tag<options_context>(context);
   std::set<attributes::klass_name, attributes::compare_klass_name_by_name> implemented_interfaces;
   std::set<attributes::klass_name, attributes::compare_klass_name_by_name> interfaces;

   std::function<void(attributes::klass_name const&, bool)> inherit_algo =
       [&] (attributes::klass_name const& klass, bool is_implemented)
       {
          // TODO we could somehow cache klass_def instantiations
          attributes::klass_def c(get_klass(klass, cls.unit), cls.unit);
          for(auto&& inherit : c.immediate_inherits)
            {
               if (inherit.is_beta && !options.want_beta)
                   continue;

               switch(inherit.type)
                 {
                 case attributes::class_type::mixin:
                 case attributes::class_type::interface_:
                   interfaces.insert(inherit);
                   if (is_implemented)
                     implemented_interfaces.insert(inherit);
                   inherit_algo(inherit, is_implemented);
                   break;
                 case attributes::class_type::abstract_:
                 case attributes::class_type::regular:
                   inherit_algo(inherit, true);
                 default:
                   break;
                 }
            }
       };

   inherit_algo(get_klass_name(cls), false);

   for (auto&& inherit : implemented_interfaces)
     interfaces.erase(inherit);


   return interfaces;
}

// Returns the set of interfaces implemented by this type that haven't been implemented
// by a regular parent class and beta path information
template<typename Context>
std::set<std::pair<attributes::klass_name, bool>, attributes::compare_klass_name_by_name>
  non_implemented_interfaces_and_beta(attributes::klass_def const& cls, Context const& context)
{
   auto options = efl::eolian::grammar::context_find_tag<options_context>(context);
   std::set<std::pair<attributes::klass_name, bool>, attributes::compare_klass_name_by_name> implemented_interfaces;
   std::set<std::pair<attributes::klass_name, bool>, attributes::compare_klass_name_by_name> interfaces;

   std::function<void(attributes::klass_name const&, bool, bool)> inherit_algo =
       [&] (attributes::klass_name const& klass, bool is_implemented, bool pbeta)
       {
          // TODO we could somehow cache klass_def instantiations
          attributes::klass_def c(get_klass(klass, cls.unit), cls.unit);
          for(auto&& inherit : c.immediate_inherits)
            {
               auto beta = pbeta | inherit.is_beta;
               switch(inherit.type)
                 {
                 case attributes::class_type::mixin:
                 case attributes::class_type::interface_:
                   interfaces.insert(std::make_pair(inherit, beta));
                   if (is_implemented)
                     implemented_interfaces.insert(std::make_pair(inherit, beta));
                   inherit_algo(inherit, is_implemented, beta);
                   break;
                 case attributes::class_type::abstract_:
                 case attributes::class_type::regular:
                   inherit_algo(inherit, true, beta);
                 default:
                   break;
                 }
            }
       };

   inherit_algo(get_klass_name(cls), false, cls.is_beta);

   for (auto&& inherit : implemented_interfaces)
     interfaces.erase(inherit);

   return interfaces;
}
  

/*
 * Determines whether this class has any regular ancestor or not
 */
bool has_regular_ancestor(attributes::klass_def const& cls)
{
   auto inherits = cls.inherits;
   std::function<bool(attributes::klass_name const&)> is_regular =
       [&] (attributes::klass_name const& klass)
       {
          return klass.type == attributes::class_type::regular || klass.type == attributes::class_type::abstract_;
       };

   return std::any_of(inherits.begin(), inherits.end(), is_regular);
}

/*
 * Sugar for checking if a given class in in the inheritance tree
 */
bool inherits_from(attributes::klass_def const& cls, std::string const& name)
{
   return std::any_of(cls.inherits.begin(), cls.inherits.end(),
           [&](attributes::klass_name const& inherit)
           {
                return name_helpers::klass_full_concrete_or_interface_name(inherit) == name;
           });
}

/*
 * Gets all methods that this class should implement (i.e. that come from an unimplemented interface/mixin and the class itself)
 */
template<typename Context>
std::vector<attributes::function_def> get_all_implementable_methods(attributes::klass_def const& cls, Context const& context
                                                                    , bool insert_properties = false)
{
   bool want_beta = efl::eolian::grammar::context_find_tag<options_context>(context).want_beta;
   std::vector<attributes::function_def> ret;
   auto filter = [&](attributes::function_def const& func) {
       if (!want_beta)
         return !func.is_beta
           && (
               insert_properties
               ? true
               :
               !(func.type == attributes::function_type::property
                 || func.type == attributes::function_type::prop_set
                 || func.type == attributes::function_type::prop_get));
       else
         return insert_properties ? true
           : !(func.type == attributes::function_type::property
               || func.type == attributes::function_type::prop_set
               || func.type == attributes::function_type::prop_get);
   };

   std::copy_if(cls.functions.begin(), cls.functions.end(), std::back_inserter(ret), filter);

   // Non implemented interfaces
   std::set<attributes::klass_name, attributes::compare_klass_name_by_name> implemented_interfaces;
   std::set<attributes::klass_name, attributes::compare_klass_name_by_name> interfaces;
   std::function<void(attributes::klass_name const&, bool)> inherit_algo =
       [&] (attributes::klass_name const &klass, bool is_implemented)
       {
           attributes::klass_def c(get_klass(klass, cls.unit), cls.unit);
           for (auto&& inherit: c.immediate_inherits)
             {
                switch(inherit.type)
                  {
                  case attributes::class_type::mixin:
                  case attributes::class_type::interface_:
                    interfaces.insert(inherit);
                    if (is_implemented)
                      implemented_interfaces.insert(inherit);
                    inherit_algo(inherit, is_implemented);
                    break;
                  case attributes::class_type::abstract_:
                  case attributes::class_type::regular:
                    inherit_algo(inherit, true);
                  default:
                    break;
                  }
             }
       };

   inherit_algo(attributes::get_klass_name(cls), false);

   for (auto&& inherit : implemented_interfaces)
     interfaces.erase(inherit);

    for (auto&& inherit : interfaces)
    {
        attributes::klass_def klass(get_klass(inherit, cls.unit), cls.unit);
        std::copy_if(klass.functions.cbegin(), klass.functions.cend(), std::back_inserter(ret), filter);
    }

  return ret;
}

/*
 * Gets all methods that this class should implement (i.e. that come from an unimplemented interface/mixin and the class itself)
 */
template<typename Context>
std::vector<attributes::property_def> get_all_implementable_properties(attributes::klass_def const& cls, Context const& context)
{
   bool want_beta = efl::eolian::grammar::context_find_tag<options_context>(context).want_beta;
   std::vector<attributes::property_def> ret;
   auto filter_beta = [want_beta, cls](attributes::property_def const& prop) {
       if (!want_beta)
         return !cls.is_beta && (!prop.getter || !prop.getter->is_beta)
           && (!prop.setter || !prop.setter->is_beta);
       else
         return true;
   };

   std::copy_if(cls.properties.begin(), cls.properties.end(), std::back_inserter(ret), filter_beta);

   // Non implemented interfaces
   std::set<attributes::klass_name, attributes::compare_klass_name_by_name> implemented_interfaces;
   std::set<attributes::klass_name, attributes::compare_klass_name_by_name> interfaces;
   std::function<void(attributes::klass_name const&, bool)> inherit_algo =
       [&] (attributes::klass_name const &klass, bool is_implemented)
       {
           attributes::klass_def c(get_klass(klass, cls.unit), cls.unit);
           for (auto&& inherit: c.immediate_inherits)
             {
                switch(inherit.type)
                  {
                  case attributes::class_type::mixin:
                  case attributes::class_type::interface_:
                    interfaces.insert(inherit);
                    if (is_implemented)
                      implemented_interfaces.insert(inherit);
                    inherit_algo(inherit, is_implemented);
                    break;
                  case attributes::class_type::abstract_:
                  case attributes::class_type::regular:
                    inherit_algo(inherit, true);
                  default:
                    break;
                  }
             }
       };

   inherit_algo(attributes::get_klass_name(cls), false);

   for (auto&& inherit : implemented_interfaces)
     interfaces.erase(inherit);

    for (auto&& inherit : interfaces)
    {
        attributes::klass_def klass(get_klass(inherit, cls.unit), cls.unit);
        std::copy_if(klass.properties.cbegin(), klass.properties.cend(), std::back_inserter(ret), filter_beta);
    }

  return ret;
}

template<typename Klass>
inline bool is_managed_interface(Klass const& klass)
{
    return klass.type == attributes::class_type::interface_
           || klass.type == attributes::class_type::mixin;
}


/*
 * Gets all methods that this class should register (i.e. that comes from it and non-public interface methods
 * that this class is the first one implementing)
 */
template<typename Context>
std::vector<attributes::function_def> get_all_registerable_methods(attributes::klass_def const& cls, Context const& context)
{
   std::vector<attributes::function_def> ret;

   auto implementable_methods = get_all_implementable_methods(cls, context);

   std::copy_if(implementable_methods.cbegin(), implementable_methods.cend(), std::back_inserter(ret)
                , [&cls](attributes::function_def const & func) {

                    if (cls == func.klass)
                      return true;
                    if (func.type == attributes::function_type::property
                        || func.type == attributes::function_type::prop_set
                        || func.type == attributes::function_type::prop_get)
                      return false;

                    if (is_managed_interface(func.klass) && func.is_static)
                      return true;

                    if (!is_managed_interface(func.klass) || func.scope != attributes::member_scope::scope_public)
                      return true;
                    return false;
               });

   return ret;
}

/*
 * Checks whether the given is unique going up the inheritance tree from leaf_klass
 */
inline bool is_unique_event(attributes::event_def const& evt
                         , attributes::klass_def const& leaf_klass)
{
  auto events = leaf_klass.get_all_events();
  int i = 1;
  return !std::any_of(events.cbegin(), events.cend(),
                         [&evt, &i](const attributes::event_def &other) {
                            return evt.name == other.name && i++ == 2;
                     });
}

inline std::vector<attributes::constructor_def> reorder_constructors(std::vector<attributes::constructor_def> constructors)
{
  auto is_required = [](attributes::constructor_def const& ctr) { return !ctr.is_optional; };
  std::stable_partition(constructors.begin(), constructors.end(), is_required);
  return constructors;
}

enum class has_property_wrapper_bit
{
 has_none               = 0
 , has_getter           = 1 << 0
 , has_setter           = 1 << 1
 , has_indexer          = 1 << 2
 , has_key_tuple        = 1 << 3
 , has_value_tuple      = 1 << 4
 , has_set_error_check  = 1 << 5
 , has_get_error_check  = 1 << 6
};

has_property_wrapper_bit& operator|=(has_property_wrapper_bit& self, has_property_wrapper_bit bit)
{
  self = static_cast<has_property_wrapper_bit>(static_cast<int>(self) | static_cast<int>(bit));
  return self;
}

bool operator&(has_property_wrapper_bit self, has_property_wrapper_bit bit)
{
  return static_cast<int>(self) & static_cast<int>(bit);
}

template <typename Context>
has_property_wrapper_bit has_property_wrapper(attributes::property_def const& property, attributes::klass_def const* implementing_klass
                                              , Context const& context)
{
  using efl::eolian::grammar::context_find_tag;
  has_property_wrapper_bit r = has_property_wrapper_bit::has_none;
  
  if (blacklist::is_property_blacklisted(property, *implementing_klass, context))
    return r;

  bool has_getter = property.getter.is_engaged();
  bool has_setter = property.setter.is_engaged();

  bool is_interface = context_find_tag<class_context>(context).current_wrapper_kind == class_context::interface;
  bool is_static = (property.getter.is_engaged() && property.getter->is_static)
    || (has_setter && property.setter->is_static);
  bool is_concrete = context_find_tag<class_context>(context).current_wrapper_kind == class_context::concrete;
      
  if (is_static)
    {
       if (is_interface) return r;
       else if (is_concrete) return r;
    }

  // EINA_LOG_ERR("Generating property %s", name_helpers::property_managed_name(property).c_str());
  // C# interface can have only 
  if (is_interface)
    {
       has_getter = has_getter && property.getter->scope == attributes::member_scope:: scope_public;
    }

  if (!has_getter)
    {
       return r;
    }

  if (property.getter->explicit_return_type != attributes::void_)
    {
       return r;
    }
  else if (has_setter)
    {
       if (property.setter->explicit_return_type != attributes::void_)
         has_setter = false; // do not generate setter
       else if (property.setter->keys != property.getter->keys)
         has_setter = false;
       else if (property.setter->values != property.getter->values)
         has_setter = false;
    }

  if (is_interface)
    {
       if (property.getter->scope != attributes::member_scope::scope_public)
         return r;
       else if (has_setter && property.setter->scope != attributes::member_scope::scope_public)
         has_setter = false;
    }
  
  if (has_getter)
    r |= has_property_wrapper_bit::has_getter;
  if (has_setter)
    r |= has_property_wrapper_bit::has_setter;
  
  if (property.getter->keys.size() == 1)
    r |= has_property_wrapper_bit::has_indexer;
  else if (property.getter->keys.size() > 1)
  {
    r |= has_property_wrapper_bit::has_indexer;
    r |= has_property_wrapper_bit::has_key_tuple;
  }

  if (property.getter->values.size() > 1)
    r |= has_property_wrapper_bit::has_value_tuple;

  return r;
}

} // namespace helpers

} // namespace eolian_mono

#endif