summaryrefslogtreecommitdiff
path: root/src/bin/eolian_cxx/type_lookup.hh
blob: a1b32204f45b1afe1902608920369200f9594817 (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

#ifndef EOLIAN_CXX_TYPE_LOOKUP_HH
#define EOLIAN_CXX_TYPE_LOOKUP_HH

#include <algorithm>
#include <string>
#include <vector>
#include <cctype>
#include <iterator>
#include <stdexcept>
#include <cassert>
#include <cstddef>

#include <Eolian.h>
#include <eolian_database.h>

#include <Eina.hh>

#include "eo_types.hh"
#include "safe_strings.hh"

namespace eolian_cxx {

typedef std::vector<efl::eolian::eolian_type> lookup_table_type;
extern const lookup_table_type type_lookup_table;

inline std::string
class_format_cxx(std::string const& fullname)
{
   auto current = fullname.begin(), last = fullname.end();
   auto found = std::find(current, last, '.');
   std::string new_string;
   while (current != last)
     {
        if(found == last)
          {
            new_string.insert(new_string.end(), current, found);
            current = found;
          }
        else
          {
            new_string += std::tolower(*current);
            new_string.insert(new_string.end(), std::next(current), found);
            new_string += "::";
            current = std::next(found);
            found = std::find(current, last, '.');
          }
     }
   return new_string;
}

inline bool
type_is_complex(Eolian_Type const& type)
{
   return ::eolian_type_type_get(&type) == EOLIAN_TYPE_COMPLEX;
}

inline efl::eolian::eolian_type
type_from_eolian(Eolian_Type const& type)
{
   efl::eolian::eolian_type x;

   Eolian_Type_Type tpt = ::eolian_type_type_get(&type);
   if (tpt == EOLIAN_TYPE_POINTER || tpt == EOLIAN_TYPE_REGULAR)
     {
        Eolian_Type const* base_type = ::eolian_type_base_type_get(&type);
        if (base_type && ::eolian_type_type_get(base_type) == EOLIAN_TYPE_CLASS)
          {
             Eolian_Class const* klass = ::eolian_type_class_get(base_type);
             if (klass)
               {
                  x.category = efl::eolian::eolian_type::simple_;
                  x.is_class = true;
                  x.binding_requires_optional = false;
                  x.binding = "::" + class_format_cxx(safe_str(::eolian_class_full_name_get(klass)));

                  Eina_Stringshare* klass_file = ::eolian_class_file_get(klass);
                  if (klass_file)
                    x.includes = {safe_str(klass_file) + ".hh"};
               }
          }
     }

   x.native = normalize_spaces(safe_str(::eolian_type_c_type_get(&type)));
   x.is_own = ::eolian_type_is_own(&type);
   x.is_const = ::eolian_type_is_const(&type);
   return x;
}

template <typename Iterator>
inline const efl::eolian::eolian_type&
type_find(Iterator first, Iterator last, efl::eolian::eolian_type const& type)
{
   auto res = std::find_if
     (first, last,
      [&type] (efl::eolian::eolian_type const& x)
      {
        return (x.native == type.native && x.is_own == type.is_own);
      });
   return (res != last) ? *res : type;
}

inline efl::eolian::eolian_type_instance
type_lookup(const Eolian_Type* type,
            lookup_table_type const& lut = type_lookup_table)
{
   if (type == NULL) return { efl::eolian::void_type }; // XXX shouldn't
   // assert(type != NULL);

   std::vector<Eolian_Type const*> types;
   types.push_back(type);

   if (::eolian_type_type_get(type) == EOLIAN_TYPE_POINTER && type_is_complex(*eolian_type_base_type_get(type)))
     {
        efl::eina::iterator<Eolian_Type const> end;
        efl::eina::iterator<Eolian_Type const> it
          (::eolian_type_subtypes_get(eolian_type_base_type_get(type)));
        while(it != end)
          {
             if(Eolian_Type const* t = &*it)
               types.push_back(t), ++it;
          }
     }

   efl::eolian::eolian_type_instance v(types.size());
   for (std::size_t i = 0; i != types.size(); ++i)
     {
        v.parts[i] = type_find(lut.begin(), lut.end(), type_from_eolian(*types[i]));
     }

   // Let's degrade to opaque classes when not enough information
   // is available for complex types
   if(v.parts.size() == 1 && type_is_complex(v.front()))
     {
       efl::eolian::eolian_type tmp = v.front();
       return {efl::eolian::type_to_native(tmp)};
     }

   return v;
}

} // namespace eolian_cxx {

#endif // EOLIAN_CXX_TYPE_LOOKUP_HH