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
|
#pragma once
#include "yql_ast.h"
#include "yql_expr_types.h"
#include "yql_type_string.h"
#include "yql_expr_builder.h"
#include "yql_gc_nodes.h"
#include "yql_constraint.h"
#include "yql_pos_handle.h"
#include <ydb/library/yql/utils/yql_panic.h>
#include <ydb/library/yql/public/issue/yql_issue_manager.h>
#include <ydb/library/yql/public/udf/udf_data_type.h>
#include <library/cpp/string_utils/levenshtein_diff/levenshtein_diff.h>
#include <library/cpp/enumbitset/enumbitset.h>
#include <library/cpp/containers/stack_vector/stack_vec.h>
#include <library/cpp/deprecated/enum_codegen/enum_codegen.h>
#include <util/string/builder.h>
#include <util/generic/array_ref.h>
#include <util/generic/deque.h>
#include <util/generic/cast.h>
#include <util/generic/hash.h>
#include <util/generic/maybe.h>
#include <util/generic/bt_exception.h>
#include <util/generic/algorithm.h>
#include <util/digest/murmur.h>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <span>
#include <stack>
//#define YQL_CHECK_NODES_CONSISTENCY
#ifdef YQL_CHECK_NODES_CONSISTENCY
#define ENSURE_NOT_DELETED \
YQL_ENSURE(!Dead(), "Access to dead node # " << UniqueId_ << ": " << Type_ << " '" << ContentUnchecked() << "'");
#define ENSURE_NOT_FROZEN \
YQL_ENSURE(!Frozen(), "Change in frozen node # " << UniqueId_ << ": " << Type_ << " '" << ContentUnchecked() << "'");
#define ENSURE_NOT_FROZEN_CTX \
YQL_ENSURE(!Frozen, "Change in frozen expr context.");
#else
#define ENSURE_NOT_DELETED Y_VERIFY_DEBUG(!Dead(), "Access to dead node # %lu: %d '%s'", UniqueId_, (int)Type_, TString(ContentUnchecked()).data());
#define ENSURE_NOT_FROZEN Y_VERIFY_DEBUG(!Frozen());
#define ENSURE_NOT_FROZEN_CTX Y_VERIFY_DEBUG(!Frozen);
#endif
namespace NYql {
using NUdf::EDataSlot;
class TUnitExprType;
class TMultiExprType;
class TTupleExprType;
class TStructExprType;
class TItemExprType;
class TListExprType;
class TStreamExprType;
class TDataExprType;
class TWorldExprType;
class TOptionalExprType;
class TCallableExprType;
class TResourceExprType;
class TTypeExprType;
class TDictExprType;
class TVoidExprType;
class TNullExprType;
class TGenericExprType;
class TTaggedExprType;
class TErrorExprType;
class TVariantExprType;
class TStreamExprType;
class TFlowExprType;
class TEmptyListExprType;
class TEmptyDictExprType;
const size_t DefaultMistypeDistance = 3;
extern const TStringBuf ZeroString;
struct TTypeAnnotationVisitor {
virtual ~TTypeAnnotationVisitor() = default;
virtual void Visit(const TUnitExprType& type) = 0;
virtual void Visit(const TMultiExprType& type) = 0;
virtual void Visit(const TTupleExprType& type) = 0;
virtual void Visit(const TStructExprType& type) = 0;
virtual void Visit(const TItemExprType& type) = 0;
virtual void Visit(const TListExprType& type) = 0;
virtual void Visit(const TStreamExprType& type) = 0;
virtual void Visit(const TFlowExprType& type) = 0;
virtual void Visit(const TDataExprType& type) = 0;
virtual void Visit(const TWorldExprType& type) = 0;
virtual void Visit(const TOptionalExprType& type) = 0;
virtual void Visit(const TCallableExprType& type) = 0;
virtual void Visit(const TResourceExprType& type) = 0;
virtual void Visit(const TTypeExprType& type) = 0;
virtual void Visit(const TDictExprType& type) = 0;
virtual void Visit(const TVoidExprType& type) = 0;
virtual void Visit(const TNullExprType& type) = 0;
virtual void Visit(const TGenericExprType& type) = 0;
virtual void Visit(const TTaggedExprType& type) = 0;
virtual void Visit(const TErrorExprType& type) = 0;
virtual void Visit(const TVariantExprType& type) = 0;
virtual void Visit(const TEmptyListExprType& type) = 0;
virtual void Visit(const TEmptyDictExprType& type) = 0;
};
enum ETypeAnnotationFlags {
TypeNonComposable = 0x01,
TypeNonPersistable = 0x02,
TypeNonComputable = 0x04,
TypeNonInspectable = 0x08,
TypeNonHashable = 0x10,
TypeNonEquatable = 0x20,
TypeNonComparable = 0x40,
TypeHasNull = 0x80,
TypeHasOptional = 0x100,
TypeHasManyValues = 0x200,
TypeHasBareYson = 0x400,
};
const ui64 TypeHashMagic = 0x10000;
inline ui64 StreamHash(const void* buffer, size_t size, ui64 seed) {
return MurmurHash(buffer, size, seed);
}
inline ui64 StreamHash(ui64 value, ui64 seed) {
return MurmurHash(&value, sizeof(value), seed);
}
void ReportError(TExprContext& ctx, const TIssue& issue);
class TTypeAnnotationNode {
protected:
TTypeAnnotationNode(ETypeAnnotationKind kind, ui32 flags, ui64 hash)
: Kind(kind)
, Flags(flags)
, Hash(hash)
{
}
public:
virtual ~TTypeAnnotationNode() = default;
template <typename T>
const T* Cast() const {
static_assert(std::is_base_of<TTypeAnnotationNode, T>::value,
"Should be derived from TTypeAnnotationNode");
const auto ret = dynamic_cast<const T*>(this);
YQL_ENSURE(ret, "Cannot cast type " << *this << " to " << ETypeAnnotationKind(T::KindValue));
return ret;
}
template <typename T>
const T* UserCast(TPosition pos, TExprContext& ctx) const {
static_assert(std::is_base_of<TTypeAnnotationNode, T>::value,
"Should be derived from TTypeAnnotationNode");
const auto ret = dynamic_cast<const T*>(this);
if (!ret) {
ReportError(ctx, TIssue(pos, TStringBuilder() << "Cannot cast type " << *this << " to " << ETypeAnnotationKind(T::KindValue)));
}
return ret;
}
ETypeAnnotationKind GetKind() const {
return Kind;
}
bool IsComposable() const {
return (GetFlags() & TypeNonComposable) == 0;
}
bool IsPersistable() const {
return (GetFlags() & TypeNonPersistable) == 0;
}
bool IsComputable() const {
return (GetFlags() & TypeNonComputable) == 0;
}
bool IsInspectable() const {
return (GetFlags() & TypeNonInspectable) == 0;
}
bool IsHashable() const {
return IsPersistable() && (GetFlags() & TypeNonHashable) == 0;
}
bool IsEquatable() const {
return IsPersistable() && (GetFlags() & TypeNonEquatable) == 0;
}
bool IsComparable() const {
return IsPersistable() && (GetFlags() & TypeNonComparable) == 0;
}
bool HasNull() const {
return (GetFlags() & TypeHasNull) != 0;
}
bool HasOptional() const {
return (GetFlags() & TypeHasOptional) != 0;
}
bool HasOptionalOrNull() const {
return (GetFlags() & (TypeHasOptional | TypeHasNull)) != 0;
}
bool IsOptionalOrNull() const {
auto kind = GetKind();
return kind == ETypeAnnotationKind::Optional || kind == ETypeAnnotationKind::Null;
}
bool IsSingleton() const {
return (GetFlags() & TypeHasManyValues) == 0;
}
bool HasBareYson() const {
return (GetFlags() & TypeHasBareYson) != 0;
}
ui32 GetFlags() const {
return Flags;
}
ui64 GetHash() const {
return Hash;
}
bool Equals(const TTypeAnnotationNode& node) const;
void Accept(TTypeAnnotationVisitor& visitor) const;
void Out(IOutputStream& out) const {
out << FormatType(this);
}
struct THash {
size_t operator()(const TTypeAnnotationNode* node) const {
return node->GetHash();
}
};
struct TEqual {
bool operator()(const TTypeAnnotationNode* one, const TTypeAnnotationNode* two) const {
return one->Equals(*two);
}
};
typedef std::vector<const TTypeAnnotationNode*> TListType;
typedef std::span<const TTypeAnnotationNode*> TSpanType;
protected:
template <typename T>
static ui32 CombineFlags(const T& items) {
ui32 flags = 0;
for (auto& item : items) {
flags |= item->GetFlags();
}
return flags;
}
private:
const ETypeAnnotationKind Kind;
const ui32 Flags;
const ui64 Hash;
};
class TUnitExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Unit;
TUnitExprType(ui64 hash)
: TTypeAnnotationNode(KindValue,
TypeNonComputable | TypeNonPersistable, hash)
{
}
static ui64 MakeHash() {
return TypeHashMagic | (ui64)ETypeAnnotationKind::Unit;
}
bool operator==(const TUnitExprType& other) const {
Y_UNUSED(other);
return true;
}
};
class TTupleExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Tuple;
TTupleExprType(ui64 hash, const TTypeAnnotationNode::TListType& items)
: TTypeAnnotationNode(KindValue, CombineFlags(items), hash)
, Items(items)
{
}
static ui64 MakeHash(const TTypeAnnotationNode::TListType& items) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Tuple;
hash = StreamHash(items.size(), hash);
for (const auto& item : items) {
hash = StreamHash(item->GetHash(), hash);
}
return hash;
}
size_t GetSize() const {
return Items.size();
}
const TTypeAnnotationNode::TListType& GetItems() const {
return Items;
}
bool operator==(const TTupleExprType& other) const {
if (GetSize() != other.GetSize()) {
return false;
}
for (ui32 i = 0, e = GetSize(); i < e; ++i) {
if (GetItems()[i] != other.GetItems()[i]) {
return false;
}
}
return true;
}
bool Validate(TPosition position, TExprContext& ctx) const;
bool Validate(TPositionHandle position, TExprContext& ctx) const;
private:
TTypeAnnotationNode::TListType Items;
};
class TMultiExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Multi;
TMultiExprType(ui64 hash, const TTypeAnnotationNode::TListType& items)
: TTypeAnnotationNode(KindValue, CombineFlags(items), hash)
, Items(items)
{
}
static ui64 MakeHash(const TTypeAnnotationNode::TListType& items) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Multi;
hash = StreamHash(items.size(), hash);
for (const auto& item : items) {
hash = StreamHash(item->GetHash(), hash);
}
return hash;
}
size_t GetSize() const {
return Items.size();
}
const TTypeAnnotationNode::TListType& GetItems() const {
return Items;
}
bool operator==(const TMultiExprType& other) const {
if (GetSize() != other.GetSize()) {
return false;
}
for (ui32 i = 0, e = GetSize(); i < e; ++i) {
if (GetItems()[i] != other.GetItems()[i]) {
return false;
}
}
return true;
}
bool Validate(TPosition position, TExprContext& ctx) const;
bool Validate(TPositionHandle position, TExprContext& ctx) const;
private:
TTypeAnnotationNode::TListType Items;
};
struct TExprContext;
bool ValidateName(TPosition position, TStringBuf name, TStringBuf descr, TExprContext& ctx);
bool ValidateName(TPositionHandle position, TStringBuf name, TStringBuf descr, TExprContext& ctx);
class TItemExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Item;
TItemExprType(ui64 hash, const TStringBuf& name, const TTypeAnnotationNode* itemType)
: TTypeAnnotationNode(KindValue, itemType->GetFlags(), hash)
, Name(name)
, ItemType(itemType)
{
}
static ui64 MakeHash(const TStringBuf& name, const TTypeAnnotationNode* itemType) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Item;
hash = StreamHash(name.size(), hash);
hash = StreamHash(name.data(), name.size(), hash);
return StreamHash(itemType->GetHash(), hash);
}
bool Validate(TPosition position, TExprContext& ctx) const;
bool Validate(TPositionHandle position, TExprContext& ctx) const;
const TStringBuf& GetName() const {
return Name;
}
const TTypeAnnotationNode* GetItemType() const {
return ItemType;
}
bool operator==(const TItemExprType& other) const {
return GetName() == other.GetName() && GetItemType() == other.GetItemType();
}
private:
const TStringBuf Name;
const TTypeAnnotationNode* ItemType;
};
class TStructExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Struct;
struct TItemLess {
bool operator()(const TItemExprType* x, const TItemExprType* y) const {
return x->GetName() < y->GetName();
};
bool operator()(const TItemExprType* x, const TStringBuf& y) const {
return x->GetName() < y;
};
bool operator()(const TStringBuf& x, const TItemExprType* y) const {
return x < y->GetName();
};
};
TStructExprType(ui64 hash, const TVector<const TItemExprType*>& items)
: TTypeAnnotationNode(KindValue, TypeNonComparable | CombineFlags(items), hash)
, Items(items)
{
}
static ui64 MakeHash(const TVector<const TItemExprType*>& items) {
Y_VERIFY_DEBUG(IsSorted(items.begin(), items.end(), TItemLess()));
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Struct;
hash = StreamHash(items.size(), hash);
for (const auto& item : items) {
hash = StreamHash(item->GetHash(), hash);
}
return hash;
}
bool Validate(TPosition position, TExprContext& ctx) const;
bool Validate(TPositionHandle position, TExprContext& ctx) const;
size_t GetSize() const {
return Items.size();
}
const TVector<const TItemExprType*>& GetItems() const {
return Items;
}
TMaybe<ui32> FindItem(const TStringBuf& name) const {
auto it = LowerBound(Items.begin(), Items.end(), name, TItemLess());
if (it == Items.end() || (*it)->GetName() != name) {
return TMaybe<ui32>();
}
return it - Items.begin();
}
const TTypeAnnotationNode* FindItemType(const TStringBuf& name) const {
const auto it = LowerBound(Items.begin(), Items.end(), name, TItemLess());
if (it == Items.end() || (*it)->GetName() != name) {
return nullptr;
}
return (*it)->GetItemType();
}
TMaybe<TStringBuf> FindMistype(const TStringBuf& name) const {
for (const auto& item: Items) {
if (NLevenshtein::Distance(name, item->GetName()) < DefaultMistypeDistance) {
return item->GetName();
}
}
return TMaybe<TStringBuf>();
}
bool operator==(const TStructExprType& other) const {
if (GetSize() != other.GetSize()) {
return false;
}
for (ui32 i = 0, e = GetSize(); i < e; ++i) {
if (GetItems()[i] != other.GetItems()[i]) {
return false;
}
}
return true;
}
private:
TVector<const TItemExprType*> Items;
};
class TListExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::List;
TListExprType(ui64 hash, const TTypeAnnotationNode* itemType)
: TTypeAnnotationNode(KindValue, itemType->GetFlags(), hash)
, ItemType(itemType)
{
}
static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::List;
return StreamHash(itemType->GetHash(), hash);
}
const TTypeAnnotationNode* GetItemType() const {
return ItemType;
}
bool operator==(const TListExprType& other) const {
return GetItemType() == other.GetItemType();
}
private:
const TTypeAnnotationNode* ItemType;
};
class TStreamExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Stream;
TStreamExprType(ui64 hash, const TTypeAnnotationNode* itemType)
: TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeNonPersistable, hash)
, ItemType(itemType)
{
}
static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Stream;
return StreamHash(itemType->GetHash(), hash);
}
const TTypeAnnotationNode* GetItemType() const {
return ItemType;
}
bool operator==(const TStreamExprType& other) const {
return GetItemType() == other.GetItemType();
}
private:
const TTypeAnnotationNode* ItemType;
};
class TFlowExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Flow;
TFlowExprType(ui64 hash, const TTypeAnnotationNode* itemType)
: TTypeAnnotationNode(KindValue, itemType->GetFlags() | TypeNonPersistable, hash)
, ItemType(itemType)
{
}
static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Flow;
return StreamHash(itemType->GetHash(), hash);
}
const TTypeAnnotationNode* GetItemType() const {
return ItemType;
}
bool operator==(const TFlowExprType& other) const {
return GetItemType() == other.GetItemType();
}
private:
const TTypeAnnotationNode* ItemType;
};
class TDataExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Data;
TDataExprType(ui64 hash, EDataSlot slot)
: TTypeAnnotationNode(KindValue, GetFlags(slot), hash)
, Slot(slot)
{
}
static ui32 GetFlags(EDataSlot slot) {
ui32 ret = TypeHasManyValues;
auto props = NUdf::GetDataTypeInfo(slot).Features;
if (!(props & NUdf::CanHash)) {
ret |= TypeNonHashable;
}
if (!(props & NUdf::CanEquate)) {
ret |= TypeNonEquatable;
}
if (!(props & NUdf::CanCompare)) {
ret |= TypeNonComparable;
}
if (slot == NUdf::EDataSlot::Yson) {
ret |= TypeHasBareYson;
}
return ret;
}
static ui64 MakeHash(EDataSlot slot) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Data;
auto dataType = NUdf::GetDataTypeInfo(slot).Name;
hash = StreamHash(dataType.size(), hash);
return StreamHash(dataType.data(), dataType.size(), hash);
}
EDataSlot GetSlot() const {
return Slot;
}
TStringBuf GetName() const {
return NUdf::GetDataTypeInfo(Slot).Name;
}
bool operator==(const TDataExprType& other) const {
return Slot == other.Slot;
}
private:
EDataSlot Slot;
};
class TDataExprParamsType : public TDataExprType {
public:
TDataExprParamsType(ui64 hash, EDataSlot slot, const TStringBuf& one, const TStringBuf& two)
: TDataExprType(hash, slot), One(one), Two(two)
{}
static ui64 MakeHash(EDataSlot slot, const TStringBuf& one, const TStringBuf& two) {
auto hash = TDataExprType::MakeHash(slot);
hash = StreamHash(one.size(), hash);
hash = StreamHash(one.data(), one.size(), hash);
hash = StreamHash(two.size(), hash);
hash = StreamHash(two.data(), two.size(), hash);
return hash;
}
const TStringBuf& GetParamOne() const {
return One;
}
const TStringBuf& GetParamTwo() const {
return Two;
}
bool operator==(const TDataExprParamsType& other) const {
return GetSlot() == other.GetSlot() && GetParamOne() == other.GetParamOne() && GetParamTwo() == other.GetParamTwo();
}
bool Validate(TPosition position, TExprContext& ctx) const;
bool Validate(TPositionHandle position, TExprContext& ctx) const;
private:
const TStringBuf One, Two;
};
class TWorldExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::World;
TWorldExprType(ui64 hash)
: TTypeAnnotationNode(KindValue,
TypeNonComposable | TypeNonComputable | TypeNonPersistable | TypeNonInspectable, hash)
{
}
static ui64 MakeHash() {
return TypeHashMagic | (ui64)ETypeAnnotationKind::World;
}
bool operator==(const TWorldExprType& other) const {
Y_UNUSED(other);
return true;
}
};
class TOptionalExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Optional;
TOptionalExprType(ui64 hash, const TTypeAnnotationNode* itemType)
: TTypeAnnotationNode(KindValue, GetFlags(itemType), hash)
, ItemType(itemType)
{
}
static ui32 GetFlags(const TTypeAnnotationNode* itemType) {
auto ret = TypeHasOptional | itemType->GetFlags();
if (itemType->GetKind() == ETypeAnnotationKind::Data &&
itemType->Cast<TDataExprType>()->GetSlot() == NUdf::EDataSlot::Yson) {
ret = ret & ~TypeHasBareYson;
}
return ret;
}
static ui64 MakeHash(const TTypeAnnotationNode* itemType) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Optional;
return StreamHash(itemType->GetHash(), hash);
}
const TTypeAnnotationNode* GetItemType() const {
return ItemType;
}
bool operator==(const TOptionalExprType& other) const {
return GetItemType() == other.GetItemType();
}
private:
const TTypeAnnotationNode* ItemType;
};
class TVariantExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Variant;
TVariantExprType(ui64 hash, const TTypeAnnotationNode* underlyingType)
: TTypeAnnotationNode(KindValue, MakeFlags(underlyingType), hash)
, UnderlyingType(underlyingType)
{
}
static ui64 MakeHash(const TTypeAnnotationNode* underlyingType) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Variant;
return StreamHash(underlyingType->GetHash(), hash);
}
const TTypeAnnotationNode* GetUnderlyingType() const {
return UnderlyingType;
}
bool operator==(const TVariantExprType& other) const {
return GetUnderlyingType() == other.GetUnderlyingType();
}
bool Validate(TPosition position, TExprContext& ctx) const;
bool Validate(TPositionHandle position, TExprContext& ctx) const;
static ui32 MakeFlags(const TTypeAnnotationNode* underlyingType);
private:
const TTypeAnnotationNode* UnderlyingType;
};
class TTypeExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Type;
TTypeExprType(ui64 hash, const TTypeAnnotationNode* type)
: TTypeAnnotationNode(KindValue, TypeNonPersistable | TypeNonComputable, hash)
, Type(type)
{
}
static ui64 MakeHash(const TTypeAnnotationNode* type) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Type;
return StreamHash(type->GetHash(), hash);
}
const TTypeAnnotationNode* GetType() const {
return Type;
}
bool operator==(const TTypeExprType& other) const {
return GetType() == other.GetType();
}
private:
const TTypeAnnotationNode* Type;
};
class TDictExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Dict;
TDictExprType(ui64 hash, const TTypeAnnotationNode* keyType, const TTypeAnnotationNode* payloadType)
: TTypeAnnotationNode(KindValue, TypeNonComparable | keyType->GetFlags() | payloadType->GetFlags(), hash)
, KeyType(keyType)
, PayloadType(payloadType)
{
}
static ui64 MakeHash(const TTypeAnnotationNode* keyType, const TTypeAnnotationNode* payloadType) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Dict;
return StreamHash(StreamHash(keyType->GetHash(), hash), payloadType->GetHash());
}
bool Validate(TPosition position, TExprContext& ctx) const;
bool Validate(TPositionHandle position, TExprContext& ctx) const;
const TTypeAnnotationNode* GetKeyType() const {
return KeyType;
}
const TTypeAnnotationNode* GetPayloadType() const {
return PayloadType;
}
bool operator==(const TDictExprType& other) const {
return GetKeyType() == other.GetKeyType() &&
GetPayloadType() == other.GetPayloadType();
}
private:
const TTypeAnnotationNode* KeyType;
const TTypeAnnotationNode* PayloadType;
};
class TVoidExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Void;
TVoidExprType(ui64 hash)
: TTypeAnnotationNode(KindValue, 0, hash)
{
}
static ui64 MakeHash() {
return TypeHashMagic | (ui64)ETypeAnnotationKind::Void;
}
bool operator==(const TVoidExprType& other) const {
Y_UNUSED(other);
return true;
}
};
class TNullExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Null;
TNullExprType(ui64 hash)
: TTypeAnnotationNode(KindValue, TypeHasNull, hash)
{
}
static ui64 MakeHash() {
return TypeHashMagic | (ui64)ETypeAnnotationKind::Null;
}
bool operator==(const TNullExprType& other) const {
Y_UNUSED(other);
return true;
}
};
struct TArgumentFlags {
enum {
AutoMap = 0x01,
};
};
class TCallableExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Callable;
struct TArgumentInfo {
const TTypeAnnotationNode* Type = nullptr;
TStringBuf Name;
ui64 Flags = 0;
bool operator==(const TArgumentInfo& other) const {
return Type == other.Type && Name == other.Name && Flags == other.Flags;
}
bool operator!=(const TArgumentInfo& other) const {
return !(*this == other);
}
};
TCallableExprType(ui64 hash, const TTypeAnnotationNode* returnType, const TVector<TArgumentInfo>& arguments
, size_t optionalArgumentsCount, const TStringBuf& payload)
: TTypeAnnotationNode(KindValue, MakeFlags(returnType), hash)
, ReturnType(returnType)
, Arguments(arguments)
, OptionalArgumentsCount(optionalArgumentsCount)
, Payload(payload)
{
for (ui32 i = 0; i < Arguments.size(); ++i) {
const auto& arg = Arguments[i];
if (!arg.Name.empty()) {
IndexByName.insert({ arg.Name, i });
}
}
}
static ui64 MakeHash(const TTypeAnnotationNode* returnType, const TVector<TArgumentInfo>& arguments
, size_t optionalArgumentsCount, const TStringBuf& payload) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Callable;
hash = StreamHash(returnType->GetHash(), hash);
hash = StreamHash(arguments.size(), hash);
for (const auto& arg : arguments) {
hash = StreamHash(arg.Name.size(), hash);
hash = StreamHash(arg.Name.data(), arg.Name.size(), hash);
hash = StreamHash(arg.Flags, hash);
hash = StreamHash(arg.Type->GetHash(), hash);
}
hash = StreamHash(optionalArgumentsCount, hash);
hash = StreamHash(payload.size(), hash);
hash = StreamHash(payload.data(), payload.size(), hash);
return hash;
}
const TTypeAnnotationNode* GetReturnType() const {
return ReturnType;
}
size_t GetOptionalArgumentsCount() const {
return OptionalArgumentsCount;
}
const TStringBuf& GetPayload() const {
return Payload;
}
size_t GetArgumentsSize() const {
return Arguments.size();
}
const TVector<TArgumentInfo>& GetArguments() const {
return Arguments;
}
bool operator==(const TCallableExprType& other) const {
if (GetArgumentsSize() != other.GetArgumentsSize()) {
return false;
}
if (GetOptionalArgumentsCount() != other.GetOptionalArgumentsCount()) {
return false;
}
if (GetReturnType() != other.GetReturnType()) {
return false;
}
for (ui32 i = 0, e = GetArgumentsSize(); i < e; ++i) {
if (GetArguments()[i] != other.GetArguments()[i]) {
return false;
}
}
return true;
}
bool Validate(TPosition position, TExprContext& ctx) const;
bool Validate(TPositionHandle position, TExprContext& ctx) const;
TMaybe<ui32> ArgumentIndexByName(const TStringBuf& name) const {
auto it = IndexByName.find(name);
if (it == IndexByName.end()) {
return {};
}
return it->second;
}
private:
static ui32 MakeFlags(const TTypeAnnotationNode* returnType) {
ui32 flags = TypeNonPersistable;
flags |= returnType->GetFlags();
return flags;
}
private:
const TTypeAnnotationNode* ReturnType;
TVector<TArgumentInfo> Arguments;
const size_t OptionalArgumentsCount;
const TStringBuf Payload;
THashMap<TStringBuf, ui32> IndexByName;
};
class TGenericExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Generic;
TGenericExprType(ui64 hash)
: TTypeAnnotationNode(KindValue, TypeNonComputable, hash)
{
}
static ui64 MakeHash() {
return TypeHashMagic | (ui64)ETypeAnnotationKind::Generic;
}
bool operator==(const TGenericExprType& other) const {
Y_UNUSED(other);
return true;
}
};
class TResourceExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Resource;
TResourceExprType(ui64 hash, const TStringBuf& tag)
: TTypeAnnotationNode(KindValue, TypeNonPersistable | TypeHasManyValues, hash)
, Tag(tag)
{}
static ui64 MakeHash(const TStringBuf& tag) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Resource;
hash = StreamHash(tag.size(), hash);
return StreamHash(tag.data(), tag.size(), hash);
}
const TStringBuf& GetTag() const {
return Tag;
}
bool operator==(const TResourceExprType& other) const {
return Tag == other.Tag;
}
private:
const TStringBuf Tag;
};
class TTaggedExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Tagged;
TTaggedExprType(ui64 hash, const TTypeAnnotationNode* baseType, const TStringBuf& tag)
: TTypeAnnotationNode(KindValue, baseType->GetFlags(), hash)
, BaseType(baseType)
, Tag(tag)
{}
static ui64 MakeHash(const TTypeAnnotationNode* baseType, const TStringBuf& tag) {
ui64 hash = TypeHashMagic | (ui64)ETypeAnnotationKind::Tagged;
hash = StreamHash(baseType->GetHash(), hash);
hash = StreamHash(tag.size(), hash);
return StreamHash(tag.data(), tag.size(), hash);
}
const TStringBuf& GetTag() const {
return Tag;
}
const TTypeAnnotationNode* GetBaseType() const {
return BaseType;
}
bool operator==(const TTaggedExprType& other) const {
return Tag == other.Tag && GetBaseType() == other.GetBaseType();
}
bool Validate(TPosition position, TExprContext& ctx) const;
bool Validate(TPositionHandle position, TExprContext& ctx) const;
private:
const TTypeAnnotationNode* BaseType;
const TStringBuf Tag;
};
class TErrorExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::Error;
TErrorExprType(ui64 hash, const TIssue& error)
: TTypeAnnotationNode(KindValue, 0, hash)
, Error(error)
{}
static ui64 MakeHash(const TIssue& error) {
return error.Hash();
}
const TIssue& GetError() const {
return Error;
}
bool operator==(const TErrorExprType& other) const {
return Error == other.Error;
}
private:
const TIssue Error;
};
class TEmptyListExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::EmptyList;
TEmptyListExprType(ui64 hash)
: TTypeAnnotationNode(KindValue, 0, hash)
{
}
static ui64 MakeHash() {
return TypeHashMagic | (ui64)ETypeAnnotationKind::EmptyList;
}
bool operator==(const TEmptyListExprType& other) const {
Y_UNUSED(other);
return true;
}
};
class TEmptyDictExprType : public TTypeAnnotationNode {
public:
static constexpr ETypeAnnotationKind KindValue = ETypeAnnotationKind::EmptyDict;
TEmptyDictExprType(ui64 hash)
: TTypeAnnotationNode(KindValue, 0, hash)
{
}
static ui64 MakeHash() {
return TypeHashMagic | (ui64)ETypeAnnotationKind::EmptyDict;
}
bool operator==(const TEmptyDictExprType& other) const {
Y_UNUSED(other);
return true;
}
};
inline bool TTypeAnnotationNode::Equals(const TTypeAnnotationNode& node) const {
if (this == &node) {
return true;
}
if (Hash != node.GetHash()) {
return false;
}
if (Kind != node.GetKind()) {
return false;
}
switch (Kind) {
case ETypeAnnotationKind::Unit:
return static_cast<const TUnitExprType&>(*this) == static_cast<const TUnitExprType&>(node);
case ETypeAnnotationKind::Tuple:
return static_cast<const TTupleExprType&>(*this) == static_cast<const TTupleExprType&>(node);
case ETypeAnnotationKind::Struct:
return static_cast<const TStructExprType&>(*this) == static_cast<const TStructExprType&>(node);
case ETypeAnnotationKind::Item:
return static_cast<const TItemExprType&>(*this) == static_cast<const TItemExprType&>(node);
case ETypeAnnotationKind::List:
return static_cast<const TListExprType&>(*this) == static_cast<const TListExprType&>(node);
case ETypeAnnotationKind::Data:
return static_cast<const TDataExprType&>(*this) == static_cast<const TDataExprType&>(node);
case ETypeAnnotationKind::World:
return static_cast<const TWorldExprType&>(*this) == static_cast<const TWorldExprType&>(node);
case ETypeAnnotationKind::Optional:
return static_cast<const TOptionalExprType&>(*this) == static_cast<const TOptionalExprType&>(node);
case ETypeAnnotationKind::Type:
return static_cast<const TTypeExprType&>(*this) == static_cast<const TTypeExprType&>(node);
case ETypeAnnotationKind::Dict:
return static_cast<const TDictExprType&>(*this) == static_cast<const TDictExprType&>(node);
case ETypeAnnotationKind::Void:
return static_cast<const TVoidExprType&>(*this) == static_cast<const TVoidExprType&>(node);
case ETypeAnnotationKind::Null:
return static_cast<const TNullExprType&>(*this) == static_cast<const TNullExprType&>(node);
case ETypeAnnotationKind::Callable:
return static_cast<const TCallableExprType&>(*this) == static_cast<const TCallableExprType&>(node);
case ETypeAnnotationKind::Generic:
return static_cast<const TGenericExprType&>(*this) == static_cast<const TGenericExprType&>(node);
case ETypeAnnotationKind::Resource:
return static_cast<const TResourceExprType&>(*this) == static_cast<const TResourceExprType&>(node);
case ETypeAnnotationKind::Tagged:
return static_cast<const TTaggedExprType&>(*this) == static_cast<const TTaggedExprType&>(node);
case ETypeAnnotationKind::Error:
return static_cast<const TErrorExprType&>(*this) == static_cast<const TErrorExprType&>(node);
case ETypeAnnotationKind::Variant:
return static_cast<const TVariantExprType&>(*this) == static_cast<const TVariantExprType&>(node);
case ETypeAnnotationKind::Stream:
return static_cast<const TStreamExprType&>(*this) == static_cast<const TStreamExprType&>(node);
case ETypeAnnotationKind::Flow:
return static_cast<const TFlowExprType&>(*this) == static_cast<const TFlowExprType&>(node);
case ETypeAnnotationKind::EmptyList:
return static_cast<const TEmptyListExprType&>(*this) == static_cast<const TEmptyListExprType&>(node);
case ETypeAnnotationKind::EmptyDict:
return static_cast<const TEmptyDictExprType&>(*this) == static_cast<const TEmptyDictExprType&>(node);
case ETypeAnnotationKind::Multi:
return static_cast<const TMultiExprType&>(*this) == static_cast<const TMultiExprType&>(node);
case ETypeAnnotationKind::LastType:
YQL_ENSURE(false, "Incorrect type");
}
return false;
}
inline void TTypeAnnotationNode::Accept(TTypeAnnotationVisitor& visitor) const {
switch (Kind) {
case ETypeAnnotationKind::Unit:
return visitor.Visit(static_cast<const TUnitExprType&>(*this));
case ETypeAnnotationKind::Tuple:
return visitor.Visit(static_cast<const TTupleExprType&>(*this));
case ETypeAnnotationKind::Struct:
return visitor.Visit(static_cast<const TStructExprType&>(*this));
case ETypeAnnotationKind::Item:
return visitor.Visit(static_cast<const TItemExprType&>(*this));
case ETypeAnnotationKind::List:
return visitor.Visit(static_cast<const TListExprType&>(*this));
case ETypeAnnotationKind::Data:
return visitor.Visit(static_cast<const TDataExprType&>(*this));
case ETypeAnnotationKind::World:
return visitor.Visit(static_cast<const TWorldExprType&>(*this));
case ETypeAnnotationKind::Optional:
return visitor.Visit(static_cast<const TOptionalExprType&>(*this));
case ETypeAnnotationKind::Type:
return visitor.Visit(static_cast<const TTypeExprType&>(*this));
case ETypeAnnotationKind::Dict:
return visitor.Visit(static_cast<const TDictExprType&>(*this));
case ETypeAnnotationKind::Void:
return visitor.Visit(static_cast<const TVoidExprType&>(*this));
case ETypeAnnotationKind::Null:
return visitor.Visit(static_cast<const TNullExprType&>(*this));
case ETypeAnnotationKind::Callable:
return visitor.Visit(static_cast<const TCallableExprType&>(*this));
case ETypeAnnotationKind::Generic:
return visitor.Visit(static_cast<const TGenericExprType&>(*this));
case ETypeAnnotationKind::Resource:
return visitor.Visit(static_cast<const TResourceExprType&>(*this));
case ETypeAnnotationKind::Tagged:
return visitor.Visit(static_cast<const TTaggedExprType&>(*this));
case ETypeAnnotationKind::Error:
return visitor.Visit(static_cast<const TErrorExprType&>(*this));
case ETypeAnnotationKind::Variant:
return visitor.Visit(static_cast<const TVariantExprType&>(*this));
case ETypeAnnotationKind::Stream:
return visitor.Visit(static_cast<const TStreamExprType&>(*this));
case ETypeAnnotationKind::Flow:
return visitor.Visit(static_cast<const TFlowExprType&>(*this));
case ETypeAnnotationKind::EmptyList:
return visitor.Visit(static_cast<const TEmptyListExprType&>(*this));
case ETypeAnnotationKind::EmptyDict:
return visitor.Visit(static_cast<const TEmptyDictExprType&>(*this));
case ETypeAnnotationKind::Multi:
return visitor.Visit(static_cast<const TMultiExprType&>(*this));
case ETypeAnnotationKind::LastType:
YQL_ENSURE(false, "Incorrect type");
}
}
class TExprNode {
friend class TExprNodeBuilder;
friend class TExprNodeReplaceBuilder;
friend struct TExprContext;
private:
struct TExprFlags {
enum : ui16 {
Default = 0,
Dead = 0x01,
Frozen = 0x02,
};
static constexpr ui32 FlagsMask = 0x03; // all flags should fit here
};
public:
typedef TIntrusivePtr<TExprNode> TPtr;
typedef std::vector<TPtr> TListType;
typedef TArrayRef<const TPtr> TChildrenType;
struct TPtrHash : private std::hash<const TExprNode*> {
size_t operator()(const TPtr& p) const {
return std::hash<const TExprNode*>::operator()(p.Get());
}
};
#define YQL_EXPR_NODE_TYPE_MAP(xx) \
xx(List, 0) \
xx(Atom, 1) \
xx(Callable, 2) \
xx(Lambda, 3) \
xx(Argument, 4) \
xx(Arguments, 5) \
xx(World, 7)
enum EType : ui8 {
YQL_EXPR_NODE_TYPE_MAP(ENUM_VALUE_GEN)
};
static constexpr ui32 TypeMask = 0x07; // all types should fit here
#define YQL_EXPR_NODE_STATE_MAP(xx) \
xx(Initial, 0) \
xx(TypeInProgress, 1) \
xx(TypePending, 2) \
xx(TypeComplete, 3) \
xx(ConstrInProgress, 4) \
xx(ConstrPending, 5) \
xx(ConstrComplete, 6) \
xx(ExecutionRequired, 7) \
xx(ExecutionInProgress, 8) \
xx(ExecutionPending, 9) \
xx(ExecutionComplete, 10) \
xx(Error, 11) \
xx(Last, 12)
enum class EState : ui8 {
YQL_EXPR_NODE_STATE_MAP(ENUM_VALUE_GEN)
};
static TPtr GetResult(const TPtr& node) {
return node->Type() == Callable ? node->Result : node;
}
const TExprNode& GetResult() const {
ENSURE_NOT_DELETED
return Type() == Callable ? *Result : *this;
}
bool HasResult() const {
ENSURE_NOT_DELETED
return Type() != Callable || bool(Result);
}
void SetResult(TPtr&& result) {
ENSURE_NOT_DELETED
ENSURE_NOT_FROZEN
Result = std::move(result);
}
bool IsCallable(const TStringBuf& name) const {
ENSURE_NOT_DELETED
return Type() == TExprNode::Callable && Content() == name;
}
bool IsCallable(const std::initializer_list<std::string_view>& names) const {
ENSURE_NOT_DELETED
return Type() == TExprNode::Callable && names.end() != std::find(names.begin(), names.end(), Content());
}
template <class TKey>
bool IsCallable(const THashSet<TKey>& names) const {
ENSURE_NOT_DELETED
return Type() == TExprNode::Callable && names.contains(Content());
}
bool IsCallable() const {
ENSURE_NOT_DELETED
return Type() == TExprNode::Callable;
}
bool IsAtom() const {
ENSURE_NOT_DELETED
return Type() == TExprNode::Atom;
}
bool IsWorld() const {
ENSURE_NOT_DELETED
return Type() == TExprNode::World;
}
bool IsAtom(const TStringBuf& content) const {
ENSURE_NOT_DELETED
return Type() == TExprNode::Atom && Content() == content;
}
bool IsList() const {
ENSURE_NOT_DELETED
return Type() == TExprNode::List;
}
bool IsLambda() const {
ENSURE_NOT_DELETED
return Type() == TExprNode::Lambda;
}
bool IsArgument() const {
ENSURE_NOT_DELETED
return Type() == TExprNode::Argument;
}
bool IsComposable() const {
ENSURE_NOT_DELETED
return !IsLambda() && TypeAnnotation_->IsComposable();
}
bool IsPersistable() const {
ENSURE_NOT_DELETED
return !IsLambda() && TypeAnnotation_->IsPersistable();
}
bool IsComputable() const {
ENSURE_NOT_DELETED
return !IsLambda() && TypeAnnotation_->IsComputable();
}
bool IsInspectable() const {
ENSURE_NOT_DELETED
return !IsLambda() && TypeAnnotation_->IsInspectable();
}
bool ForDisclosing() const {
ENSURE_NOT_DELETED
return Type() == TExprNode::List && ShallBeDisclosed;
}
void SetDisclosing() {
ENSURE_NOT_DELETED
Y_ENSURE(Type() == TExprNode::List, "Must be list.");
ShallBeDisclosed = true;
}
ui32 GetFlagsToCompare() const {
ENSURE_NOT_DELETED
ui32 ret = Flags();
if ((ret & TNodeFlags::BinaryContent) == 0) {
ret |= TNodeFlags::ArbitraryContent | TNodeFlags::MultilineContent;
}
return ret;
}
TString Dump() const;
bool StartsExecution() const {
ENSURE_NOT_DELETED
return State == EState::ExecutionComplete
|| State == EState::ExecutionInProgress
|| State == EState::ExecutionRequired
|| State == EState::ExecutionPending;
}
bool IsComplete() const {
YQL_ENSURE(HasLambdaScope);
return !OuterLambda;
}
void Ref() {
ENSURE_NOT_DELETED
ENSURE_NOT_FROZEN
Y_ENSURE(RefCount_ < Max<ui32>());
++RefCount_;
}
void UnRef() {
ENSURE_NOT_DELETED
ENSURE_NOT_FROZEN
if (!--RefCount_) {
Result.Reset();
Children_.clear();
Constraints_.Clear();
MarkDead();
}
}
ui32 UseCount() const { return RefCount_; }
bool Unique() const { return 1U == UseCount(); }
bool Dead() const {
return ExprFlags_ & TExprFlags::Dead;
}
TPositionHandle Pos() const {
ENSURE_NOT_DELETED
return Position_;
}
TPosition Pos(const TExprContext& ctx) const;
EType Type() const {
ENSURE_NOT_DELETED
return (EType)Type_;
}
TListType::size_type ChildrenSize() const {
ENSURE_NOT_DELETED
return Children_.size();
}
TExprNode* Child(ui32 index) const {
ENSURE_NOT_DELETED
Y_ENSURE(index < Children_.size(), "index out of range");
return Children_[index].Get();
}
TPtr ChildPtr(ui32 index) const {
ENSURE_NOT_DELETED
Y_ENSURE(index < Children_.size(), "index out of range");
return Children_[index];
}
TPtr& ChildRef(ui32 index) {
ENSURE_NOT_DELETED
ENSURE_NOT_FROZEN
Y_ENSURE(index < Children_.size(), "index out of range");
return Children_[index];
}
const TExprNode& Head() const {
ENSURE_NOT_DELETED
Y_ENSURE(!Children_.empty(), "no children");
return *Children_.front();
}
TExprNode& Head() {
ENSURE_NOT_DELETED
Y_ENSURE(!Children_.empty(), "no children");
return *Children_.front();
}
TPtr HeadPtr() const {
ENSURE_NOT_DELETED
Y_ENSURE(!Children_.empty(), "no children");
return Children_.front();
}
TPtr& HeadRef() {
ENSURE_NOT_DELETED
ENSURE_NOT_FROZEN
Y_ENSURE(!Children_.empty(), "no children");
return Children_.front();
}
const TExprNode& Tail() const {
ENSURE_NOT_DELETED
Y_ENSURE(!Children_.empty(), "no children");
return *Children_.back();
}
TExprNode& Tail() {
ENSURE_NOT_DELETED
Y_ENSURE(!Children_.empty(), "no children");
return *Children_.back();
}
TPtr TailPtr() const {
ENSURE_NOT_DELETED
Y_ENSURE(!Children_.empty(), "no children");
return Children_.back();
}
TPtr& TailRef() {
ENSURE_NOT_DELETED
ENSURE_NOT_FROZEN
Y_ENSURE(!Children_.empty(), "no children");
return Children_.back();
}
TChildrenType Children() const {
ENSURE_NOT_DELETED
return TChildrenType(Children_.data(), Children_.size());
}
TListType ChildrenList() const {
ENSURE_NOT_DELETED
return Children_;
}
void ChangeChildrenInplace(TListType&& newChildren) {
ENSURE_NOT_DELETED
Children_ = std::move(newChildren);
}
template<class F>
void ForEachChild(const F& visitor) const {
for (const auto& child : Children_)
visitor(*child);
}
TStringBuf Content() const {
ENSURE_NOT_DELETED
return ContentUnchecked();
}
ui32 Flags() const {
ENSURE_NOT_DELETED
return Flags_;
}
void NormalizeAtomFlags(const TExprNode& otherAtom) {
ENSURE_NOT_DELETED
ENSURE_NOT_FROZEN
Y_ENSURE(Type_ == Atom && otherAtom.Type_ == Atom, "Expected atoms");
Y_ENSURE((Flags_ & TNodeFlags::BinaryContent) ==
(otherAtom.Flags_ & TNodeFlags::BinaryContent), "Mismatch binary atom flags");
if (!(Flags_ & TNodeFlags::BinaryContent)) {
Flags_ = Min(Flags_, otherAtom.Flags_);
}
}
ui64 UniqueId() const {
ENSURE_NOT_DELETED
return UniqueId_;
}
const TConstraintNode* GetConstraint(TStringBuf name) const {
ENSURE_NOT_DELETED
Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
return Constraints_.GetConstraint(name);
}
template <class TConstraintType>
const TConstraintType* GetConstraint() const {
ENSURE_NOT_DELETED
Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
return Constraints_.GetConstraint<TConstraintType>();
}
const TConstraintNode::TListType& GetAllConstraints() const {
ENSURE_NOT_DELETED
Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
return Constraints_.GetAllConstraints();
}
const TConstraintSet& GetConstraintSet() const {
ENSURE_NOT_DELETED
Y_ENSURE(static_cast<EState>(State) >= EState::ConstrComplete);
return Constraints_;
}
void AddConstraint(const TConstraintNode* node) {
ENSURE_NOT_DELETED
ENSURE_NOT_FROZEN
Y_ENSURE(static_cast<EState>(State) >= EState::TypeComplete);
Y_ENSURE(!StartsExecution());
Constraints_.AddConstraint(node);
State = EState::ConstrComplete;
}
void CopyConstraints(const TExprNode& node) {
ENSURE_NOT_DELETED
ENSURE_NOT_FROZEN
Y_ENSURE(static_cast<EState>(State) >= EState::TypeComplete);
Constraints_ = node.Constraints_;
State = EState::ConstrComplete;
}
void SetConstraints(const TConstraintSet& constraints) {
ENSURE_NOT_DELETED
ENSURE_NOT_FROZEN
Y_ENSURE(static_cast<EState>(State) >= EState::TypeComplete);
Constraints_ = constraints;
State = EState::ConstrComplete;
}
static TPtr NewAtom(ui64 uniqueId, TPositionHandle pos, const TStringBuf& content, ui32 flags) {
return Make(pos, Atom, {}, content, flags, uniqueId);
}
static TPtr NewArgument(ui64 uniqueId, TPositionHandle pos, const TStringBuf& name) {
return Make(pos, Argument, {}, name, 0, uniqueId);
}
static TPtr NewArguments(ui64 uniqueId, TPositionHandle pos, TListType&& argNodes) {
return Make(pos, Arguments, std::move(argNodes), ZeroString, 0, uniqueId);
}
static TPtr NewLambda(ui64 uniqueId, TPositionHandle pos, TListType&& lambda) {
return Make(pos, Lambda, std::move(lambda), ZeroString, 0, uniqueId);
}
static TPtr NewLambda(ui64 uniqueId, TPositionHandle pos, TPtr&& args, TListType&& body) {
TListType lambda(body.size() + 1U);
lambda.front() = std::move(args);
std::move(body.rbegin(), body.rend(), lambda.rbegin());
return NewLambda(uniqueId, pos, std::move(lambda));
}
static TPtr NewLambda(ui64 uniqueId, TPositionHandle pos, TPtr&& args, TPtr&& body) {
TListType children(body ? 2 : 1);
children.front() = std::move(args);
if (body) {
children.back() = std::move(body);
}
return NewLambda(uniqueId, pos, std::move(children));
}
static TPtr NewWorld(ui64 uniqueId, TPositionHandle pos) {
return Make(pos, World, {}, {}, 0, uniqueId);
}
static TPtr NewList(ui64 uniqueId, TPositionHandle pos, TListType&& children) {
return Make(pos, List, std::move(children), ZeroString, 0, uniqueId);
}
static TPtr NewCallable(ui64 uniqueId, TPositionHandle pos, const TStringBuf& name, TListType&& children) {
return Make(pos, Callable, std::move(children), name, 0, uniqueId);
}
TPtr Clone(ui64 newUniqueId) const {
ENSURE_NOT_DELETED
return Make(Position_, (EType)Type_, TListType(Children_), Content(), Flags_, newUniqueId);
}
static TPtr NewNode(TPositionHandle position, EType type, TListType&& children, const TStringBuf& content, ui32 flags, ui64 uniqueId) {
return Make(position, type, std::move(children), content, flags, uniqueId);
}
TPtr ChangeContent(ui64 newUniqueId, const TStringBuf& content) const {
ENSURE_NOT_DELETED
return Make(Position_, (EType)Type_, TListType(Children_), content, Flags_, newUniqueId);
}
TPtr ChangeChildren(ui64 newUniqueId, TListType&& children) const {
ENSURE_NOT_DELETED
return Make(Position_, (EType)Type_, std::move(children), Content(), Flags_, newUniqueId);
}
TPtr ChangeChild(ui64 newUniqueId, ui32 index, TPtr&& child) const {
ENSURE_NOT_DELETED
Y_ENSURE(index < Children_.size(), "index out of range");
TListType newChildren(Children_);
newChildren[index] = std::move(child);
return Make(Position_, (EType)Type_, std::move(newChildren), Content(), Flags_, newUniqueId);
}
void SetTypeAnn(const TTypeAnnotationNode* typeAnn) {
TypeAnnotation_ = typeAnn;
State = TypeAnnotation_ ? EState::TypeComplete : EState::Initial;
}
const TTypeAnnotationNode* GetTypeAnn() const {
return TypeAnnotation_;
}
EState GetState() const {
return State;
}
void SetState(EState state) {
State = state;
}
ui32 GetArgIndex() const {
YQL_ENSURE(Type() == EType::Argument);
return ArgIndex;
}
void SetArgIndex(ui32 argIndex) {
YQL_ENSURE(Type() == EType::Argument);
YQL_ENSURE(argIndex <= Max<ui16>());
ArgIndex = (ui16)argIndex;
}
ui64 GetHash() const {
Y_VERIFY_DEBUG(HashAbove == HashBelow);
return HashAbove;
}
void SetHash(ui64 hash) {
HashAbove = HashBelow = hash;
}
ui64 GetHashAbove() const {
return HashAbove;
}
void SetHashAbove(ui64 hash) {
HashAbove = hash;
}
ui64 GetHashBelow() const {
return HashBelow;
}
void SetHashBelow(ui64 hash) {
HashBelow = hash;
}
ui64 GetBloom() const {
return Bloom;
}
void SetBloom(ui64 bloom) {
Bloom = bloom;
}
// return pair of outer and inner lambda.
std::optional<std::pair<const TExprNode*, const TExprNode*>> GetDependencyScope() const {
if (HasLambdaScope) {
return std::make_pair(OuterLambda, InnerLambda);
}
return std::nullopt;
}
void SetDependencyScope(const TExprNode* outerLambda, const TExprNode* innerLambda) {
Y_VERIFY_DEBUG(outerLambda == innerLambda || outerLambda->GetLambdaLevel() < innerLambda->GetLambdaLevel(), "Wrong scope of closures.");
HasLambdaScope = 1;
OuterLambda = outerLambda;
InnerLambda = innerLambda;
}
ui16 GetLambdaLevel() const { return LambdaLevel; }
void SetLambdaLevel(ui16 lambdaLevel) { LambdaLevel = lambdaLevel; }
bool IsUsedInDependsOn() const {
YQL_ENSURE(Type() == EType::Argument);
return UsedInDependsOn;
}
void SetUsedInDependsOn() {
YQL_ENSURE(Type() == EType::Argument);
UsedInDependsOn = 1;
}
void SetUnorderedChildren() {
YQL_ENSURE(Type() == EType::List || Type() == EType::Callable);
UnordChildren = 1;
}
bool UnorderedChildren() const {
YQL_ENSURE(Type() == EType::List || Type() == EType::Callable);
return bool(UnordChildren);
}
~TExprNode() {
Y_VERIFY(Dead(), "Node (id: %lu, type: %s, content: '%s') not dead on destruction.",
UniqueId_, ToString(Type_).data(), TString(ContentUnchecked()).data());
Y_VERIFY(!UseCount(), "Node (id: %lu, type: %s, content: '%s') has non-zero use count on destruction.",
UniqueId_, ToString(Type_).data(), TString(ContentUnchecked()).data());
}
private:
static TPtr Make(TPositionHandle position, EType type, TListType&& children, const TStringBuf& content, ui32 flags, ui64 uniqueId) {
Y_ENSURE(flags <= TNodeFlags::FlagsMask);
Y_ENSURE(children.size() <= Max<ui32>());
Y_ENSURE(content.size() <= Max<ui32>());
for (size_t i = 0; i < children.size(); ++i) {
Y_ENSURE(children[i], "Unable to create node " << content << ": " << i << "th child is null");
}
return TPtr(new TExprNode(position, type, std::move(children), content.data(), ui32(content.size()), flags, uniqueId));
}
TExprNode(TPositionHandle position, EType type, TListType&& children,
const char* content, ui32 contentSize, ui32 flags, ui64 uniqueId)
: Children_(std::move(children))
, Content_(content)
, UniqueId_(uniqueId)
, Position_(position)
, ContentSize(contentSize)
, Type_(type)
, Flags_(flags)
, ExprFlags_(TExprFlags::Default)
, State(EState::Initial)
, HasLambdaScope(0)
, UsedInDependsOn(0)
, UnordChildren(0)
, ShallBeDisclosed(0)
{}
TExprNode(const TExprNode&) = delete;
TExprNode(TExprNode&&) = delete;
TExprNode& operator=(const TExprNode&) = delete;
TExprNode& operator=(TExprNode&&) = delete;
bool Frozen() const {
return ExprFlags_ & TExprFlags::Frozen;
}
void MarkFrozen(bool frozen = true) {
if (frozen) {
ExprFlags_ |= TExprFlags::Frozen;
} else {
ExprFlags_ &= ~TExprFlags::Frozen;
}
}
void MarkDead() {
ExprFlags_ |= TExprFlags::Dead;
}
TStringBuf ContentUnchecked() const {
return TStringBuf(Content_, ContentSize);
}
TListType Children_;
TConstraintSet Constraints_;
const char* Content_ = nullptr;
const TExprNode* OuterLambda = nullptr;
const TExprNode* InnerLambda = nullptr;
TPtr Result;
ui64 HashAbove = 0ULL;
ui64 HashBelow = 0ULL;
ui64 Bloom = 0ULL;
const ui64 UniqueId_;
const TTypeAnnotationNode* TypeAnnotation_ = nullptr;
const TPositionHandle Position_;
ui32 RefCount_ = 0U;
const ui32 ContentSize;
ui16 ArgIndex = ui16(-1);
ui16 LambdaLevel = 0; // filled together with OuterLambda
ui16 IntermediateHashesCount = 0;
static_assert(TypeMask <= 7, "EType wont fit in 3 bits, increase Type_ bitfield size");
static_assert(TNodeFlags::FlagsMask <= 7, "TNodeFlags wont fit in 3 bits, increase Flags_ bitfield size");
static_assert(TExprFlags::FlagsMask <= 3, "TExprFlags wont fit in 2 bits, increase ExprFlags_ bitfield size");
static_assert(int(EState::Last) <= 16, "EState wont fit in 4 bits, increase State bitfield size");
struct {
ui8 Type_ : 3;
ui8 Flags_ : 3;
ui8 ExprFlags_ : 2;
EState State : 4;
ui8 HasLambdaScope : 1;
ui8 UsedInDependsOn : 1;
ui8 UnordChildren : 1;
ui8 ShallBeDisclosed: 1;
};
};
class TExportTable {
public:
using TSymbols = THashMap<TString, TExprNode::TPtr>;
TExportTable() = default;
TExportTable(TExprContext& ctx, TSymbols&& symbols)
: Symbols_(std::move(symbols))
, Ctx_(&ctx)
{}
const TSymbols& Symbols() const {
return Symbols_;
}
TSymbols& Symbols(TExprContext& ctx) {
if (Ctx_) {
YQL_ENSURE(Ctx_ == &ctx);
} else {
Ctx_ = &ctx;
}
return Symbols_;
}
TExprContext& ExprCtx() const {
YQL_ENSURE(Ctx_);
return *Ctx_;
}
private:
TSymbols Symbols_;
TExprContext* Ctx_ = nullptr;
};
using TModulesTable = THashMap<TString, TExportTable>;
class IModuleResolver {
public:
typedef std::shared_ptr<IModuleResolver> TPtr;
virtual bool AddFromFile(const TStringBuf& file, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion) = 0;
virtual bool AddFromUrl(const TStringBuf& file, const TStringBuf& url, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion) = 0;
virtual bool AddFromMemory(const TStringBuf& file, const TString& body, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion) = 0;
virtual bool AddFromMemory(const TStringBuf& file, const TString& body, TExprContext& ctx, ui16 syntaxVersion, ui32 packageVersion, TString& moduleName, std::vector<TString>* exports = nullptr, std::vector<TString>* imports = nullptr) = 0;
virtual bool Link(TExprContext& ctx) = 0;
virtual void UpdateNextUniqueId(TExprContext& ctx) const = 0;
virtual ui64 GetNextUniqueId() const = 0;
virtual void RegisterPackage(const TString& package) = 0;
virtual bool SetPackageDefaultVersion(const TString& package, ui32 version) = 0;
virtual const TExportTable* GetModule(const TString& module) const = 0;
/*
Create new resolver which will use already collected modules in readonly manner.
Parent resolver should be alive while using child due to raw data sharing.
*/
virtual IModuleResolver::TPtr CreateMutableChild() const = 0;
virtual ~IModuleResolver() = default;
};
struct TExprStep {
enum ELevel {
ExpandApplyForLambdas,
ValidateProviders,
Configure,
ExprEval,
DiscoveryIO,
Epochs,
Intents,
LoadTablesMetadata,
RewriteIO,
Recapture,
LastLevel
};
TExprStep()
{
}
void Done(ELevel level) {
Steps_.Set(level);
}
void Reset() {
Steps_.Reset();
}
TExprStep& Repeat(ELevel level) {
Steps_.Reset(level);
return *this;
}
bool IsDone(ELevel level) {
return Steps_.Test(level);
}
private:
TEnumBitSet<ELevel, ExpandApplyForLambdas, LastLevel> Steps_;
};
template <typename T>
struct TMakeTypeImpl;
template <class T>
using TNodeMap = std::unordered_map<const TExprNode*, T>;
using TNodeSet = std::unordered_set<const TExprNode*>;
using TNodeOnNodeOwnedMap = TNodeMap<TExprNode::TPtr>;
using TParentsMap = TNodeMap<TNodeSet>;
using TNodeMultiSet = std::unordered_multiset<const TExprNode*>;
using TParentsMultiMap = TNodeMap<TNodeMultiSet>;
template <>
struct TMakeTypeImpl<TVoidExprType> {
static const TVoidExprType* Make(TExprContext& ctx);
};
template <>
struct TMakeTypeImpl<TNullExprType> {
static const TNullExprType* Make(TExprContext& ctx);
};
template <>
struct TMakeTypeImpl<TEmptyListExprType> {
static const TEmptyListExprType* Make(TExprContext& ctx);
};
template <>
struct TMakeTypeImpl<TEmptyDictExprType> {
static const TEmptyDictExprType* Make(TExprContext& ctx);
};
template <>
struct TMakeTypeImpl<TUnitExprType> {
static const TUnitExprType* Make(TExprContext& ctx);
};
template <>
struct TMakeTypeImpl<TWorldExprType> {
static const TWorldExprType* Make(TExprContext& ctx);
};
template <>
struct TMakeTypeImpl<TGenericExprType> {
static const TGenericExprType* Make(TExprContext& ctx);
};
template <>
struct TMakeTypeImpl<TItemExprType> {
static const TItemExprType* Make(TExprContext& ctx, const TStringBuf& name, const TTypeAnnotationNode* itemType);
};
template <>
struct TMakeTypeImpl<TListExprType> {
static const TListExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
};
template <>
struct TMakeTypeImpl<TOptionalExprType> {
static const TOptionalExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
};
template <>
struct TMakeTypeImpl<TVariantExprType> {
static const TVariantExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* underlyingType);
};
template <>
struct TMakeTypeImpl<TErrorExprType> {
static const TErrorExprType* Make(TExprContext& ctx, const TIssue& error);
};
template <>
struct TMakeTypeImpl<TDictExprType> {
static const TDictExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* keyType,
const TTypeAnnotationNode* payloadType);
};
template <>
struct TMakeTypeImpl<TTypeExprType> {
static const TTypeExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* baseType);
};
template <>
struct TMakeTypeImpl<TDataExprType> {
static const TDataExprType* Make(TExprContext& ctx, EDataSlot slot);
};
template <>
struct TMakeTypeImpl<TDataExprParamsType> {
static const TDataExprParamsType* Make(TExprContext& ctx, EDataSlot slot, const TStringBuf& one, const TStringBuf& two);
};
template <>
struct TMakeTypeImpl<TCallableExprType> {
static const TCallableExprType* Make(
TExprContext& ctx, const TTypeAnnotationNode* returnType, const TVector<TCallableExprType::TArgumentInfo>& arguments,
size_t optionalArgumentsCount, const TStringBuf& payload);
};
template <>
struct TMakeTypeImpl<TResourceExprType> {
static const TResourceExprType* Make(TExprContext& ctx, const TStringBuf& tag);
};
template <>
struct TMakeTypeImpl<TTaggedExprType> {
static const TTaggedExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* baseType, const TStringBuf& tag);
};
template <>
struct TMakeTypeImpl<TStructExprType> {
static const TStructExprType* Make(TExprContext& ctx, const TVector<const TItemExprType*>& items);
};
template <>
struct TMakeTypeImpl<TTupleExprType> {
static const TTupleExprType* Make(TExprContext& ctx, const TTypeAnnotationNode::TListType& items);
};
template <>
struct TMakeTypeImpl<TMultiExprType> {
static const TMultiExprType* Make(TExprContext& ctx, const TTypeAnnotationNode::TListType& items);
};
template <>
struct TMakeTypeImpl<TStreamExprType> {
static const TStreamExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
};
template <>
struct TMakeTypeImpl<TFlowExprType> {
static const TFlowExprType* Make(TExprContext& ctx, const TTypeAnnotationNode* itemType);
};
using TSingletonTypeCache = std::tuple<
const TVoidExprType*,
const TNullExprType*,
const TUnitExprType*,
const TEmptyListExprType*,
const TEmptyDictExprType*,
const TWorldExprType*,
const TGenericExprType*,
const TTupleExprType*,
const TStructExprType*,
const TMultiExprType*
>;
struct TExprContext : private TNonCopyable {
class TFreezeGuard {
public:
TFreezeGuard(const TFreezeGuard&) = delete;
TFreezeGuard& operator=(const TFreezeGuard&) = delete;
TFreezeGuard(TExprContext& ctx)
: Ctx(ctx)
{
Ctx.Freeze();
}
~TFreezeGuard() {
Ctx.UnFreeze();
}
private:
TExprContext& Ctx;
};
TIssueManager IssueManager;
TNodeMap<TIssues> AssociativeIssues;
TMemoryPool StringPool;
std::unordered_set<std::string_view> Strings;
std::stack<std::unique_ptr<const TTypeAnnotationNode>> TypeNodes;
std::stack<std::unique_ptr<const TConstraintNode>> ConstraintNodes;
std::deque<std::unique_ptr<TExprNode>> ExprNodes;
TSingletonTypeCache SingletonTypeCache;
std::unordered_set<const TTypeAnnotationNode*, TTypeAnnotationNode::THash, TTypeAnnotationNode::TEqual> TypeSet;
std::unordered_set<const TConstraintNode*, TConstraintNode::THash, TConstraintNode::TEqual> ConstraintSet;
std::unordered_map<const TTypeAnnotationNode*, TExprNode::TPtr> TypeAsNodeCache;
std::unordered_set<TStringBuf, THash<TStringBuf>> DisabledConstraints;
ui64 NextUniqueId = 0;
ui64 NodeAllocationCounter = 0;
ui64 NodesAllocationLimit = 3000000;
ui64 StringsAllocationLimit = 100000000;
ui64 RepeatTransformLimit = 1000000;
ui64 RepeatTransformCounter = 0;
TGcNodeConfig GcConfig;
std::unordered_multimap<ui64, TExprNode*> UniqueNodes;
TExprStep Step;
bool Frozen;
explicit TExprContext(ui64 nextUniqueId = 0ULL);
~TExprContext();
ui64 AllocateNextUniqueId() {
ENSURE_NOT_FROZEN_CTX
const auto ret = ++NextUniqueId;
return ret;
}
TStringBuf AppendString(const TStringBuf& buf) {
ENSURE_NOT_FROZEN_CTX
if (buf.size() == 0) {
return ZeroString;
}
auto it = Strings.find(buf);
if (it != Strings.end()) {
return *it;
}
auto newBuf = StringPool.AppendString(buf);
Strings.insert(it, newBuf);
return newBuf;
}
TPositionHandle AppendPosition(const TPosition& pos);
TPosition GetPosition(TPositionHandle handle) const;
TExprNodeBuilder Builder(TPositionHandle pos) {
return TExprNodeBuilder(pos, *this);
}
[[nodiscard]]
TExprNode::TPtr RenameNode(const TExprNode& node, const TStringBuf& name);
[[nodiscard]]
TExprNode::TPtr ShallowCopy(const TExprNode& node);
[[nodiscard]]
TExprNode::TPtr ChangeChildren(const TExprNode& node, TExprNode::TListType&& children);
[[nodiscard]]
TExprNode::TPtr ChangeChild(const TExprNode& node, ui32 index, TExprNode::TPtr&& child);
[[nodiscard]]
TExprNode::TPtr ExactChangeChildren(const TExprNode& node, TExprNode::TListType&& children);
[[nodiscard]]
TExprNode::TPtr ExactShallowCopy(const TExprNode& node);
[[nodiscard]]
TExprNode::TPtr DeepCopyLambda(const TExprNode& node, TExprNode::TListType&& body);
[[nodiscard]]
TExprNode::TPtr DeepCopyLambda(const TExprNode& node, TExprNode::TPtr&& body = TExprNode::TPtr());
[[nodiscard]]
TExprNode::TPtr FuseLambdas(const TExprNode& outer, const TExprNode& inner);
using TCustomDeepCopier = std::function<bool(const TExprNode& node, TExprNode::TListType& newChildren)>;
[[nodiscard]]
TExprNode::TPtr DeepCopy(const TExprNode& node, TExprContext& nodeContext, TNodeOnNodeOwnedMap& deepClones,
bool internStrings, bool copyTypes, bool copyResult = false, TCustomDeepCopier customCopier = {});
[[nodiscard]]
TExprNode::TPtr SwapWithHead(const TExprNode& node);
TExprNode::TPtr ReplaceNode(TExprNode::TPtr&& start, const TExprNode& src, TExprNode::TPtr dst);
TExprNode::TPtr ReplaceNodes(TExprNode::TPtr&& start, const TNodeOnNodeOwnedMap& replaces);
template<bool KeepTypeAnns = false>
TExprNode::TListType ReplaceNodes(TExprNode::TListType&& start, const TNodeOnNodeOwnedMap& replaces);
TExprNode::TPtr NewAtom(TPositionHandle pos, const TStringBuf& content, ui32 flags = TNodeFlags::ArbitraryContent) {
++NodeAllocationCounter;
const auto node = TExprNode::NewAtom(AllocateNextUniqueId(), pos, AppendString(content), flags);
ExprNodes.emplace_back(node.Get());
return node;
}
TExprNode::TPtr NewArgument(TPositionHandle pos, const TStringBuf& name) {
++NodeAllocationCounter;
const auto node = TExprNode::NewArgument(AllocateNextUniqueId(), pos, AppendString(name));
ExprNodes.emplace_back(node.Get());
return node;
}
TExprNode::TPtr NewArguments(TPositionHandle pos, TExprNode::TListType&& argNodes) {
++NodeAllocationCounter;
const auto node = TExprNode::NewArguments(AllocateNextUniqueId(), pos, std::move(argNodes));
ExprNodes.emplace_back(node.Get());
return node;
}
TExprNode::TPtr NewLambda(TPositionHandle pos, TExprNode::TListType&& lambda) {
++NodeAllocationCounter;
const auto node = TExprNode::NewLambda(AllocateNextUniqueId(), pos, std::move(lambda));
ExprNodes.emplace_back(node.Get());
return node;
}
TExprNode::TPtr NewLambda(TPositionHandle pos, TExprNode::TPtr&& args, TExprNode::TListType&& body) {
++NodeAllocationCounter;
const auto node = TExprNode::NewLambda(AllocateNextUniqueId(), pos, std::move(args), std::move(body));
ExprNodes.emplace_back(node.Get());
return node;
}
TExprNode::TPtr NewLambda(TPositionHandle pos, TExprNode::TPtr&& args, TExprNode::TPtr&& body) {
++NodeAllocationCounter;
const auto node = TExprNode::NewLambda(AllocateNextUniqueId(), pos, std::move(args), std::move(body));
ExprNodes.emplace_back(node.Get());
return node;
}
TExprNode::TPtr NewWorld(TPositionHandle pos) {
++NodeAllocationCounter;
const auto node = TExprNode::NewWorld(AllocateNextUniqueId(), pos);
ExprNodes.emplace_back(node.Get());
return node;
}
TExprNode::TPtr NewList(TPositionHandle pos, TExprNode::TListType&& children) {
++NodeAllocationCounter;
const auto node = TExprNode::NewList(AllocateNextUniqueId(), pos, std::move(children));
ExprNodes.emplace_back(node.Get());
return node;
}
TExprNode::TPtr NewCallable(TPositionHandle pos, const TStringBuf& name, TExprNode::TListType&& children) {
++NodeAllocationCounter;
const auto node = TExprNode::NewCallable(AllocateNextUniqueId(), pos, AppendString(name), std::move(children));
ExprNodes.emplace_back(node.Get());
return node;
}
TExprNode::TPtr NewAtom(TPosition pos, const TStringBuf& content, ui32 flags = TNodeFlags::ArbitraryContent) {
return NewAtom(AppendPosition(pos), content, flags);
}
TExprNode::TPtr NewArgument(TPosition pos, const TStringBuf& name) {
return NewArgument(AppendPosition(pos), name);
}
TExprNode::TPtr NewArguments(TPosition pos, TExprNode::TListType&& argNodes) {
return NewArguments(AppendPosition(pos), std::move(argNodes));
}
TExprNode::TPtr NewLambda(TPosition pos, TExprNode::TListType&& lambda) {
return NewLambda(AppendPosition(pos), std::move(lambda));
}
TExprNode::TPtr NewLambda(TPosition pos, TExprNode::TPtr&& args, TExprNode::TListType&& body) {
return NewLambda(AppendPosition(pos), std::move(args), std::move(body));
}
TExprNode::TPtr NewLambda(TPosition pos, TExprNode::TPtr&& args, TExprNode::TPtr&& body) {
return NewLambda(AppendPosition(pos), std::move(args), std::move(body));
}
TExprNode::TPtr NewWorld(TPosition pos) {
return NewWorld(AppendPosition(pos));
}
TExprNode::TPtr NewList(TPosition pos, TExprNode::TListType&& children) {
return NewList(AppendPosition(pos), std::move(children));
}
TExprNode::TPtr NewCallable(TPosition pos, const TStringBuf& name, TExprNode::TListType&& children) {
return NewCallable(AppendPosition(pos), name, std::move(children));
}
TExprNode::TPtr WrapByCallableIf(bool condition, const TStringBuf& callable, TExprNode::TPtr&& node);
template <typename T, typename... Args>
const T* MakeType(Args&&... args);
template <typename T, typename... Args>
const T* MakeConstraint(Args&&... args);
void AddError(const TIssue& error) {
ENSURE_NOT_FROZEN_CTX
IssueManager.RaiseIssue(error);
}
bool AddWarning(const TIssue& warning) {
ENSURE_NOT_FROZEN_CTX
return IssueManager.RaiseWarning(warning);
}
void Freeze();
void UnFreeze();
void Reset();
template <class TConstraint>
bool IsConstraintEnabled() const {
return DisabledConstraints.find(TConstraint::Name()) == DisabledConstraints.end();
}
private:
using TPositionHandleEqualPred = std::function<bool(TPositionHandle, TPositionHandle)>;
using TPositionHandleHasher = std::function<size_t(TPositionHandle)>;
bool IsEqual(TPositionHandle a, TPositionHandle b) const;
size_t GetHash(TPositionHandle p) const;
std::unordered_set<TPositionHandle, TPositionHandleHasher, TPositionHandleEqualPred> PositionSet;
std::deque<TPosition> Positions;
};
template <typename T, typename... Args>
inline const T* TExprContext::MakeConstraint(Args&&... args) {
ENSURE_NOT_FROZEN_CTX
if (!IsConstraintEnabled<T>()) {
return nullptr;
}
T sample(*this, std::forward<Args>(args)...);
const auto it = ConstraintSet.find(&sample);
if (ConstraintSet.cend() != it) {
return static_cast<const T*>(*it);
}
ConstraintNodes.emplace(new T(std::move(sample)));
const auto ins = ConstraintSet.emplace(ConstraintNodes.top().get());
return static_cast<const T*>(*ins.first);
}
#undef ENSURE_NOT_DELETED
#undef ENSURE_NOT_FROZEN
#undef ENSURE_NOT_FROZEN_CTX
inline bool IsSameAnnotation(const TTypeAnnotationNode& left, const TTypeAnnotationNode& right) {
return &left == &right;
}
template <typename T, typename... Args>
const T* TExprContext::MakeType(Args&&... args) {
return TMakeTypeImpl<T>::Make(*this, std::forward<Args>(args)...);
}
struct TExprAnnotationFlags {
enum {
None = 0x00,
Position = 0x01,
Types = 0x02
};
};
///////////////////////////////////////////////////////////////////////////////
// TNodeException
///////////////////////////////////////////////////////////////////////////////
class TNodeException: public yexception {
public:
TNodeException();
explicit TNodeException(const TExprNode& node);
explicit TNodeException(const TExprNode* node);
explicit TNodeException(const TPositionHandle& pos);
inline const TPositionHandle& Pos() const {
return Pos_;
}
private:
const TPositionHandle Pos_;
};
bool CompileExpr(TAstNode& astRoot, TExprNode::TPtr& exprRoot, TExprContext& ctx,
IModuleResolver* resolver, bool hasAnnotations = false, ui32 typeAnnotationIndex = Max<ui32>(), ui16 syntaxVersion = 0);
bool CompileExpr(TAstNode& astRoot, TExprNode::TPtr& exprRoot, TExprContext& ctx,
IModuleResolver* resolver, ui32 annotationFlags, ui16 syntaxVersion = 0);
struct TLibraryCohesion {
TExportTable Exports;
TNodeMap<std::pair<TString, TString>> Imports;
};
bool CompileExpr(TAstNode& astRoot, TLibraryCohesion& cohesion, TExprContext& ctx, ui16 syntaxVersion = 0);
const TTypeAnnotationNode* CompileTypeAnnotation(const TAstNode& node, TExprContext& ctx);
// validate consistency of arguments and lambdas
void CheckArguments(const TExprNode& root);
void CheckCounts(const TExprNode& root);
// Compare expression trees and return first diffrent nodes.
bool CompareExprTrees(const TExprNode*& one, const TExprNode*& two);
bool CompareExprTreeParts(const TExprNode& one, const TExprNode& two, const TNodeMap<ui32>& argsMap);
void GatherParents(const TExprNode& node, TParentsMap& parentsMap, bool withLeaves = false);
void GatherParentsMulti(const TExprNode& node, TParentsMultiMap& parentsMap, bool withLeaves = false);
struct TConvertToAstSettings {
ui32 AnnotationFlags = 0;
bool RefAtoms = false;
std::function<bool(const TExprNode&)> NoInlineFunc;
bool PrintArguments = false;
};
TAstParseResult ConvertToAst(const TExprNode& root, TExprContext& ctx, const TConvertToAstSettings& settings);
// refAtoms allows omit copying of atom bodies - they will be referenced from expr graph
TAstParseResult ConvertToAst(const TExprNode& root, TExprContext& ctx, ui32 annotationFlags, bool refAtoms);
TExprNode::TListType GetLambdaBody(const TExprNode& lambda);
} // namespace NYql
template<>
inline void Out<NYql::TTypeAnnotationNode>(
IOutputStream &out, const NYql::TTypeAnnotationNode& type)
{
type.Out(out);
}
#include "yql_expr_builder.inl"
|