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
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
|
/* valagidlparser.vala
*
* Copyright (C) 2006-2010 Jürg Billeter
* Copyright (C) 2006-2008 Raffaele Sandrini
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
* Raffaele Sandrini <raffaele@sandrini.ch>
*/
using GLib;
/**
* Code visitor parsing all GIDL files.
*/
public class Vala.GIdlParser : CodeVisitor {
private CodeContext context;
private SourceFile current_source_file;
private SourceReference current_source_reference;
private Namespace current_namespace;
private TypeSymbol current_data_type;
private Map<string,string> codenode_attributes_map;
private Map<PatternSpec*,string> codenode_attributes_patterns;
private Set<string> current_type_symbol_set;
private Map<string,TypeSymbol> cname_type_map;
static GLib.Regex type_from_string_regex;
/**
* Parse all source files in the specified code context and build a
* code tree.
*
* @param context a code context
*/
public void parse (CodeContext context) {
cname_type_map = new HashMap<string,TypeSymbol> (str_hash, str_equal);
this.context = context;
context.accept (this);
cname_type_map = null;
}
public override void visit_namespace (Namespace ns) {
ns.accept_children (this);
}
public override void visit_class (Class cl) {
visit_type (cl);
}
public override void visit_struct (Struct st) {
visit_type (st);
}
public override void visit_interface (Interface iface) {
visit_type (iface);
}
public override void visit_enum (Enum en) {
visit_type (en);
}
public override void visit_error_domain (ErrorDomain ed) {
visit_type (ed);
}
public override void visit_delegate (Delegate d) {
visit_type (d);
}
private void visit_type (TypeSymbol t) {
if (!cname_type_map.contains (get_cname (t))) {
cname_type_map[get_cname (t)] = t;
}
}
public override void visit_source_file (SourceFile source_file) {
if (source_file.filename.has_suffix (".gi")) {
parse_file (source_file);
}
}
private void parse_file (SourceFile source_file) {
string metadata_filename = "%s.metadata".printf (source_file.filename.substring (0, source_file.filename.length - ".gi".length));
current_source_file = source_file;
codenode_attributes_map = new HashMap<string,string> (str_hash, str_equal);
codenode_attributes_patterns = new HashMap<PatternSpec*,string> (direct_hash, (EqualFunc) PatternSpec.equal);
if (FileUtils.test (metadata_filename, FileTest.EXISTS)) {
try {
string metadata;
FileUtils.get_contents (metadata_filename, out metadata, null);
foreach (string line in metadata.split ("\n")) {
if (line.has_prefix ("#")) {
// ignore comment lines
continue;
}
var tokens = line.split (" ", 2);
if (null == tokens[0]) {
continue;
}
if (-1 != tokens[0].index_of_char ('*')) {
PatternSpec* pattern = new PatternSpec (tokens[0]);
codenode_attributes_patterns[pattern] = tokens[0];
}
codenode_attributes_map[tokens[0]] = tokens[1];
}
} catch (FileError e) {
Report.error (null, "Unable to read metadata file: %s".printf (e.message));
}
}
try {
var modules = Idl.parse_file (source_file.filename);
current_source_reference = new SourceReference (source_file, SourceLocation (null, 0, 0), SourceLocation (null, 0, 0));
foreach (weak IdlModule module in modules) {
var ns = parse_module (module);
if (ns != null) {
context.root.add_namespace (ns);
}
}
} catch (MarkupError e) {
Report.error (null, "Unable to parse GIDL file: %s".printf (e.message));
}
}
private string fix_type_name (string type_name, Symbol container) {
var attributes = get_attributes (type_name);
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "name") {
return eval (nv[1]);
}
}
}
if (type_name.has_prefix (container.name)) {
return type_name.substring (container.name.length);
} else {
var cprefix = get_cprefix (container);
if (type_name.has_prefix (cprefix)) {
return type_name.substring (cprefix.length);;
}
}
return type_name;
}
private string fix_const_name (string const_name, Symbol container) {
var pref = get_lower_case_cprefix (container).ascii_up ();
if (const_name.has_prefix (pref)) {
return const_name.substring (pref.length);
}
return const_name;
}
private string get_cheader_filename (Symbol sym) {
var cheader_filename = sym.get_attribute_string ("CCode", "cheader_filename");
if (cheader_filename != null) {
return cheader_filename;
}
if (sym.parent_symbol != null) {
return get_cheader_filename (sym.parent_symbol);
} else if (sym.source_reference != null) {
return sym.source_reference.file.get_cinclude_filename ();
}
return "";
}
private string get_cname (Symbol sym, Symbol? container = null) {
if (container == null) {
container = sym.parent_symbol;
}
var cname = sym.get_attribute_string ("CCode", "cname");
if (cname != null) {
return cname;
}
if (sym is Method) {
var name = sym.name;
if (sym is CreationMethod) {
if (name == null || name == ".new") {
name = "new";
} else {
name = "new_"+name;
}
}
if (container != null) {
return "%s%s".printf (get_lower_case_cprefix (container), name);
} else {
return name;
}
} else {
if (container != null) {
return "%s%s".printf (get_cprefix (container), sym.name);
} else {
return sym.name;
}
}
}
private string get_finish_cname (Method m) {
var finish_cname = m.get_attribute_string ("CCode", "finish_name");
if (finish_cname != null) {
return finish_cname;
}
var result = get_cname (m);
if (result.has_suffix ("_async")) {
result = result.substring (0, result.length - "_async".length);
}
return result + "_finish";
}
private string get_lower_case_cname (Symbol sym) {
var lower_case_csuffix = Symbol.camel_case_to_lower_case (sym.name);
if (sym is ObjectTypeSymbol) {
// remove underscores in some cases to avoid conflicts of type macros
if (lower_case_csuffix.has_prefix ("type_")) {
lower_case_csuffix = "type" + lower_case_csuffix.substring ("type_".length);
} else if (lower_case_csuffix.has_prefix ("is_")) {
lower_case_csuffix = "is" + lower_case_csuffix.substring ("is_".length);
}
if (lower_case_csuffix.has_suffix ("_class")) {
lower_case_csuffix = lower_case_csuffix.substring (0, lower_case_csuffix.length - "_class".length) + "class";
}
}
if (sym.parent_symbol != null) {
return "%s%s".printf (get_lower_case_cprefix (sym.parent_symbol), lower_case_csuffix);
} else {
return lower_case_csuffix;
}
}
private string get_lower_case_cprefix (Symbol sym) {
if (sym.name == null) {
return "";
}
string cprefix;
cprefix = sym.get_attribute_string ("CCode", "lower_case_cprefix");
if (cprefix == null && (sym is ObjectTypeSymbol || sym is Struct)) {
cprefix = sym.get_attribute_string ("CCode", "cprefix");
}
if (cprefix != null) {
return cprefix;
}
return get_lower_case_cname (sym) + "_";
}
public string get_cprefix (Symbol sym) {
if (sym is ObjectTypeSymbol) {
return get_cname (sym);
} else if (sym is Enum || sym is ErrorDomain) {
return "%s_".printf (get_lower_case_cname (sym).ascii_up ());
} else if (sym is Namespace) {
if (sym.name != null) {
var cprefix = sym.get_attribute_string ("CCode", "cprefix");
if (cprefix != null) {
return cprefix;
}
if (sym.parent_symbol != null) {
return "%s%s".printf (get_cprefix (sym.parent_symbol), sym.name);
} else {
return sym.name;
}
} else {
return "";
}
} else if (sym.name != null) {
return sym.name;
}
return "";
}
private string[] get_attributes_for_node (IdlNode node) {
string name;
if (node.type == IdlNodeTypeId.FUNCTION) {
name = ((IdlNodeFunction) node).symbol;
} else if (node.type == IdlNodeTypeId.SIGNAL) {
name = "%s::%s".printf (get_cname (current_data_type), node.name);
} else if (node.type == IdlNodeTypeId.PROPERTY) {
name = "%s:%s".printf (get_cname (current_data_type), node.name);
} else if (node.type == IdlNodeTypeId.FIELD) {
name = "%s.%s".printf (get_cname (current_data_type), node.name);
} else {
name = node.name;
}
return get_attributes (name);
}
private void add_symbol_to_container (Symbol container, Symbol sym) {
if (container is Class) {
unowned Class cl = (Class) container;
if (sym is Class) {
cl.add_class ((Class) sym);
} else if (sym is Constant) {
cl.add_constant ((Constant) sym);
} else if (sym is Enum) {
cl.add_enum ((Enum) sym);
} else if (sym is Field) {
cl.add_field ((Field) sym);
} else if (sym is Method) {
cl.add_method ((Method) sym);
} else if (sym is Property) {
cl.add_property ((Property) sym);
} else if (sym is Signal) {
cl.add_signal ((Signal) sym);
} else if (sym is Struct) {
cl.add_struct ((Struct) sym);
}
} else if (container is Enum) {
unowned Enum en = (Enum) container;
if (sym is EnumValue) {
en.add_value ((EnumValue) sym);
} else if (sym is Constant) {
en.add_constant ((Constant) sym);
} else if (sym is Method) {
en.add_method ((Method) sym);
}
} else if (container is Interface) {
unowned Interface iface = (Interface) container;
if (sym is Class) {
iface.add_class ((Class) sym);
} else if (sym is Constant) {
iface.add_constant ((Constant) sym);
} else if (sym is Enum) {
iface.add_enum ((Enum) sym);
} else if (sym is Field) {
iface.add_field ((Field) sym);
} else if (sym is Method) {
iface.add_method ((Method) sym);
} else if (sym is Property) {
iface.add_property ((Property) sym);
} else if (sym is Signal) {
iface.add_signal ((Signal) sym);
} else if (sym is Struct) {
iface.add_struct ((Struct) sym);
}
} else if (container is Namespace) {
unowned Namespace ns = (Namespace) container;
if (sym is Namespace) {
ns.add_namespace ((Namespace) sym);
} else if (sym is Class) {
ns.add_class ((Class) sym);
} else if (sym is Constant) {
ns.add_constant ((Constant) sym);
} else if (sym is Delegate) {
ns.add_delegate ((Delegate) sym);
} else if (sym is Enum) {
ns.add_enum ((Enum) sym);
} else if (sym is ErrorDomain) {
ns.add_error_domain ((ErrorDomain) sym);
} else if (sym is Field) {
ns.add_field ((Field) sym);
} else if (sym is Interface) {
ns.add_interface ((Interface) sym);
} else if (sym is Method) {
ns.add_method ((Method) sym);
} else if (sym is Namespace) {
ns.add_namespace ((Namespace) sym);
} else if (sym is Struct) {
ns.add_struct ((Struct) sym);
}
} else if (container is Struct) {
unowned Struct st = (Struct) container;
if (sym is Constant) {
st.add_constant ((Constant) sym);
} else if (sym is Field) {
st.add_field ((Field) sym);
} else if (sym is Method) {
st.add_method ((Method) sym);
} else if (sym is Property) {
st.add_property ((Property) sym);
}
}
if (!(sym is Namespace) && container is Namespace) {
// set C headers
sym.set_attribute_string ("CCode", "cheader_filename", get_cheader_filename (sym));
}
}
private void parse_node (IdlNode node, IdlModule module, Symbol container) {
if (node.type == IdlNodeTypeId.CALLBACK) {
var cb = parse_delegate ((IdlNodeFunction) node);
if (cb == null) {
return;
}
cb.name = fix_type_name (cb.name, container);
add_symbol_to_container (container, cb);
current_source_file.add_node (cb);
} else if (node.type == IdlNodeTypeId.STRUCT) {
parse_struct ((IdlNodeStruct) node, container, module);
} else if (node.type == IdlNodeTypeId.UNION) {
parse_union ((IdlNodeUnion) node, container, module);
} else if (node.type == IdlNodeTypeId.BOXED) {
parse_boxed ((IdlNodeBoxed) node, container, module);
} else if (node.type == IdlNodeTypeId.ENUM) {
parse_enum ((IdlNodeEnum) node, container, module, false);
} else if (node.type == IdlNodeTypeId.FLAGS) {
parse_enum ((IdlNodeEnum) node, container, module, true);
} else if (node.type == IdlNodeTypeId.OBJECT) {
parse_object ((IdlNodeInterface) node, container, module);
} else if (node.type == IdlNodeTypeId.INTERFACE) {
parse_interface ((IdlNodeInterface) node, container, module);
} else if (node.type == IdlNodeTypeId.CONSTANT) {
var c = parse_constant ((IdlNodeConstant) node);
if (c != null) {
c.name = fix_const_name (c.name, container);
add_symbol_to_container (container, c);
current_source_file.add_node (c);
}
} else if (node.type == IdlNodeTypeId.FUNCTION) {
var m = parse_function ((IdlNodeFunction) node);
if (m != null) {
m.binding = MemberBinding.STATIC;
add_symbol_to_container (container, m);
current_source_file.add_node (m);
}
}
}
private Symbol? get_container_from_name (string name) {
var path = name.split (".");
Symbol? cp = current_namespace;
if (cp.parent_symbol != context.root) {
cp = cp.parent_symbol;
}
Symbol? cc = null;
foreach ( unowned string tok in path ) {
cc = cp.scope.lookup (tok) as Symbol;
if ( cc == null ) {
cc = new Namespace (tok, current_source_reference);
add_symbol_to_container (cp, cc);
}
cp = cc;
}
return cc;
}
private Namespace? parse_module (IdlModule module) {
Symbol sym = context.root.scope.lookup (module.name);
Namespace ns;
if (sym is Namespace) {
ns = (Namespace) sym;
if (ns.external_package) {
ns.attributes = null;
ns.source_reference = current_source_reference;
}
} else {
ns = new Namespace (module.name, current_source_reference);
}
current_namespace = ns;
var attributes = get_attributes (ns.name);
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "cheader_filename") {
ns.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "cprefix") {
ns.set_attribute_string ("CCode", "cprefix", eval (nv[1]));
} else if (nv[0] == "lower_case_cprefix") {
ns.set_attribute_string ("CCode", "lower_case_cprefix", eval (nv[1]));
} else if (nv[0] == "gir_namespace") {
ns.source_reference.file.gir_namespace = eval (nv[1]);
ns.set_attribute_string ("CCode", "gir_namespace", eval (nv[1]));
} else if (nv[0] == "gir_version") {
ns.source_reference.file.gir_version = eval (nv[1]);
ns.set_attribute_string ("CCode", "gir_version", eval (nv[1]));
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
ns.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
ns.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
ns.set_attribute_string ("Deprecated", "since", eval (nv[1]));
}
}
}
var deferred = new ArrayList<unowned IdlNode> ();
foreach (weak IdlNode node in module.entries) {
bool is_deferred = false;
var child_attributes = get_attributes_for_node (node);
if (child_attributes != null) {
foreach (unowned string attr in child_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "parent") {
deferred.add (node);
is_deferred = true;
}
}
}
if (!is_deferred) {
parse_node (node, module, ns);
}
}
foreach (unowned IdlNode node in deferred) {
Symbol container = ns;
var child_attributes = get_attributes_for_node (node);
if (child_attributes != null) {
foreach (unowned string attr in child_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "parent") {
container = get_container_from_name (eval (nv[1]));
}
}
}
if (container is Namespace) {
current_namespace = (Namespace) container;
} else {
current_data_type = (TypeSymbol) container;
}
parse_node (node, module, container);
current_namespace = ns;
current_data_type = null;
}
current_namespace = null;
if (sym is Namespace) {
return null;
}
return ns;
}
private Delegate? parse_delegate (IdlNodeFunction f_node) {
weak IdlNode node = (IdlNode) f_node;
var return_type = parse_param (f_node.result);
var cb = new Delegate (node.name, return_type, current_source_reference);
cb.access = SymbolAccessibility.PUBLIC;
cb.has_target = false;
bool check_has_target = true;
bool suppress_throws = false;
string? error_types = null;
var attributes = get_attributes (node.name);
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return null;
}
} else if (nv[0] == "cheader_filename") {
cb.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "has_target") {
if (eval (nv[1]) == "0") {
check_has_target = false;
} else if (eval (nv[1]) == "1") {
cb.has_target = true;
}
} else if (nv[0] == "transfer_ownership") {
if (eval (nv[1]) == "1") {
return_type.value_owned = true;
}
} else if (nv[0] == "is_array") {
if (eval (nv[1]) == "1") {
return_type.value_owned = true;
return_type = new ArrayType (return_type, 1, return_type.source_reference);
cb.return_type = return_type;
}
} else if (nv[0] == "throws") {
if (eval (nv[1]) == "0") {
suppress_throws = true;
}
} else if (nv[0] == "error_types") {
error_types = eval (nv[1]);
} else if (nv[0] == "array_length_type") {
cb.set_attribute_string ("CCode", "array_length_type", eval (nv[1]));
} else if (nv[0] == "type_name") {
cb.return_type = return_type = parse_type_from_string (eval (nv[1]), return_type.value_owned);
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
cb.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
cb.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
cb.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "type_arguments") {
parse_type_arguments_from_string (return_type, eval (nv[1]));
} else if (nv[0] == "instance_pos") {
cb.set_attribute_double ("CCode", "instance_pos", double.parse (eval (nv[1])));
} else if (nv[0] == "type_parameters") {
foreach (string type_param_name in eval (nv[1]).split (",")) {
cb.add_type_parameter (new TypeParameter (type_param_name, current_source_reference));
}
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
cb.set_attribute ("Experimental", true);
}
}
}
}
uint remaining_params = f_node.parameters.length ();
foreach (weak IdlNodeParam param in f_node.parameters) {
weak IdlNode param_node = (IdlNode) param;
if (check_has_target && remaining_params == 1 && (param_node.name == "user_data" || param_node.name == "data")) {
// hide user_data parameter for instance delegates
cb.has_target = true;
} else {
// check for GError parameter
if (suppress_throws == false && param_is_exception (param)) {
if (error_types == null)
cb.add_error_type (parse_type (param.type));
remaining_params--;
continue;
}
string param_name = param_node.name;
if (param_name == "string") {
// avoid conflict with string type
param_name = "str";
} else if (param_name == "self") {
// avoid conflict with delegate target
param_name = "_self";
}
ParameterDirection direction;
var param_type = parse_param (param, out direction);
var p = new Parameter (param_name, param_type);
p.direction = direction;
bool hide_param = false;
bool show_param = false;
bool array_requested = false;
bool out_requested = false;
attributes = get_attributes ("%s.%s".printf (node.name, param_node.name));
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
hide_param = true;
} else if (eval (nv[1]) == "0") {
show_param = true;
}
} else if (nv[0] == "is_array") {
if (eval (nv[1]) == "1") {
param_type.value_owned = true;
param_type = new ArrayType (param_type, 1, param_type.source_reference);
p.variable_type = param_type;
if (!out_requested) {
p.direction = ParameterDirection.IN;
}
array_requested = true;
}
} else if (nv[0] == "is_out") {
if (eval (nv[1]) == "1") {
p.direction = ParameterDirection.OUT;
out_requested = true;
if (!array_requested && param_type is ArrayType) {
var array_type = (ArrayType) param_type;
param_type = array_type.element_type;
p.variable_type = param_type;
}
}
} else if (nv[0] == "is_ref") {
if (eval (nv[1]) == "1") {
p.direction = ParameterDirection.REF;
if (!array_requested && param_type is ArrayType) {
var array_type = (ArrayType) param_type;
param_type = array_type.element_type;
p.variable_type = param_type;
}
}
} else if (nv[0] == "takes_ownership") {
if (eval (nv[1]) == "1") {
param_type.value_owned = true;
}
} else if (nv[0] == "nullable") {
if (eval (nv[1]) == "1") {
param_type.nullable = true;
}
} else if (nv[0] == "type_arguments") {
parse_type_arguments_from_string (param_type, eval (nv[1]));
} else if (nv[0] == "no_array_length") {
if (eval (nv[1]) == "1") {
p.set_attribute_bool ("CCode", "array_length", false);
}
} else if (nv[0] == "array_length_type") {
p.set_attribute_string ("CCode", "array_length_type", eval (nv[1]));
} else if (nv[0] == "array_null_terminated") {
if (eval (nv[1]) == "1") {
p.set_attribute_bool ("CCode", "array_length", false);
p.set_attribute_bool ("CCode", "array_null_terminated", true);
}
} else if (nv[0] == "type_name") {
p.variable_type = param_type = parse_type_from_string (eval (nv[1]), false);
}
}
}
if (show_param || !hide_param) {
cb.add_parameter (p);
}
}
remaining_params--;
}
if (suppress_throws == false && error_types != null) {
var type_args = eval (error_types).split (",");
foreach (string type_arg in type_args) {
cb.add_error_type (parse_type_from_string (type_arg, true));
}
}
return cb;
}
private bool is_reference_type (string cname) {
var st_attributes = get_attributes (cname);
if (st_attributes != null) {
foreach (string attr in st_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "is_value_type" && eval (nv[1]) == "1") {
return false;
}
}
}
return true;
}
private void parse_struct (IdlNodeStruct st_node, Symbol container, IdlModule module) {
weak IdlNode node = (IdlNode) st_node;
if (st_node.deprecated) {
return;
}
string name = fix_type_name (node.name, container);
if (!is_reference_type (node.name)) {
var st = container.scope.lookup (name) as Struct;
if (st == null) {
st = new Struct (name, current_source_reference);
st.access = SymbolAccessibility.PUBLIC;
var st_attributes = get_attributes (node.name);
if (st_attributes != null) {
foreach (string attr in st_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "cheader_filename") {
st.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return;
}
} else if (nv[0] == "base_type") {
st.base_type = parse_type_string (eval (nv[1]));
} else if (nv[0] == "rank") {
st.set_rank (int.parse (eval (nv[1])));
} else if (nv[0] == "simple_type") {
if (eval (nv[1]) == "1") {
st.set_simple_type (true);
}
} else if (nv[0] == "immutable") {
if (eval (nv[1]) == "1") {
st.set_attribute ("Immutable", true);
}
} else if (nv[0] == "has_type_id") {
if (eval (nv[1]) == "0") {
st.set_attribute_bool ("CCode", "has_type_id", false);
}
} else if (nv[0] == "type_id") {
st.set_attribute_string ("CCode", "type_id", eval (nv[1]));
} else if (nv[0] == "has_copy_function") {
if (eval (nv[1]) == "0") {
st.set_attribute_bool ("CCode", "has_copy_function", false);
}
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
st.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
st.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
st.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "has_destroy_function") {
if (eval (nv[1]) == "0") {
st.set_attribute_bool ("CCode", "has_destroy_function", false);
}
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
st.set_attribute ("Experimental", true);
}
}
}
}
add_symbol_to_container (container, st);
current_source_file.add_node (st);
}
current_data_type = st;
foreach (weak IdlNode member in st_node.members) {
if (member.type == IdlNodeTypeId.FUNCTION) {
var m = parse_function ((IdlNodeFunction) member);
if (m != null) {
st.add_method (m);
}
} else if (member.type == IdlNodeTypeId.FIELD) {
var f = parse_field ((IdlNodeField) member);
if (f != null) {
st.add_field (f);
}
}
}
current_data_type = null;
} else {
bool ref_function_void = false;
string ref_function = null;
string unref_function = null;
string copy_function = null;
string free_function = null;
var cl = container.scope.lookup (name) as Class;
if (cl == null) {
string base_class = null;
bool is_fundamental = false;
cl = new Class (name, current_source_reference);
cl.access = SymbolAccessibility.PUBLIC;
var cl_attributes = get_attributes (node.name);
if (cl_attributes != null) {
foreach (string attr in cl_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "cheader_filename") {
cl.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "base_class") {
base_class = eval (nv[1]);
} else if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return;
}
} else if (nv[0] == "is_immutable") {
if (eval (nv[1]) == "1") {
cl.is_immutable = true;
}
} else if (nv[0] == "const_cname") {
cl.set_attribute_string ("CCode", "const_cname", eval (nv[1]));
} else if (nv[0] == "is_fundamental") {
if (eval (nv[1]) == "1") {
is_fundamental = true;
}
} else if (nv[0] == "abstract" && base_class != null) {
if (eval (nv[1]) == "1") {
cl.is_abstract = true;
}
} else if (nv[0] == "free_function") {
free_function = eval (nv[1]);
} else if (nv[0] == "ref_function") {
ref_function = eval (nv[1]);
} else if (nv[0] == "unref_function") {
unref_function = eval (nv[1]);
} else if (nv[0] == "copy_function") {
copy_function = eval (nv[1]);
} else if (nv[0] == "ref_function_void") {
if (eval (nv[1]) == "1") {
ref_function_void = true;
}
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
cl.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
cl.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
cl.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "type_parameters") {
foreach (string type_param_name in eval (nv[1]).split (",")) {
cl.add_type_parameter (new TypeParameter (type_param_name, current_source_reference));
}
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
cl.set_attribute ("Experimental", true);
}
} else if (nv[0] == "delegate_target_cname") {
cl.set_attribute_string ("CCode", "delegate_target_cname", eval (nv[1]));
}
}
}
add_symbol_to_container (container, cl);
current_source_file.add_node (cl);
if (base_class != null) {
var parent = parse_type_string (base_class);
cl.add_base_type (parent);
}
if (base_class == null && !is_fundamental) {
cl.is_compact = true;
}
}
current_data_type = cl;
foreach (weak IdlNode member in st_node.members) {
if (member.type == IdlNodeTypeId.FUNCTION) {
if ((ref_function == null) && (member.name == "ref")) {
ref_function = ((IdlNodeFunction) member).symbol;
ref_function_void = (parse_type (((IdlNodeFunction) member).result.type) is VoidType);
} else if ((unref_function == null) && (member.name == "unref")) {
unref_function = ((IdlNodeFunction) member).symbol;
} else if ((free_function == null) && (member.name == "free" || member.name == "destroy")) {
free_function = ((IdlNodeFunction) member).symbol;
} else {
if ((copy_function == null) && (member.name == "copy")) {
copy_function = ((IdlNodeFunction) member).symbol;
}
var m = parse_function ((IdlNodeFunction) member);
if (m != null) {
cl.add_method (m);
}
}
} else if (member.type == IdlNodeTypeId.FIELD) {
var f = parse_field ((IdlNodeField) member);
if (f != null) {
cl.add_field (f);
}
}
}
if (ref_function != null) {
cl.set_attribute_string ("CCode", "ref_function", ref_function);
if (ref_function_void) {
cl.set_attribute_bool ("CCode", "ref_function_void", ref_function_void);
}
} else if (copy_function != null) {
cl.set_attribute_string ("CCode", "copy_function", copy_function);
}
if (unref_function != null) {
cl.set_attribute_string ("CCode", "unref_function", unref_function);
} else if (free_function != null && free_function != "%sfree".printf (get_lower_case_cprefix (cl))) {
cl.set_attribute_string ("CCode", "free_function", free_function);
}
current_data_type = null;
}
}
private void parse_union (IdlNodeUnion un_node, Symbol container, IdlModule module) {
weak IdlNode node = (IdlNode) un_node;
if (un_node.deprecated) {
return;
}
string name = fix_type_name (node.name, container);
if (!is_reference_type (node.name)) {
var st = container.scope.lookup (name) as Struct;
if (st == null) {
st = new Struct (name, current_source_reference);
st.access = SymbolAccessibility.PUBLIC;
var st_attributes = get_attributes (node.name);
if (st_attributes != null) {
foreach (string attr in st_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "cheader_filename") {
st.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
st.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
st.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
st.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return;
}
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
st.set_attribute ("Experimental", true);
}
}
}
}
add_symbol_to_container (container, st);
current_source_file.add_node (st);
}
current_data_type = st;
foreach (weak IdlNode member in un_node.members) {
if (member.type == IdlNodeTypeId.FUNCTION) {
var m = parse_function ((IdlNodeFunction) member);
if (m != null) {
st.add_method (m);
}
} else if (member.type == IdlNodeTypeId.FIELD) {
var f = parse_field ((IdlNodeField) member);
if (f != null) {
st.add_field (f);
}
}
}
current_data_type = null;
} else {
var cl = container.scope.lookup (name) as Class;
if (cl == null) {
cl = new Class (name, current_source_reference);
cl.access = SymbolAccessibility.PUBLIC;
cl.is_compact = true;
var cl_attributes = get_attributes (node.name);
if (cl_attributes != null) {
foreach (string attr in cl_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "cheader_filename") {
cl.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return;
}
}
}
}
add_symbol_to_container (container, cl);
current_source_file.add_node (cl);
}
current_data_type = cl;
bool ref_function_void = false;
string ref_function = null;
string unref_function = null;
string copy_function = null;
string free_function = null;
foreach (weak IdlNode member in un_node.members) {
if (member.type == IdlNodeTypeId.FUNCTION) {
if (member.name == "ref") {
ref_function = ((IdlNodeFunction) member).symbol;
ref_function_void = (parse_type (((IdlNodeFunction) member).result.type) is VoidType);
} else if (member.name == "unref") {
unref_function = ((IdlNodeFunction) member).symbol;
} else if (member.name == "free" || member.name == "destroy") {
free_function = ((IdlNodeFunction) member).symbol;
} else {
if (member.name == "copy") {
copy_function = ((IdlNodeFunction) member).symbol;
}
var m = parse_function ((IdlNodeFunction) member);
if (m != null) {
cl.add_method (m);
}
}
} else if (member.type == IdlNodeTypeId.FIELD) {
var f = parse_field ((IdlNodeField) member);
if (f != null) {
cl.add_field (f);
}
}
}
if (ref_function != null) {
cl.set_attribute_string ("CCode", "ref_function", ref_function);
if (ref_function_void) {
cl.set_attribute_bool ("CCode", "ref_function_void", ref_function_void);
}
} else if (copy_function != null) {
cl.set_attribute_string ("CCode", "copy_function", copy_function);
}
if (unref_function != null) {
cl.set_attribute_string ("CCode", "unref_function", unref_function);
} else if (free_function != null && free_function != "%sfree".printf (get_lower_case_cprefix (cl))) {
cl.set_attribute_string ("CCode", "free_function", free_function);
}
current_data_type = null;
}
}
private void parse_boxed (IdlNodeBoxed boxed_node, Symbol container, IdlModule module) {
weak IdlNode node = (IdlNode) boxed_node;
string name = fix_type_name (node.name, container);
var node_attributes = get_attributes (node.name);
if (node_attributes != null) {
foreach (string attr in node_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "hidden") {
return;
}
}
}
if (!is_reference_type (node.name)) {
var st = container.scope.lookup (name) as Struct;
if (st == null) {
st = new Struct (name, current_source_reference);
st.access = SymbolAccessibility.PUBLIC;
var st_attributes = get_attributes (node.name);
if (st_attributes != null) {
foreach (string attr in st_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "cheader_filename") {
st.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
st.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
st.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
st.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "immutable") {
if (eval (nv[1]) == "1") {
st.set_attribute ("Immutable", true);
}
} else if (nv[0] == "has_copy_function") {
if (eval (nv[1]) == "0") {
st.set_attribute_bool ("CCode", "has_copy_function", false);
}
} else if (nv[0] == "has_destroy_function") {
if (eval (nv[1]) == "0") {
st.set_attribute_bool ("CCode", "has_destroy_function", false);
}
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
st.set_attribute ("Experimental", true);
}
}
}
}
add_symbol_to_container (container, st);
current_source_file.add_node (st);
}
current_data_type = st;
foreach (weak IdlNode member in boxed_node.members) {
if (member.type == IdlNodeTypeId.FUNCTION) {
var m = parse_function ((IdlNodeFunction) member);
if (m != null) {
st.add_method (m);
}
} else if (member.type == IdlNodeTypeId.FIELD) {
var f = parse_field ((IdlNodeField) member);
if (f != null) {
st.add_field (f);
}
}
}
current_data_type = null;
} else {
bool ref_function_void = false;
string ref_function = null;
string unref_function = null;
string copy_function = null;
string free_function = null;
var cl = container.scope.lookup (name) as Class;
if (cl == null) {
string base_class = null;
cl = new Class (name, current_source_reference);
cl.access = SymbolAccessibility.PUBLIC;
cl.is_compact = true;
if (boxed_node.gtype_init != null) {
cl.set_attribute_string ("CCode", "type_id", "%s ()".printf (boxed_node.gtype_init));
}
var cl_attributes = get_attributes (node.name);
if (cl_attributes != null) {
foreach (string attr in cl_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "cheader_filename") {
cl.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "base_class") {
base_class = eval (nv[1]);
} else if (nv[0] == "is_immutable") {
if (eval (nv[1]) == "1") {
cl.is_immutable = true;
}
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
cl.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
cl.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
cl.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "const_cname") {
cl.set_attribute_string ("CCode", "const_cname", eval (nv[1]));
} else if (nv[0] == "free_function") {
free_function = eval (nv[1]);
} else if (nv[0] == "ref_function") {
ref_function = eval (nv[1]);
} else if (nv[0] == "unref_function") {
unref_function = eval (nv[1]);
} else if (nv[0] == "copy_function") {
copy_function = eval (nv[1]);
} else if (nv[0] == "ref_function_void") {
if (eval (nv[1]) == "1") {
ref_function_void = true;
}
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
cl.set_attribute ("Experimental", true);
}
}
}
}
add_symbol_to_container (container, cl);
current_source_file.add_node (cl);
if (base_class != null) {
var parent = parse_type_string (base_class);
cl.add_base_type (parent);
}
}
current_data_type = cl;
foreach (weak IdlNode member in boxed_node.members) {
if (member.type == IdlNodeTypeId.FUNCTION) {
if (member.name == "ref") {
ref_function = ((IdlNodeFunction) member).symbol;
ref_function_void = (parse_type (((IdlNodeFunction) member).result.type) is VoidType);
} else if (member.name == "unref") {
unref_function = ((IdlNodeFunction) member).symbol;
} else if (member.name == "free" || member.name == "destroy") {
free_function = ((IdlNodeFunction) member).symbol;
} else {
if (member.name == "copy") {
copy_function = ((IdlNodeFunction) member).symbol;
}
var m = parse_function ((IdlNodeFunction) member);
if (m != null) {
cl.add_method (m);
}
}
} else if (member.type == IdlNodeTypeId.FIELD) {
var f = parse_field ((IdlNodeField) member);
if (f != null) {
cl.add_field (f);
}
}
}
if (ref_function != null) {
cl.set_attribute_string ("CCode", "ref_function", ref_function);
if (ref_function_void) {
cl.set_attribute_bool ("CCode", "ref_function_void", ref_function_void);
}
} else if (copy_function != null) {
cl.set_attribute_string ("CCode", "copy_function", copy_function);
}
if (unref_function != null) {
cl.set_attribute_string ("CCode", "unref_function", unref_function);
} else if (free_function != null && free_function != "%sfree".printf (get_lower_case_cprefix (cl))) {
cl.set_attribute_string ("CCode", "free_function", free_function);
}
current_data_type = null;
}
}
private void parse_enum (IdlNodeEnum en_node, Symbol container, IdlModule module, bool is_flags) {
weak IdlNode node = (IdlNode) en_node;
string name = fix_type_name (node.name, container);
bool existing = true;
var en = container.scope.lookup (name) as Enum;
if (en == null) {
en = new Enum (name, current_source_reference);
en.access = SymbolAccessibility.PUBLIC;
existing = false;
} else {
// ignore dummy enum values in -custom.vala files
// they exist for syntactical reasons
var dummy = (EnumValue) en.scope.lookup ("__DUMMY__");
if (dummy != null) {
en.get_values ().remove (dummy);
en.scope.remove ("__DUMMY__");
}
}
if (en_node.gtype_name == null || en_node.gtype_name == "") {
en.set_attribute_bool ("CCode", "has_type_id", false);
}
string common_prefix = null;
foreach (weak IdlNode value in en_node.values) {
var val_attributes = get_attributes (value.name);
bool is_hidden = false;
if (val_attributes != null) {
foreach (string attr in val_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "hidden" && eval(nv[1]) == "1") {
is_hidden = true;
}
}
}
if (is_hidden) {
continue;
}
if (common_prefix == null) {
common_prefix = value.name;
while (common_prefix.length > 0 && !common_prefix.has_suffix ("_")) {
// FIXME: could easily be made faster
common_prefix = common_prefix.substring (0, common_prefix.length - 1);
}
} else {
while (!value.name.has_prefix (common_prefix)) {
common_prefix = common_prefix.substring (0, common_prefix.length - 1);
}
}
while (common_prefix.length > 0 && (!common_prefix.has_suffix ("_") ||
(value.name.get_char (common_prefix.length).isdigit ()) && (value.name.length - common_prefix.length) <= 1)) {
// enum values may not consist solely of digits
common_prefix = common_prefix.substring (0, common_prefix.length - 1);
}
}
bool is_errordomain = false;
string cheader_filename = null;
var en_attributes = get_attributes (node.name);
if (en_attributes != null) {
foreach (string attr in en_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "common_prefix") {
common_prefix = eval (nv[1]);
} else if (nv[0] == "cheader_filename") {
cheader_filename = eval (nv[1]);
en.set_attribute_string ("CCode", "cheader_filename", cheader_filename);
} else if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return;
}
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
en.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "default_value") {
en.set_attribute_string ("CCode", "default_value", eval (nv[1]));
} else if (nv[0] == "replacement") {
en.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
en.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "rename_to") {
en.name = eval (nv[1]);
} else if (nv[0] == "errordomain") {
if (eval (nv[1]) == "1") {
is_errordomain = true;
}
} else if (nv[0] == "to_string") {
var return_type = new UnresolvedType ();
return_type.unresolved_symbol = new UnresolvedSymbol (null, "string");
return_type.value_owned = false;
var m = new Method ("to_string", return_type, current_source_reference);
m.access = SymbolAccessibility.PUBLIC;
m.set_attribute_string ("CCode", "cname", eval(nv[1]));
en.add_method (m);
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
en.set_attribute ("Experimental", true);
}
}
}
}
en.set_attribute_string ("CCode", "cprefix", common_prefix);
foreach (weak IdlNode value2 in en_node.values) {
EnumValue ev = new EnumValue (value2.name.substring (common_prefix.length), null);
var val_attributes = get_attributes (value2.name);
bool is_hidden = false;
if (val_attributes != null) {
foreach (string attr in val_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "hidden" && eval(nv[1]) == "1") {
is_hidden = true;
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
ev.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
ev.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
ev.set_attribute_string ("Deprecated", "since", eval (nv[1]));
}
}
}
if (!is_hidden) {
en.add_value (ev);
}
}
if (is_errordomain) {
var ed = new ErrorDomain (en.name, current_source_reference);
ed.access = SymbolAccessibility.PUBLIC;
ed.set_attribute_string ("CCode", "cprefix", common_prefix);
if (cheader_filename != null) {
ed.set_attribute_string ("CCode", "cheader_filename", cheader_filename);
}
foreach (EnumValue ev in en.get_values ()) {
ed.add_code (new ErrorCode (ev.name));
}
current_source_file.add_node (ed);
if (!existing) {
add_symbol_to_container (container, ed);
}
} else {
en.set_attribute ("Flags", is_flags);
current_source_file.add_node (en);
if (!existing) {
add_symbol_to_container (container, en);
}
}
}
private void parse_object (IdlNodeInterface node, Symbol container, IdlModule module) {
string name = fix_type_name (((IdlNode) node).name, container);
string base_class = null;
var cl = container.scope.lookup (name) as Class;
if (cl == null) {
cl = new Class (name, current_source_reference);
cl.access = SymbolAccessibility.PUBLIC;
var attributes = get_attributes (node.gtype_name);
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "cheader_filename") {
cl.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "base_class") {
base_class = eval (nv[1]);
} else if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return;
}
} else if (nv[0] == "type_check_function") {
cl.set_attribute_string ("CCode", "type_check_function", eval (nv[1]));
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
cl.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
cl.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
cl.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "type_id") {
cl.set_attribute_string ("CCode", "type_id", eval (nv[1]));
} else if (nv[0] == "abstract") {
if (eval (nv[1]) == "1") {
cl.is_abstract = true;
}
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
cl.set_attribute ("Experimental", true);
}
} else if (nv[0] == "compact") {
if (eval (nv[1]) == "1") {
cl.is_compact = true;
}
} else if (nv[0] == "ref_function") {
cl.set_attribute_string ("CCode", "ref_function", eval (nv[1]));
} else if (nv[0] == "unref_function") {
cl.set_attribute_string ("CCode", "unref_function", eval (nv[1]));
} else if (nv[0] == "copy_function") {
cl.set_attribute_string ("CCode", "copy_function", eval (nv[1]));
} else if (nv[0] == "free_function") {
cl.set_attribute_string ("CCode", "free_function", eval (nv[1]));
}
}
}
add_symbol_to_container (container, cl);
current_source_file.add_node (cl);
}
if (base_class != null) {
var parent = parse_type_string (base_class);
cl.add_base_type (parent);
} else if (node.parent != null) {
if (!cl.is_compact) {
var parent = parse_type_string (node.parent);
cl.add_base_type (parent);
}
} else {
var gobject_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, "GLib"), "Object");
cl.add_base_type (new UnresolvedType.from_symbol (gobject_symbol));
}
foreach (string iface_name in node.interfaces) {
bool skip_iface = false;
var attributes = get_attributes (iface_name);
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
skip_iface = true;
}
}
}
}
if (skip_iface) {
continue;
}
var iface = parse_type_string (iface_name);
cl.add_base_type (iface);
}
current_data_type = cl;
current_type_symbol_set = new HashSet<string> (str_hash, str_equal);
var current_type_func_map = new HashMap<string,weak IdlNodeFunction> (str_hash, str_equal);
var current_type_vfunc_map = new HashMap<string,string> (str_hash, str_equal);
foreach (weak IdlNode member in node.members) {
if (member.type == IdlNodeTypeId.FUNCTION) {
current_type_func_map.set (member.name, (IdlNodeFunction) member);
}
if (member.type == IdlNodeTypeId.VFUNC) {
current_type_vfunc_map.set (member.name, "1");
}
}
foreach (weak IdlNode member in node.members) {
if (member.type == IdlNodeTypeId.FUNCTION) {
// Ignore if vfunc (handled below)
if (!current_type_vfunc_map.contains (member.name)) {
var m = parse_function ((IdlNodeFunction) member);
if (m != null) {
cl.add_method (m);
}
}
} else if (member.type == IdlNodeTypeId.VFUNC) {
var m = parse_virtual ((IdlNodeVFunc) member, current_type_func_map.get (member.name));
if (m != null) {
cl.add_method (m);
}
} else if (member.type == IdlNodeTypeId.PROPERTY) {
var prop = parse_property ((IdlNodeProperty) member);
if (prop != null) {
cl.add_property (prop);
}
} else if (member.type == IdlNodeTypeId.SIGNAL) {
var sig = parse_signal ((IdlNodeSignal) member);
if (sig != null) {
cl.add_signal (sig);
}
}
}
foreach (weak IdlNode member in node.members) {
if (member.type == IdlNodeTypeId.FIELD) {
if (!current_type_symbol_set.contains (member.name)) {
var f = parse_field ((IdlNodeField) member);
if (f != null) {
cl.add_field (f);
}
}
}
}
foreach (Property prop in cl.get_properties ()) {
var getter = "get_%s".printf (prop.name);
if (prop.get_accessor != null && !current_type_symbol_set.contains (getter)) {
prop.set_attribute ("NoAccessorMethod", true);
}
var setter = "set_%s".printf (prop.name);
if (prop.set_accessor != null && prop.set_accessor.writable
&& !current_type_symbol_set.contains (setter)) {
prop.set_attribute ("NoAccessorMethod", true);
}
if (prop.get_attribute ("NoAccessorMethod") != null && prop.get_accessor != null) {
prop.get_accessor.value_type.value_owned = true;
}
}
handle_async_methods (cl);
if (cl.default_construction_method == null) {
// always provide constructor in generated bindings
// to indicate that implicit Object () chainup is allowed
var cm = new CreationMethod (null, null, cl.source_reference);
cm.has_construct_function = false;
cm.access = SymbolAccessibility.PROTECTED;
cl.add_method (cm);
}
current_data_type = null;
current_type_symbol_set = null;
}
private void parse_interface (IdlNodeInterface node, Symbol container, IdlModule module) {
string name = fix_type_name (node.gtype_name, container);
var iface = container.scope.lookup (name) as Interface;
if (iface == null) {
iface = new Interface (name, current_source_reference);
iface.access = SymbolAccessibility.PUBLIC;
var attributes = get_attributes (node.gtype_name);
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "cheader_filename") {
iface.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return;
}
} else if (nv[0] == "type_cname") {
iface.set_attribute_string ("CCode", "type_cname", eval (nv[1]));
} else if (nv[0] == "lower_case_csuffix") {
iface.set_attribute_string ("CCode", "lower_case_csuffix", eval (nv[1]));
}
}
}
foreach (string prereq_name in node.prerequisites) {
var prereq = parse_type_string (prereq_name);
iface.add_prerequisite (prereq);
}
add_symbol_to_container (container, iface);
current_source_file.add_node (iface);
}
current_data_type = iface;
current_type_symbol_set = new HashSet<string> (str_hash, str_equal);
var current_type_func_map = new HashMap<string,weak IdlNodeFunction> (str_hash, str_equal);
var current_type_vfunc_map = new HashMap<string,string> (str_hash, str_equal);
foreach (weak IdlNode member in node.members) {
if (member.type == IdlNodeTypeId.FUNCTION) {
current_type_func_map.set (member.name, (IdlNodeFunction) member);
}
if (member.type == IdlNodeTypeId.VFUNC) {
current_type_vfunc_map.set (member.name, "1");
}
}
foreach (weak IdlNode member in node.members) {
if (member.type == IdlNodeTypeId.FUNCTION) {
// Ignore if vfunc (handled below)
if (!current_type_vfunc_map.contains (member.name)) {
var m = parse_function ((IdlNodeFunction) member, true);
if (m != null) {
iface.add_method (m);
}
}
} else if (member.type == IdlNodeTypeId.VFUNC) {
var m = parse_virtual ((IdlNodeVFunc) member, current_type_func_map.get (member.name), true);
if (m != null) {
iface.add_method (m);
}
} else if (member.type == IdlNodeTypeId.PROPERTY) {
var prop = parse_property ((IdlNodeProperty) member);
if (prop != null) {
iface.add_property (prop);
}
} else if (member.type == IdlNodeTypeId.SIGNAL) {
var sig = parse_signal ((IdlNodeSignal) member);
if (sig != null) {
iface.add_signal (sig);
sig.is_virtual = false;
}
}
}
foreach (Property prop in iface.get_properties ()) {
var getter = "get_%s".printf (prop.name);
if (prop.get_accessor != null && !current_type_symbol_set.contains (getter)) {
prop.set_attribute ("NoAccessorMethod", true);
}
var setter = "set_%s".printf (prop.name);
if (prop.set_accessor != null && prop.set_accessor.writable
&& !current_type_symbol_set.contains (setter)) {
prop.set_attribute ("NoAccessorMethod", true);
}
if (prop.get_attribute ("NoAccessorMethod") != null && prop.get_accessor != null) {
prop.get_accessor.value_type.value_owned = true;
}
}
handle_async_methods (iface);
current_data_type = null;
}
void handle_async_methods (ObjectTypeSymbol type_symbol) {
Set<Method> finish_methods = new HashSet<Method> ();
var methods = type_symbol.get_methods ();
foreach (Method m in methods) {
if (m.coroutine) {
string finish_method_base;
if (m.name.has_suffix ("_async")) {
finish_method_base = m.name.substring (0, m.name.length - "_async".length);
} else {
finish_method_base = m.name;
}
var finish_method = type_symbol.scope.lookup (finish_method_base + "_finish") as Method;
// check if the method is using non-standard finish method name
if (finish_method == null) {
var method_cname = get_finish_cname (m);
foreach (Method method in type_symbol.get_methods ()) {
if (get_cname (method) == method_cname) {
finish_method = method;
break;
}
}
}
if (finish_method != null) {
m.return_type = finish_method.return_type.copy ();
var a = finish_method.get_attribute ("CCode");
if (a != null && a.has_argument ("array_length")) {
m.set_attribute_bool ("CCode", "array_length", a.get_bool ("array_length"));
}
if (a != null && a.has_argument ("array_null_terminated")) {
m.set_attribute_bool ("CCode", "array_null_terminated", a.get_bool ("array_null_terminated"));
}
foreach (var param in finish_method.get_parameters ()) {
if (param.direction == ParameterDirection.OUT) {
var async_param = param.copy ();
if (m.scope.lookup (param.name) != null) {
// parameter name conflict
async_param.name += "_out";
}
m.add_parameter (async_param);
}
}
foreach (DataType error_type in finish_method.get_error_types ()) {
m.add_error_type (error_type.copy ());
}
finish_methods.add (finish_method);
}
}
}
foreach (Method m in finish_methods)
{
type_symbol.scope.remove (m.name);
methods.remove (m);
}
}
private DataType? parse_type (IdlNodeType type_node, out ParameterDirection direction = null) {
direction = ParameterDirection.IN;
var type = new UnresolvedType ();
if (type_node.tag == TypeTag.VOID) {
if (type_node.is_pointer) {
return new PointerType (new VoidType ());
} else {
return new VoidType ();
}
} else if (type_node.tag == TypeTag.BOOLEAN) {
type.unresolved_symbol = new UnresolvedSymbol (null, "bool");
} else if (type_node.tag == TypeTag.INT8) {
type.unresolved_symbol = new UnresolvedSymbol (null, "char");
} else if (type_node.tag == TypeTag.UINT8) {
type.unresolved_symbol = new UnresolvedSymbol (null, "uchar");
} else if (type_node.tag == TypeTag.INT16) {
type.unresolved_symbol = new UnresolvedSymbol (null, "int16");
} else if (type_node.tag == TypeTag.UINT16) {
type.unresolved_symbol = new UnresolvedSymbol (null, "uint16");
} else if (type_node.tag == TypeTag.INT32) {
type.unresolved_symbol = new UnresolvedSymbol (null, "int32");
} else if (type_node.tag == TypeTag.UINT32) {
type.unresolved_symbol = new UnresolvedSymbol (null, "uint32");
} else if (type_node.tag == TypeTag.INT64) {
type.unresolved_symbol = new UnresolvedSymbol (null, "int64");
} else if (type_node.tag == TypeTag.UINT64) {
type.unresolved_symbol = new UnresolvedSymbol (null, "uint64");
} else if (type_node.tag == TypeTag.INT) {
type.unresolved_symbol = new UnresolvedSymbol (null, "int");
} else if (type_node.tag == TypeTag.UINT) {
type.unresolved_symbol = new UnresolvedSymbol (null, "uint");
} else if (type_node.tag == TypeTag.LONG) {
type.unresolved_symbol = new UnresolvedSymbol (null, "long");
} else if (type_node.tag == TypeTag.ULONG) {
type.unresolved_symbol = new UnresolvedSymbol (null, "ulong");
} else if (type_node.tag == TypeTag.SSIZE) {
type.unresolved_symbol = new UnresolvedSymbol (null, "ssize_t");
} else if (type_node.tag == TypeTag.SIZE) {
type.unresolved_symbol = new UnresolvedSymbol (null, "size_t");
} else if (type_node.tag == TypeTag.FLOAT) {
type.unresolved_symbol = new UnresolvedSymbol (null, "float");
} else if (type_node.tag == TypeTag.DOUBLE) {
type.unresolved_symbol = new UnresolvedSymbol (null, "double");
} else if (type_node.tag == TypeTag.UTF8) {
type.unresolved_symbol = new UnresolvedSymbol (null, "string");
} else if (type_node.tag == TypeTag.FILENAME) {
type.unresolved_symbol = new UnresolvedSymbol (null, "string");
} else if (type_node.tag == TypeTag.ARRAY) {
var element_type = parse_type (type_node.parameter_type1);
type = element_type as UnresolvedType;
if (type == null) {
return element_type;
}
element_type.value_owned = true;
return new ArrayType (element_type, 1, element_type.source_reference);
} else if (type_node.tag == TypeTag.LIST) {
type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, "GLib"), "List");
} else if (type_node.tag == TypeTag.SLIST) {
type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, "GLib"), "SList");
} else if (type_node.tag == TypeTag.HASH) {
type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, "GLib"), "HashTable");
} else if (type_node.tag == TypeTag.ERROR) {
type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, "GLib"), "Error");
} else if (type_node.is_interface) {
var n = type_node.@interface;
if (n == "") {
return null;
}
if (n.has_prefix ("const-")) {
n = n.substring ("const-".length);
}
if (type_node.is_pointer &&
(n == "gchar" || n == "char")) {
type.unresolved_symbol = new UnresolvedSymbol (null, "string");
if (type_node.unparsed.has_suffix ("**")) {
direction = ParameterDirection.OUT;
}
} else if (n == "gunichar") {
type.unresolved_symbol = new UnresolvedSymbol (null, "unichar");
} else if (n == "gchar") {
type.unresolved_symbol = new UnresolvedSymbol (null, "char");
} else if (n == "guchar" || n == "guint8") {
type.unresolved_symbol = new UnresolvedSymbol (null, "uchar");
if (type_node.is_pointer) {
type.value_owned = true;
return new ArrayType (type, 1, type.source_reference);
}
} else if (n == "gushort") {
type.unresolved_symbol = new UnresolvedSymbol (null, "ushort");
} else if (n == "gshort") {
type.unresolved_symbol = new UnresolvedSymbol (null, "short");
} else if (n == "gconstpointer" || n == "void") {
return new PointerType (new VoidType ());
} else if (n == "goffset" || n == "off_t") {
type.unresolved_symbol = new UnresolvedSymbol (null, "int64");
} else if (n == "value_array") {
type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, "GLib"), "ValueArray");
} else if (n == "time_t") {
type.unresolved_symbol = new UnresolvedSymbol (null, "ulong");
} else if (n == "socklen_t") {
type.unresolved_symbol = new UnresolvedSymbol (null, "uint32");
} else if (n == "mode_t") {
type.unresolved_symbol = new UnresolvedSymbol (null, "uint");
} else if (n == "gint" || n == "pid_t") {
type.unresolved_symbol = new UnresolvedSymbol (null, "int");
} else if (n == "unsigned" || n == "unsigned-int") {
type.unresolved_symbol = new UnresolvedSymbol (null, "uint");
} else if (n == "FILE") {
type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, "GLib"), "FileStream");
} else if (n == "struct") {
return new PointerType (new VoidType ());
} else if (n == "iconv_t") {
return new PointerType (new VoidType ());
} else if (n == "GType") {
type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, "GLib"), "Type");
if (type_node.is_pointer) {
type.value_owned = true;
return new ArrayType (type, 1, type.source_reference);
}
} else if (n == "GStrv") {
type.unresolved_symbol = new UnresolvedSymbol (null, "string");
type.value_owned = true;
return new ArrayType (type, 1, type.source_reference);
} else {
var named_type = parse_type_string (n);
type = named_type as UnresolvedType;
if (type == null) {
return named_type;
}
if (is_simple_type (n)) {
if (type_node.is_pointer) {
direction = ParameterDirection.OUT;
}
} else if (type_node.unparsed.has_suffix ("**")) {
direction = ParameterDirection.OUT;
}
}
} else {
stdout.printf ("%d\n", type_node.tag);
}
return type;
}
private bool is_simple_type (string type_name) {
var st = cname_type_map[type_name] as Struct;
if (st != null && st.is_simple_type ()) {
return true;
}
return false;
}
private DataType parse_type_string (string n) {
if (n == "va_list") {
// unsupported
return new PointerType (new VoidType ());
}
var type = new UnresolvedType ();
var dt = cname_type_map[n];
if (dt != null) {
UnresolvedSymbol parent_symbol = null;
if (dt.parent_symbol.name != null) {
parent_symbol = new UnresolvedSymbol (null, dt.parent_symbol.name);
}
type.unresolved_symbol = new UnresolvedSymbol (parent_symbol, dt.name);
return type;
}
var type_attributes = get_attributes (n);
string ns_name = null;
if (null != type_attributes) {
foreach (string attr in type_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "cprefix") {
type.unresolved_symbol = new UnresolvedSymbol (null, n.substring (eval (nv[1]).length));
} else if (nv[0] == "name") {
type.unresolved_symbol = new UnresolvedSymbol (null, eval (nv[1]));
} else if (nv[0] == "namespace") {
ns_name = eval (nv[1]);
} else if (nv[0] == "rename_to") {
type.unresolved_symbol = new UnresolvedSymbol (null, eval (nv[1]));
}
}
}
if (type.unresolved_symbol != null) {
if (type.unresolved_symbol.name == "pointer") {
return new PointerType (new VoidType ());
}
if (ns_name != null) {
type.unresolved_symbol.inner = new UnresolvedSymbol (null, ns_name);
}
return type;
}
if (n.has_prefix (current_namespace.name)) {
type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, current_namespace.name), n.substring (current_namespace.name.length));
} else if (current_namespace.parent_symbol != null && current_namespace.parent_symbol.name != null && n.has_prefix (current_namespace.parent_symbol.name)) {
type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, current_namespace.parent_symbol.name), n.substring (current_namespace.parent_symbol.name.length));
} else if (n.has_prefix ("G")) {
type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, "GLib"), n.substring (1));
} else {
var name_parts = n.split (".", 2);
if (name_parts[1] == null) {
type.unresolved_symbol = new UnresolvedSymbol (null, name_parts[0]);
} else {
type.unresolved_symbol = new UnresolvedSymbol (new UnresolvedSymbol (null, name_parts[0]), name_parts[1]);
}
}
return type;
}
private DataType? parse_param (IdlNodeParam param, out ParameterDirection direction = null) {
var type = parse_type (param.type, out direction);
// disable for now as null_ok not yet correctly set
// type.non_null = !param.null_ok;
return type;
}
private UnresolvedSymbol? parse_symbol_from_string (string symbol_string, SourceReference? source_reference = null) {
UnresolvedSymbol? sym = null;
foreach (unowned string s in symbol_string.split (".")) {
sym = new UnresolvedSymbol (sym, s, source_reference);
}
if (sym == null) {
Report.error (source_reference, "a symbol must be specified");
}
return sym;
}
private bool parse_type_arguments_from_string (DataType parent_type, string type_arguments, SourceReference? source_reference = null) {
int type_arguments_length = (int) type_arguments.length;
GLib.StringBuilder current = new GLib.StringBuilder.sized (type_arguments_length);
int depth = 0;
for (var c = 0 ; c < type_arguments_length ; c++) {
if (type_arguments[c] == '<' || type_arguments[c] == '[') {
depth++;
current.append_unichar (type_arguments[c]);
} else if (type_arguments[c] == '>' || type_arguments[c] == ']') {
depth--;
current.append_unichar (type_arguments[c]);
} else if (type_arguments[c] == ',') {
if (depth == 0) {
var dt = parse_type_from_string (current.str, true, source_reference);
if (dt == null) {
return false;
}
parent_type.add_type_argument (dt);
current.truncate ();
} else {
current.append_unichar (type_arguments[c]);
}
} else {
current.append_unichar (type_arguments[c]);
}
}
var dt = parse_type_from_string (current.str, true, source_reference);
if (dt == null) {
return false;
}
parent_type.add_type_argument (dt);
return true;
}
private DataType? parse_type_from_string (string type_string, bool owned_by_default, SourceReference? source_reference = null) {
if (type_from_string_regex == null) {
try {
type_from_string_regex = new GLib.Regex ("^(?:(owned|unowned|weak) +)?([0-9a-zA-Z_\\.]+)(?:<(.+)>)?(\\*+)?(\\[(,*)?\\])?(\\?)?$", GLib.RegexCompileFlags.ANCHORED | GLib.RegexCompileFlags.DOLLAR_ENDONLY | GLib.RegexCompileFlags.OPTIMIZE);
} catch (GLib.RegexError e) {
GLib.error ("Unable to compile regex: %s", e.message);
}
}
GLib.MatchInfo match;
if (!type_from_string_regex.match (type_string, 0, out match)) {
Report.error (source_reference, "unable to parse type");
return null;
}
DataType? type = null;
var ownership_data = match.fetch (1);
var type_name = match.fetch (2);
var type_arguments_data = match.fetch (3);
var pointers_data = match.fetch (4);
var array_data = match.fetch (5);
var array_dimension_data = match.fetch (6);
var nullable_data = match.fetch (7);
var nullable = nullable_data != null && nullable_data.length > 0;
if (ownership_data == null && type_name == "void") {
if (array_data == null && !nullable) {
type = new VoidType (source_reference);
if (pointers_data != null) {
for (int i=0; i < pointers_data.length; i++) {
type = new PointerType (type);
}
}
return type;
} else {
Report.error (source_reference, "invalid void type");
return null;
}
}
bool value_owned = owned_by_default;
if (ownership_data == "owned") {
value_owned = true;
} else if (ownership_data == "unowned") {
value_owned = false;
}
var sym = parse_symbol_from_string (type_name, source_reference);
if (sym == null) {
return null;
}
type = new UnresolvedType.from_symbol (sym, source_reference);
if (type_arguments_data != null && type_arguments_data.length > 0) {
if (!parse_type_arguments_from_string (type, type_arguments_data, source_reference)) {
return null;
}
}
if (pointers_data != null) {
for (int i=0; i < pointers_data.length; i++) {
type = new PointerType (type);
}
}
if (array_data != null && array_data.length > 0) {
type.value_owned = true;
type = new ArrayType (type, array_dimension_data.length + 1, source_reference);
}
type.nullable = nullable;
type.value_owned = value_owned;
return type;
}
private Method? create_method (string name, string symbol, IdlNodeParam? res, GLib.List<IdlNodeParam>? parameters, bool is_constructor, bool is_interface) {
DataType return_type = null;
if (res != null) {
return_type = parse_param (res);
}
Method m;
if (!is_interface && (is_constructor || name.has_prefix ("new"))) {
m = new CreationMethod (null, name, current_source_reference);
m.has_construct_function = false;
if (m.name == "new") {
m.name = null;
} else if (m.name.has_prefix ("new_")) {
m.name = m.name.substring ("new_".length);
}
// For classes, check whether a creation method return type equals to the
// type of the class created. If the types do not match (e.g. in most
// gtk widgets) add an attribute to the creation method indicating the used
// return type.
if (current_data_type is Class && res != null) {
if ("%s*".printf (get_cname (current_data_type)) != res.type.unparsed) {
m.set_attribute_string ("CCode", "type", res.type.unparsed);
}
}
} else {
m = new Method (name, return_type, current_source_reference);
}
m.access = SymbolAccessibility.PUBLIC;
if (current_type_symbol_set != null) {
current_type_symbol_set.add (name);
}
if (current_data_type != null) {
var sig_attributes = get_attributes ("%s::%s".printf (get_cname (current_data_type), name));
if (sig_attributes != null) {
foreach (string attr in sig_attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "has_emitter" && eval (nv[1]) == "1") {
return null;
}
}
}
}
bool add_ellipsis = false;
bool suppress_throws = false;
string? error_types = null;
Symbol? container = null;
var attributes = get_attributes (symbol);
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "name") {
m.name = eval (nv[1]);
} else if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return null;
}
} else if (nv[0] == "ellipsis") {
if (eval (nv[1]) == "1") {
add_ellipsis = true;
}
} else if (nv[0] == "printf_format") {
if (eval (nv[1]) == "1") {
m.set_attribute ("PrintfFormat", true);
}
} else if (nv[0] == "transfer_ownership") {
if (eval (nv[1]) == "1") {
return_type.value_owned = true;
}
} else if (nv[0] == "destroys_instance") {
if (eval (nv[1]) == "1") {
m.set_attribute ("DestroysInstance", true, m.source_reference);
}
} else if (nv[0] == "nullable") {
if (eval (nv[1]) == "1" && !(return_type is VoidType)) {
return_type.nullable = true;
}
} else if (nv[0] == "sentinel") {
m.set_attribute_string ("CCode", "sentinel", eval (nv[1]));
} else if (nv[0] == "is_array") {
if (eval (nv[1]) == "1") {
return_type.value_owned = true;
return_type = new ArrayType (return_type, 1, return_type.source_reference);
m.return_type = return_type;
}
} else if (nv[0] == "is_pointer") {
if (eval (nv[1]) == "1") {
return_type = new PointerType (return_type, return_type.source_reference);
m.return_type = return_type;
}
} else if (nv[0] == "throws") {
if (eval (nv[1]) == "0") {
suppress_throws = true;
}
} else if (nv[0] == "error_types") {
error_types = eval (nv[1]);
} else if (nv[0] == "no_array_length") {
if (eval (nv[1]) == "1") {
m.set_attribute_bool ("CCode", "array_length", false);
}
} else if (nv[0] == "array_null_terminated") {
if (eval (nv[1]) == "1") {
m.set_attribute_bool ("CCode", "array_length", false);
m.set_attribute_bool ("CCode", "array_null_terminated", true);;
}
} else if (nv[0] == "array_length_type") {
m.set_attribute_string ("CCode", "array_length_type", eval (nv[1]));
} else if (nv[0] == "type_name") {
m.return_type = return_type = parse_type_from_string (eval (nv[1]), return_type.value_owned);
} else if (nv[0] == "ctype") {
m.set_attribute_string ("CCode", "type", eval (nv[1]));
} else if (nv[0] == "type_arguments") {
parse_type_arguments_from_string (return_type, eval (nv[1]));
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
m.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
m.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
m.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "cheader_filename") {
m.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "abstract") {
if (eval (nv[1]) == "1") {
m.is_abstract = true;
}
} else if (nv[0] == "virtual") {
if (eval (nv[1]) == "1") {
m.is_virtual = true;
}
} else if (nv[0] == "vfunc_name") {
m.set_attribute_string ("CCode", "vfunc_name", eval (nv[1]));
} else if (nv[0] == "finish_name") {
m.set_attribute_string ("CCode", "finish_name", eval (nv[1]));
} else if (nv[0] == "async") {
if (eval (nv[1]) == "1") {
// force async function, even if it doesn't end in _async
m.coroutine = true;
}
} else if (nv[0] == "parent") {
container = get_container_from_name (eval (nv[1]));
var prefix = get_lower_case_cprefix (container);
if (symbol.has_prefix (prefix)) {
m.name = symbol.substring (prefix.length);
}
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
m.set_attribute ("Experimental", true);
}
} else if (nv[0] == "simple_generics") {
if (eval (nv[1]) == "1") {
m.set_attribute_bool ("CCode", "simple_generics", true);
}
}
}
}
bool first = true;
Parameter last_param = null;
DataType last_param_type = null;
foreach (weak IdlNodeParam param in parameters) {
weak IdlNode param_node = (IdlNode) param;
if (first) {
first = false;
if (!(m is CreationMethod) &&
current_data_type != null &&
param.type.is_interface &&
(param_node.name == "self" ||
param.type.@interface.has_suffix (get_cname (current_data_type)))) {
// instance method
continue;
} else if (!(m is CreationMethod) &&
current_data_type != null &&
param.type.is_interface &&
(param_node.name == "klass" ||
param.type.@interface.has_suffix ("%sClass".printf(get_cname (current_data_type))))) {
// class method
m.binding = MemberBinding.CLASS;
if (m.name.has_prefix ("class_")) {
m.name = m.name.substring ("class_".length, m.name.length - "class_".length);
}
continue;
} else {
// static method
m.binding = MemberBinding.STATIC;
}
}
if (param.type.@interface == "GAsyncReadyCallback" && (symbol.has_suffix ("_async") || m.coroutine)) {
// async method
m.coroutine = true;
continue;
}
// check for GError parameter
if (suppress_throws == false && param_is_exception (param)) {
if (error_types == null)
m.add_error_type (parse_type (param.type));
continue;
}
string param_name = param_node.name;
if (param_name == "result") {
// avoid conflict with generated result variable
param_name = "_result";
} else if (param_name == "string") {
// avoid conflict with string type
param_name = "str";
}
ParameterDirection direction;
var param_type = parse_param (param, out direction);
var p = new Parameter (param_name, param_type);
p.direction = direction;
bool hide_param = false;
bool show_param = false;
bool set_array_length_pos = false;
double array_length_pos = 0;
bool set_delegate_target_pos = false;
double delegate_target_pos = 0;
bool array_requested = false;
bool out_requested = false;
attributes = get_attributes ("%s.%s".printf (symbol, param_node.name));
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "is_array") {
if (eval (nv[1]) == "1") {
param_type.value_owned = true;
param_type = new ArrayType (param_type, 1, param_type.source_reference);
p.variable_type = param_type;
if (!out_requested) {
p.direction = ParameterDirection.IN;
}
array_requested = true;
}
} else if (nv[0] == "is_pointer") {
if (eval (nv[1]) == "1") {
param_type = new PointerType (param_type, return_type.source_reference);
p.variable_type = param_type;
if (!out_requested) {
p.direction = ParameterDirection.IN;
}
}
} else if (nv[0] == "is_out") {
if (eval (nv[1]) == "1") {
p.direction = ParameterDirection.OUT;
out_requested = true;
if (!array_requested && param_type is ArrayType) {
var array_type = (ArrayType) param_type;
param_type = array_type.element_type;
p.variable_type = param_type;
}
}
} else if (nv[0] == "is_ref") {
if (eval (nv[1]) == "1") {
p.direction = ParameterDirection.REF;
if (!array_requested && param_type is ArrayType) {
var array_type = (ArrayType) param_type;
param_type = array_type.element_type;
p.variable_type = param_type;
}
}
} else if (nv[0] == "nullable") {
if (eval (nv[1]) == "1" && !(param_type is VoidType)) {
param_type.nullable = true;
}
} else if (nv[0] == "transfer_ownership") {
if (eval (nv[1]) == "1") {
param_type.value_owned = true;
}
} else if (nv[0] == "takes_ownership") {
if (eval (nv[1]) == "1") {
param_type.value_owned = true;
}
} else if (nv[0] == "value_owned") {
if (eval (nv[1]) == "0") {
param_type.value_owned = false;
} else if (eval (nv[1]) == "1") {
param_type.value_owned = true;
}
} else if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
hide_param = true;
} else if (eval (nv[1]) == "0") {
show_param = true;
}
} else if (nv[0] == "no_array_length") {
if (eval (nv[1]) == "1") {
p.set_attribute_bool ("CCode", "array_length", false);
}
} else if (nv[0] == "array_length_type") {
p.set_attribute_string ("CCode", "array_length_type", eval (nv[1]));
} else if (nv[0] == "array_null_terminated") {
if (eval (nv[1]) == "1") {
p.set_attribute_bool ("CCode", "array_length", false);
p.set_attribute_bool ("CCode", "array_null_terminated", true);
}
} else if (nv[0] == "array_length_pos") {
set_array_length_pos = true;
array_length_pos = double.parse (eval (nv[1]));
} else if (nv[0] == "delegate_target_pos") {
set_delegate_target_pos = true;
delegate_target_pos = double.parse (eval (nv[1]));
} else if (nv[0] == "type_name") {
p.variable_type = param_type = parse_type_from_string (eval (nv[1]), false);
} else if (nv[0] == "ctype") {
p.set_attribute_string ("CCode", "type", eval (nv[1]));
} else if (nv[0] == "type_arguments") {
parse_type_arguments_from_string (param_type, eval (nv[1]));
} else if (nv[0] == "default_value") {
var val = eval (nv[1]);
if (val == "null") {
p.initializer = new NullLiteral (param_type.source_reference);
} else if (val == "true") {
p.initializer = new BooleanLiteral (true, param_type.source_reference);
} else if (val == "false") {
p.initializer = new BooleanLiteral (false, param_type.source_reference);
} else if (val == "") {
p.initializer = new StringLiteral ("\"\"", param_type.source_reference);
} else {
if (int64.try_parse (val)) {
p.initializer = new IntegerLiteral (val, param_type.source_reference);
} else {
if (double.try_parse (val)) {
p.initializer = new RealLiteral (val, param_type.source_reference);
} else {
if (val.has_prefix ("\"") && val.has_suffix ("\"")) {
p.initializer = new StringLiteral (val, param_type.source_reference);
} else {
foreach (var member in val.split (".")) {
p.initializer = new MemberAccess (p.initializer, member, param_type.source_reference);
}
}
}
}
}
}
}
}
if (last_param != null && p.name == "n_" + last_param.name) {
if (!(last_param_type is ArrayType)) {
// last_param is array, p is array length
last_param_type.value_owned = true;
last_param_type = new ArrayType (last_param_type, 1, last_param_type.source_reference);
last_param.variable_type = last_param_type;
last_param.direction = ParameterDirection.IN;
}
// hide array length param
hide_param = true;
} else if (last_param != null && p.name == "user_data") {
// last_param is delegate
// hide deleate target param
hide_param = true;
}
if (show_param || !hide_param) {
m.add_parameter (p);
if (set_array_length_pos) {
p.set_attribute_double ("CCode", "array_length_pos", array_length_pos);
}
if (set_delegate_target_pos) {
p.set_attribute_double ("CCode", "delegate_target_pos", delegate_target_pos);
}
}
last_param = p;
last_param_type = param_type;
}
if (suppress_throws == false && error_types != null) {
var type_args = eval (error_types).split (",");
foreach (string type_arg in type_args) {
m.add_error_type (parse_type_from_string (type_arg, true));
}
}
if (first) {
// no parameters => static method
m.binding = MemberBinding.STATIC;
}
if (last_param != null && last_param.name.has_prefix ("first_")) {
last_param.ellipsis = true;
} else if (add_ellipsis) {
m.add_parameter (new Parameter.with_ellipsis ());
}
if (container == null) {
container = current_data_type;
if (container == null) {
container = current_namespace;
}
}
if (symbol != get_cname (m, container)) {
m.set_attribute_string ("CCode", "cname", symbol);
}
return m;
}
private bool param_is_exception (IdlNodeParam param) {
if (!param.type.is_error) {
return false;
}
var s = param.type.unparsed.chomp ();
if (s.has_suffix ("**")) {
return true;
}
return false;
}
private Method? parse_function (IdlNodeFunction f, bool is_interface = false) {
weak IdlNode node = (IdlNode) f;
if (f.deprecated) {
return null;
}
return create_method (node.name, f.symbol, f.result, f.parameters, f.is_constructor, is_interface);
}
private Method parse_virtual (IdlNodeVFunc v, IdlNodeFunction? func, bool is_interface = false) {
weak IdlNode node = (IdlNode) v;
string symbol = "%s%s".printf (get_lower_case_cprefix (current_data_type), node.name);
if (func != null) {
symbol = func.symbol;
}
Method m = create_method (node.name, symbol, v.result, func != null ? func.parameters : v.parameters, false, is_interface);
if (m != null) {
m.binding = MemberBinding.INSTANCE;
m.is_virtual = !(m.is_abstract || is_interface);
m.is_abstract = m.is_abstract || is_interface;
var attributes = get_attributes (symbol);
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "virtual") {
if (eval (nv[1]) == "0") {
m.is_virtual = false;
m.is_abstract = false;
} else {
m.is_virtual = true;
m.is_abstract = false;
}
}
}
}
if (func == null) {
m.set_attribute ("NoWrapper", true);
}
}
return m;
}
private string fix_prop_name (string name) {
var str = new StringBuilder ();
string i = name;
while (i.length > 0) {
unichar c = i.get_char ();
if (c == '-') {
str.append_c ('_');
} else {
str.append_unichar (c);
}
i = i.next_char ();
}
return str.str;
}
private Property? parse_property (IdlNodeProperty prop_node) {
weak IdlNode node = (IdlNode) prop_node;
if (prop_node.deprecated) {
return null;
}
if (!prop_node.readable && !prop_node.writable) {
// buggy GIDL definition
prop_node.readable = true;
prop_node.writable = true;
}
var prop = new Property (fix_prop_name (node.name), parse_type (prop_node.type), null, null, current_source_reference);
prop.access = SymbolAccessibility.PUBLIC;
prop.interface_only = true;
if (prop_node.type.is_interface && prop_node.type.interface == "GStrv") {
prop.set_attribute_bool ("CCode", "array_length", false);
prop.set_attribute_bool ("CCode", "array_null_terminated", true);
}
if (prop_node.readable) {
prop.get_accessor = new PropertyAccessor (true, false, false, prop.property_type.copy (), null, null);
}
if (prop_node.writable) {
prop.set_accessor = new PropertyAccessor (false, false, false, prop.property_type.copy (), null, null);
if (prop_node.construct_only) {
prop.set_accessor.construction = true;
} else {
prop.set_accessor.writable = true;
prop.set_accessor.construction = prop_node.@construct;
}
}
var attributes = get_attributes ("%s:%s".printf (get_cname (current_data_type), node.name));
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return null;
}
} else if (nv[0] == "type_arguments") {
parse_type_arguments_from_string (prop.property_type, eval (nv[1]));
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
prop.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
prop.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
prop.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "accessor_method") {
if (eval (nv[1]) == "0") {
prop.set_attribute ("NoAccessorMethod", true);
}
} else if (nv[0] == "owned_get") {
if (eval (nv[1]) == "1") {
prop.get_accessor.value_type.value_owned = true;
}
} else if (nv[0] == "type_name") {
prop.property_type = parse_type_from_string (eval (nv[1]), false);
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
prop.set_attribute ("Experimental", true);
}
} else if (nv[0] == "nullable") {
if (eval (nv[1]) == "1" && !(prop.property_type is VoidType)) {
prop.property_type.nullable = true;
}
} else if (nv[0] == "abstract") {
if (eval (nv[1]) == "1") {
prop.is_abstract = true;
}
}
}
}
if (current_type_symbol_set != null) {
current_type_symbol_set.add (prop.name);
}
return prop;
}
private Constant? parse_constant (IdlNodeConstant const_node) {
weak IdlNode node = (IdlNode) const_node;
var type = parse_type (const_node.type);
if (type == null) {
return null;
}
var c = new Constant (node.name, type, null, current_source_reference);
c.external = true;
string[] attributes = get_attributes (node.name);
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "cheader_filename") {
c.set_attribute_string ("CCode", "cheader_filename", eval (nv[1]));
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
c.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
c.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
c.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return null;
}
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
c.set_attribute ("Experimental", true);
}
}
}
}
c.access = SymbolAccessibility.PUBLIC;
return c;
}
private Field? parse_field (IdlNodeField field_node) {
weak IdlNode node = (IdlNode) field_node;
bool unhidden = false;
var type = parse_type (field_node.type);
if (type == null) {
return null;
}
string cheader_filename = null;
string ctype = null;
string array_length_cname = null;
string array_length_type = null;
bool array_null_terminated = false;
bool deprecated = false;
string deprecated_since = null;
string replacement = null;
bool experimental = false;
bool no_delegate_target = false;
var attributes = get_attributes ("%s.%s".printf (get_cname (current_data_type), node.name));
if (attributes != null) {
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return null;
} else {
unhidden = true;
}
} else if (nv[0] == "is_array") {
if (eval (nv[1]) == "1") {
type.value_owned = true;
type = new ArrayType (type, 1, type.source_reference);
}
} else if (nv[0] == "weak") {
if (eval (nv[1]) == "0") {
type.value_owned = true;
}
} else if (nv[0] == "value_owned") {
if (eval (nv[1]) == "0") {
type.value_owned = false;
} else if (eval (nv[1]) == "1") {
type.value_owned = true;
}
} else if (nv[0] == "type_name") {
type = parse_type_from_string (eval (nv[1]), true);
} else if (nv[0] == "type_arguments") {
parse_type_arguments_from_string (type, eval (nv[1]));
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
deprecated = true;
}
} else if (nv[0] == "replacement") {
replacement = eval (nv[1]);
} else if (nv[0] == "deprecated_since") {
deprecated_since = eval (nv[1]);
} else if (nv[0] == "cheader_filename") {
cheader_filename = eval (nv[1]);
} else if (nv[0] == "ctype") {
ctype = eval (nv[1]);
} else if (nv[0] == "array_null_terminated") {
if (eval (nv[1]) == "1") {
array_null_terminated = true;
}
} else if (nv[0] == "array_length_cname") {
array_length_cname = eval (nv[1]);
} else if (nv[0] == "array_length_type") {
array_length_type = eval (nv[1]);
} else if (nv[0] == "no_delegate_target") {
if (eval (nv[1]) == "1") {
no_delegate_target = true;
}
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
experimental = true;
}
}
}
}
if (node.name.has_prefix("_") && !unhidden) {
return null;
}
if (current_type_symbol_set != null) {
current_type_symbol_set.add (node.name);
}
string field_name = node.name;
if (field_name == "string") {
// avoid conflict with string type
field_name = "str";
}
var field = new Field (field_name, type, null, current_source_reference);
field.access = SymbolAccessibility.PUBLIC;
if (field_name != node.name) {
field.set_attribute_string ("CCode", "cname", node.name);
}
if (deprecated) {
field.set_attribute ("Deprecated", true);
if (deprecated_since != null) {
field.set_attribute_string ("Deprecated", "since", deprecated_since);
}
if (replacement != null) {
field.set_attribute_string ("Deprecated", "replacement", replacement);
}
}
if (experimental) {
field.set_attribute ("Experimental", true);
}
if (ctype != null) {
field.set_attribute_string ("CCode", "type", ctype);
}
if (cheader_filename != null) {
field.set_attribute_string ("CCode", "cheader_filename", cheader_filename);
}
if (array_null_terminated) {
field.set_attribute_bool ("CCode", "array_null_terminated", true);
}
if (array_length_cname != null || array_length_type != null) {
if (array_length_cname != null) {
field.set_attribute_string ("CCode", "array_length_cname", array_length_cname);
}
if (array_length_type != null) {
field.set_attribute_string ("CCode", "array_length_type", array_length_type);
}
} else if (field.variable_type is ArrayType) {
field.set_attribute_bool ("CCode", "array_length", false);
}
if (no_delegate_target) {
field.set_attribute_bool ("CCode", "delegate_target", false);
}
return field;
}
private string[]? get_attributes (string codenode) {
var attributes = codenode_attributes_map.get (codenode);
if (attributes == null) {
var dot_required = (-1 != codenode.index_of_char ('.'));
var colon_required = (-1 != codenode.index_of_char (':'));
var pattern_specs = codenode_attributes_patterns.get_keys ();
foreach (PatternSpec* pattern in pattern_specs) {
var pspec = codenode_attributes_patterns[pattern];
if ((dot_required && -1 == pspec.index_of_char ('.')) ||
(colon_required && -1 == pspec.index_of_char (':'))) {
continue;
}
if (pattern->match_string (codenode)) {
return get_attributes (pspec);
}
}
}
if (attributes == null) {
return null;
}
GLib.SList<string> attr_list = new GLib.SList<string> ();
var attr = new GLib.StringBuilder.sized (attributes.length);
var attributes_len = attributes.length;
unowned string remaining = attributes;
bool quoted = false, escaped = false;
for (int b = 0 ; b < attributes_len ; b++) {
unichar c = remaining.get_char ();
if (escaped) {
escaped = false;
attr.append_unichar (c);
} else {
if (c == '"') {
attr.append_unichar (c);
quoted = !quoted;
} else if (c == '\\') {
escaped = true;
} else if (!quoted && (c == ' ')) {
attr_list.prepend (attr.str);
attr.truncate (0);
} else {
attr.append_unichar (c);
}
}
remaining = (string) ((char*) remaining + remaining.index_of_nth_char (1));
}
if (attr.len > 0) {
attr_list.prepend (attr.str);
}
var attrs = new string[attr_list.length ()];
unowned GLib.SList<string>? attr_i = attr_list;
for (int a = 0 ; a < attrs.length ; a++, attr_i = attr_i.next) {
attrs[(attrs.length - 1) - a] = attr_i.data;
}
return attrs;
}
private string eval (string s) {
return ((s.length >= 2) && s.has_prefix ("\"") && s.has_suffix ("\"")) ? s.substring (1, s.length - 2) : s;
}
private Signal? parse_signal (IdlNodeSignal sig_node) {
weak IdlNode node = (IdlNode) sig_node;
if (sig_node.deprecated || sig_node.result == null) {
return null;
}
var sig = new Signal (fix_prop_name (node.name), parse_param (sig_node.result), current_source_reference);
sig.access = SymbolAccessibility.PUBLIC;
var attributes = get_attributes ("%s::%s".printf (get_cname (current_data_type), sig.name));
if (attributes != null) {
string ns_name = null;
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "name") {
sig.set_attribute_string ("CCode", "cname", sig.name);
sig.name = eval (nv[1]);
} else if (nv[0] == "has_emitter" && eval (nv[1]) == "1") {
sig.set_attribute ("HasEmitter", true);
} else if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
return null;
}
} else if (nv[0] == "deprecated") {
if (eval (nv[1]) == "1") {
sig.set_attribute ("Deprecated", true);
}
} else if (nv[0] == "replacement") {
sig.set_attribute_string ("Deprecated", "replacement", eval (nv[1]));
} else if (nv[0] == "deprecated_since") {
sig.set_attribute_string ("Deprecated", "since", eval (nv[1]));
} else if (nv[0] == "transfer_ownership") {
if (eval (nv[1]) == "1") {
sig.return_type.value_owned = true;
}
} else if (nv[0] == "namespace_name") {
ns_name = eval (nv[1]);
} else if (nv[0] == "type_name") {
sig.return_type = parse_type_from_string (eval (nv[1]), false);
} else if (nv[0] == "type_arguments") {
parse_type_arguments_from_string (sig.return_type, eval (nv[1]));
} else if (nv[0] == "experimental") {
if (eval (nv[1]) == "1") {
sig.set_attribute ("Experimental", true);
}
}
}
if (ns_name != null) {
((UnresolvedType) sig.return_type).unresolved_symbol.inner = new UnresolvedSymbol (null, ns_name);
}
}
sig.is_virtual = true;
bool first = true;
foreach (weak IdlNodeParam param in sig_node.parameters) {
if (first) {
// ignore implicit first signal parameter (sender)
first = false;
continue;
}
weak IdlNode param_node = (IdlNode) param;
ParameterDirection direction;
var param_type = parse_param (param, out direction);
var p = new Parameter (param_node.name, param_type);
p.direction = direction;
bool hide_param = false;
bool show_param = false;
attributes = get_attributes ("%s::%s.%s".printf (get_cname (current_data_type), sig.name, param_node.name));
if (attributes != null) {
string ns_name = null;
foreach (string attr in attributes) {
var nv = attr.split ("=", 2);
if (nv[0] == "hidden") {
if (eval (nv[1]) == "1") {
hide_param = true;
} else if (eval (nv[1]) == "0") {
show_param = true;
}
} else if (nv[0] == "is_array") {
if (eval (nv[1]) == "1") {
param_type.value_owned = true;
param_type = new ArrayType (param_type, 1, param_type.source_reference);
p.variable_type = param_type;
p.direction = ParameterDirection.IN;
}
} else if (nv[0] == "no_array_length") {
if (eval (nv[1]) == "1") {
p.set_attribute_bool ("CCode", "array_length", false);
}
} else if (nv[0] == "array_length_type") {
p.set_attribute_string ("CCode", "array_length_type", nv[1]);
} else if (nv[0] == "array_null_terminated") {
if (eval (nv[1]) == "1") {
p.set_attribute_bool ("CCode", "array_length", false);
p.set_attribute_bool ("CCode", "array_null_terminated", true);
}
} else if (nv[0] == "is_out") {
if (eval (nv[1]) == "1") {
p.direction = ParameterDirection.OUT;
}
} else if (nv[0] == "is_ref") {
if (eval (nv[1]) == "1") {
p.direction = ParameterDirection.REF;
}
} else if (nv[0] == "nullable") {
if (eval (nv[1]) == "1" && !(param_type is VoidType)) {
param_type.nullable = true;
}
} else if (nv[0] == "transfer_ownership") {
if (eval (nv[1]) == "1") {
param_type.value_owned = true;
}
} else if (nv[0] == "type_name") {
p.variable_type = param_type = parse_type_from_string (eval (nv[1]), false);
} else if (nv[0] == "type_arguments") {
parse_type_arguments_from_string (p.variable_type, eval (nv[1]));
} else if (nv[0] == "namespace_name") {
ns_name = eval (nv[1]);
}
}
if (ns_name != null) {
((UnresolvedType) param_type).unresolved_symbol.inner = new UnresolvedSymbol (null, ns_name);
}
}
if (show_param || !hide_param) {
sig.add_parameter (p);
}
}
return sig;
}
}
// vim:sw=8 noet
|