aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/eolian_mono/eolian/mono/type_impl.hh
blob: c449d216a5b80739a716fa4119de382a5247fc02 (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
#ifndef EOLIAN_MONO_TYPE_IMPL_HH
#define EOLIAN_MONO_TYPE_IMPL_HH

#include "grammar/generator.hpp"
#include "grammar/klass_def.hpp"
#include "grammar/case.hpp"
#include "name_helpers.hh"

namespace eolian_mono {

namespace eina = efl::eina;

template <typename T>
T const* as_const_pointer(T* p) { return p; }

inline
attributes::regular_type_def replace_base_type(attributes::regular_type_def v, std::string name)
{
  v.base_type = name;
  return v;
}

template <typename T>
attributes::regular_type_def replace_base_integer(attributes::regular_type_def v)
{
  bool s = std::is_signed<T>::value;
  switch (sizeof(T))
  {
  case 1: return s ? replace_base_type(v, " sbyte") : replace_base_type(v, " byte");
  case 2: return s ? replace_base_type(v, " short") : replace_base_type(v, " ushort");
  case 4: return s ? replace_base_type(v, " int") : replace_base_type(v, " uint");
  case 8: return s ? replace_base_type(v, " long") : replace_base_type(v, " ulong");
  default: return v;
  }
}

template <typename T>
attributes::regular_type_def replace_base_opt_integer(attributes::regular_type_def v)
{
  bool s = std::is_signed<T>::value;
  switch (sizeof(T))
  {
  case 1: return s ? replace_base_type(v, " sbyte?") : replace_base_type(v, " byte?");
  case 2: return s ? replace_base_type(v, " short?") : replace_base_type(v, " ushort?");
  case 4: return s ? replace_base_type(v, " int?") : replace_base_type(v, " uint?");
  case 8: return s ? replace_base_type(v, " long?") : replace_base_type(v, " ulong?");
  default: return v;
  }
}
  
inline
attributes::complex_type_def replace_outer(attributes::complex_type_def v, attributes::regular_type_def const& regular)
{
  v.outer = regular;
  return v;
}
      
template <typename Array, typename F, int N, typename A>
eina::optional<bool> call_match(Array const (&array)[N], F f, A a)
{
   typedef Array const* iterator_type;
   iterator_type match_iterator = &array[0], match_last = match_iterator + N;
   match_iterator = std::find_if(match_iterator, match_last, f);
   if(match_iterator != match_last)
     {
        return a(match_iterator->function());
     }
   return {nullptr};
}
      
template <typename OutputIterator, typename Context>
struct visitor_generate
{
   mutable OutputIterator sink;
   Context const* context;
   std::string c_type;
   bool is_out;
   bool is_return;
   bool is_ptr;
   mutable bool is_optional;

   typedef visitor_generate<OutputIterator, Context> visitor_type;
   typedef bool result_type;
  
   bool operator()(attributes::regular_type_def const& regular) const
   {
      using attributes::regular_type_def;
      struct match
      {
        eina::optional<std::string> name;
        eina::optional<bool> has_own;
        std::function<attributes::type_def::variant_type()> function;
      }
      const optional_match_table[] =
        {
           // signed primitives
             {"byte", nullptr, [&] { return replace_base_type(regular, " sbyte?"); }}
           , {"float", nullptr, [&] { return replace_base_type(regular, " float?"); }}
           , {"double", nullptr, [&] { return replace_base_type(regular, " double?"); }}
           , {"bool", nullptr, [&] { return replace_base_type(regular, " bool?"); }}
           , {"short", nullptr, [&] { return replace_base_opt_integer<short>(regular); }}
           , {"int", nullptr, [&] { return replace_base_opt_integer<int>(regular); }}
           , {"long", nullptr, [&] { return replace_base_opt_integer<long>(regular); }}
           , {"llong", nullptr, [&] { return replace_base_opt_integer<long long>(regular); }}
           , {"int8", nullptr, [&] { return replace_base_type(regular, " sbyte?"); }}
           , {"int16", nullptr, [&] { return replace_base_type(regular, " short?"); }}
           , {"int32", nullptr, [&] { return replace_base_type(regular, " int?"); }}
           , {"int64", nullptr, [&] { return replace_base_type(regular, " long?"); }}
           , {"ssize", nullptr, [&] { return replace_base_opt_integer<ssize_t>(regular); }}
           // unsigned primitives
           , {"ubyte", nullptr, [&] { return replace_base_type(regular, " byte?"); }}
           , {"ushort", nullptr, [&] { return replace_base_opt_integer<unsigned short>(regular); }}
           , {"uint", nullptr, [&] { return replace_base_opt_integer<unsigned int>(regular); }}
           , {"ulong", nullptr, [&] { return replace_base_opt_integer<unsigned long>(regular); }}
           , {"ullong", nullptr, [&] { return replace_base_opt_integer<unsigned long long>(regular); }}
           , {"uint8", nullptr, [&] { return replace_base_type(regular, " byte?"); }}
           , {"uint16", nullptr, [&] { return replace_base_type(regular, " ushort?"); }}
           , {"uint32", nullptr, [&] { return replace_base_type(regular, " uint?"); }}
           , {"uint64", nullptr, [&] { return replace_base_type(regular, " ulong?"); }}
           , {"size", nullptr, [&] { return replace_base_opt_integer<size_t>(regular); }}
           
           , {"ptrdiff", nullptr, [&] { return replace_base_opt_integer<ptrdiff_t>(regular); }}
           , {"intptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr?"); }}
           , {"uintptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr?"); }}
           , {"void_ptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr?"); }}
        };
      struct match
      const match_table[] =
        {
           // signed primitives
             {"byte", nullptr, [&] { return replace_base_type(regular, " sbyte"); }}
           , {"short", nullptr, [&] { return replace_base_integer<short>(regular); }}
           , {"int", nullptr, [&] { return replace_base_integer<int>(regular); }}
           , {"long", nullptr, [&] { return replace_base_integer<long>(regular); }}
           , {"llong", nullptr, [&] { return replace_base_integer<long long>(regular); }}
           , {"int8", nullptr, [&] { return replace_base_type(regular, " sbyte"); }}
           , {"int16", nullptr, [&] { return replace_base_type(regular, " short"); }}
           , {"int32", nullptr, [&] { return replace_base_type(regular, " int"); }}
           , {"int64", nullptr, [&] { return replace_base_type(regular, " long"); }}
           , {"ssize", nullptr, [&] { return replace_base_integer<ssize_t>(regular); }}
           // unsigned primitives
           , {"ubyte", nullptr, [&] { return replace_base_type(regular, " byte"); }}
           , {"ushort", nullptr, [&] { return replace_base_integer<unsigned short>(regular); }}
           , {"uint", nullptr, [&] { return replace_base_integer<unsigned int>(regular); }}
           , {"ulong", nullptr, [&] { return replace_base_integer<unsigned long>(regular); }}
           , {"ullong", nullptr, [&] { return replace_base_integer<unsigned long long>(regular); }}
           , {"uint8", nullptr, [&] { return replace_base_type(regular, " byte"); }}
           , {"uint16", nullptr, [&] { return replace_base_type(regular, " ushort"); }}
           , {"uint32", nullptr, [&] { return replace_base_type(regular, " uint"); }}
           , {"uint64", nullptr, [&] { return replace_base_type(regular, " ulong"); }}
           , {"size", nullptr, [&] { return replace_base_integer<size_t>(regular); }}
           
           , {"ptrdiff", nullptr, [&] { return replace_base_integer<ptrdiff_t>(regular); }}
           , {"intptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr"); }}
           , {"uintptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr"); }}
           , {"void_ptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr"); }}
           , {"void", nullptr, [&]
               {
                  regular_type_def r = regular;
                  r.namespaces.clear();
                  if (is_out) // @inout too
                      r.base_type = " System.IntPtr";
                  else
                      r.base_type = " void";
                  return r;
              }}
           , {"Eina.Error", nullptr, [&] // Eina.Error
              {
                return regular_type_def{" Eina.Error", regular.base_qualifier, {}};
              }} // TODO
           , {"string", nullptr, [&]
              {
                regular_type_def r = regular;
                r.base_qualifier.qualifier ^= qualifier_info::is_ref;
                return replace_base_type(r, " System.String");
              }}
           , {"mstring", nullptr, [&]
              {
                regular_type_def r = regular;
                r.base_qualifier.qualifier ^= qualifier_info::is_ref;
                return replace_base_type(r, " System.String");
              }}
           , {"stringshare", nullptr, [&]
              {
                regular_type_def r = regular;
                r.base_qualifier.qualifier ^= qualifier_info::is_ref;
                return replace_base_type(r, " System.String");
              }}
           , {"strbuf", nullptr, [&]
              {
                return regular_type_def{" Eina.Strbuf", regular.base_qualifier, {}};
              }}
           , {"any_value", true, [&]
              { return regular_type_def{" Eina.Value", regular.base_qualifier, {}};
              }}
           , {"any_value", false, [&]
              { return regular_type_def{" Eina.Value", regular.base_qualifier, {}};
              }}
           , {"any_value_ptr", nullptr, [&] 
              { return regular_type_def{" Eina.Value", regular.base_qualifier, {}};
              }} // FIXME add proper support for any_value_ptr
        };
        std::string full_type_name = name_helpers::type_full_eolian_name(regular);
        if(eina::optional<bool> b = call_match
         (optional_match_table
          , [&] (match const& m)
          {
            return is_optional
              && (!m.name || *m.name == regular.base_type || *m.name == full_type_name)
              && (!m.has_own || *m.has_own == (bool)(regular.base_qualifier & qualifier_info::is_own))
            ;
          }
          , [&] (attributes::type_def::variant_type const& v)
          {
            return v.visit(*this); // we want to keep is_out info
          }))
        {
           return *b;
        }
        else if (is_optional && (regular.is_struct() || regular.is_enum() || regular.is_struct_opaque()))
        {
          attributes::regular_type_def r = regular;
          r.base_type.push_back('?');
          is_optional = false;
          return (*this)(r);
        }
        else if(eina::optional<bool> b = call_match
         (match_table
          , [&] (match const& m)
          {
            return (!m.name || *m.name == regular.base_type || *m.name == full_type_name)
            && (!m.has_own || *m.has_own == (bool)(regular.base_qualifier & qualifier_info::is_own))
            ;
          }
          , [&] (attributes::type_def::variant_type const& v)
          {
            return v.visit(*this); // we want to keep is_out info
          }))
        {
           return *b;
        }
      // in A @optional -> optional<A>
      // in A& @optional -> optional<A&>
      // in A& @optional -> optional<A&>
      // in own(A&) @optional -> A*
      //
      // out A @optional -> optional<A&>
      // out A& @optional -> optional<A&>
      // out own(A&) @optional -> optional<A*&>
      // else if(regular.base_qualifier & qualifier_info::is_optional)
      //  {
      //    attributes::regular_type_def no_optional_regular = regular;
      //    no_optional_regular.base_qualifier.qualifier ^= qualifier_info::is_optional;
      //    if(is_out)
      //      {
      //        if(no_optional_regular.base_qualifier & qualifier_info::is_own)
      //          {
      //            return as_generator(" ::efl::eina::optional<").generate(sink, attributes::unused, *context)
      //              && (*this)(no_optional_regular)
      //              && as_generator("&>").generate(sink, attributes::unused, *context);
      //         }
      //        else if(no_optional_regular.base_qualifier & qualifier_info::is_ref)
      //          {
      //             no_optional_regular.base_qualifier.qualifier ^= qualifier_info::is_ref;
      //             return (*this)(no_optional_regular)
      //               && as_generator("**").generate(sink, attributes::unused, *context);
      //          }
      //        else
      //          return (*this)(no_optional_regular)
      //            && as_generator("*").generate(sink, attributes::unused, *context);
      //      }
      //    else
      //      {
      //        // regular.base_qualifier & qualifier_info::is_ref
      //        return as_generator(" ::efl::eina::optional<").generate(sink, attributes::unused, *context)
      //          && (*this)(no_optional_regular)
      //          && as_generator(">").generate(sink, attributes::unused, *context);
      //      }
      //  }
      // else if((is_return || is_out) && regular.base_qualifier & qualifier_info::is_ref
      //         && regular.base_qualifier & qualifier_info::is_own)
      //   {
      //     if(as_generator
      //        (
      //         " ::std::unique_ptr<"
      //         << *(string << "_")
      //         << string
      //         << (regular.base_qualifier & qualifier_info::is_const ? " const" : "")
      //         << ", ::efl::eina::malloc_deleter>"
      //        )
      //        .generate(sink, std::make_tuple(regular.namespaces, regular.base_type), *context))
      //       return true;
      //     else
      //       return false;
      //   }
      // else if(Eolian_Typedecl const* typedecl = eolian_state_struct_by_name_get(c_type.c_str()))
      //   {
      //   return as_generator
      //        (
      //         *(string << ".")
      //         << string
      //        )
      //     .generate(sink, std::make_tuple(regular.namespaces, regular.base_type), *context);
      //   }
      else
        {
          return as_generator(string).generate(sink, name_helpers::type_full_managed_name(regular), *context);
        }
   }
   bool operator()(attributes::klass_name klass) const
   {
     if(klass.type == attributes::class_type::regular || klass.type == attributes::class_type::abstract_)
       return as_generator(string).generate(sink, name_helpers::klass_full_concrete_name(klass), *context);
     else
       return as_generator(string).generate(sink, name_helpers::klass_full_interface_name(klass), *context);
   }
   bool operator()(attributes::complex_type_def const& complex) const
   {
      using attributes::regular_type_def;
      using attributes::complex_type_def;
      using attributes::qualifier_info;
       struct match
      {
        eina::optional<std::string> name;
        eina::optional<bool> has_own;
        eina::optional<bool> is_const;
        std::function<attributes::type_def::variant_type()> function;
      } const matches[] =
      {
        {"list", nullptr, nullptr, [&]
         {
           complex_type_def c = complex;
           c.outer.base_type = "Eina.List";
           return c;
         }}
        , {"inlist", nullptr, nullptr, [&]
           {
           complex_type_def c = complex;
           c.outer.base_type = "Eina.Inlist";
           return c;
         }}
        , {"array", nullptr, nullptr, [&]
           {
           complex_type_def c = complex;
           c.outer.base_type = "Eina.Array";
           return c;
         }}
        , {"inarray", nullptr, nullptr, [&]
           {
           complex_type_def c = complex;
           c.outer.base_type = "Eina.Inarray";
           return c;
         }}
        , {"hash", nullptr, nullptr
           , [&]
           {
             complex_type_def c = complex;
             c.outer.base_type = "Eina.Hash";
             return c;
         }}
        , {"future", nullptr, nullptr, [&]
           {
             (*this)(regular_type_def{" Eina.Future", complex.outer.base_qualifier, {}});
             return attributes::type_def::variant_type();
           }           
          }
        , {"iterator", nullptr, nullptr, [&]
           {
             complex_type_def c = complex;
             c.outer.base_type = "Eina.Iterator";
             return c;
           }           
          }
        , {"accessor", nullptr, nullptr, [&]
           {
             complex_type_def c = complex;
             c.outer.base_type = "Eina.Accessor";
             return c;
           }           
          }
      };

      auto default_match = [&] (attributes::complex_type_def const& complex)
        {
          regular_type_def no_pointer_regular = complex.outer;
          // std::vector<attributes::pointer_indirection> pointers;
          // pointers.swap(no_pointer_regular.pointers);
          // if(is_out)
          //   pointers.push_back({{attributes::qualifier_info::is_none, {}}, true});
          return visitor_type{sink, context, c_type, false}(no_pointer_regular)
            && as_generator("<" << (type % ", ") << ">").generate(sink, complex.subtypes, *context)
          ;
            // && detail::generate_pointers(sink, pointers, *context, false);
        };
       
      if(eina::optional<bool> b = call_match
         (matches
          , [&] (match const& m)
          {
            return (!m.name || *m.name == complex.outer.base_type)
            && (!m.has_own || *m.has_own == bool(complex.outer.base_qualifier & qualifier_info::is_own))
            && (!m.is_const || *m.is_const == bool(complex.outer.base_qualifier & qualifier_info::is_const));
          }
          , [&] (attributes::type_def::variant_type const& v)
          {
            if(v.empty())
              return true;
            else if(attributes::complex_type_def const* complex
               = eina::get<attributes::complex_type_def>(&v))
              return default_match(*complex);
            else
              return v.visit(*this);
          }))
        return *b;
      else
        {
          return default_match(complex);
        }
   }
};
      
}

#endif