summaryrefslogtreecommitdiff
path: root/src/bin/eolian_mono/eolian/mono/marshall_type_impl.hh
blob: 69b4a9afc1b7fd776faf9ce69ed9b2411a078361 (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
/*
 * 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_MARSHALL_TYPE_IMPL_HH
#define EOLIAN_MONO_MARSHALL_TYPE_IMPL_HH

#include "grammar/generator.hpp"
#include "grammar/klass_def.hpp"
#include "grammar/case.hpp"
#include "helpers.hh"
#include "name_helpers.hh"
#include "type_impl.hh"
#include "generation_contexts.hh"
#include "blacklist.hh"

namespace eolian_mono {

namespace eina = efl::eina;

namespace detail {
  
template <typename OutputIterator, typename Context>
struct marshall_type_visitor_generate
{
   mutable OutputIterator sink;
   Context const* context;
   std::string c_type;
   bool is_out;
   bool is_return;
   bool is_ptr;
   bool is_special_subtype;

   typedef marshall_type_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 match_table[] =
        {
           // signed primitives
           {"string", true, [&]
              {
                regular_type_def r = regular;
                r.base_qualifier.qualifier ^= qualifier_info::is_ref;
                return replace_base_type(r, "System.String");
              }}
           , {"string", false, [&]
              {
                regular_type_def r = regular;
                r.base_qualifier.qualifier ^= qualifier_info::is_ref;
                return replace_base_type(r, "System.String");
              }}
           , {"mstring", true, [&]
              {
                regular_type_def r = regular;
                r.base_qualifier.qualifier ^= qualifier_info::is_ref;
                return replace_base_type(r, "System.String");
              }}
           , {"mstring", false, [&]
              {
                regular_type_def r = regular;
                r.base_qualifier.qualifier ^= qualifier_info::is_ref;
                return replace_base_type(r, "System.String");
              }}
           , {"stringshare", true, [&]
              {
                regular_type_def r = regular;
                r.base_qualifier.qualifier ^= qualifier_info::is_ref;
                if (is_special_subtype)
                  return replace_base_type(r, "Eina.Stringshare");
                return replace_base_type(r, "System.String");
              }}
           , {"stringshare", false, [&]
              {
                regular_type_def r = regular;
                r.base_qualifier.qualifier ^= qualifier_info::is_ref;
                if (is_special_subtype)
                  return replace_base_type(r, "Eina.Stringshare");
                return replace_base_type(r, "System.String");
              }}
           , {"strbuf", nullptr, [&]
              {
                regular_type_def r = regular;
                r.base_qualifier.qualifier ^= qualifier_info::is_ref;
                return replace_base_type(r, "Eina.Strbuf");
              }}
           , {"Binbuf", true, [&]
              {
                regular_type_def r = regular;
                r.base_type = "System.IntPtr";
                r.namespaces.clear();
                return r;
              }}
           , {"Binbuf", false, [&]
              {
                regular_type_def r = regular;
                r.base_type = "System.IntPtr";
                r.namespaces.clear();
                return r;
              }}
           , {"binbuf", nullptr, [&]
              {
                regular_type_def r = regular;
                r.base_type = "System.IntPtr";
                r.namespaces.clear();
                return r;
              }}
           , {"event", nullptr, [&]
              {
                regular_type_def r = regular;
                r.base_type = "Efl.Event.NativeStruct";
                r.namespaces.clear();
                return r;
              }}
           , {"any_value", true, [&]
               {
                regular_type_def r = regular;
                r.namespaces.clear();
                if (is_ptr)
                    r.base_type = "Eina.Value";
                else
                    r.base_type = "Eina.ValueNative";
                return r;
               }}
           , {"any_value", false, [&]
               {
                regular_type_def r = regular;
                r.namespaces.clear();
                if (is_ptr)
                    r.base_type = "Eina.Value";
                else
                    r.base_type = "Eina.ValueNative";
                return r;
               }}
           , {"any_value_ref", true, [&]
               {
                regular_type_def r = regular;
                r.namespaces.clear();
                r.base_type = "Eina.Value";
                return r;
               }}
           , {"any_value_ref", false, [&]
               {
                regular_type_def r = regular;
                r.namespaces.clear();
                r.base_type = "Eina.Value";
                return r;
               }}
           , {"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;
               }}
           , {"Value_Type", nullptr, [&]
               {
                regular_type_def r = regular;
                r.namespaces.clear();
                r.base_type = "Eina.ValueTypeBox";
                return r;
               }}
        };

        if (regular.is_struct() && !blacklist::is_struct_blacklisted(regular) && !(bool)(regular.base_qualifier & qualifier_info::is_own))
          {
             if ((is_out || is_return) && is_ptr)
                 return as_generator("System.IntPtr").generate(sink, attributes::unused, *context);
             return as_generator(string << ".NativeStruct")
                    .generate(sink, name_helpers::type_full_managed_name(regular), *context);
          }
        else if (eina::optional<bool> b = call_match
         (match_table
          , [&] (match const& m)
          {
            return (!m.name || *m.name == regular.base_type)
            && (!m.has_own || *m.has_own == (bool)(regular.base_qualifier & qualifier_info::is_own))
            ;
          }
          , [&] (attributes::type_def::variant_type const& v)
          {
            return v.visit(visitor_regular_type_def_printer<OutputIterator, Context>{sink, context}); // we want to keep is_out info
          }))
        {
           return *b;
        }
      else if (is_ptr && helpers::need_pointer_conversion(&regular))
        {
           regular_type_def r = regular;
           r.base_type = "System.IntPtr";
           r.namespaces.clear();
           return visitor_generate<OutputIterator, Context>{
             sink
             , context
             , c_type
             , is_out
             , is_return
             , is_ptr
             , false
             , is_special_subtype
           }(r);
        }
      else
        {
          return visitor_generate<OutputIterator, Context>{
            sink
            , context
            , c_type
            , is_out
            , is_return
            , is_ptr
            , false
            , is_special_subtype
          }(regular);
        }
   }
   bool operator()(attributes::klass_name klass_name) const
   {
     return visitor_generate<OutputIterator, Context>{
       sink
       , context
       , c_type
       , is_out
       , is_return
       , is_ptr
       , false
       , is_special_subtype
     }(klass_name);
   }
   bool operator()(attributes::complex_type_def const& complex) const
   {
      using attributes::regular_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[] =
      {
        {"array", nullptr, nullptr, [&]
           {
              return regular_type_def{"System.IntPtr", complex.outer.base_qualifier, {}};
           }
        }
        ,{"list", nullptr, nullptr, [&]
           {
              return regular_type_def{"System.IntPtr", complex.outer.base_qualifier, {}};
           }
        }
        ,{"hash", nullptr, nullptr, [&]
           {
              return regular_type_def{"System.IntPtr", complex.outer.base_qualifier, {}};
           }
        }
        ,{"iterator", nullptr, nullptr, [&]
           {
              return regular_type_def{"System.IntPtr", complex.outer.base_qualifier, {}};
           }
        }
        ,{"accessor", nullptr, nullptr, [&]
           {
              return regular_type_def{"System.IntPtr", complex.outer.base_qualifier, {}};
           }
        }
      };

      auto default_match = [&] (attributes::complex_type_def const& complex)
        {
          regular_type_def no_pointer_regular = complex.outer;
          return visitor_type{sink, context, c_type, false, false, false, false}(no_pointer_regular)
            && as_generator("<" << (type(false, false, true) % ", ") << ">").generate(sink, complex.subtypes, *context);
        };

      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;
        }

     return visitor_generate<OutputIterator, Context>{
       sink
       , context
       , c_type
       , is_out
       , is_return
       , is_ptr
       , false
       , is_special_subtype
     }(complex);
   }
};
} }

#endif