summaryrefslogtreecommitdiff
path: root/src/lib/eolian_cxx/grammar/klass_def.hpp
blob: 9969e89075f976f6cb0351c96c8c7d35dc90d89d (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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
#ifndef EOLIAN_CXX_KLASS_DEF_HH
#define EOLIAN_CXX_KLASS_DEF_HH

#include "grammar/type_traits.hpp"
#include "grammar/attributes.hpp"
#include "grammar/qualifier_def.hpp"
#include "grammar/string.hpp"
#include "grammar/sequence.hpp"
#include "grammar/kleene.hpp"
#include "grammar/case.hpp"

#include <Eolian.h>

#include <Eina.hh>

#include <vector>
#include <memory>
#include <set>
#include <iosfwd>
#include <string>

namespace efl { namespace eolian { namespace grammar {

namespace attributes {

struct complex_type_def;

}

namespace attributes {

template <typename...Args, std::size_t I>
bool lexicographical_compare_impl(std::tuple<Args...> const&
                                  , std::tuple<Args...> const&
                                  , std::integral_constant<std::size_t, I>
                                  , std::true_type)
{
  return true;
}
template <typename...Args, std::size_t I>
bool lexicographical_compare_impl(std::tuple<Args...> const& lhs
                                  , std::tuple<Args...> const& rhs
                                  , std::integral_constant<std::size_t, I>
                                  , std::false_type)
{
  return std::get<I>(lhs) < std::get<I>(rhs)
      || (!(std::get<I>(rhs) < std::get<I>(lhs))
          && lexicographical_compare_impl(lhs, rhs, std::integral_constant<std::size_t, I+1>()
                                          , std::integral_constant<bool, I + 1 == sizeof...(Args)>())
         )
    ;
}
template <typename...Args>
bool lexicographical_compare(std::tuple<Args...> const& lhs
                             , std::tuple<Args...> const& rhs)
{
  return lexicographical_compare_impl(lhs, rhs, std::integral_constant<std::size_t, 0ul>(), std::false_type());
}
template <typename T, typename U>
bool lexicographical_compare(std::tuple<T, U> const& lhs
                             , std::tuple<T, U> const& rhs)
{
  return std::get<0>(lhs) < std::get<0>(rhs)
       || (!(std::get<0>(rhs) < std::get<0>(lhs))
          && std::get<1>(lhs) < std::get<1>(rhs));
}

enum class typedecl_type
{
  unknown,
  struct_,
  struct_opaque,
  enum_,
  alias,
  function_ptr,
};

inline std::ostream& operator<<(std::ostream& s, typedecl_type dec)
{
  switch(dec)
  {
  case typedecl_type::unknown:
    return s << "unknown";
  case typedecl_type::struct_:
    return s << "struct_";
  case typedecl_type::struct_opaque:
    return s << "struct_opaque";
  case typedecl_type::enum_:
    return s << "enum_";
  case typedecl_type::alias:
    return s << "alias";
  case typedecl_type::function_ptr:
    return s << "function_ptr";
  };
  return s;
}

inline typedecl_type typedecl_type_get(Eolian_Typedecl const* decl)
{
  if (!decl)
    return typedecl_type::unknown;

  Eolian_Typedecl_Type t = eolian_typedecl_type_get(decl);
  switch (t)
  {
  case EOLIAN_TYPEDECL_UNKNOWN: return typedecl_type::unknown;
  case EOLIAN_TYPEDECL_STRUCT: return typedecl_type::struct_;
  case EOLIAN_TYPEDECL_STRUCT_OPAQUE: return typedecl_type::struct_opaque;
  case EOLIAN_TYPEDECL_ENUM: return typedecl_type::enum_;
  case EOLIAN_TYPEDECL_ALIAS: return typedecl_type::alias;
  case EOLIAN_TYPEDECL_FUNCTION_POINTER: return typedecl_type::function_ptr;
  default: return typedecl_type::unknown;
  }
}

enum class variable_type
{
  unknown,
  constant,
  global
};


struct type_def;
bool operator==(type_def const& rhs, type_def const& lhs);
bool operator!=(type_def const& rhs, type_def const& lhs);

enum class class_type
{
  regular, abstract_, mixin, interface_
};

inline std::ostream& operator<<(std::ostream& s, class_type t)
{
  switch(t)
  {
  case class_type::regular:
    return s << "regular";
  case class_type::abstract_:
    return s << "abstract_";
  case class_type::mixin:
    return s << "mixin";
  case class_type::interface_:
    return s << "interface_";
  };
  return s;
}

struct klass_name
{
   std::vector<std::string> namespaces;
   std::string eolian_name;
   qualifier_def base_qualifier;
   class_type type;
   std::string klass_get_name;

   friend inline std::ostream& operator<<(std::ostream& s, klass_name const& name)
   {
     s << "[ namespaces: {";
     std::copy(name.namespaces.begin(), name.namespaces.end(), std::ostream_iterator<std::string>(s, ","));
     return s << "}, eolian_name: " << name.eolian_name << " base_qualifier: " << name.base_qualifier
              << " type: " << name.type << " klass_get_name: " << name.klass_get_name << "]";
   }

   klass_name() {
   }

   klass_name(std::vector<std::string> namespaces
              , std::string eolian_name, qualifier_def base_qualifier
              , class_type type, std::string klass_get_name)
     : namespaces(namespaces), eolian_name(eolian_name), base_qualifier(base_qualifier)
     , type(type), klass_get_name(klass_get_name) {}
   klass_name(Eolian_Class const* klass, qualifier_def base_qualifier)
     : eolian_name( ::eolian_class_short_name_get(klass))
              , base_qualifier(base_qualifier)
              , klass_get_name( ::eolian_class_c_get_function_name_get(klass))
   {
     for(efl::eina::iterator<const char> namespace_iterator ( ::eolian_class_namespaces_get(klass))
           , namespace_last; namespace_iterator != namespace_last; ++namespace_iterator)
       {
         namespaces.push_back(&*namespace_iterator);
       }
     switch(eolian_class_type_get(klass))
       {
       case EOLIAN_CLASS_REGULAR:
         type = class_type::regular;
         break;
       case EOLIAN_CLASS_ABSTRACT:
         type = class_type::abstract_;
         break;
       case EOLIAN_CLASS_MIXIN:
         type = class_type::mixin;
         break;
       case EOLIAN_CLASS_INTERFACE:
         type = class_type::interface_;
         break;
       default:
         throw std::runtime_error("Class with unknown type");
       }
   }
};

inline bool operator==(klass_name const& lhs, klass_name const& rhs)
{
  return lhs.namespaces == rhs.namespaces && lhs.eolian_name == rhs.eolian_name
    && lhs.base_qualifier == rhs.base_qualifier/* && lhs.pointers == rhs.pointers*/;
}
inline bool operator!=(klass_name const& lhs, klass_name const& rhs)
{
  return !(lhs == rhs);
}
inline bool operator<(klass_name const& lhs, klass_name const& rhs)
{
  typedef std::tuple<std::vector<std::string>const&
                     , std::string const&
                     , qualifier_def const&
                     , class_type
                     > tuple_type;
  return lexicographical_compare(tuple_type(lhs.namespaces, lhs.eolian_name
                                            , lhs.base_qualifier
                                            , lhs.type)
                                 , tuple_type(rhs.namespaces, rhs.eolian_name
                                              , rhs.base_qualifier
                                              , rhs.type));
}

struct documentation_def
{
   std::string summary;
   std::string description;
   std::string since;
   std::vector<std::string> desc_paragraphs;
   std::string full_text;

   documentation_def() = default;
   documentation_def(std::string summary, std::string description, std::string since, std::string full_text)
     : summary(summary), description(description), since(since), full_text(full_text)
   {}
   documentation_def(Eolian_Documentation const* eolian_doc)
   {
      const char *str;

      if (!eolian_doc)
        return;

      str = eolian_documentation_summary_get(eolian_doc);
      if (str)
        full_text = summary = str;

      str = eolian_documentation_description_get(eolian_doc);
      if (str) {
        description = str;
        full_text += "\n" + description;
      }

      str = eolian_documentation_since_get(eolian_doc);
      if (str) {
        since = str;
        full_text += "\n" + since;
      }

      efl::eina::ptr_list<const char> l(eolian_documentation_string_split(description.c_str()));
      for (auto&& i : l)
        desc_paragraphs.push_back({&i});
   }

   friend inline bool operator==(documentation_def const& lhs, documentation_def const& rhs)
   {
      return lhs.summary == rhs.summary
        && lhs.description == rhs.description
        && lhs.since == rhs.since;
   }
};

template <>
struct tuple_element<0ul, klass_name>
{
  typedef std::vector<std::string> type;
  static type& get(klass_name& klass) { return klass.namespaces; }
  static type const& get(klass_name const& klass) { return klass.namespaces; }
};
template <>
struct tuple_element<1ul, klass_name>
{
  typedef std::string type;
  static type& get(klass_name& klass) { return klass.eolian_name; }
  static type const& get(klass_name const& klass) { return klass.eolian_name; }
};
template <int N>
struct tuple_element<N, klass_name const> : tuple_element<N, klass_name> {};

template <int N>
typename tuple_element<N, klass_name>::type&
get(klass_name& klass)
{
  return tuple_element<N, klass_name>::get(klass);
}
template <int N>
typename tuple_element<N, klass_name>::type const&
get(klass_name const& klass)
{
  return tuple_element<N, klass_name>::get(klass);
}

struct regular_type_def
{
   regular_type_def() : type_type(typedecl_type::unknown), is_undefined(false) {}
   regular_type_def(std::string base_type, qualifier_def qual, std::vector<std::string> namespaces
                    , typedecl_type type_type = typedecl_type::unknown, bool is_undefined = false)
     : base_type(std::move(base_type)), base_qualifier(qual), namespaces(std::move(namespaces))
     , type_type(type_type), is_undefined(is_undefined) {}

   bool is_type(typedecl_type tt) const { return type_type == tt; }
   bool is_unknown() const { return is_type(typedecl_type::unknown); }
   bool is_struct() const { return is_type(typedecl_type::struct_); }
   bool is_struct_opaque() const { return is_type(typedecl_type::struct_opaque); }
   bool is_enum() const { return is_type(typedecl_type::enum_); }
   bool is_alias() const { return is_type(typedecl_type::alias); }
   bool is_function_ptr() const { return is_type(typedecl_type::function_ptr); }

   friend inline std::ostream& operator<<(std::ostream& s, regular_type_def const& def)
   {
     s << "[ base_type: " << def.base_type << " base_qualifier: " << def.base_qualifier
       << " namespaces: ";
     std::copy(def.namespaces.begin(), def.namespaces.end(), std::ostream_iterator<std::string>(s, ", "));
     return s << " type_type: " << def.type_type << " is_undefined " << def.is_undefined << "]";
   }

   std::string base_type;
   qualifier_def base_qualifier;
   std::vector<std::string> namespaces;
   typedecl_type type_type;
   bool is_undefined;
};

inline bool operator==(regular_type_def const& rhs, regular_type_def const& lhs)
{
  return rhs.base_type == lhs.base_type && rhs.base_qualifier == lhs.base_qualifier;
}
inline bool operator!=(regular_type_def const& rhs, regular_type_def const& lhs)
{
  return !(rhs == lhs);
}

struct complex_type_def
{
   regular_type_def outer;
   std::vector<type_def> subtypes;

   friend inline std::ostream& operator<<(std::ostream& s, complex_type_def const& def)
   {
     s << "[ outer " << def.outer << " subtypes: {";
     std::copy(def.subtypes.begin(), def.subtypes.end(), std::ostream_iterator<type_def>(s, ", "));
     return s << "}]";
   }
};

inline bool operator==(complex_type_def const& lhs, complex_type_def const& rhs)
{
  return lhs.outer == rhs.outer && lhs.subtypes == rhs.subtypes;
}
inline bool operator!=(complex_type_def const& lhs, complex_type_def const& rhs)
{
  return !(lhs == rhs);
}

struct type_def
{
   typedef eina::variant<klass_name, regular_type_def, complex_type_def> variant_type;
   variant_type original_type;
   std::string c_type;
   bool has_own;
   bool is_ptr;

   type_def() = default;
   type_def(variant_type original_type, std::string c_type, bool has_own)
     : original_type(original_type), c_type(c_type), has_own(has_own) {}

   type_def(Eolian_Type const* eolian_type, Eolian_Unit const* unit, Eolian_C_Type_Type ctype)
   {
     set(eolian_type, unit, ctype);
   }
   void set(Eolian_Type const* eolian_type, Eolian_Unit const* unit, Eolian_C_Type_Type ctype);
   void set(Eolian_Expression_Type eolian_exp_type);

   friend inline bool operator<(type_def const& lhs, type_def const& rhs)
   {
      return lhs.c_type < rhs.c_type;
   }
   friend inline std::ostream& operator<<(std::ostream& s, type_def const& rhs)
   {
      return s << "[ original: " << rhs.original_type << " c_type "
               << rhs.c_type << " has_own " << rhs.has_own << " is_ptr "
               << rhs.is_ptr << "]";
   }
};

struct get_qualifier_visitor
{
  typedef qualifier_def result_type;
  template <typename T>
  qualifier_def operator()(T const& object) const
  {
     return object.base_qualifier;
  }
  qualifier_def operator()(complex_type_def const& complex) const
  {
    return complex.outer.base_qualifier;
  }
};

inline bool operator==(type_def const& lhs, type_def const& rhs)
{
  return lhs.original_type == rhs.original_type && lhs.c_type == rhs.c_type;
}
inline bool operator!=(type_def const& lhs, type_def const& rhs)
{
  return !(lhs == rhs);
}

type_def const void_ {attributes::regular_type_def{"void", {qualifier_info::is_none, {}}, {}}, "void", false};

inline void type_def::set(Eolian_Type const* eolian_type, Eolian_Unit const* unit, Eolian_C_Type_Type ctype)
{
   c_type = ::eolian_type_c_type_get(eolian_type, ctype);
   // ::eina_stringshare_del(stringshare); // this crashes
   Eolian_Type const* stp = eolian_type_base_type_get(eolian_type);
   has_own = !!::eolian_type_is_owned(eolian_type);
   is_ptr = !!::eolian_type_is_ptr(eolian_type);
   switch( ::eolian_type_type_get(eolian_type))
     {
     case EOLIAN_TYPE_VOID:
       original_type = attributes::regular_type_def{"void", {qualifiers(eolian_type), {}}, {}};
       break;
     case EOLIAN_TYPE_REGULAR:
       if (!stp)
         {
           bool is_undefined = false;
           Eolian_Typedecl const* decl = eolian_type_typedecl_get(eolian_type);
           typedecl_type type_type = (decl ? typedecl_type_get(decl) : typedecl_type::unknown);
           if(decl && eolian_typedecl_type_get(decl) == EOLIAN_TYPEDECL_ALIAS)
             {
               Eolian_Type const* aliased = eolian_typedecl_base_type_get(decl);
               if(aliased && eolian_type_type_get(aliased) == EOLIAN_TYPE_UNDEFINED)
                 {
                   is_undefined = true;
                 }
             }
           if(c_type == "va_list *")
             throw std::runtime_error("");
           std::vector<std::string> namespaces;
           for(efl::eina::iterator<const char> namespace_iterator( ::eolian_type_namespaces_get(eolian_type))
                 , namespace_last; namespace_iterator != namespace_last; ++namespace_iterator)
             namespaces.push_back(&*namespace_iterator);
           original_type = {regular_type_def{ ::eolian_type_short_name_get(eolian_type), {qualifiers(eolian_type), {}}, namespaces, type_type, is_undefined}};
         }
       else
         {
           complex_type_def complex
            {{::eolian_type_short_name_get(eolian_type), {qualifiers(eolian_type), {}}, {}}, {}};
           while (stp)
             {
                complex.subtypes.push_back({stp, unit, EOLIAN_C_TYPE_DEFAULT});
                stp = eolian_type_next_type_get(stp);
             }
           original_type = complex;
         }
       break;
     case EOLIAN_TYPE_CLASS:
       {
          Eolian_Class const* klass = eolian_type_class_get(eolian_type);
          original_type = klass_name(klass, {qualifiers(eolian_type), {}});
       }
       break;
     default:
       throw std::runtime_error("Type not supported");
       break;
     }
}

inline void type_def::set(Eolian_Expression_Type eolian_exp_type)
{
    switch(eolian_exp_type)
      {
      case EOLIAN_EXPR_INT:
        original_type = attributes::regular_type_def{"int", {{}, {}}, {}};
        c_type = "int";
        break;
      default:
        // FIXME implement the remaining types
        EINA_LOG_ERR("Unsupported expression type");
        std::abort();
        break;
      }
}

struct alias_def
{
  std::string eolian_name;
  std::string cxx_name;
  std::vector<std::string> namespaces;
  bool is_undefined;
  type_def base_type {};
  documentation_def documentation;

  alias_def(Eolian_Typedecl const* alias_obj, Eolian_Unit const* unit)
  {
     cxx_name = eolian_name = ::eolian_typedecl_short_name_get(alias_obj);

     for(efl::eina::iterator<const char> namespace_iterator( ::eolian_typedecl_namespaces_get(alias_obj))
          , namespace_last; namespace_iterator != namespace_last; ++namespace_iterator)
       {
          this->namespaces.push_back((&*namespace_iterator));
       }

     Eolian_Type const* bt = ::eolian_typedecl_base_type_get(alias_obj);
     if (eolian_type_type_get(bt) == EOLIAN_TYPE_UNDEFINED)
       is_undefined = true;
     else
       {
          base_type = type_def(::eolian_typedecl_base_type_get(alias_obj), unit, EOLIAN_C_TYPE_DEFAULT);
          is_undefined = false;
       }

     documentation = ::eolian_typedecl_documentation_get(alias_obj);
  }
};

enum class parameter_direction
{
  unknown, in, inout, out
};

namespace detail {
struct add_optional_qualifier_visitor
{
  typedef void result_type;
  template <typename T>
  void operator()(T&  object) const
  {
    object.base_qualifier.qualifier |= qualifier_info::is_optional;
  }
  void operator()(complex_type_def& complex) const
  {
    (*this)(complex.outer);
  }
};
}

struct parameter_def
{
  parameter_direction direction;
  type_def type;
  std::string param_name;
  documentation_def documentation;
  Eolian_Unit const* unit;

  friend inline bool operator==(parameter_def const& lhs, parameter_def const& rhs)
  {
    return lhs.direction == rhs.direction
      && lhs.type == rhs.type
      && lhs.param_name == rhs.param_name
      && lhs.documentation == rhs.documentation;
  }
  friend inline bool operator!=(parameter_def const& lhs, parameter_def const& rhs)
  {
    return !(lhs == rhs);
  }

  parameter_def(parameter_direction direction, type_def type, std::string param_name,
                documentation_def documentation, Eolian_Unit const* unit)
    : direction(std::move(direction)), type(std::move(type)), param_name(std::move(param_name)), documentation(documentation), unit(unit) {}
  parameter_def(Eolian_Function_Parameter const* param, Eolian_Unit const* unit)
    : type( ::eolian_parameter_type_get(param), unit, EOLIAN_C_TYPE_PARAM)
    , param_name( ::eolian_parameter_name_get(param)), unit(unit)
  {
     Eolian_Parameter_Dir direction = ::eolian_parameter_direction_get(param);
     switch(direction)
       {
       case EOLIAN_UNKNOWN_PARAM:
       case EOLIAN_IN_PARAM:
         this->direction = parameter_direction::in;
         break;
       case EOLIAN_INOUT_PARAM:
         this->direction = parameter_direction::inout;
         break;
       case EOLIAN_OUT_PARAM:
         this->direction = parameter_direction::out;
         break;
       }
     if( ::eolian_parameter_is_optional(param))
       type.original_type.visit(detail::add_optional_qualifier_visitor{});

     documentation = eolian_parameter_documentation_get(param);
  }
};

template <>
struct tuple_element<0ul, parameter_def>
{
  typedef parameter_direction type;
  static type const& get(parameter_def const& p) { return p.direction; }
  static type& get(parameter_def& p) { return p.direction; }
};
template <>
struct tuple_element<1ul, parameter_def>
{
  typedef type_def type;
  static type const& get(parameter_def const& p) { return p.type; }
  static type& get(parameter_def& p) { return p.type; }
};
template <>
struct tuple_element<2ul, parameter_def>
{
  typedef std::string type;
  static type const& get(parameter_def const& p) { return p.param_name; }
  static type& get(parameter_def& p) { return p.param_name; }
};
template <>
struct tuple_element<3ul, parameter_def>
{
  typedef std::string type;
  static type const& get(parameter_def const& p) { return p.type.c_type; }
  static type& get(parameter_def& p) { return p.type.c_type; }
};
template <int I>
typename tuple_element<I, parameter_def>::type const& get(parameter_def const& p)
{ return tuple_element<I, parameter_def>::get(p); }
template <int I>
typename tuple_element<I, parameter_def>::type& get(parameter_def& p)
{ return tuple_element<I, parameter_def>::get(p); }

enum class function_type
{
  unresolved,
  property,
  prop_set,
  prop_get,
  method,
  function_pointer
};

struct function_def
{
  klass_name klass; // Klass information for function_def as method
  type_def return_type;
  std::string name;
  std::vector<parameter_def> parameters;
  std::string c_name;
  std::string filename;
  std::vector<std::string> namespaces; // Namespaces for top-level function pointers
  documentation_def documentation;
  documentation_def return_documentation;
  documentation_def property_documentation;
  function_type type;
  bool is_beta;
  bool is_protected;
  bool is_static;
  Eolian_Unit const* unit;

  friend inline bool operator==(function_def const& lhs, function_def const& rhs)
  {
    return lhs.klass == rhs.klass
      && lhs.return_type == rhs.return_type
      && lhs.name == rhs.name
      && lhs.parameters == rhs.parameters
      && lhs.c_name == rhs.c_name
      && lhs.filename == rhs.filename
      && lhs.namespaces == rhs.namespaces
      && lhs.documentation == rhs.documentation
      && lhs.return_documentation == rhs.return_documentation
      && lhs.property_documentation == rhs.property_documentation
      && lhs.type == rhs.type
      && lhs.is_beta == rhs.is_beta
      && lhs.is_protected == rhs.is_protected
      && lhs.is_static == rhs.is_static;
  }
  friend inline bool operator!=(function_def const& lhs, function_def const& rhs)
  {
    return !(lhs == rhs);
  }
  function_def() = default;
  function_def(klass_name _klass,
               type_def _return_type, std::string const& _name,
               std::vector<parameter_def> const& _parameters,
               std::string const& _c_name,
               std::string _filename,
               std::vector<std::string> const& _namespaces,
               documentation_def _documentation,
               documentation_def _return_documentation,
               documentation_def _property_documentation,
               function_type _type,
               bool _is_beta = false,
               bool _is_protected = false,
               Eolian_Unit const* unit = nullptr)
    : klass(_klass), return_type(_return_type), name(_name),
      parameters(_parameters), c_name(_c_name), filename(_filename),
      namespaces(_namespaces),
      documentation(_documentation),
      return_documentation(_return_documentation),
      property_documentation(_property_documentation),
      type(_type),
      is_beta(_is_beta), is_protected(_is_protected),
      unit(unit) {}

  function_def( ::Eolian_Function const* function, Eolian_Function_Type type, Eolian_Typedecl const* tp, Eolian_Unit const* unit)
    : return_type(void_), unit(unit)
  {
    Eolian_Type const* r_type = ::eolian_function_return_type_get(function, type);
    name = ::eolian_function_name_get(function);
    return_documentation = eolian_function_return_documentation_get(function, type);
    if(r_type)
      return_type.set(r_type, unit, EOLIAN_C_TYPE_RETURN);
     if(type == EOLIAN_METHOD || type == EOLIAN_FUNCTION_POINTER)
       {
          for(efl::eina::iterator<Eolian_Function_Parameter> param_iterator ( ::eolian_function_parameters_get(function))
            , param_last; param_iterator != param_last; ++param_iterator)
            {
               parameters.push_back({&*param_iterator, unit});
            }
       }
     else if(type == EOLIAN_PROP_GET || type == EOLIAN_PROP_SET)
       {
         if(type == EOLIAN_PROP_GET)
           name += "_get";
         else
           name += "_set";
         for(efl::eina::iterator<Eolian_Function_Parameter> param_iterator
               ( ::eolian_property_keys_get(function, type))
               , param_last; param_iterator != param_last; ++param_iterator)
           {
              parameters.push_back({&*param_iterator, unit});
           }
         std::vector<parameter_def> values;
         for(efl::eina::iterator<Eolian_Function_Parameter> param_iterator
               ( ::eolian_property_values_get(function, type))
               , param_last; param_iterator != param_last; ++param_iterator)
           {
              values.push_back({&*param_iterator, unit});
           }

         if(!r_type && type == EOLIAN_PROP_GET && values.size() == 1)
           {
             return_type = values[0].type;
             if (return_documentation.summary.empty())
               return_documentation = values[0].documentation;

           }
         else if(type == EOLIAN_PROP_GET)
           {
             for(auto&& v : values)
               {
                 v.direction = parameter_direction::out;
                 parameters.push_back(v);
               }
           }
         else
           parameters.insert(parameters.end(), values.begin(), values.end());
       }
     c_name = eolian_function_full_c_name_get(function, type, EINA_FALSE);
     if (type != EOLIAN_FUNCTION_POINTER)
       {
          const Eolian_Class *eolian_klass = eolian_function_class_get(function);
          filename = eolian_object_file_get((const Eolian_Object *)eolian_klass);
          klass = klass_name(eolian_klass,
              {attributes::qualifier_info::is_none, std::string()});
       }
     else
       {
          filename = "";

          if (tp)
            {
               for (efl::eina::iterator<const char> ns_iterator(::eolian_typedecl_namespaces_get(tp)), ns_last;
                    ns_iterator != ns_last;
                    ns_iterator++)
                 namespaces.push_back(&*ns_iterator);
            }
       }
     is_beta = eolian_function_is_beta(function);
     is_protected = eolian_function_scope_get(function, type) == EOLIAN_SCOPE_PROTECTED;
     is_static = eolian_function_is_class(function);

     Eolian_Implement const* implement = eolian_function_implement_get(function);
     if (!implement)
       return;

     documentation = eolian_implement_documentation_get(implement, type);


     if (type == EOLIAN_PROP_GET || type == EOLIAN_PROP_SET)
       property_documentation = eolian_implement_documentation_get(implement, EOLIAN_PROPERTY);

     switch (type)
       {
       case EOLIAN_UNRESOLVED:
         this->type = function_type::unresolved;
         break;
       case EOLIAN_PROPERTY:
         this->type = function_type::property;
         break;
       case EOLIAN_PROP_GET:
         this->type = function_type::prop_get;
         break;
       case EOLIAN_PROP_SET:
         this->type = function_type::prop_set;
         break;
       case EOLIAN_METHOD:
         this->type = function_type::method;
         break;
       case EOLIAN_FUNCTION_POINTER:
         this->type = function_type::function_pointer;
         break;
       }
  }

  std::string template_statement() const
  {
     std::string statement;
     char template_typename = 'F';
     for (auto const& param : this->parameters)
       {
          attributes::regular_type_def const* typ =
                efl::eina::get<attributes::regular_type_def>(&param.type.original_type);
          if (typ && typ->is_function_ptr())
            {
               char typenam[2] = { 0, };
               typenam[0] = template_typename++;
               if (statement.empty())
                 statement = std::string("template <typename ") + typenam;
               else
                 statement += std::string(", typename ") + typenam;
            }
       }
     if (statement.empty()) return statement;
     else return statement + ">";
  }

  std::vector<std::string> opening_statements() const
  {
     std::vector<std::string> statements;
     char template_typename = 'F';
     for (auto const& param : this->parameters)
       {
          attributes::regular_type_def const* typ =
                efl::eina::get<attributes::regular_type_def>(&param.type.original_type);
          if (typ && typ->is_function_ptr())
            {
               char typenam[2] = { 0, };
               typenam[0] = template_typename++;
               std::string statement = "auto fw_" + param.param_name + " = new ::efl::eolian::function_wrapper<";
               statement += param.type.c_type + ", " + typenam + ">(" + param.param_name + ");";
               statements.push_back(statement);
            }
       }
     return statements;
  }
};

template <>
struct tuple_element<0ul, function_def>
{
   typedef type_def type;
   static type& get(function_def& f) { return f.return_type; }
   static type const& get(function_def const& f) { return f.return_type; }
};

template <>
struct tuple_element<1ul, function_def>
{
   typedef std::string type;
   static type& get(function_def& f) { return f.name; }
   static type const& get(function_def const& f) { return f.name; }
};

template <>
struct tuple_element<2ul, function_def>
{
   typedef std::vector<parameter_def> type;
   static type& get(function_def& f) { return f.parameters; }
   static type const& get(function_def const& f) { return f.parameters; }
};

struct property_def
{
  klass_name klass;
  std::string name;
  documentation_def documentation;

  efl::eina::optional<function_def> getter;
  efl::eina::optional<function_def> setter;

  friend inline bool operator==(property_def const& lhs, property_def const& rhs)
  {
    return lhs.klass == rhs.klass
      && lhs.name == rhs.name
      && lhs.documentation == rhs.documentation
      && lhs.getter == rhs.getter
      && lhs.setter == rhs.setter;
  }

  friend inline bool operator!=(property_def const& lhs, property_def const& rhs)
  {
    return !(lhs == rhs);
  }

  property_def() = default;
  property_def(Eolian_Function const *function, efl::eina::optional<function_def> getter
              , efl::eina::optional<function_def> setter, Eolian_Unit const*)
              : getter(getter), setter(setter)
  {
    name = ::eolian_function_name_get(function);

    const Eolian_Class *eolian_klass = eolian_function_class_get(function);
    klass = klass_name(eolian_klass, {attributes::qualifier_info::is_none, std::string()});

    Eolian_Implement const* implement = ::eolian_function_implement_get(function);
    if (!implement)
      return;

    Eolian_Function_Type type = ::eolian_function_type_get(function);
    if (type == EOLIAN_PROP_GET || type == EOLIAN_PROP_SET || type == EOLIAN_PROPERTY)
      {
         documentation = eolian_implement_documentation_get(implement, EOLIAN_PROPERTY);
         // If property-level documentation is empty, use the getter- or setter-level
         // docs as fallback (if present).
         if (documentation.summary.empty())
           documentation = eolian_implement_documentation_get(implement, EOLIAN_PROP_GET);
         if (documentation.summary.empty())
           documentation = eolian_implement_documentation_get(implement, EOLIAN_PROP_SET);
      }
  }
};

struct variable_def
{
  std::string name;
  std::string full_name;
  type_def base_type;
  documentation_def documentation;
  variable_type type;
  std::vector<std::string> namespaces;
  Eolian_Value expression_value;
  bool is_extern : 1;

  friend inline bool operator==(variable_def const& lhs, variable_def const& rhs)
  {
    return lhs.name == rhs.name
      && lhs.full_name == rhs.full_name
      && lhs.base_type == rhs.base_type
      && lhs.documentation == rhs.documentation
      && lhs.type == rhs.type
      && lhs.namespaces == rhs.namespaces
      && lhs.expression_value.type == rhs.expression_value.type
      && lhs.expression_value.value.ll == rhs.expression_value.value.ll
      && lhs.is_extern == rhs.is_extern;
  }

  friend inline bool operator!=(variable_def const& lhs, variable_def const& rhs)
  {
    return !(lhs == rhs);
  }

  variable_def() = default;
  variable_def(Eolian_Variable const* variable, Eolian_Unit const* unit)
        : name(::eolian_variable_short_name_get(variable))
        , full_name(::eolian_variable_name_get(variable))
        , base_type(::eolian_variable_base_type_get(variable), unit, ::EOLIAN_C_TYPE_DEFAULT)
        , documentation(::eolian_variable_documentation_get(variable))
        , type(static_cast<variable_type>(::eolian_variable_type_get(variable)))
        , expression_value()
        , is_extern(::eolian_variable_is_extern(variable))
  {
     for(efl::eina::iterator<const char> namespace_iterator( ::eolian_variable_namespaces_get(variable))
          , namespace_last; namespace_iterator != namespace_last; ++namespace_iterator)
       {
          this->namespaces.push_back((&*namespace_iterator));
       }

     if (this->type == variable_type::constant)
       {
          auto expr = ::eolian_variable_value_get(variable);
          if (!expr)
            throw std::runtime_error("Could not get constant variable value expression");

          this->expression_value = ::eolian_expression_eval_type(expr, ::eolian_variable_base_type_get(variable));
       }
  }
};


// template <int N>
// struct tuple_element<N, function_def const> : tuple_element<N, function_def> {};
// template <int N>
// struct tuple_element<N, function_def&> : tuple_element<N, function_def> {};
// template <int N>
// struct tuple_element<N, function_def const&> : tuple_element<N, function_def> {};

// template <std::size_t I>
// typename tuple_element<I, function_def>::type const&
// get(function_def const& f)
// {
//   return tuple_element<I, function_def>::get(f);
// }

// template <std::size_t I>
// typename tuple_element<I, function_def>::type&
// get(function_def& f)
// {
//   return tuple_element<I, function_def>::get(f);
// }

struct compare_klass_name_by_name
{
  bool operator()(klass_name const& lhs, klass_name const& rhs) const
  {
    return lhs.namespaces < rhs.namespaces
        || (!(rhs.namespaces < lhs.namespaces) && lhs.eolian_name < rhs.eolian_name);
  }
};

struct event_def
{
  klass_name klass;
  eina::optional<type_def> type;
  std::string name, c_name;
  bool beta, protect;
  documentation_def documentation;

  friend inline bool operator==(event_def const& lhs, event_def const& rhs)
  {
    return lhs.klass == rhs.klass
      && lhs.type == rhs.type
      && lhs.name == rhs.name
      && lhs.c_name == rhs.c_name
      && lhs.beta == rhs.beta
      && lhs.protect == rhs.protect
      && lhs.documentation == rhs.documentation;
  }
  friend inline bool operator!=(event_def const& lhs, event_def const& rhs)
  {
    return !(lhs == rhs);
  }

  event_def(klass_name _klass, type_def type, std::string name, std::string c_name,
          bool beta, bool protect, documentation_def documentation)
    : klass(_klass), type(type), name(name), c_name(c_name), beta(beta), protect(protect)
    , documentation(documentation) {}

  event_def(Eolian_Event const* event, Eolian_Class const* cls, Eolian_Unit const* unit)
    : klass(cls, {attributes::qualifier_info::is_none, std::string()})
    , type( ::eolian_event_type_get(event) ? eina::optional<type_def>{{::eolian_event_type_get(event), unit, EOLIAN_C_TYPE_DEFAULT}} : eina::optional<type_def>{})
    , name( ::eolian_event_name_get(event))
    , c_name( ::eolian_event_c_name_get(event))
    , beta( ::eolian_event_is_beta(event))
    , protect( ::eolian_event_scope_get(event) == EOLIAN_SCOPE_PROTECTED)
    , documentation( ::eolian_event_documentation_get(event)) {}
};

template <>
struct tuple_element<0, event_def>
{
  typedef eina::optional<type_def> type;
  static type& get(event_def& def) { return def.type; }
  static type const& get(event_def const& def) { return def.type; }
};
template <>
struct tuple_element<1, event_def>
{
  typedef std::string type;
  static type& get(event_def& def) { return def.name; }
  static type const& get(event_def const& def) { return def.name; }
};
template <>
struct tuple_element<2, event_def>
{
  typedef std::string type;
  static type& get(event_def& def) { return def.c_name; }
  static type const& get(event_def const& def) { return def.c_name; }
};
template <int N>
struct tuple_element<N, event_def const> : tuple_element<N, event_def> {};
template <int N>
auto get(event_def const& def) -> decltype(tuple_element<N, event_def>::get(def))
{
  return tuple_element<N, event_def>::get(def);
}
template <int N>
auto get(event_def& def) -> decltype(tuple_element<N, event_def>::get(def))
{
  return tuple_element<N, event_def>::get(def);
}

struct part_def
{
   klass_name klass;
   std::string name;
   documentation_def documentation;
   //bool beta, protect; // can it be applied??

   friend inline bool operator==(part_def const& lhs, part_def const& rhs)
   {
     return lhs.klass == rhs.klass
       && lhs.name == rhs.name;
   }
   friend inline bool operator!=(part_def const& lhs, part_def const& rhs)
   {
     return !(lhs == rhs);
   }
   friend inline bool operator<(part_def const& lhs, part_def const& rhs)
   {
      return lhs.name < rhs.name ||
            lhs.klass < rhs.klass;
   }

   part_def(Eolian_Part const* part, Eolian_Unit const*)
      : klass(klass_name(::eolian_part_class_get(part), {attributes::qualifier_info::is_none, std::string()}))
      , name(::eolian_part_name_get(part))
      , documentation(::eolian_part_documentation_get(part)) {}
};

struct constructor_def
{
    std::string name;
    klass_name klass;
    function_def function;
    bool is_optional;
    bool is_ctor_param;

    friend inline bool operator==(constructor_def const& lhs, constructor_def const& rhs)
    {
      return lhs.name == rhs.name
        && lhs.klass == rhs.klass
        && lhs.function == rhs.function
        && lhs.is_optional == rhs.is_optional
        && lhs.is_ctor_param == rhs.is_ctor_param;
    }

    friend inline bool operator!=(constructor_def const& lhs, constructor_def const& rhs)
    {
      return !(lhs == rhs);
    }

    constructor_def(Eolian_Constructor const* constructor, Eolian_Unit const* unit)
        : name(::eolian_constructor_name_get(constructor))
        , klass(::eolian_constructor_class_get(constructor), {})
        , is_optional(::eolian_constructor_is_optional(constructor))
        , is_ctor_param(::eolian_constructor_is_ctor_param(constructor))
    {
         Eolian_Function const* eo_function = ::eolian_constructor_function_get(constructor);
         Eolian_Function_Type eo_func_type = ::eolian_function_type_get(eo_function);
         if (eo_func_type == ::EOLIAN_PROPERTY)
           eo_func_type = ::EOLIAN_PROP_SET;
         function = function_def(eo_function, eo_func_type, NULL, unit);
    }
};

inline Eolian_Class const* get_klass(klass_name const& klass_name_, Eolian_Unit const* unit);

struct klass_def
{
  std::string eolian_name;
  std::string cxx_name;
  std::string filename;
  documentation_def documentation;
  std::vector<std::string> namespaces;
  std::vector<function_def> functions;
  std::vector<property_def> properties;
  std::vector<constructor_def> constructors;
  std::set<klass_name, compare_klass_name_by_name> inherits;
  class_type type;
  std::vector<event_def> events;
  std::set<klass_name, compare_klass_name_by_name> immediate_inherits;
  eina::optional<klass_name> parent;
  std::set<klass_name, compare_klass_name_by_name> extensions;
  std::string klass_get_name;

  std::set<part_def> parts;
  Eolian_Unit const* unit;

  friend inline bool operator==(klass_def const& lhs, klass_def const& rhs)
  {
    return lhs.eolian_name == rhs.eolian_name
      && lhs.cxx_name == rhs.cxx_name
      && lhs.filename == lhs.filename
      && lhs.namespaces == rhs.namespaces
      && lhs.functions == rhs.functions
      && lhs.properties == rhs.properties
      && lhs.inherits == rhs.inherits
      && lhs.type == rhs.type
      && lhs.events == rhs.events
      && lhs.parts == rhs.parts
      && lhs.klass_get_name == rhs.klass_get_name;
  }
  friend inline bool operator!=(klass_def const& lhs, klass_def const& rhs)
  {
    return !(lhs == rhs);
  }
  friend inline bool operator<(klass_def const& lhs, klass_def const& rhs)
  {
     return lhs.eolian_name < rhs.eolian_name
       || lhs.cxx_name < rhs.cxx_name
       || lhs.namespaces < rhs.namespaces
       || lhs.parts < rhs.parts;
  }

  klass_def(std::string eolian_name, std::string cxx_name, std::string filename
            , documentation_def documentation
            , std::vector<std::string> namespaces
            , std::vector<function_def> functions
            , std::vector<property_def> properties
            , std::set<klass_name, compare_klass_name_by_name> inherits
            , class_type type
            , std::set<klass_name, compare_klass_name_by_name> immediate_inherits
            , std::string klass_get_name)
    : eolian_name(eolian_name), cxx_name(cxx_name), filename(filename)
    , documentation(documentation)
    , namespaces(namespaces)
    , functions(functions), properties(properties), inherits(inherits), type(type)
    , immediate_inherits(immediate_inherits)
    , klass_get_name(klass_get_name)
  {}
  klass_def(std::string _eolian_name, std::string _cxx_name
            , std::vector<std::string> _namespaces
            , std::vector<function_def> _functions
            , std::vector<property_def> _properties
            , std::set<klass_name, compare_klass_name_by_name> _inherits
            , class_type _type, Eolian_Unit const* unit
            , std::string klass_get_name)
    : eolian_name(_eolian_name), cxx_name(_cxx_name)
    , namespaces(_namespaces)
    , functions(_functions), properties(_properties), inherits(_inherits), type(_type)
    , klass_get_name(klass_get_name), unit(unit)
  {}
  klass_def(Eolian_Class const* klass, Eolian_Unit const* unit)
    : klass_get_name( ::eolian_class_c_get_function_name_get(klass))
    , unit(unit)
  {
     for(efl::eina::iterator<const char> namespace_iterator( ::eolian_class_namespaces_get(klass))
           , namespace_last; namespace_iterator != namespace_last; ++namespace_iterator)
       {
          this->namespaces.push_back(&*namespace_iterator);
       }
     cxx_name = eolian_name = eolian_class_short_name_get(klass);
     filename = eolian_object_file_get((const Eolian_Object *)klass);
     for(efl::eina::iterator<Eolian_Function const> eolian_functions ( ::eolian_class_functions_get(klass, EOLIAN_PROPERTY))
       , functions_last; eolian_functions != functions_last; ++eolian_functions)
       {
         Eolian_Function const* function = &*eolian_functions;
         Eolian_Function_Type func_type = ::eolian_function_type_get(function);
         if(func_type == EOLIAN_PROPERTY)
           {
             efl::eina::optional<function_def> getter(nullptr);
             efl::eina::optional<function_def> setter(nullptr);
             try {
                if(! ::eolian_function_is_legacy_only(function, EOLIAN_PROP_GET)
                   && ::eolian_function_scope_get(function, EOLIAN_PROP_GET) != EOLIAN_SCOPE_PRIVATE)
                  {
                     function_def f(function, EOLIAN_PROP_GET, NULL, unit);
                     functions.push_back(f);
                     getter = efl::eina::optional<function_def>(f);
                  }
             } catch(std::exception const&) {}
             try {
                if(! ::eolian_function_is_legacy_only(function, EOLIAN_PROP_SET)
                   && ::eolian_function_scope_get(function, EOLIAN_PROP_SET) != EOLIAN_SCOPE_PRIVATE)
                  {
                     function_def f(function, EOLIAN_PROP_SET, NULL, unit);
                     functions.push_back(f);
                     setter = efl::eina::optional<function_def>(f);
                  }
             } catch(std::exception const&) {}
             if (getter.is_engaged() || setter.is_engaged())
               properties.push_back({function, getter, setter, unit});
           }
         else
           try {
             if(! ::eolian_function_is_legacy_only(function, func_type)
                && ::eolian_function_scope_get(function, func_type) != EOLIAN_SCOPE_PRIVATE)
               {
                  efl::eina::optional<function_def> getter(nullptr);
                  efl::eina::optional<function_def> setter(nullptr);
                  function_def f(function, func_type, NULL, unit);
                  if (func_type == EOLIAN_PROP_GET)
                    getter = efl::eina::optional<function_def>(f);
                  else if (func_type == EOLIAN_PROP_SET)
                    setter = efl::eina::optional<function_def>(f);
                  functions.push_back(f);
                  if (func_type == EOLIAN_PROP_GET || func_type == EOLIAN_PROP_SET)
                    properties.push_back({function, getter, setter, unit});
               }
           } catch(std::exception const&) {}
       }
     for(efl::eina::iterator<Eolian_Function const> eolian_functions ( ::eolian_class_functions_get(klass, EOLIAN_METHOD))
       , functions_last; eolian_functions != functions_last; ++eolian_functions)
       {
         try {
             Eolian_Function const* function = &*eolian_functions;
             Eolian_Function_Type func_type = eolian_function_type_get(function);
             if(! ::eolian_function_is_legacy_only(function, EOLIAN_METHOD)
                && ::eolian_function_scope_get(function, func_type) != EOLIAN_SCOPE_PRIVATE)
               functions.push_back({function, EOLIAN_METHOD, NULL, unit});
         } catch(std::exception const&) {}
       }
     if(::eolian_class_parent_get(klass))
       {
          parent = eina::optional<klass_name>({::eolian_class_parent_get(klass), {}});
          immediate_inherits.insert(*parent);
       }
     for(efl::eina::iterator<Eolian_Class const> inherit_iterator ( ::eolian_class_extensions_get(klass))
           , inherit_last; inherit_iterator != inherit_last; ++inherit_iterator)
       {
         Eolian_Class const* inherit = &*inherit_iterator;
         klass_name extension(inherit, {});
         immediate_inherits.insert(extension);
         extensions.insert(extension);
       }
     std::function<void(Eolian_Class const*)> inherit_algo =
       [&] (Eolian_Class const* inherit_klass)
       {
         if(::eolian_class_parent_get(inherit_klass))
           {
             Eolian_Class const* inherit = ::eolian_class_parent_get(inherit_klass);
             inherits.insert({inherit, {}});
             inherit_algo(inherit);
           }
         for(efl::eina::iterator<Eolian_Class const> inherit_iterator ( ::eolian_class_extensions_get(inherit_klass))
               , inherit_last; inherit_iterator != inherit_last; ++inherit_iterator)
           {
             Eolian_Class const* inherit = &*inherit_iterator;
             inherits.insert({inherit, {}});
             inherit_algo(inherit);
           }
       };
     inherit_algo(klass);

     for(efl::eina::iterator<Eolian_Part const> parts_itr ( ::eolian_class_parts_get(klass))
       , parts_last; parts_itr != parts_last; ++parts_itr)
       {
          parts.insert({&*parts_itr, unit});
       }

     switch(eolian_class_type_get(klass))
       {
       case EOLIAN_CLASS_REGULAR:
         type = class_type::regular;
         break;
       case EOLIAN_CLASS_ABSTRACT:
         type = class_type::abstract_;
         break;
       case EOLIAN_CLASS_MIXIN:
         type = class_type::mixin;
         break;
       case EOLIAN_CLASS_INTERFACE:
         type = class_type::interface_;
         break;
       default:
         throw std::runtime_error("Class with unknown type");
       }
     for(efl::eina::iterator<Eolian_Event const> event_iterator( ::eolian_class_events_get(klass))
           , event_last; event_iterator != event_last; ++event_iterator)
       {
         try {
           events.push_back({&*event_iterator, klass, unit});
         } catch(std::exception const&) {}
       }

     for(efl::eina::iterator<Eolian_Constructor const> constructor_iterator(::eolian_class_constructors_get(klass))
             , constructor_last; constructor_iterator != constructor_last; ++constructor_iterator)
         constructors.push_back({&*constructor_iterator, unit});

     documentation = eolian_class_documentation_get(klass);
  }

  // TODO memoize the return?
  std::vector<function_def> get_all_methods() const
  {
      std::vector<function_def> ret;

      std::copy(functions.cbegin(), functions.cend(), std::back_inserter(ret));

      for (auto inherit : inherits)
       {
          klass_def klass(get_klass(inherit, unit), unit);
          std::copy(klass.functions.cbegin(), klass.functions.cend(),
                    std::back_inserter(ret));
       }

      return ret;
  }

  std::vector<part_def> get_all_parts() const
  {
      std::vector<part_def> ret;

      std::copy(parts.cbegin(), parts.cend(), std::back_inserter(ret));

      for (auto inherit : inherits)
        {
           klass_def klass(get_klass(inherit, unit), unit);
           std::copy(klass.parts.cbegin(), klass.parts.cend(),
                     std::back_inserter(ret));
        }

      return ret;
  }

  std::vector<event_def> get_all_events() const
  {
      std::vector<event_def> ret;

      std::copy(events.cbegin(), events.cend(), std::back_inserter(ret));

      for (auto inherit : inherits)
        {
           klass_def klass(get_klass(inherit, unit), unit);
           std::copy(klass.events.cbegin(), klass.events.cend(),
                     std::back_inserter(ret));
        }

      return ret;
  }

  std::vector<constructor_def> get_all_constructors() const
  {
      std::vector<constructor_def> ret;

      std::copy(constructors.cbegin(), constructors.cend(), std::back_inserter(ret));

      for (auto inherit : inherits)
        {
           klass_def klass(get_klass(inherit, unit), unit);
           std::copy(klass.constructors.cbegin(), klass.constructors.cend(),
                     std::back_inserter(ret));
        }

      return ret;
  }
};

struct value_def
{
  typedef eina::variant<int> variant_type; // FIXME support other types
  variant_type value;
  std::string literal;
  type_def type;

  value_def() = default;
  value_def(Eolian_Value value_obj)
  {
    type.set(value_obj.type);
    value = value_obj.value.i;
    literal = eolian_expression_value_to_literal(&value_obj);
  }
};

struct enum_value_def
{
  value_def value;
  std::string name;
  std::string c_name;
  documentation_def documentation;

  enum_value_def(Eolian_Enum_Type_Field const* enum_field, Eolian_Unit const*)
  {
      name = eolian_typedecl_enum_field_name_get(enum_field);
      c_name = eolian_typedecl_enum_field_c_name_get(enum_field);
      auto exp = eolian_typedecl_enum_field_value_get(enum_field, EINA_TRUE);
      value = eolian_expression_eval(exp, EOLIAN_MASK_INT); // FIXME hardcoded int
      documentation = eolian_typedecl_enum_field_documentation_get(enum_field);
  }
};

struct enum_def
{
  std::string eolian_name;
  std::string cxx_name;
  std::vector<std::string> namespaces;
  std::vector<enum_value_def> fields;
  documentation_def documentation;

  enum_def(Eolian_Typedecl const* enum_obj, Eolian_Unit const* unit)
  {
     for(efl::eina::iterator<const char> namespace_iterator( ::eolian_typedecl_namespaces_get(enum_obj))
           , namespace_last; namespace_iterator != namespace_last; ++namespace_iterator)
       {
          this->namespaces.push_back((&*namespace_iterator));
       }
     cxx_name = eolian_name = eolian_typedecl_short_name_get(enum_obj);

     for (efl::eina::iterator<const Eolian_Enum_Type_Field> field_iterator(::eolian_typedecl_enum_fields_get(enum_obj))
             , field_last; field_iterator != field_last; ++field_iterator)
       {
          // Fill the types
          enum_value_def field_def(&*field_iterator, unit);
          this->fields.push_back(field_def);
       }

     documentation = ::eolian_typedecl_documentation_get(enum_obj);
  }
};

struct struct_field_def
{
  type_def type;
  std::string name;
  documentation_def documentation;

  struct_field_def(Eolian_Struct_Type_Field const* struct_field, Eolian_Unit const* unit)
  {
     name = eolian_typedecl_struct_field_name_get(struct_field);
     try {
        type.set(eolian_typedecl_struct_field_type_get(struct_field), unit, EOLIAN_C_TYPE_DEFAULT);
     } catch(std::runtime_error const&) { /* Silently skip pointer fields*/ }
     documentation = ::eolian_typedecl_struct_field_documentation_get(struct_field);
  }

};

struct struct_def
{
  std::string eolian_name;
  std::string cxx_name;
  std::vector<std::string> namespaces;
  std::vector<struct_field_def> fields;
  documentation_def documentation;

  struct_def(Eolian_Typedecl const* struct_obj, Eolian_Unit const* unit)
  {
     for(efl::eina::iterator<const char> namespace_iterator( ::eolian_typedecl_namespaces_get(struct_obj))
           , namespace_last; namespace_iterator != namespace_last; ++namespace_iterator)
       {
          this->namespaces.push_back((&*namespace_iterator));
       }
     cxx_name = eolian_name = eolian_typedecl_short_name_get(struct_obj);

     for(efl::eina::iterator<const Eolian_Struct_Type_Field> field_iterator(::eolian_typedecl_struct_fields_get(struct_obj))
             , field_last; field_iterator != field_last; ++field_iterator)
       {
          struct_field_def field_def(&*field_iterator, unit);
          this->fields.push_back(field_def);
       }

     documentation = ::eolian_typedecl_documentation_get(struct_obj);
  }
};

inline klass_name get_klass_name(klass_def const& klass)
{
  return {klass.namespaces, klass.eolian_name, {qualifier_info::is_none, {}}, klass.type, klass.klass_get_name};
}

inline Eolian_Class const* get_klass(klass_name const& klass_name_, Eolian_Unit const* unit)
{
  std::string klass_name;
  if(!as_generator(*(string << ".") << string)
     .generate(std::back_insert_iterator<std::string>(klass_name)
               , std::make_tuple(klass_name_.namespaces, klass_name_.eolian_name)
               , context_null{}))
    return nullptr;
  else
    return ::eolian_unit_class_by_name_get(unit, klass_name.c_str());
}

inline std::vector<std::string> cpp_namespaces(std::vector<std::string> namespaces)
{
  if(namespaces.empty())
    namespaces.push_back("nonamespace");
  return namespaces;
}

inline bool has_events(klass_def const &klass)
{
    for (auto&& e : klass.events)
      {
         (void)e;
         return true;
      }

    for (auto&& c : klass.inherits)
      {
        attributes::klass_def parent(get_klass(c, klass.unit), klass.unit);
         for (auto&& e : parent.events)
           {
              (void)e;
              return true;
           }
      }

    return false;
}

template<typename T>
inline bool has_type_return(klass_def const &klass, T visitor)
{
    for (auto&& f : klass.functions)
      {
         if (f.return_type.original_type.visit(visitor))
           return true;
      }

    for (auto&& c : klass.inherits)
      {
        attributes::klass_def parent(get_klass(c, klass.unit), klass.unit);
         if (has_type_return(parent, visitor))
           return true;
      }

    return false;
}

struct string_return_visitor
{
    typedef string_return_visitor visitor_type;
    typedef bool result_type;
    template <typename T>
    bool operator()(T const&) const { return false; }
    bool operator()(regular_type_def const& regular) const
    {
        return regular.base_type == "string";
    }
};

struct stringshare_return_visitor
{
    typedef stringshare_return_visitor visitor_type;
    typedef bool result_type;
    template <typename T>
    bool operator()(T const&) const { return false; }
    bool operator()(regular_type_def const& regular) const
    {
        return regular.base_type == "stringshare";
    }
};

inline bool has_string_return(klass_def const &klass)
{
    return has_type_return(klass, string_return_visitor{});
}

inline bool has_stringshare_return(klass_def const &klass)
{
    return has_type_return(klass, stringshare_return_visitor{});
}

}
namespace type_traits {

template <>
struct is_tuple<attributes::parameter_def> : std::true_type {};
template <>
struct is_tuple<attributes::event_def> : std::true_type {};
  
}

} } }

#endif