aboutsummaryrefslogtreecommitdiffstats
path: root/src/scripts/elua/apps/gendoc.lua
blob: 36c47e2af3e4a2da8837c2a15570931f9651c553 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
local getopt = require("getopt")

local serializer = require("serializer")

local stats = require("docgen.stats")
local dutil = require("docgen.util")
local writer = require("docgen.writer")
local keyref = require("docgen.keyref")
local dtree = require("docgen.doctree")
local mono = require("docgen.mono")

local printgen = function() end

local propt_to_type = {
    [dtree.Function.PROPERTY] = "(get, set)",
    [dtree.Function.PROP_GET] = "(get)",
    [dtree.Function.PROP_SET] = "(set)",
}

local gen_cparam = function(par, out)
    local part = par:type_get()
    out = out or (par:direction_get() == par.OUT)
    local tstr = part:c_type_get()
    if out then
        tstr = dtree.type_cstr_get(tstr, "*")
    end
    return dtree.type_cstr_get(tstr, par:name_get())
end

local get_func_csig_part = function(cn, tp)
    if not tp then
        return "void " .. cn
    end
    return dtree.type_cstr_get(tp, cn)
end

local gen_func_csig = function(f, ftype)
    ftype = ftype or f.METHOD
    assert(ftype ~= f.PROPERTY)

    local cn = f:full_c_name_get(ftype)
    local rtype = f:return_type_get(ftype)

    local fparam = "Eo *obj"
    if f:is_class() then
        fparam = "Efl_Class *klass"
    elseif f:is_const() or ftype == f.PROP_GET then
        fparam = "const Eo *obj"
    end

    if f:type_get() == f.METHOD then
        local pars = f:parameters_get()
        local cnrt = get_func_csig_part(cn, rtype)
        for i = 1, #pars do
            pars[i] = gen_cparam(pars[i])
        end
        table.insert(pars, 1, fparam);
        return cnrt .. "(" .. table.concat(pars, ", ") .. ");"
    end

    local keys = f:property_keys_get(ftype)
    local vals = f:property_values_get(ftype)

    if ftype == f.PROP_SET then
        local cnrt = get_func_csig_part(cn, rtype)
        local pars = {}
        for i, par in ipairs(keys) do
            pars[#pars + 1] = gen_cparam(par)
        end
        for i, par in ipairs(vals) do
            pars[#pars + 1] = gen_cparam(par)
        end
        table.insert(pars, 1, fparam);
        return cnrt .. "(" .. table.concat(pars, ", ") .. ");"
    end

    -- getters
    local cnrt
    if not rtype then
        if #vals == 1 then
            cnrt = get_func_csig_part(cn, vals[1]:type_get())
            table.remove(vals, 1)
        else
            cnrt = get_func_csig_part(cn)
        end
    else
        cnrt = get_func_csig_part(cn, rtype)
    end
    local pars = {}
    for i, par in ipairs(keys) do
        pars[#pars + 1] = gen_cparam(par)
    end
    for i, par in ipairs(vals) do
        pars[#pars + 1] = gen_cparam(par, true)
    end
    table.insert(pars, 1, fparam);
    return cnrt .. "(" .. table.concat(pars, ", ") .. ");"
end

local gen_func_namesig = function(fn, cl, buf, isprop, isget, isset)
    if isprop then
        buf[#buf + 1] = "@property "
    end
    buf[#buf + 1] = fn:name_get()
    buf[#buf + 1] = " "
    if fn:is_beta() then
        buf[#buf + 1] = "@beta "
    end
    if not isprop then
        if fn:scope_get(fn.METHOD) == fn.scope.PROTECTED then
            buf[#buf + 1] = "@protected "
        end
    elseif isget and isset then
        if fn:scope_get(fn.PROP_GET) == fn.scope.PROTECTED and
           fn:scope_get(fn.PROP_SET) == fn.scope.PROTECTED then
            buf[#buf + 1] = "@protected "
        end
    end
    if fn:is_class() then
        buf[#buf + 1] = "@class "
    end
    if fn:is_const() then
        buf[#buf + 1] = "@const "
    end
end

local gen_func_param = function(fp, buf, nodir)
    -- TODO: default value
    buf[#buf + 1] = "        "
    local dirs = {
        [dtree.Parameter.IN] = "@in ",
        [dtree.Parameter.OUT] = "@out ",
        [dtree.Parameter.INOUT] = "@inout ",
    }
    if not nodir then buf[#buf + 1] = dirs[fp:direction_get()] end
    buf[#buf + 1] = fp:name_get()
    buf[#buf + 1] = ": "
    buf[#buf + 1] = fp:type_get():serialize()
    local dval = fp:default_value_get()
    if dval then
        buf[#buf + 1] = " ("
        buf[#buf + 1] = dval:serialize()
        buf[#buf + 1] = ")"
    end
    if fp:is_nonull() then
        buf[#buf + 1] = " @nonull"
    end
    if fp:is_nullable() then
        buf[#buf + 1] = " @nullable"
    end
    if fp:is_optional() then
        buf[#buf + 1] = " @optional"
    end
    buf[#buf + 1] = ";\n"
end

local gen_func_return = function(fp, ftype, buf, indent)
    local rett = fp:return_type_get(ftype)
    if not rett then
        return
    end
    buf[#buf + 1] = indent and ("    "):rep(indent) or "    "
    buf[#buf + 1] = "return: "
    buf[#buf + 1] = rett:serialize()
    local dval = fp:return_default_value_get(ftype)
    if dval then
        buf[#buf + 1] = " ("
        buf[#buf + 1] = dval:serialize()
        buf[#buf + 1] = ")"
    end
    if fp:return_is_warn_unused(ftype) then
        buf[#buf + 1] = " @warn_unused"
    end
    buf[#buf + 1] = ";\n"
end

local gen_method_sig = function(fn, cl)
    local buf = {}
    gen_func_namesig(fn, cl, buf, false, false, false)

    local fimp = fn:implement_get()

    if fimp:is_pure_virtual(fn.METHOD) then
        buf[#buf + 1] = "@pure_virtual "
    end
    buf[#buf + 1] = "{"
    local params = fn:parameters_get()
    local rtp = fn:return_type_get(fn.METHOD)
    if #params == 0 and not rtp then
        buf[#buf + 1] = "}"
        return table.concat(buf)
    end
    buf[#buf + 1] = "\n"
    if #params > 0 then
        buf[#buf + 1] = "    params {\n"
        for i, fp in ipairs(params) do
            gen_func_param(fp, buf)
        end
        buf[#buf + 1] = "    }\n"
    end
    gen_func_return(fn, fn.METHOD, buf)
    buf[#buf + 1] = "}"
    return table.concat(buf)
end

local eovals_check_same = function(a1, a2)
    if #a1 ~= #a2 then return false end
    for i, v in ipairs(a1) do
        if v ~= a2[i] then return false end
    end
    return true
end

local gen_prop_keyvals = function(tbl, kword, buf, indent)
    local ind = indent and ("    "):rep(indent) or "    "
    if #tbl == 0 then return end
    buf[#buf + 1] = "    "
    buf[#buf + 1] = ind
    buf[#buf + 1] = kword
    buf[#buf + 1] = " {\n"
    for i, v in ipairs(tbl) do
        buf[#buf + 1] = ind
        gen_func_param(v, buf, true)
    end
    buf[#buf + 1] = "    "
    buf[#buf + 1] = ind
    buf[#buf + 1] = "}\n"
end

local gen_prop_sig = function(fn, cl)
    local buf = {}
    local fnt = fn:type_get()
    local isget = (fnt == fn.PROPERTY or fnt == fn.PROP_GET)
    local isset = (fnt == fn.PROPERTY or fnt == fn.PROP_SET)
    gen_func_namesig(fn, cl, buf, true, isget, isset)

    local pimp = fn:implement_get()

    local gvirt = pimp:is_pure_virtual(fn.PROP_GET)
    local svirt = pimp:is_pure_virtual(fn.PROP_SET)

    if (not isget or gvirt) and (not isset or svirt) then
        buf[#buf + 1] = "@pure_virtual "
    end

    local gkeys = isget and fn:property_keys_get(fn.PROP_GET) or {}
    local skeys = isset and fn:property_keys_get(fn.PROP_SET) or {}
    local gvals = isget and fn:property_values_get(fn.PROP_GET) or {}
    local svals = isget and fn:property_values_get(fn.PROP_SET) or {}
    local grtt = isget and fn:return_type_get(fn.PROP_GET) or nil
    local srtt = isset and fn:return_type_get(fn.PROP_SET) or nil

    local keys_same = eovals_check_same(gkeys, skeys)
    local vals_same = eovals_check_same(gvals, svals)

    buf[#buf + 1] = "{\n"

    if isget then
        buf[#buf + 1] = "    get "
        if fn:scope_get(fn.PROP_GET) == fn.scope.PROTECTED and
           fn:scope_get(fn.PROP_SET) ~= fn.scope.PROTECTED then
            buf[#buf + 1] = "@protected "
        end
        buf[#buf + 1] = "{"
        if (#gkeys == 0 or keys_same) and (#gvals == 0 or vals_same) and
           (not grtt or grtt == srtt) then
            buf[#buf + 1] = "}\n"
        else
            buf[#buf + 1] = "\n"
            if not keys_same then gen_prop_keyvals(gkeys, "keys", buf) end
            if not vals_same then gen_prop_keyvals(gvals, "values", buf) end
            if grtt ~= srtt then
                gen_func_return(fn, fn.PROP_GET, buf, 2)
            end
            buf[#buf + 1] = "    }\n"
        end
    end

    if isset then
        buf[#buf + 1] = "    set "
        if fn:scope_get(fn.PROP_SET) == fn.scope.PROTECTED and
           fn:scope_get(fn.PROP_GET) ~= fn.scope.PROTECTED then
            buf[#buf + 1] = "@protected "
        end
        buf[#buf + 1] = "{"
        if (#skeys == 0 or keys_same) and (#svals == 0 or vals_same) and
           (not srtt or grtt == srtt) then
            buf[#buf + 1] = "}\n"
        else
            buf[#buf + 1] = "\n"
            if not keys_same then gen_prop_keyvals(skeys, "keys", buf) end
            if not vals_same then gen_prop_keyvals(svals, "values", buf) end
            if grtt ~= srtt then
                gen_func_return(fn, fn.PROP_SET, buf, 2)
            end
            buf[#buf + 1] = "    }\n"
        end
    end

    if keys_same then gen_prop_keyvals(gkeys, "keys", buf, 0) end
    if vals_same then gen_prop_keyvals(gvals, "values", buf, 0) end

    buf[#buf + 1] = "}"
    return table.concat(buf)
end

-- builders

local nspaces_group = function(ns)
    if #ns <= 2 then
        return ns[1]
    end

    if ns[1] == "efl" and (ns[2] == "class" or ns[2] == "interface" or
                           ns[2] == "object" or ns[2] == "promise") then
        return ns[1]
    end

    return ns[1] .. "." .. ns[2]
end

local nspaces_filter = function(items, ns)
    local out = {}

    for _, item in ipairs(items) do
        local group = nspaces_group(item:nspaces_get())
        if group == ns then out[#out + 1] = item end
    end

    return out
end

local build_method, build_property, build_event

local build_reftable = function(f, title, ctype, t, iscl)
    if not t or #t == 0 then
        return
    end

    local nt = {}
    for i, v in ipairs(t) do
        nt[#nt + 1] = {
            writer.Buffer():write_link(
                iscl and v:nspaces_get(true)
                      or dtree.Node.nspaces_get(v, true),
                v:name_get()
            ):finish(),
            v:doc_get():brief_get()
        }
    end
    table.sort(nt, function(v1, v2) return v1[1] < v2[1] end)
    f:write_table({ title, "Brief description" }, nt)
end

local build_ref_group = function(f, ns, classlist, aliases, structs, enums, consts, globals)
    local classes = {}
    local ifaces = {}
    local mixins = {}

    for i, cl in ipairs(classlist) do
        local tp = cl:type_get()
        if tp == dtree.Class.REGULAR or tp == dtree.Class.ABSTRACT then
            classes[#classes + 1] = cl
        elseif tp == dtree.Class.MIXIN then
            mixins[#mixins + 1] = cl
        elseif tp == dtree.Class.INTERFACE then
            ifaces[#ifaces + 1] = cl
        end
    end

    local title = ns:gsub("(%l)(%w*)", function(a,b) return a:upper()..b end) --string.sub(ns, 1, 1):upper() .. string.sub(ns, 2):lower()
    f:write_h(title, 2)

    build_reftable(f, "Classes", "class", classes, true)
    build_reftable(f, "Interfaces", "interface", ifaces, true)
    build_reftable(f, "Mixins", "mixin", mixins, true)

    build_reftable(f, "Aliases", "alias", aliases)
    build_reftable(f, "Structures", "struct", structs)
    build_reftable(f, "Enums", "enum", enums)
    build_reftable(f, "Constants", "constant", consts)
    build_reftable(f, "Globals", "global", globals)

    f:write_nl()
end

local build_ref = function()
    local f = writer.Writer("start", "EFL Reference")
    printgen("Generating reference...")

    f:write_editable({ "reference" }, "general")
    f:write_nl()

    local classlist = dtree.Class.all_get()
    local aliases = dtree.Typedecl.all_aliases_get()
    local structs = dtree.Typedecl.all_structs_get()
    local enums = dtree.Typedecl.all_enums_get()
    local consts = dtree.Variable.all_constants_get()
    local globals = dtree.Variable.all_globals_get()

    grouped = {}
    groups = {}
    for i, cl in ipairs(classlist) do
        local ns = cl:nspaces_get()
        local name = nspaces_group(cl:nspaces_get())

        local group = grouped[name]
        if not group then
            group = {}
            grouped[name] = group
            groups[#groups + 1] = name
        end

        group[#group + 1] = cl
    end
    table.sort(groups)
 
    for _, ns in ipairs(groups) do
        build_ref_group(f, ns, grouped[ns], nspaces_filter(aliases, ns), nspaces_filter(structs, ns),
                        nspaces_filter(enums, ns), nspaces_filter(consts, ns), nspaces_filter(globals, ns))
    end

    f:finish()
end

local build_inherits
build_inherits = function(cl, t, lvl)
    t = t or {}
    lvl = lvl or 0
    local lbuf = writer.Buffer()
    if lvl > 0 then
        lbuf:write_link(cl:nspaces_get(true), cl:name_get())
        lbuf:write_raw(" ")
        lbuf:write_i("(" .. cl:type_str_get() .. ")")
 
        t[#t + 1] = { lvl - 1, lbuf:finish() }
    end

    for i, acl in ipairs(cl:inherits_get()) do
        build_inherits(acl, t, lvl + 1)
    end
    return t
end

local build_inherit_summary
build_inherit_summary = function(cl, buf)
    buf = buf or writer.Buffer()
    buf:write_raw(" => ")

    buf:write_link(cl:nspaces_get(true), cl:name_get())
    buf:write_raw(" ")
    buf:write_i("(" .. cl:type_str_get() .. ")")

    local inherits = cl:inherits_get()
    if #inherits ~= 0 then
        build_inherit_summary(inherits[1], buf)
    end
    return buf
end

local default_theme_light = {
    classes = {
        regular = {
            style = "filled",
            color = "black",
            fill_color = "white",
            font_color = "black",
            primary_color = "black",
            primary_fill_color = "gray",
            primary_font_color = "black"
        },
        abstract = {
            style = "filled",
            color = "black",
            fill_color = "white",
            font_color = "black",
            primary_color = "black",
            primary_fill_color = "gray",
            primary_font_color = "black"
        },
        mixin = {
            style = "filled",
            color = "blue",
            fill_color = "white",
            font_color = "black",
            primary_color = "blue",
            primary_fill_color = "skyblue",
            primary_font_color = "black"
        },
        interface = {
            style = "filled",
            color = "cornflowerblue",
            fill_color = "white",
            font_color = "black",
            primary_color = "cornflowerblue",
            primary_fill_color = "azure",
            primary_font_color = "black"
        }
    },
    node = {
        shape = "box"
    },
    edge = {
        color = "black"
    },
    bg_color = "transparent",
    rank_dir = "TB",
    size = "6"
}

local default_theme_dark = {
    classes = {
        regular = {
            style = "filled",
            color = "gray15",
            fill_color = "gray15",
            font_color = "white",
            primary_color = "gray15",
            primary_fill_color = "black",
            primary_font_color = "white"
        },
        abstract = {
            style = "filled",
            color = "gray15",
            fill_color = "gray15",
            font_color = "white",
            primary_color = "gray15",
            primary_fill_color = "black",
            primary_font_color = "white"
        },
        mixin = {
            style = "filled",
            color = "deepskyblue",
            fill_color = "gray15",
            font_color = "white",
            primary_color = "deepskyblue",
            primary_fill_color = "deepskyblue4",
            primary_font_color = "white"
        },
        interface = {
            style = "filled",
            color = "cornflowerblue",
            fill_color = "gray15",
            font_color = "white",
            primary_color = "cornflowerblue",
            primary_fill_color = "dodgerblue4",
            primary_font_color = "white"
        }
    },
    node = {
        shape = "box"
    },
    edge = {
        color = "gray35"
    },
    bg_color = "transparent",
    rank_dir = "TB",
    size = "6"
}

local current_theme = default_theme_dark

local validate_ctheme = function(tb, name)
    if type(tb.classes[name]) ~= "table" then
        return false
    end
    local t = tb.classes[name]
    if type(t.style) ~= "string" then
        return false
    end
    if type(t.color) ~= "string" then
        return false
    end
    if type(t.fill_color) ~= "string" then
        return false
    end
    if type(t.font_color) ~= "string" then
        return false
    end
    if not t.primary_color then
        t.primary_color = t.color
    end
    if not t.primary_fill_color then
        t.primary_fill_color = t.fill_color
    end
    if not t.primary_font_color then
        t.primary_font_color = t.font_color
    end
    if type(t.primary_color) ~= "string" then
        return false
    end
    if type(t.primary_fill_color) ~= "string" then
        return false
    end
    if type(t.primary_font_color) ~= "string" then
        return false
    end
    return true
end

local validate_theme = function(tb)
    if type(tb) ~= "table" then
        return false
    end
    if type(tb.classes) ~= "table" then
        return false
    end
    if not tb.node then
        tb.node = current_theme.node
    end
    if not tb.edge then
        tb.edge = current_theme.edge
    end
    if not tb.bg_color then
        tb.bg_color = current_theme.bg_color
    end
    if not tb.rank_dir then
        tb.rank_dir = current_theme.rank_dir
    end
    if not tb.size then
        tb.size = current_theme.size
    end
    if type(tb.node) ~= "table" then
        return false
    end
    if type(tb.edge) ~= "table" then
        return false
    end
    if type(tb.bg_color) ~= "string" then
        return false
    end
    if type(tb.rank_dir) ~= "string" then
        return false
    end
    if type(tb.size) ~= "string" then
        return false
    end
    if not validate_ctheme(tb, "regular") then
        return false
    end
    if not validate_ctheme(tb, "abstract") then
        return false
    end
    if not validate_ctheme(tb, "mixin") then
        return false
    end
    if not validate_ctheme(tb, "interface") then
        return false
    end
    return true
end

local set_theme = function(tname)
    local tf = io.open(tname)
    if tf then
        local cont = tf:read("*all")
        tf:close()
        local tb, err = serializer.deserialize(cont)
        if not tb then
            error("error parsing theme '" .. tname .. "': " .. err)
        end
        if not validate_theme(tb) then
            error("invalid theme '" .. tname .. "'")
        end
        current_theme = tb
    else
        error("theme '" .. tname .. "' does not exist")
    end
end

local find_parent_impl
find_parent_impl = function(fulln, cl)
    for i, pcl in ipairs(cl:inherits_get()) do
        for j, impl in ipairs(pcl:implements_get()) do
            if impl:name_get() == fulln then
                return impl, pcl
            end
        end
        local pimpl, pcl = find_parent_impl(fulln, pcl)
        if pimpl then
            return pimpl, pcl
        end
    end
    return nil, cl
end

local find_parent_briefdoc
find_parent_briefdoc = function(fulln, cl)
    local pimpl, pcl = find_parent_impl(fulln, cl)
    if not pimpl then
        return dtree.Doc():brief_get()
    end
    local pdoc = pimpl:doc_get(dtree.Function.METHOD, true)
    local pdocf = pimpl:fallback_doc_get(true)
    if not pdoc:exists() and (not pdocf or not pdocf:exists()) then
        return find_parent_briefdoc(fulln, pcl)
    end
    return pdoc:brief_get(pdocf)
end

local build_functable = function(f, tcl, tbl)
    if #tbl == 0 then
        return
    end
    local nt = {}
    for i, implt in ipairs(tbl) do
        local lbuf = writer.Buffer()

        local cl, impl = unpack(implt)
        local func = impl:function_get()

        local wt = {}
        wt[0] = cl
        wt[1] = func
        wt[2] = impl

        nt[#nt + 1] = wt
    end

    local get_best_scope = function(f)
        local ft = f:type_get()
        if ft == f.PROPERTY then
            local fs1, fs2 = f:scope_get(f.PROP_GET), f:scope_get(f.PROP_SET)
            if fs1 == f.scope.PUBLIC or fs2 == f.scope.PUBLIC then
                return f.scope.PUBLIC
            elseif fs1 == f.scope.PROTECTED or fs2 == f.scope.PROTECTED then
                return f.scope.PROTECTED
            else
                return f.scope.PRIVATE
            end
        else
            return f:scope_get(ft)
        end
    end
    table.sort(nt, function(v1, v2)
        local cl1, cl2 = v1[0], v2[0]
        if cl1 ~= cl2 then
            return cl1:name_get() < cl2:name_get()
        end

        local f1, f2 = v1[1], v2[1]
        local f1s, f2s = get_best_scope(f1), get_best_scope(f2)
        if f1s ~= f2s then
            if f1s ~= f1.scope.PROTECED then
                -- public funcs go first, private funcs go last
                return f1s == f1.scope.PUBLIC
            else
                -- protected funcs go second
                return f2s == f2.scope.PRIVATE
            end
        end
        return f1:name_get() < f2:name_get()
    end)

    return nt
end

local write_description = function(f, impl, func, cl)
    local over = impl:is_overridden(cl)
    local bdoc

    local doc = impl:doc_get(func.METHOD, true)
    local docf = impl:fallback_doc_get(true)
    if over and (not doc:exists() and (not docf or not docf:exists())) then
        bdoc = find_parent_briefdoc(impl:name_get(), cl)
    else
        bdoc = doc:brief_get(docf)
    end
    if bdoc ~= "No description supplied." then
        f:write_raw(bdoc)
    end
end

local write_function = function(f, func, cl)
    local llbuf = writer.Buffer()
    llbuf:write_link(func:nspaces_get(cl, true), func:name_get())
    f:write_b(llbuf:finish())

    local pt = propt_to_type[func:type_get()]
    if pt then
        f:write_raw(" ")
        local llbuf = writer.Buffer()
        llbuf:write_b(pt)
        f:write_i(llbuf:finish())
    end
end

local write_scope = function(f, func)
    local ftt = {
        [func.scope.PROTECTED] = "protected",
        [func.scope.PRIVATE] = "private"
    }
    if func:is_class() then
        f:write_raw(" ")
        f:write_m("class")
    end
    if func:type_get() == func.PROPERTY then
        local ft1, ft2 = ftt[func:scope_get(func.PROP_GET)],
                         ftt[func:scope_get(func.PROP_SET)]
        if ft1 and ft1 == ft2 then
            f:write_raw(" ")
            f:write_m(ft1)
        elseif ft1 or ft2 then
            local s = ""
            if ft1 then
                s = s .. ft1 .. " get" .. (ft2 and ", " or "")
            end
            if ft2 then
                s = s .. ft2 .. " set"
            end
            f:write_raw(" ")
            f:write_m(s)
        end
    else
        local ft = ftt[func:scope_get(func:type_get())]
        if ft then
            f:write_raw(" ")
            f:write_m(ft)
        end
    end
end

local write_functable = function(f, tcl, tbl)
    if #tbl == 0 then
        return
    end
    local nt = build_functable(t, tcl, tbl)

    local wrote = false
    for i, wt in ipairs(nt) do
        local cl = wt[0]
        local func = wt[1]
        local impl = wt[2]

        local ocl = impl:class_get()
        local func = impl:function_get()
        local over = impl:is_overridden(cl)

        -- function
        write_function(f, func, cl)
        -- scope
        write_scope(f, func)

        -- overrides
        if over then
            -- TODO: possibly also mention which part of a property was
            -- overridden and where, get/set override point might differ!
            -- but we get latest doc every time so it's ok for now
            local llbuf = writer.Buffer()
            llbuf:write_raw(" [Overridden from ")
            llbuf:write_link(ocl:nspaces_get(true), ocl:name_get())
            llbuf:write_raw("]")
            f:write_i(llbuf:finish())
        end

        -- description
        f:write_br(true)
        f:write_raw("> ")
        write_description(f, impl, func, cl)
 
        -- code snippets
        f:write_nl()
        local codes = {}
        if func:type_get() ~= dtree.Function.PROPERTY then
            codes[#codes + 1] = gen_func_csig(func, func:type_get())
        else
            codes[#codes + 1] = gen_func_csig(func, dtree.Function.PROP_GET)
            codes[#codes + 1] = gen_func_csig(func, dtree.Function.PROP_SET)
        end
        f:write_code(table.concat(codes, "\n"), "c")
        f:write_br(true)

        if cl == tcl then
            if impl:is_prop_get() or impl:is_prop_set() then
                build_property(impl, cl)
            else
                build_method(impl, cl)
            end
        end
    end
    f:write_nl()
end

local write_inherit_functable = function(f, tcl, tbl)
    if #tbl == 0 then
        return
    end
    local nt = build_functable(t, tcl, tbl)

    local prevcl = tcl
    for i, wt in ipairs(nt) do
        local cl = wt[0]
        local func = wt[1]
        local impl = wt[2]

        local ocl = impl:class_get()
        local func = impl:function_get()

	-- class grouping for inheritance
        if cl ~= prevcl then
            prevcl = cl
            f:write_raw("^ ")
            f:write_link(cl:nspaces_get(true), cl:name_get())
            f:write_raw(" ^^^")
            f:write_nl()
        end

        -- scope
        f:write_raw("| ")
        write_scope(f, func)
        f:write_raw(" | ")
        -- function
        write_function(f, func, cl)
        f:write_raw(" | ")
        -- description
	write_description(f, impl, func, cl)
        f:write_raw(" |")
        f:write_nl()
    end
    f:write_nl()
end

-- finds all stuff that is callable on a class, respecting
-- overrides and not duplicating, does a depth-first search
local find_callables
find_callables = function(cl, omeths, events, written)
    for i, pcl in ipairs(cl:inherits_get()) do
        for j, impl in ipairs(pcl:implements_get()) do
            local func = impl:function_get()
            local fid = func:id_get()
            if not written[fid] then
                omeths[#omeths + 1] = { pcl, impl }
                written[fid] = true
            end
        end
        for i, ev in ipairs(pcl:events_get()) do
            local evid = ev:name_get()
            if not written[evid] then
                events[#events + 1] = { pcl, ev }
                written[evid] = true
            end
        end
        find_callables(pcl, omeths, events, written)
    end
end

local build_evcsig = function(ev)
    local csbuf = { ev:c_name_get(), "(" }
    csbuf[#csbuf + 1] = dtree.type_cstr_get(ev:type_get())
    if ev:is_beta() then
        csbuf[#csbuf + 1] = ", @beta"
    end
    if ev:is_hot() then
        csbuf[#csbuf + 1] = ", @hot"
    end
    if ev:is_restart() then
        csbuf[#csbuf + 1] = ", @restart"
    end
    csbuf[#csbuf + 1] = ")";
    return table.concat(csbuf)
end

local build_evtable = function(f, tcl, tbl, newm)
    if #tbl == 0 then
        return
    end
    local nt = {}
    for i, evt in ipairs(tbl) do
        local lbuf = writer.Buffer()
        local evn
        local cl, ev
        if not newm then
            cl, ev = evt[1], evt[2]
        else
            cl, ev = tcl, evt
        end

        local wt = {}
        wt[0] = cl
        wt[1] = ev
        wt[2] = ev:name_get()

	nt[#nt + 1] = wt
    end

    table.sort(nt, function(v1, v2)
        if v1[0] ~= v2[0] then
            return v1[0]:name_get() < v2[0]:name_get()
        end

        return v1[2] < v2[2]
    end)

    return nt
end

local write_event_scope = function(f, ev)
    local ett = {
        [ev.scope.PROTECTED] = "protected",
        [ev.scope.PRIVATE] = "private"
    }
    local ets = ett[ev:scope_get()]
    if ets then
        f:write_raw(" ")
        f:write_m(ets)
    end
end

local write_evtable = function(f, tcl, tbl)
    if #tbl == 0 then
        return
    end
    local nt = build_evtable(f, tcl, tbl, true)
    for i, wt in ipairs(nt) do
        local evn
        local cl, ev = wt[0], wt[1]

        local llbuf = writer.Buffer()
        llbuf:write_link(ev:nspaces_get(cl, true), wt[2])
        f:write_b(llbuf:finish())

        -- scope
        write_event_scope(f, ev)
 
        -- description
        local bdoc = ev:doc_get():brief_get()
        if bdoc ~= "No description supplied." then
            f:write_br(true)
            f:write_raw("> ")
            f:write_raw(bdoc)
        end

        f:write_nl()
        f:write_code(build_evcsig(ev), "c");
        f:write_br()

        if cl == tcl then
            build_event(ev, cl)
        end
    end
end


local write_inherit_evtable = function(f, tcl, tbl)
    if #tbl == 0 then
        return
    end
    local nt = build_evtable(f, tcl, tbl, false)
    local prevcl
    for i, wt in ipairs(nt) do
        local evn
        local cl, ev = wt[0], wt[1]

        if cl ~= prevcl then
            prevcl = cl
            f:write_raw("^ ")
            f:write_link(cl:nspaces_get(true), cl:name_get())
            f:write_raw(" ^^^")
            f:write_nl()
        end
 
        f:write_raw("| ")
        -- scope
        write_event_scope(f, ev)
        f:write_raw(" | ")

        local llbuf = writer.Buffer()
        llbuf:write_link(ev:nspaces_get(cl, true), wt[2])
        f:write_b(llbuf:finish())

        f:write_raw(" | ")
	local bdoc = ev:doc_get():brief_get()
        if bdoc ~= "No description supplied." then
            f:write_raw(bdoc)
        end

        f:write_raw(" |")
        f:write_nl()
    end
end

local build_class = function(cl)
    local cln = cl:nspaces_get()
    local fulln = cl:name_get()
    local f = writer.Writer(cln, fulln)
    printgen("Generating class: " .. fulln)

    mono.build_class(cl)

    f:write_h(cl:name_get() .. " (" .. cl:type_str_get() .. ")", 1)
 
    f:write_h("Description", 2)
    f:write_raw(cl:doc_get():full_get(nil, true))
    f:write_nl(2)

    f:write_editable(cln, "description")
    f:write_nl()

    local inherits = cl:inherits_get()
    if #inherits ~= 0 then
        f:write_h("Inheritance", 2)

        f:write_raw(build_inherit_summary(inherits[1]):finish())
	f:write_nl()

        f:write_folded("Full hierarchy", function()
            f:write_list(build_inherits(cl))
        end)
        f:write_nl()
    end

    local written = {}
    local ievs = {}
    local meths, omeths = {}, {}
    for i, impl in ipairs(cl:implements_get()) do
        local func = impl:function_get()
        written[func:id_get()] = true
        meths[#meths + 1] = { cl, impl }
    end
    find_callables(cl, omeths, ievs, written)

    f:write_h("Members", 2)
    write_functable(f, cl, meths, true)
    if #omeths ~= 0 then
        f:write_h("Inherited", 3)
    end
    write_inherit_functable(f, cl, omeths, false)

    f:write_h("Events", 2)
    write_evtable(f, cl, cl:events_get(), true)
    if #ievs ~= 0 then
        f:write_h("Inherited", 3)
    end
    write_inherit_evtable(f, cl, ievs, false)

    f:finish()
end

local build_classes = function()
    for i, cl in ipairs(dtree.Class.all_get()) do
        build_class(cl)
    end
end

local write_tsigs = function(f, tp, ns)
    f:write_h("Signature", 2)
    f:write_code(tp:serialize())
    f:write_nl()

    f:write_h("C signature", 2)
    f:write_code(tp:serialize_c(ns), "c")
    f:write_nl()
end

local build_alias = function(tp)
    local ns = dtree.Node.nspaces_get(tp)
    local fulln = tp:name_get()
    local f = writer.Writer(ns, fulln)
    printgen("Generating alias: " .. fulln)

    f:write_h("Description", 2)
    f:write_raw(tp:doc_get():full_get(nil, true))
    f:write_nl(2)

    f:write_editable(ns, "description")
    f:write_nl()

    write_tsigs(f, tp, ns)

    f:finish()
end

local build_struct = function(tp)
    local ns = dtree.Node.nspaces_get(tp)
    local fulln = tp:name_get()
    local f = writer.Writer(ns, fulln)
    printgen("Generating struct: " .. fulln)

    f:write_h("Description", 2)
    f:write_raw(tp:doc_get():full_get(nil, true))
    f:write_nl(2)

    f:write_editable(ns, "description")
    f:write_nl()

    f:write_h("Fields", 2)

    f:write_editable(ns, "fields")
    f:write_nl()

    local arr = {}
    for i, fl in ipairs(tp:struct_fields_get()) do
        local buf = writer.Buffer()
        buf:write_b(fl:name_get())
        buf:write_raw(" - ", fl:doc_get():full_get())
        arr[#arr + 1] = buf:finish()
    end
    f:write_list(arr)
    f:write_nl()

    write_tsigs(f, tp, ns)

    f:finish()
end

local build_enum = function(tp)
    local ns = dtree.Node.nspaces_get(tp)
    local fulln = tp:name_get()
    local f = writer.Writer(ns, fulln)
    printgen("Generating enum: " .. fulln)

    f:write_h("Description", 2)
    f:write_raw(tp:doc_get():full_get(nil, true))
    f:write_nl(2)

    f:write_editable(ns, "description")
    f:write_nl()

    f:write_h("Fields", 2)

    f:write_editable(ns, "fields")
    f:write_nl()

    local arr = {}
    for i, fl in ipairs(tp:enum_fields_get()) do
        local buf = writer.Buffer()
        buf:write_b(fl:name_get())
        buf:write_raw(" - ", fl:doc_get():full_get())
        arr[#arr + 1] = buf:finish()
    end
    f:write_list(arr)
    f:write_nl()

    write_tsigs(f, tp, ns)

    f:finish()
end

local build_variable = function(v, constant)
    local ns = v:nspaces_get()
    local fulln = v:name_get()
    local f = writer.Writer(ns, fulln)
    printgen("Generating variable: " .. fulln)

    f:write_h("Description", 2)
    f:write_raw(v:doc_get():full_get(nil, true))
    f:write_nl(2)

    f:write_editable(ns, "description")
    f:write_nl()

    write_tsigs(f, v, ns)

    f:finish()
end

local build_typedecls = function()
    for i, tp in ipairs(dtree.Typedecl.all_aliases_get()) do
        build_alias(tp)
    end

    for i, tp in ipairs(dtree.Typedecl.all_structs_get()) do
        build_struct(tp)
    end

    for i, tp in ipairs(dtree.Typedecl.all_enums_get()) do
        build_enum(tp)
    end
end

local build_variables = function()
    for i, v in ipairs(dtree.Variable.all_constants_get()) do
        build_variable(v, true)
    end

    for i, v in ipairs(dtree.Variable.all_globals_get()) do
        build_variable(v, false)
    end
end

local build_parlist = function(f, pl, nodir)
    local params = {}
    for i, p in ipairs(pl) do
        local buf = writer.Buffer()
        buf:write_b(p:name_get())
        if not nodir then
            buf:write_raw(" ")
            buf:write_i("(", p:direction_name_get(), ")")
        end
        buf:write_raw(" - ", p:doc_get():full_get())
        params[#params + 1] = buf:finish()
    end
    f:write_list(params)
end

local build_vallist = function(f, pg, ps, title)
    if #pg == #ps then
        local same = true
        for i = 1, #pg do
            if not pg[i]:is_same(ps[i]) then
                same = false
                break
            end
        end
        if same then ps = {} end
    end
    if #pg > 0 or #ps > 0 then
        f:write_h(title, 2)
        if #pg > 0 then
            if #ps > 0 then
                f:write_h("Getter", 3)
            end
            build_parlist(f, pg, true)
        end
        if #ps > 0 then
            if #pg > 0 then
                f:write_h("Setter", 3)
            end
            build_parlist(f, ps, true)
        end
    end
end

local find_parent_doc
find_parent_doc = function(fulln, cl, ftype)
    local pimpl, pcl = find_parent_impl(fulln, cl)
    if not pimpl then
        return dtree.Doc()
    end
    local pdoc = pimpl:doc_get(ftype)
    if not pdoc:exists() then
        return find_parent_doc(fulln, pcl, ftype)
    end
    return pdoc
end

local write_inherited_from = function(f, impl, cl, over, prop)
    if not over then
        return
    end
    local buf = writer.Buffer()
    buf:write_raw("Overridden from ")
    local pimpl, pcl = find_parent_impl(impl:name_get(), cl)
    buf:write_link(
        impl:function_get():nspaces_get(pcl, true), impl:name_get()
    )
    if prop then
        buf:write_raw(" ")
        local lbuf = writer.Buffer()
        lbuf:write_raw("(")
        if impl:is_prop_get() then
            lbuf:write_raw("get")
            if impl:is_prop_set() then
                lbuf:write_raw(", ")
            end
        end
        if impl:is_prop_set() then
            lbuf:write_raw("set")
        end
        lbuf:write_raw(")")
        buf:write_b(lbuf:finish())
    end
    buf:write_raw(".")
    f:write_i(buf:finish())
end

local impls_of = {}

local get_all_impls_of
get_all_impls_of = function(tbl, cl, fn, got)
    local cfn = cl:name_get()
    if got[cfn] then
        return
    end
    got[cfn] = true
    for i, imp in ipairs(cl:implements_get()) do
        local ofn = imp:function_get()
        if ofn:is_same(fn) then
            tbl[#tbl + 1] = cl
            break
        end
    end
    for i, icl in ipairs(cl:children_get()) do
        get_all_impls_of(tbl, icl, fn, got)
    end
end

local write_ilist = function(f, impl, cl)
    local fn = impl:function_get()
    local fnn = fn:name_get()
    local ocl = fn:implement_get():class_get()
    local onm = ocl:name_get() .. "." .. fnn
    local imps = impls_of[onm]
    if not imps then
        imps = {}
        impls_of[onm] = imps
        get_all_impls_of(imps, ocl, fn, {})
    end

    f:write_h("Implemented by", 2)
    local t = {}
    for i, icl in ipairs(imps) do
        local buf = writer.Buffer()
        local cfn = icl:name_get() .. "." .. fnn
        if icl:is_same(cl) then
            buf:write_b(cfn)
        else
            buf:write_link(fn:nspaces_get(icl, true), cfn)
        end
        t[#t + 1] = buf:finish()
    end
    f:write_list(t)
end

build_method = function(impl, cl)
    local over = impl:is_overridden(cl)
    local fn = impl:function_get()
    local mns = fn:nspaces_get(cl)
    local methn = cl:name_get() .. "." .. fn:name_get()
    local f = writer.Writer(mns, methn)
    printgen("Generating method: " .. methn)

    local doc = impl:doc_get(fn.METHOD)
    if over and not doc:exists() then
        doc = find_parent_doc(impl:name_get(), cl, fn.METHOD)
    end

    f:write_h("Description", 2)
    f:write_raw(doc:full_get(nil, true))
    f:write_nl()

    f:write_editable(mns, "description")
    f:write_nl()

    write_inherited_from(f, impl, cl, over, false)

    f:write_h("Signature", 2)
    f:write_code(gen_method_sig(fn, cl))
    f:write_nl()

    f:write_h("C signature", 2)
    f:write_code(gen_func_csig(fn, nil), "c")
    f:write_nl()

    local pars = fn:parameters_get()
    if #pars > 0 then
        f:write_h("Parameters", 2)
        build_parlist(f, pars)
        f:write_nl()
    end

    write_ilist(f, impl, cl)
    f:write_nl()

    f:finish()
end

build_property = function(impl, cl)
    local over = impl:is_overridden(cl)
    local fn = impl:function_get()
    local pns = fn:nspaces_get(cl)
    local propn = cl:name_get() .. "." .. fn:name_get()
    local f = writer.Writer(pns, propn)
    printgen("Generating property: " .. propn)

    local pimp = fn:implement_get()

    local isget = pimp:is_prop_get()
    local isset = pimp:is_prop_set()

    local doc = impl:doc_get(fn.PROPERTY)
    local gdoc = impl:doc_get(fn.PROP_GET)
    local sdoc = impl:doc_get(fn.PROP_SET)

    if over then
        if not doc:exists() then
            doc = find_parent_doc(impl:name_get(), cl, fn.PROPERTY)
        end
        if isget and not gdoc:exists() then
            gdoc = find_parent_doc(impl:name_get(), cl, fn.PROP_GET)
        end
        if isset and not sdoc:exists() then
            sdoc = find_parent_doc(impl:name_get(), cl, fn.PROP_SET)
        end
    end

    if isget and isset then
        f:write_h("Description", 2)
        if doc:exists() or (not gdoc:exists() and not sdoc:exists()) then
            f:write_raw(doc:full_get(nil, true))
        end
        if (isget and gdoc:exists()) or (isset and sdoc:exists()) then
            f:write_nl(2)
        end
        f:write_nl()
        f:write_editable(pns, "description")
        f:write_nl()
    end

    local pgkeys = isget and fn:property_keys_get(fn.PROP_GET) or {}
    local pskeys = isset and fn:property_keys_get(fn.PROP_SET) or {}
    build_vallist(f, pgkeys, pskeys, "Keys")

    local pgvals = isget and fn:property_values_get(fn.PROP_GET) or {}
    local psvals = isset and fn:property_values_get(fn.PROP_SET) or {}
    build_vallist(f, pgvals, psvals, "Values")

    if isget and gdoc:exists() then
        if isset then
            f:write_h("Getter", 3)
        else
            f:write_h("Description", 2)
        end
        f:write_raw(gdoc:full_get(nil, true))
        if isset and sdoc:exists() then
            f:write_nl(2)
        end
        if isset then
            f:write_nl()
            f:write_editable(pns, "getter_description")
            f:write_nl()
        end
    end

    if isset and sdoc:exists() then
        if isget then
            f:write_h("Setter", 3)
        else
            f:write_h("Description", 2)
        end
        f:write_raw(sdoc:full_get(nil, true))
        if isget then
            f:write_nl()
            f:write_editable(pns, "getter_description")
            f:write_nl()
        end
    end

    f:write_nl()
    if not isget or not isset then
        f:write_nl()
        f:write_br()
        f:write_editable(pns, "description")
        f:write_nl()
    end

    write_inherited_from(f, impl, cl, over, true)

    f:write_h("Signature", 2)
    f:write_code(gen_prop_sig(fn, cl))
    f:write_nl()

    f:write_h("C signature", 2)
    local codes = {}
    if isget then
        codes[#codes + 1] = gen_func_csig(fn, fn.PROP_GET)
    end
    if isset then
        codes[#codes + 1] = gen_func_csig(fn, fn.PROP_SET)
    end
    f:write_code(table.concat(codes, "\n"), "c")
    f:write_nl()

    write_ilist(f, impl, cl)
    f:write_nl()

    f:finish()
end

local build_event_example = function(ev)
    local evcn = ev:c_name_get()
    local evcnl = evcn:lower()

    local dtype = "Data *"

    local tbl = { "static void\n" }
    tbl[#tbl + 1] = "on_"
    tbl[#tbl + 1] = evcnl
    tbl[#tbl + 1] = "(void *data, const Efl_Event *event)\n{\n    "
    tbl[#tbl + 1] = dtree.type_cstr_get(ev:type_get(), "info = event->info;\n")
    tbl[#tbl + 1] = "    Eo *obj = event->object;\n    "
    tbl[#tbl + 1] = dtree.type_cstr_get(dtype, "d = data;\n\n")
    tbl[#tbl + 1] = "    /* event hander code */\n}\n\n"
    tbl[#tbl + 1] = "static void\nsetup_event_handler(Eo *obj, "
    tbl[#tbl + 1] = dtree.type_cstr_get(dtype, "d")
    tbl[#tbl + 1] = ")\n{\n"
    tbl[#tbl + 1] = "    efl_event_callback_add(obj, "
    tbl[#tbl + 1] = evcn
    tbl[#tbl + 1] = ", on_"
    tbl[#tbl + 1] = evcnl
    tbl[#tbl + 1] = ", d);\n}\n"

    return table.concat(tbl)
end

build_event = function(ev, cl)
    local evn = ev:nspaces_get(cl)
    local evnm = cl:name_get() .. ": " .. ev:name_get()
    local f = writer.Writer(evn, evnm)
    printgen("Generating event: " .. evnm)

    f:write_h("Description", 2)
    f:write_raw(ev:doc_get():full_get(nil, true))
    f:write_nl()

    f:write_editable(evn, "description")
    f:write_nl()

    f:write_h("Signature", 2)
    local buf = { ev:name_get() }

    if ev:scope_get() == ev.scope.PRIVATE then
        buf[#buf + 1] = " @private"
    elseif ev:scope_get() == ev.scope.PROTECTED then
        buf[#buf + 1] = " @protected"
    end

    if ev:is_beta() then
        buf[#buf + 1] = " @beta"
    end
    if ev:is_hot() then
        buf[#buf + 1] = " @hot"
    end
    if ev:is_restart() then
        buf[#buf + 1] = " @restart"
    end

    local etp = ev:type_get()
    if etp then
        buf[#buf + 1] = ": "
        buf[#buf + 1] = etp:serialize()
    end

    buf[#buf + 1] = ";"
    f:write_code(table.concat(buf))
    f:write_nl()

    f:write_h("C information", 2)
    f:write_code(build_evcsig(ev), "c")
    f:write_nl()

    f:write_h("C usage", 2)
    f:write_code(build_event_example(ev), "c")
    f:write_nl()

    f:finish()
end

local build_stats_keyref = function()
    for i, cl in ipairs(dtree.Class.all_get()) do
        stats.check_class(cl)
        keyref.add(cl:name_get():gsub("%.", "_"), cl:nspaces_get(), "c")
        for i, imp in ipairs(cl:implements_get()) do
            -- TODO: handle doc overrides in stats system
            if not imp:is_overridden(cl) then
                local func = imp:function_get()
                local fns = func:nspaces_get(cl)
                if imp:is_prop_get() or imp:is_prop_set() then
                    if imp:is_prop_get() then
                        stats.check_property(func, cl, func.PROP_GET)
                        keyref.add(func:full_c_name_get(func.PROP_GET), fns, "c")
                    end
                    if imp:is_prop_set() then
                        stats.check_property(func, cl, func.PROP_SET)
                        keyref.add(func:full_c_name_get(func.PROP_SET), fns, "c")
                    end
                else
                    stats.check_method(func, cl)
                    keyref.add(func:full_c_name_get(func.METHOD), fns, "c")
                end
            end
        end
        for i, ev in ipairs(cl:events_get()) do
            keyref.add(ev:c_name_get(), ev:nspaces_get(cl), "c")
        end
    end
    for i, tp in ipairs(dtree.Typedecl.all_aliases_get()) do
        stats.check_alias(tp)
    end
    for i, tp in ipairs(dtree.Typedecl.all_structs_get()) do
        stats.check_struct(tp)
    end
    for i, tp in ipairs(dtree.Typedecl.all_enums_get()) do
        stats.check_enum(tp)
    end
    for i, v in ipairs(dtree.Variable.all_constants_get()) do
        stats.check_constant(v)
    end
    for i, v in ipairs(dtree.Variable.all_globals_get()) do
        stats.check_global(v)
    end
end

getopt.parse {
    args = arg,
    descs = {
        { category = "General" },
        { "h", "help", nil, help = "Show this message.", metavar = "CATEGORY",
            callback = getopt.help_cb(io.stdout)
        },
        { "v", "verbose", false, help = "Be verbose." },
        { "p", "print-gen", false, help = "Print what is being generated." },

        { category = "Generator" },
        { "r", "root", true, help = "Root path of the docs." },
        { "n", "namespace", true, help = "Root namespace of the docs." },
        { nil, "graph-theme", true, help = "Optional graph theme." },
        { nil, "graph-theme-light", false, help = "Use light builtin graph theme." },
        { nil, "disable-notes", false, help = "Disable notes plugin usage." },
        { nil, "disable-folded", false, help = "Disable folded plugin usage." },
        { nil, "disable-title", false, help = "Disable title plugin usage." },
        { "m", "use-markdown", false,
            help = "Generate Markdown instead of DokuWiki syntax." },
        { nil, "pass", true, help = "The pass to run (optional) "
            .. "(rm, ref, clist, classes, types, vars, stats or class name)." }
    },
    error_cb = function(parser, msg)
        io.stderr:write(msg, "\n")
        getopt.help(parser, io.stderr)
    end,
    done_cb = function(parser, opts, args)
        if opts["h"] then
            return
        end
        if opts["p"] then
            printgen = function(...) print(...) end
        end
        if opts["graph-theme-dark"] then
            current_theme = default_theme_light
        end
        if opts["graph-theme"] then
            set_theme(opts["graph-theme"])
        end
        local rootns = (not opts["n"] or opts["n"] == "")
            and "develop:api" or opts["n"]
        local dr
        if not opts["r"] or opts["r"] == "" then
            dr = "dokuwiki/data/pages"
        else
            dr = opts["r"]
        end
        dr = dutil.path_join(dr, dutil.nspace_to_path(rootns))
        dutil.init(dr, rootns)
        writer.set_backend("dokuwiki")
        if #args == 0 then
            dtree.scan_directory()
        else
            for i, p in ipairs(args) do
                dtree.scan_directory(p)
            end
        end

        local st = opts["pass"]

        dtree.parse(st)

        if st == "clist" then
            for i, cl in ipairs(dtree.Class.all_get()) do
                print(cl:name_get())
            end
            return
        end

        local wfeatures = {
            notes = not opts["disable-notes"],
            folds = not opts["disable-folded"],
            title = not opts["disable-title"]
        }
        writer.init(rootns, wfeatures)
        if not st or st == "rm" then
            dutil.rm_root()
            dutil.mkdir_r(nil)
        end
        if not st or st == "ref" then
            build_ref()
        end
        if not st or st == "classes" then
            build_classes()
        end
        if st and st:match("%.") then
            local cl = dtree.Class.by_name_get(st)
            if cl then
                build_class(cl)
            end
        end
        if not st or st == "types" then
            build_typedecls()
        end
        if not st or st == "vars" then
            build_variables()
        end

        if not st or st == "stats" then
            stats.init(not not opts["v"])
            build_stats_keyref()
            keyref.build()
            -- newline if printing what's being generated
            printgen()
            stats.print()
        end
    end
}

return true