Source file script_ir_translator.ml
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
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
open Alpha_context
open Micheline
open Script
open Script_tc_errors
open Script_ir_annot
open Script_typed_ir
module Typecheck_costs = Michelson_v1_gas.Cost_of.Typechecking
module Unparse_costs = Michelson_v1_gas.Cost_of.Unparsing
module Tc_context = Script_tc_context
type ex_stack_ty = Ex_stack_ty : ('a, 's) stack_ty -> ex_stack_ty
type ('a, 's, 'b, 'u) cinstr = {
apply :
'r 'f. ('a, 's) kinfo -> ('b, 'u, 'r, 'f) kinstr -> ('a, 's, 'r, 'f) kinstr;
}
type ('a, 's, 'b, 'u) descr = {
loc : Script.location;
bef : ('a, 's) stack_ty;
aft : ('b, 'u) stack_ty;
instr : ('a, 's, 'b, 'u) cinstr;
}
let close_descr {loc; bef; aft; instr} =
let kinfo = {iloc = loc; kstack_ty = aft} in
let kinfo' = {iloc = loc; kstack_ty = bef} in
let kinstr = instr.apply kinfo' (IHalt kinfo) in
{kloc = loc; kbef = bef; kaft = aft; kinstr}
let kinfo_of_descr {loc; bef; _} = {iloc = loc; kstack_ty = bef}
let compose_descr :
type a s b u c v.
Script.location ->
(a, s, b, u) descr ->
(b, u, c, v) descr ->
(a, s, c, v) descr =
fun loc d1 d2 ->
{
loc;
bef = d1.bef;
aft = d2.aft;
instr =
{
apply =
(fun _ k ->
d1.instr.apply
(kinfo_of_descr d1)
(d2.instr.apply (kinfo_of_descr d2) k));
};
}
type tc_context = Tc_context.t
type unparsing_mode = Optimized | Readable | Optimized_legacy
type type_logger =
Script.location ->
stack_ty_before:Script.expr list ->
stack_ty_after:Script.expr list ->
unit
let location = function
| Prim (loc, _, _, _)
| Int (loc, _)
| String (loc, _)
| Bytes (loc, _)
| Seq (loc, _) ->
loc
let kind_equal a b =
match (a, b) with
| (Int_kind, Int_kind)
| (String_kind, String_kind)
| (Bytes_kind, Bytes_kind)
| (Prim_kind, Prim_kind)
| (Seq_kind, Seq_kind) ->
true
| _ -> false
let kind = function
| Int _ -> Int_kind
| String _ -> String_kind
| Bytes _ -> Bytes_kind
| Prim _ -> Prim_kind
| Seq _ -> Seq_kind
let unexpected expr exp_kinds exp_ns exp_prims =
match expr with
| Int (loc, _) -> Invalid_kind (loc, Prim_kind :: exp_kinds, Int_kind)
| String (loc, _) -> Invalid_kind (loc, Prim_kind :: exp_kinds, String_kind)
| Bytes (loc, _) -> Invalid_kind (loc, Prim_kind :: exp_kinds, Bytes_kind)
| Seq (loc, _) -> Invalid_kind (loc, Prim_kind :: exp_kinds, Seq_kind)
| Prim (loc, name, _, _) -> (
let open Michelson_v1_primitives in
match (namespace name, exp_ns) with
| (Type_namespace, Type_namespace)
| (Instr_namespace, Instr_namespace)
| (Constant_namespace, Constant_namespace) ->
Invalid_primitive (loc, exp_prims, name)
| (ns, _) -> Invalid_namespace (loc, name, exp_ns, ns))
let check_kind kinds expr =
let kind = kind expr in
if List.exists (kind_equal kind) kinds then Result.return_unit
else
let loc = location expr in
error (Invalid_kind (loc, kinds, kind))
let rec ty_of_comparable_ty :
type a. a comparable_ty -> (a, Dependent_bool.yes) ty = function
| Unit_t -> Unit_t
| Never_t -> Never_t
| Int_t -> Int_t
| Nat_t -> Nat_t
| Signature_t -> Signature_t
| String_t -> String_t
| Bytes_t -> Bytes_t
| Mutez_t -> Mutez_t
| Bool_t -> Bool_t
| Key_hash_t -> Key_hash_t
| Key_t -> Key_t
| Timestamp_t -> Timestamp_t
| Address_t -> Address_t
| Tx_rollup_l2_address_t -> Tx_rollup_l2_address_t
| Chain_id_t -> Chain_id_t
| Pair_t (l, r, meta, YesYes) ->
Pair_t (ty_of_comparable_ty l, ty_of_comparable_ty r, meta, YesYes)
| Union_t (l, r, meta, YesYes) ->
Union_t (ty_of_comparable_ty l, ty_of_comparable_ty r, meta, YesYes)
| Option_t (t, meta, Yes) -> Option_t (ty_of_comparable_ty t, meta, Yes)
let rec unparse_comparable_ty_uncarbonated :
type a loc. loc:loc -> a comparable_ty -> loc Script.michelson_node =
fun ~loc -> function
| Unit_t -> Prim (loc, T_unit, [], [])
| Never_t -> Prim (loc, T_never, [], [])
| Int_t -> Prim (loc, T_int, [], [])
| Nat_t -> Prim (loc, T_nat, [], [])
| Signature_t -> Prim (loc, T_signature, [], [])
| String_t -> Prim (loc, T_string, [], [])
| Bytes_t -> Prim (loc, T_bytes, [], [])
| Mutez_t -> Prim (loc, T_mutez, [], [])
| Bool_t -> Prim (loc, T_bool, [], [])
| Key_hash_t -> Prim (loc, T_key_hash, [], [])
| Key_t -> Prim (loc, T_key, [], [])
| Timestamp_t -> Prim (loc, T_timestamp, [], [])
| Address_t -> Prim (loc, T_address, [], [])
| Tx_rollup_l2_address_t -> Prim (loc, T_tx_rollup_l2_address, [], [])
| Chain_id_t -> Prim (loc, T_chain_id, [], [])
| Pair_t (l, r, _meta, YesYes) -> (
let tl = unparse_comparable_ty_uncarbonated ~loc l in
let tr = unparse_comparable_ty_uncarbonated ~loc r in
match tr with
| Prim (_, T_pair, ts, []) -> Prim (loc, T_pair, tl :: ts, [])
| _ -> Prim (loc, T_pair, [tl; tr], []))
| Union_t (l, r, _meta, YesYes) ->
let tl = unparse_comparable_ty_uncarbonated ~loc l in
let tr = unparse_comparable_ty_uncarbonated ~loc r in
Prim (loc, T_or, [tl; tr], [])
| Option_t (t, _meta, Yes) ->
Prim (loc, T_option, [unparse_comparable_ty_uncarbonated ~loc t], [])
let unparse_memo_size ~loc memo_size =
let z = Sapling.Memo_size.unparse_to_z memo_size in
Int (loc, z)
let rec unparse_ty_entrypoints_uncarbonated :
type a ac loc.
loc:loc -> (a, ac) ty -> a entrypoints_node -> loc Script.michelson_node =
fun ~loc ty {nested = nested_entrypoints; at_node} ->
let (name, args) =
match ty with
| Unit_t -> (T_unit, [])
| Int_t -> (T_int, [])
| Nat_t -> (T_nat, [])
| Signature_t -> (T_signature, [])
| String_t -> (T_string, [])
| Bytes_t -> (T_bytes, [])
| Mutez_t -> (T_mutez, [])
| Bool_t -> (T_bool, [])
| Key_hash_t -> (T_key_hash, [])
| Key_t -> (T_key, [])
| Timestamp_t -> (T_timestamp, [])
| Address_t -> (T_address, [])
| Tx_rollup_l2_address_t -> (T_tx_rollup_l2_address, [])
| Operation_t -> (T_operation, [])
| Chain_id_t -> (T_chain_id, [])
| Never_t -> (T_never, [])
| Bls12_381_g1_t -> (T_bls12_381_g1, [])
| Bls12_381_g2_t -> (T_bls12_381_g2, [])
| Bls12_381_fr_t -> (T_bls12_381_fr, [])
| Contract_t (ut, _meta) ->
let t = unparse_ty_entrypoints_uncarbonated ~loc ut no_entrypoints in
(T_contract, [t])
| Pair_t (utl, utr, _meta, _) -> (
let tl = unparse_ty_entrypoints_uncarbonated ~loc utl no_entrypoints in
let tr = unparse_ty_entrypoints_uncarbonated ~loc utr no_entrypoints in
match tr with
| Prim (_, T_pair, ts, []) -> (T_pair, tl :: ts)
| _ -> (T_pair, [tl; tr]))
| Union_t (utl, utr, _meta, _) ->
let (entrypoints_l, entrypoints_r) =
match nested_entrypoints with
| Entrypoints_None -> (no_entrypoints, no_entrypoints)
| Entrypoints_Union {left; right} -> (left, right)
in
let tl = unparse_ty_entrypoints_uncarbonated ~loc utl entrypoints_l in
let tr = unparse_ty_entrypoints_uncarbonated ~loc utr entrypoints_r in
(T_or, [tl; tr])
| Lambda_t (uta, utr, _meta) ->
let ta = unparse_ty_entrypoints_uncarbonated ~loc uta no_entrypoints in
let tr = unparse_ty_entrypoints_uncarbonated ~loc utr no_entrypoints in
(T_lambda, [ta; tr])
| Option_t (ut, _meta, _) ->
let ut = unparse_ty_entrypoints_uncarbonated ~loc ut no_entrypoints in
(T_option, [ut])
| List_t (ut, _meta) ->
let t = unparse_ty_entrypoints_uncarbonated ~loc ut no_entrypoints in
(T_list, [t])
| Ticket_t (ut, _meta) ->
let t = unparse_comparable_ty_uncarbonated ~loc ut in
(T_ticket, [t])
| Set_t (ut, _meta) ->
let t = unparse_comparable_ty_uncarbonated ~loc ut in
(T_set, [t])
| Map_t (uta, utr, _meta) ->
let ta = unparse_comparable_ty_uncarbonated ~loc uta in
let tr = unparse_ty_entrypoints_uncarbonated ~loc utr no_entrypoints in
(T_map, [ta; tr])
| Big_map_t (uta, utr, _meta) ->
let ta = unparse_comparable_ty_uncarbonated ~loc uta in
let tr = unparse_ty_entrypoints_uncarbonated ~loc utr no_entrypoints in
(T_big_map, [ta; tr])
| Sapling_transaction_t memo_size ->
(T_sapling_transaction, [unparse_memo_size ~loc memo_size])
| Sapling_transaction_deprecated_t memo_size ->
(T_sapling_transaction_deprecated, [unparse_memo_size ~loc memo_size])
| Sapling_state_t memo_size ->
(T_sapling_state, [unparse_memo_size ~loc memo_size])
| Chest_key_t -> (T_chest_key, [])
| Chest_t -> (T_chest, [])
in
let annot =
match at_node with
| None -> []
| Some {name; original_type_expr = _} ->
[Entrypoint.unparse_as_field_annot name]
in
Prim (loc, name, args, annot)
let unparse_ty_uncarbonated ~loc ty =
unparse_ty_entrypoints_uncarbonated ~loc ty no_entrypoints
let unparse_ty ~loc ctxt ty =
Gas.consume ctxt (Unparse_costs.unparse_type ty) >|? fun ctxt ->
(unparse_ty_uncarbonated ~loc ty, ctxt)
let unparse_comparable_ty ~loc ctxt comp_ty =
Gas.consume ctxt (Unparse_costs.unparse_comparable_type comp_ty)
>|? fun ctxt -> (unparse_comparable_ty_uncarbonated ~loc comp_ty, ctxt)
let unparse_parameter_ty ~loc ctxt ty ~entrypoints =
Gas.consume ctxt (Unparse_costs.unparse_type ty) >|? fun ctxt ->
(unparse_ty_entrypoints_uncarbonated ~loc ty entrypoints.root, ctxt)
let serialize_ty_for_error ty =
unparse_ty_uncarbonated ~loc:() ty |> Micheline.strip_locations
let[@coq_axiom_with_reason "gadt"] rec comparable_ty_of_ty :
type a ac.
context ->
Script.location ->
(a, ac) ty ->
(a comparable_ty * context) tzresult =
fun ctxt loc ty ->
Gas.consume ctxt Typecheck_costs.comparable_ty_of_ty_cycle >>? fun ctxt ->
match ty with
| Unit_t -> ok ((Unit_t : a comparable_ty), ctxt)
| Never_t -> ok (Never_t, ctxt)
| Int_t -> ok (Int_t, ctxt)
| Nat_t -> ok (Nat_t, ctxt)
| Signature_t -> ok (Signature_t, ctxt)
| String_t -> ok (String_t, ctxt)
| Bytes_t -> ok (Bytes_t, ctxt)
| Mutez_t -> ok (Mutez_t, ctxt)
| Bool_t -> ok (Bool_t, ctxt)
| Key_hash_t -> ok (Key_hash_t, ctxt)
| Key_t -> ok (Key_t, ctxt)
| Timestamp_t -> ok (Timestamp_t, ctxt)
| Address_t -> ok (Address_t, ctxt)
| Tx_rollup_l2_address_t -> ok (Tx_rollup_l2_address_t, ctxt)
| Chain_id_t -> ok (Chain_id_t, ctxt)
| Pair_t (l, r, pname, _) ->
comparable_ty_of_ty ctxt loc l >>? fun (lty, ctxt) ->
comparable_ty_of_ty ctxt loc r >|? fun (rty, ctxt) ->
(Pair_t (lty, rty, pname, YesYes), ctxt)
| Union_t (l, r, meta, _) ->
comparable_ty_of_ty ctxt loc l >>? fun (lty, ctxt) ->
comparable_ty_of_ty ctxt loc r >|? fun (rty, ctxt) ->
(Union_t (lty, rty, meta, YesYes), ctxt)
| Option_t (tt, meta, _) ->
comparable_ty_of_ty ctxt loc tt >|? fun (ty, ctxt) ->
(Option_t (ty, meta, Yes), ctxt)
| Lambda_t _ | List_t _ | Ticket_t _ | Set_t _ | Map_t _ | Big_map_t _
| Contract_t _ | Operation_t | Bls12_381_fr_t | Bls12_381_g1_t
| Bls12_381_g2_t | Sapling_state_t _ | Sapling_transaction_t _
| Sapling_transaction_deprecated_t _ | Chest_key_t | Chest_t ->
let t = serialize_ty_for_error ty in
error (Comparable_type_expected (loc, t))
let rec unparse_stack_uncarbonated :
type a s. (a, s) stack_ty -> Script.expr list = function
| Bot_t -> []
| Item_t (ty, rest) ->
let uty = unparse_ty_uncarbonated ~loc:() ty in
let urest = unparse_stack_uncarbonated rest in
strip_locations uty :: urest
let serialize_stack_for_error ctxt stack_ty =
match Gas.level ctxt with
| Unaccounted -> unparse_stack_uncarbonated stack_ty
| Limited _ -> []
let unparse_unit ~loc ctxt () = ok (Prim (loc, D_Unit, [], []), ctxt)
let unparse_int ~loc ctxt v = ok (Int (loc, Script_int.to_zint v), ctxt)
let unparse_nat ~loc ctxt v = ok (Int (loc, Script_int.to_zint v), ctxt)
let unparse_string ~loc ctxt s =
ok (String (loc, Script_string.to_string s), ctxt)
let unparse_bytes ~loc ctxt s = ok (Bytes (loc, s), ctxt)
let unparse_bool ~loc ctxt b =
ok (Prim (loc, (if b then D_True else D_False), [], []), ctxt)
let unparse_timestamp ~loc ctxt mode t =
match mode with
| Optimized | Optimized_legacy ->
ok (Int (loc, Script_timestamp.to_zint t), ctxt)
| Readable -> (
Gas.consume ctxt Unparse_costs.timestamp_readable >>? fun ctxt ->
match Script_timestamp.to_notation t with
| None -> ok (Int (loc, Script_timestamp.to_zint t), ctxt)
| Some s -> ok (String (loc, s), ctxt))
let unparse_address ~loc ctxt mode {destination; entrypoint} =
match mode with
| Optimized | Optimized_legacy ->
Gas.consume ctxt Unparse_costs.contract_optimized >|? fun ctxt ->
let bytes =
Data_encoding.Binary.to_bytes_exn
Data_encoding.(tup2 Destination.encoding Entrypoint.value_encoding)
(destination, entrypoint)
in
(Bytes (loc, bytes), ctxt)
| Readable ->
Gas.consume ctxt Unparse_costs.contract_readable >|? fun ctxt ->
let notation =
Destination.to_b58check destination
^ Entrypoint.to_address_suffix entrypoint
in
(String (loc, notation), ctxt)
let unparse_tx_rollup_l2_address ~loc ctxt mode
(tx_address : tx_rollup_l2_address) =
let tx_address = Indexable.to_value tx_address in
match mode with
| Optimized | Optimized_legacy ->
Gas.consume ctxt Unparse_costs.contract_optimized >|? fun ctxt ->
let bytes =
Data_encoding.Binary.to_bytes_exn
Tx_rollup_l2_address.encoding
tx_address
in
(Bytes (loc, bytes), ctxt)
| Readable ->
Gas.consume ctxt Unparse_costs.contract_readable >|? fun ctxt ->
let b58check = Tx_rollup_l2_address.to_b58check tx_address in
(String (loc, b58check), ctxt)
let unparse_contract ~loc ctxt mode (Typed_contract {arg_ty = _; address}) =
unparse_address ~loc ctxt mode address
let unparse_signature ~loc ctxt mode s =
let s = Script_signature.get s in
match mode with
| Optimized | Optimized_legacy ->
Gas.consume ctxt Unparse_costs.signature_optimized >|? fun ctxt ->
let bytes = Data_encoding.Binary.to_bytes_exn Signature.encoding s in
(Bytes (loc, bytes), ctxt)
| Readable ->
Gas.consume ctxt Unparse_costs.signature_readable >|? fun ctxt ->
(String (loc, Signature.to_b58check s), ctxt)
let unparse_mutez ~loc ctxt v = ok (Int (loc, Z.of_int64 (Tez.to_mutez v)), ctxt)
let unparse_key ~loc ctxt mode k =
match mode with
| Optimized | Optimized_legacy ->
Gas.consume ctxt Unparse_costs.public_key_optimized >|? fun ctxt ->
let bytes =
Data_encoding.Binary.to_bytes_exn Signature.Public_key.encoding k
in
(Bytes (loc, bytes), ctxt)
| Readable ->
Gas.consume ctxt Unparse_costs.public_key_readable >|? fun ctxt ->
(String (loc, Signature.Public_key.to_b58check k), ctxt)
let unparse_key_hash ~loc ctxt mode k =
match mode with
| Optimized | Optimized_legacy ->
Gas.consume ctxt Unparse_costs.key_hash_optimized >|? fun ctxt ->
let bytes =
Data_encoding.Binary.to_bytes_exn Signature.Public_key_hash.encoding k
in
(Bytes (loc, bytes), ctxt)
| Readable ->
Gas.consume ctxt Unparse_costs.key_hash_readable >|? fun ctxt ->
(String (loc, Signature.Public_key_hash.to_b58check k), ctxt)
let unparse_operation ~loc ctxt {piop; lazy_storage_diff = _} =
let iop = Apply_results.contents_of_packed_internal_operation piop in
let bytes =
Data_encoding.Binary.to_bytes_exn
Apply_results.internal_contents_encoding
iop
in
Gas.consume ctxt (Unparse_costs.operation bytes) >|? fun ctxt ->
(Bytes (loc, bytes), ctxt)
let unparse_chain_id ~loc ctxt mode chain_id =
match mode with
| Optimized | Optimized_legacy ->
Gas.consume ctxt Unparse_costs.chain_id_optimized >|? fun ctxt ->
let bytes =
Data_encoding.Binary.to_bytes_exn Script_chain_id.encoding chain_id
in
(Bytes (loc, bytes), ctxt)
| Readable ->
Gas.consume ctxt Unparse_costs.chain_id_readable >|? fun ctxt ->
(String (loc, Script_chain_id.to_b58check chain_id), ctxt)
let unparse_bls12_381_g1 ~loc ctxt x =
Gas.consume ctxt Unparse_costs.bls12_381_g1 >|? fun ctxt ->
let bytes = Script_bls.G1.to_bytes x in
(Bytes (loc, bytes), ctxt)
let unparse_bls12_381_g2 ~loc ctxt x =
Gas.consume ctxt Unparse_costs.bls12_381_g2 >|? fun ctxt ->
let bytes = Script_bls.G2.to_bytes x in
(Bytes (loc, bytes), ctxt)
let unparse_bls12_381_fr ~loc ctxt x =
Gas.consume ctxt Unparse_costs.bls12_381_fr >|? fun ctxt ->
let bytes = Script_bls.Fr.to_bytes x in
(Bytes (loc, bytes), ctxt)
let unparse_with_data_encoding ~loc ctxt s unparse_cost encoding =
Lwt.return
( Gas.consume ctxt unparse_cost >|? fun ctxt ->
let bytes = Data_encoding.Binary.to_bytes_exn encoding s in
(Bytes (loc, bytes), ctxt) )
type ('ty, 'depth) comb_witness =
| Comb_Pair : ('t, 'd) comb_witness -> (_ * 't, unit -> 'd) comb_witness
| Comb_Any : (_, _) comb_witness
let unparse_pair (type r) ~loc unparse_l unparse_r ctxt mode
(r_comb_witness : (r, unit -> unit -> _) comb_witness) (l, (r : r)) =
unparse_l ctxt l >>=? fun (l, ctxt) ->
unparse_r ctxt r >|=? fun (r, ctxt) ->
let res =
match (mode, r_comb_witness, r) with
| (Optimized, Comb_Pair _, Micheline.Seq (_, r)) ->
Micheline.Seq (loc, l :: r)
| ( Optimized,
Comb_Pair (Comb_Pair _),
Prim (_, D_Pair, [x2; Prim (_, D_Pair, [x3; x4], [])], []) ) ->
Micheline.Seq (loc, [l; x2; x3; x4])
| (Readable, Comb_Pair _, Prim (_, D_Pair, xs, [])) ->
Prim (loc, D_Pair, l :: xs, [])
| _ ->
Prim (loc, D_Pair, [l; r], [])
in
(res, ctxt)
let unparse_union ~loc unparse_l unparse_r ctxt = function
| L l ->
unparse_l ctxt l >|=? fun (l, ctxt) -> (Prim (loc, D_Left, [l], []), ctxt)
| R r ->
unparse_r ctxt r >|=? fun (r, ctxt) -> (Prim (loc, D_Right, [r], []), ctxt)
let unparse_option ~loc unparse_v ctxt = function
| Some v ->
unparse_v ctxt v >|=? fun (v, ctxt) -> (Prim (loc, D_Some, [v], []), ctxt)
| None -> return (Prim (loc, D_None, [], []), ctxt)
let comparable_comb_witness2 :
type t. t comparable_ty -> (t, unit -> unit -> unit) comb_witness = function
| Pair_t (_, Pair_t _, _, YesYes) -> Comb_Pair (Comb_Pair Comb_Any)
| Pair_t _ -> Comb_Pair Comb_Any
| _ -> Comb_Any
let[@coq_axiom_with_reason "gadt"] rec unparse_comparable_data :
type a loc.
loc:loc ->
context ->
unparsing_mode ->
a comparable_ty ->
a ->
(loc Script.michelson_node * context) tzresult Lwt.t =
fun ~loc ctxt mode ty a ->
Gas.consume ctxt Unparse_costs.unparse_data_cycle
>>?=
fun ctxt ->
match (ty, a) with
| (Unit_t, v) -> Lwt.return @@ unparse_unit ~loc ctxt v
| (Int_t, v) -> Lwt.return @@ unparse_int ~loc ctxt v
| (Nat_t, v) -> Lwt.return @@ unparse_nat ~loc ctxt v
| (String_t, s) -> Lwt.return @@ unparse_string ~loc ctxt s
| (Bytes_t, s) -> Lwt.return @@ unparse_bytes ~loc ctxt s
| (Bool_t, b) -> Lwt.return @@ unparse_bool ~loc ctxt b
| (Timestamp_t, t) -> Lwt.return @@ unparse_timestamp ~loc ctxt mode t
| (Address_t, address) -> Lwt.return @@ unparse_address ~loc ctxt mode address
| (Tx_rollup_l2_address_t, address) ->
Lwt.return @@ unparse_tx_rollup_l2_address ~loc ctxt mode address
| (Signature_t, s) -> Lwt.return @@ unparse_signature ~loc ctxt mode s
| (Mutez_t, v) -> Lwt.return @@ unparse_mutez ~loc ctxt v
| (Key_t, k) -> Lwt.return @@ unparse_key ~loc ctxt mode k
| (Key_hash_t, k) -> Lwt.return @@ unparse_key_hash ~loc ctxt mode k
| (Chain_id_t, chain_id) ->
Lwt.return @@ unparse_chain_id ~loc ctxt mode chain_id
| (Pair_t (tl, tr, _, YesYes), pair) ->
let r_witness = comparable_comb_witness2 tr in
let unparse_l ctxt v = unparse_comparable_data ~loc ctxt mode tl v in
let unparse_r ctxt v = unparse_comparable_data ~loc ctxt mode tr v in
unparse_pair ~loc unparse_l unparse_r ctxt mode r_witness pair
| (Union_t (tl, tr, _, YesYes), v) ->
let unparse_l ctxt v = unparse_comparable_data ~loc ctxt mode tl v in
let unparse_r ctxt v = unparse_comparable_data ~loc ctxt mode tr v in
unparse_union ~loc unparse_l unparse_r ctxt v
| (Option_t (t, _, Yes), v) ->
let unparse_v ctxt v = unparse_comparable_data ~loc ctxt mode t v in
unparse_option ~loc unparse_v ctxt v
| (Never_t, _) -> .
let pack_node unparsed ctxt =
Gas.consume ctxt (Script.strip_locations_cost unparsed) >>? fun ctxt ->
let bytes =
Data_encoding.Binary.to_bytes_exn
expr_encoding
(Micheline.strip_locations unparsed)
in
Gas.consume ctxt (Script.serialized_cost bytes) >|? fun ctxt ->
let bytes = Bytes.cat (Bytes.of_string "\005") bytes in
(bytes, ctxt)
let pack_comparable_data ctxt ty data ~mode =
unparse_comparable_data ~loc:() ctxt mode ty data >>=? fun (unparsed, ctxt) ->
Lwt.return @@ pack_node unparsed ctxt
let hash_bytes ctxt bytes =
Gas.consume ctxt (Michelson_v1_gas.Cost_of.Interpreter.blake2b bytes)
>|? fun ctxt -> (Script_expr_hash.(hash_bytes [bytes]), ctxt)
let hash_comparable_data ctxt ty data =
pack_comparable_data ctxt ty data ~mode:Optimized_legacy
>>=? fun (bytes, ctxt) -> Lwt.return @@ hash_bytes ctxt bytes
let check_dupable_comparable_ty : type a. a comparable_ty -> unit = function
| Unit_t | Never_t | Int_t | Nat_t | Signature_t | String_t | Bytes_t
| Mutez_t | Bool_t | Key_hash_t | Key_t | Timestamp_t | Chain_id_t | Address_t
| Tx_rollup_l2_address_t | Pair_t _ | Union_t _ | Option_t _ ->
()
let check_dupable_ty ctxt loc ty =
let rec aux : type a ac. location -> (a, ac) ty -> (unit, error) Gas_monad.t =
fun loc ty ->
let open Gas_monad.Syntax in
let* () = Gas_monad.consume_gas Typecheck_costs.check_dupable_cycle in
match ty with
| Unit_t -> return_unit
| Int_t -> return_unit
| Nat_t -> return_unit
| Signature_t -> return_unit
| String_t -> return_unit
| Bytes_t -> return_unit
| Mutez_t -> return_unit
| Key_hash_t -> return_unit
| Key_t -> return_unit
| Timestamp_t -> return_unit
| Address_t -> return_unit
| Tx_rollup_l2_address_t -> return_unit
| Bool_t -> return_unit
| Contract_t _ -> return_unit
| Operation_t -> return_unit
| Chain_id_t -> return_unit
| Never_t -> return_unit
| Bls12_381_g1_t -> return_unit
| Bls12_381_g2_t -> return_unit
| Bls12_381_fr_t -> return_unit
| Sapling_state_t _ -> return_unit
| Sapling_transaction_t _ -> return_unit
| Sapling_transaction_deprecated_t _ -> return_unit
| Chest_t -> return_unit
| Chest_key_t -> return_unit
| Ticket_t _ -> fail @@ Unexpected_ticket loc
| Pair_t (ty_a, ty_b, _, _) ->
let* () = aux loc ty_a in
aux loc ty_b
| Union_t (ty_a, ty_b, _, _) ->
let* () = aux loc ty_a in
aux loc ty_b
| Lambda_t (_, _, _) ->
return_unit
| Option_t (ty, _, _) -> aux loc ty
| List_t (ty, _) -> aux loc ty
| Set_t (key_ty, _) ->
let () = check_dupable_comparable_ty key_ty in
return_unit
| Map_t (key_ty, val_ty, _) ->
let () = check_dupable_comparable_ty key_ty in
aux loc val_ty
| Big_map_t (key_ty, val_ty, _) ->
let () = check_dupable_comparable_ty key_ty in
aux loc val_ty
in
let gas = aux loc ty in
Gas_monad.run ctxt gas >>? fun (res, ctxt) ->
match res with Ok () -> ok ctxt | Error e -> error e
type ('ta, 'tb) eq = Eq : ('same, 'same) eq
let type_metadata_eq :
type error_trace.
error_details:error_trace error_details ->
'a ty_metadata ->
'b ty_metadata ->
(unit, error_trace) result =
fun ~error_details {size = size_a} {size = size_b} ->
Type_size.check_eq ~error_details size_a size_b
let default_ty_eq_error ty1 ty2 =
let ty1 = serialize_ty_for_error ty1 in
let ty2 = serialize_ty_for_error ty2 in
Inconsistent_types (None, ty1, ty2)
let rec comparable_ty_eq :
type ta tb error_trace.
error_details:error_trace error_details ->
ta comparable_ty ->
tb comparable_ty ->
((ta comparable_ty, tb comparable_ty) eq, error_trace) Gas_monad.t =
let open Gas_monad in
fun ~error_details ta tb ->
let open Gas_monad.Syntax in
let* () = Gas_monad.consume_gas Typecheck_costs.merge_cycle in
let type_metadata_eq meta_a meta_b =
of_result @@ type_metadata_eq ~error_details meta_a meta_b
in
let not_equal () =
of_result
@@ Error
(match error_details with
| Fast -> (Inconsistent_types_fast : error_trace)
| Informative ->
trace_of_error
@@ default_ty_eq_error
(ty_of_comparable_ty ta)
(ty_of_comparable_ty tb))
in
match (ta, tb) with
| (Unit_t, Unit_t) -> return (Eq : (ta comparable_ty, tb comparable_ty) eq)
| (Unit_t, _) -> not_equal ()
| (Never_t, Never_t) -> return Eq
| (Never_t, _) -> not_equal ()
| (Int_t, Int_t) -> return Eq
| (Int_t, _) -> not_equal ()
| (Nat_t, Nat_t) -> return Eq
| (Nat_t, _) -> not_equal ()
| (Signature_t, Signature_t) -> return Eq
| (Signature_t, _) -> not_equal ()
| (String_t, String_t) -> return Eq
| (String_t, _) -> not_equal ()
| (Bytes_t, Bytes_t) -> return Eq
| (Bytes_t, _) -> not_equal ()
| (Mutez_t, Mutez_t) -> return Eq
| (Mutez_t, _) -> not_equal ()
| (Bool_t, Bool_t) -> return Eq
| (Bool_t, _) -> not_equal ()
| (Key_hash_t, Key_hash_t) -> return Eq
| (Key_hash_t, _) -> not_equal ()
| (Key_t, Key_t) -> return Eq
| (Key_t, _) -> not_equal ()
| (Timestamp_t, Timestamp_t) -> return Eq
| (Timestamp_t, _) -> not_equal ()
| (Chain_id_t, Chain_id_t) -> return Eq
| (Chain_id_t, _) -> not_equal ()
| (Address_t, Address_t) -> return Eq
| (Address_t, _) -> not_equal ()
| (Tx_rollup_l2_address_t, Tx_rollup_l2_address_t) -> return Eq
| (Tx_rollup_l2_address_t, _) -> not_equal ()
| ( Pair_t (left_a, right_a, meta_a, YesYes),
Pair_t (left_b, right_b, meta_b, YesYes) ) ->
let* () = type_metadata_eq meta_a meta_b in
let* Eq = comparable_ty_eq ~error_details left_a left_b in
let+ Eq = comparable_ty_eq ~error_details right_a right_b in
(Eq : (ta comparable_ty, tb comparable_ty) eq)
| (Pair_t _, _) -> not_equal ()
| ( Union_t (left_a, right_a, meta_a, YesYes),
Union_t (left_b, right_b, meta_b, YesYes) ) ->
let* () = type_metadata_eq meta_a meta_b in
let* Eq = comparable_ty_eq ~error_details left_a left_b in
let+ Eq = comparable_ty_eq ~error_details right_a right_b in
(Eq : (ta comparable_ty, tb comparable_ty) eq)
| (Union_t _, _) -> not_equal ()
| (Option_t (ta, meta_a, Yes), Option_t (tb, meta_b, Yes)) ->
let* () = type_metadata_eq meta_a meta_b in
let+ Eq = comparable_ty_eq ~error_details ta tb in
(Eq : (ta comparable_ty, tb comparable_ty) eq)
| (Option_t _, _) -> not_equal ()
let memo_size_eq :
type error_trace.
error_details:error_trace error_details ->
Sapling.Memo_size.t ->
Sapling.Memo_size.t ->
(unit, error_trace) result =
fun ~error_details ms1 ms2 ->
if Sapling.Memo_size.equal ms1 ms2 then Result.return_unit
else
Error
(match error_details with
| Fast -> Inconsistent_types_fast
| Informative -> trace_of_error @@ Inconsistent_memo_sizes (ms1, ms2))
(** Same as comparable_ty_eq but for any types. *)
let ty_eq :
type a ac b bc error_trace.
error_details:error_trace error_details ->
Script.location ->
(a, ac) ty ->
(b, bc) ty ->
(((a, ac) ty, (b, bc) ty) eq, error_trace) Gas_monad.t =
fun ~error_details loc ty1 ty2 ->
let type_metadata_eq meta1 meta2 =
Gas_monad.of_result (type_metadata_eq ~error_details meta1 meta2)
|> Gas_monad.record_trace_eval ~error_details (fun () ->
let ty1 = serialize_ty_for_error ty1 in
let ty2 = serialize_ty_for_error ty2 in
Inconsistent_types (Some loc, ty1, ty2))
in
let memo_size_eq ms1 ms2 =
Gas_monad.of_result (memo_size_eq ~error_details ms1 ms2)
in
let rec help :
type ta tac tb tbc.
(ta, tac) ty ->
(tb, tbc) ty ->
(((ta, tac) ty, (tb, tbc) ty) eq, error_trace) Gas_monad.t =
fun ty1 ty2 ->
help0 ty1 ty2
|> Gas_monad.record_trace_eval ~error_details (fun () ->
default_ty_eq_error ty1 ty2)
and help0 :
type ta tac tb tbc.
(ta, tac) ty ->
(tb, tbc) ty ->
(((ta, tac) ty, (tb, tbc) ty) eq, error_trace) Gas_monad.t =
fun ty1 ty2 ->
let open Gas_monad.Syntax in
let* () = Gas_monad.consume_gas Typecheck_costs.merge_cycle in
let not_equal () =
Gas_monad.of_result
@@ Error
(match error_details with
| Fast -> (Inconsistent_types_fast : error_trace)
| Informative -> trace_of_error @@ default_ty_eq_error ty1 ty2)
in
match (ty1, ty2) with
| (Unit_t, Unit_t) -> return (Eq : ((ta, tac) ty, (tb, tbc) ty) eq)
| (Unit_t, _) -> not_equal ()
| (Int_t, Int_t) -> return Eq
| (Int_t, _) -> not_equal ()
| (Nat_t, Nat_t) -> return Eq
| (Nat_t, _) -> not_equal ()
| (Key_t, Key_t) -> return Eq
| (Key_t, _) -> not_equal ()
| (Key_hash_t, Key_hash_t) -> return Eq
| (Key_hash_t, _) -> not_equal ()
| (String_t, String_t) -> return Eq
| (String_t, _) -> not_equal ()
| (Bytes_t, Bytes_t) -> return Eq
| (Bytes_t, _) -> not_equal ()
| (Signature_t, Signature_t) -> return Eq
| (Signature_t, _) -> not_equal ()
| (Mutez_t, Mutez_t) -> return Eq
| (Mutez_t, _) -> not_equal ()
| (Timestamp_t, Timestamp_t) -> return Eq
| (Timestamp_t, _) -> not_equal ()
| (Address_t, Address_t) -> return Eq
| (Address_t, _) -> not_equal ()
| (Tx_rollup_l2_address_t, Tx_rollup_l2_address_t) -> return Eq
| (Tx_rollup_l2_address_t, _) -> not_equal ()
| (Bool_t, Bool_t) -> return Eq
| (Bool_t, _) -> not_equal ()
| (Chain_id_t, Chain_id_t) -> return Eq
| (Chain_id_t, _) -> not_equal ()
| (Never_t, Never_t) -> return Eq
| (Never_t, _) -> not_equal ()
| (Operation_t, Operation_t) -> return Eq
| (Operation_t, _) -> not_equal ()
| (Bls12_381_g1_t, Bls12_381_g1_t) -> return Eq
| (Bls12_381_g1_t, _) -> not_equal ()
| (Bls12_381_g2_t, Bls12_381_g2_t) -> return Eq
| (Bls12_381_g2_t, _) -> not_equal ()
| (Bls12_381_fr_t, Bls12_381_fr_t) -> return Eq
| (Bls12_381_fr_t, _) -> not_equal ()
| (Map_t (tal, tar, meta1), Map_t (tbl, tbr, meta2)) ->
let* () = type_metadata_eq meta1 meta2 in
let* Eq = help tar tbr in
let+ Eq = comparable_ty_eq ~error_details tal tbl in
(Eq : ((ta, tac) ty, (tb, tbc) ty) eq)
| (Map_t _, _) -> not_equal ()
| (Big_map_t (tal, tar, meta1), Big_map_t (tbl, tbr, meta2)) ->
let* () = type_metadata_eq meta1 meta2 in
let* Eq = help tar tbr in
let+ Eq = comparable_ty_eq ~error_details tal tbl in
(Eq : ((ta, tac) ty, (tb, tbc) ty) eq)
| (Big_map_t _, _) -> not_equal ()
| (Set_t (ea, meta1), Set_t (eb, meta2)) ->
let* () = type_metadata_eq meta1 meta2 in
let+ Eq = comparable_ty_eq ~error_details ea eb in
(Eq : ((ta, tac) ty, (tb, tbc) ty) eq)
| (Set_t _, _) -> not_equal ()
| (Ticket_t (ea, meta1), Ticket_t (eb, meta2)) ->
let* () = type_metadata_eq meta1 meta2 in
let+ Eq = comparable_ty_eq ~error_details ea eb in
(Eq : ((ta, tac) ty, (tb, tbc) ty) eq)
| (Ticket_t _, _) -> not_equal ()
| (Pair_t (tal, tar, meta1, cmp1), Pair_t (tbl, tbr, meta2, cmp2)) ->
let* () = type_metadata_eq meta1 meta2 in
let* Eq = help tal tbl in
let+ Eq = help tar tbr in
let Eq = Dependent_bool.merge_dand cmp1 cmp2 in
(Eq : ((ta, tac) ty, (tb, tbc) ty) eq)
| (Pair_t _, _) -> not_equal ()
| (Union_t (tal, tar, meta1, cmp1), Union_t (tbl, tbr, meta2, cmp2)) ->
let* () = type_metadata_eq meta1 meta2 in
let* Eq = help tal tbl in
let+ Eq = help tar tbr in
let Eq = Dependent_bool.merge_dand cmp1 cmp2 in
(Eq : ((ta, tac) ty, (tb, tbc) ty) eq)
| (Union_t _, _) -> not_equal ()
| (Lambda_t (tal, tar, meta1), Lambda_t (tbl, tbr, meta2)) ->
let* () = type_metadata_eq meta1 meta2 in
let* Eq = help tal tbl in
let+ Eq = help tar tbr in
(Eq : ((ta, tac) ty, (tb, tbc) ty) eq)
| (Lambda_t _, _) -> not_equal ()
| (Contract_t (tal, meta1), Contract_t (tbl, meta2)) ->
let* () = type_metadata_eq meta1 meta2 in
let+ Eq = help tal tbl in
(Eq : ((ta, tac) ty, (tb, tbc) ty) eq)
| (Contract_t _, _) -> not_equal ()
| (Option_t (tva, meta1, _), Option_t (tvb, meta2, _)) ->
let* () = type_metadata_eq meta1 meta2 in
let+ Eq = help tva tvb in
(Eq : ((ta, tac) ty, (tb, tbc) ty) eq)
| (Option_t _, _) -> not_equal ()
| (List_t (tva, meta1), List_t (tvb, meta2)) ->
let* () = type_metadata_eq meta1 meta2 in
let+ Eq = help tva tvb in
(Eq : ((ta, tac) ty, (tb, tbc) ty) eq)
| (List_t _, _) -> not_equal ()
| (Sapling_state_t ms1, Sapling_state_t ms2) ->
let+ () = memo_size_eq ms1 ms2 in
Eq
| (Sapling_state_t _, _) -> not_equal ()
| (Sapling_transaction_t ms1, Sapling_transaction_t ms2) ->
let+ () = memo_size_eq ms1 ms2 in
Eq
| (Sapling_transaction_t _, _) -> not_equal ()
| ( Sapling_transaction_deprecated_t ms1,
Sapling_transaction_deprecated_t ms2 ) ->
let+ () = memo_size_eq ms1 ms2 in
Eq
| (Sapling_transaction_deprecated_t _, _) -> not_equal ()
| (Chest_t, Chest_t) -> return Eq
| (Chest_t, _) -> not_equal ()
| (Chest_key_t, Chest_key_t) -> return Eq
| (Chest_key_t, _) -> not_equal ()
in
help ty1 ty2
[@@coq_axiom_with_reason "non-top-level mutual recursion"]
let rec stack_eq :
type ta tb ts tu.
Script.location ->
context ->
int ->
(ta, ts) stack_ty ->
(tb, tu) stack_ty ->
(((ta, ts) stack_ty, (tb, tu) stack_ty) eq * context) tzresult =
fun loc ctxt lvl stack1 stack2 ->
match (stack1, stack2) with
| (Bot_t, Bot_t) -> ok (Eq, ctxt)
| (Item_t (ty1, rest1), Item_t (ty2, rest2)) ->
Gas_monad.run ctxt @@ ty_eq ~error_details:Informative loc ty1 ty2
|> record_trace (Bad_stack_item lvl)
>>? fun (eq, ctxt) ->
eq >>? fun Eq ->
stack_eq loc ctxt (lvl + 1) rest1 rest2 >|? fun (Eq, ctxt) ->
((Eq : ((ta, ts) stack_ty, (tb, tu) stack_ty) eq), ctxt)
| (_, _) -> error Bad_stack_length
type ('a, 's) judgement =
| Typed : ('a, 's, 'b, 'u) descr -> ('a, 's) judgement
| Failed : {
descr : 'b 'u. ('b, 'u) stack_ty -> ('a, 's, 'b, 'u) descr;
}
-> ('a, 's) judgement
type ('a, 's, 'b, 'u, 'c, 'v) branch = {
branch :
'r 'f.
('a, 's, 'r, 'f) descr -> ('b, 'u, 'r, 'f) descr -> ('c, 'v, 'r, 'f) descr;
}
[@@unboxed]
let merge_branches :
type a s b u c v.
context ->
Script.location ->
(a, s) judgement ->
(b, u) judgement ->
(a, s, b, u, c, v) branch ->
((c, v) judgement * context) tzresult =
fun ctxt loc btr bfr {branch} ->
match (btr, bfr) with
| (Typed ({aft = aftbt; _} as dbt), Typed ({aft = aftbf; _} as dbf)) ->
let unmatched_branches () =
let aftbt = serialize_stack_for_error ctxt aftbt in
let aftbf = serialize_stack_for_error ctxt aftbf in
Unmatched_branches (loc, aftbt, aftbf)
in
record_trace_eval
unmatched_branches
( stack_eq loc ctxt 1 aftbt aftbf >|? fun (Eq, ctxt) ->
(Typed (branch dbt dbf), ctxt) )
| (Failed {descr = descrt}, Failed {descr = descrf}) ->
let descr ret = branch (descrt ret) (descrf ret) in
ok (Failed {descr}, ctxt)
| (Typed dbt, Failed {descr = descrf}) ->
ok (Typed (branch dbt (descrf dbt.aft)), ctxt)
| (Failed {descr = descrt}, Typed dbf) ->
ok (Typed (branch (descrt dbf.aft) dbf), ctxt)
let parse_memo_size (n : (location, _) Micheline.node) :
Sapling.Memo_size.t tzresult =
match n with
| Int (_, z) -> (
match Sapling.Memo_size.parse_z z with
| Ok _ as ok_memo_size -> ok_memo_size [@coq_cast]
| Error msg ->
error
@@ Invalid_syntactic_constant (location n, strip_locations n, msg))
| _ -> error @@ Invalid_kind (location n, [Int_kind], kind n)
type ex_comparable_ty =
| Ex_comparable_ty : 'a comparable_ty -> ex_comparable_ty
let[@coq_struct "ty"] rec parse_comparable_ty :
stack_depth:int ->
context ->
Script.node ->
(ex_comparable_ty * context) tzresult =
fun ~stack_depth ctxt ty ->
Gas.consume ctxt Typecheck_costs.parse_type_cycle >>? fun ctxt ->
if Compare.Int.(stack_depth > 10000) then
error Typechecking_too_many_recursive_calls
else
match ty with
| Prim (loc, T_unit, [], annot) ->
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty unit_key, ctxt)
| Prim (loc, T_never, [], annot) ->
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty never_key, ctxt)
| Prim (loc, T_int, [], annot) ->
check_type_annot loc annot >|? fun () -> (Ex_comparable_ty int_key, ctxt)
| Prim (loc, T_nat, [], annot) ->
check_type_annot loc annot >|? fun () -> (Ex_comparable_ty nat_key, ctxt)
| Prim (loc, T_signature, [], annot) ->
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty signature_key, ctxt)
| Prim (loc, T_string, [], annot) ->
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty string_key, ctxt)
| Prim (loc, T_bytes, [], annot) ->
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty bytes_key, ctxt)
| Prim (loc, T_mutez, [], annot) ->
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty mutez_key, ctxt)
| Prim (loc, T_bool, [], annot) ->
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty bool_key, ctxt)
| Prim (loc, T_key_hash, [], annot) ->
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty key_hash_key, ctxt)
| Prim (loc, T_key, [], annot) ->
check_type_annot loc annot >|? fun () -> (Ex_comparable_ty key_key, ctxt)
| Prim (loc, T_timestamp, [], annot) ->
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty timestamp_key, ctxt)
| Prim (loc, T_chain_id, [], annot) ->
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty chain_id_key, ctxt)
| Prim (loc, T_address, [], annot) ->
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty address_key, ctxt)
| Prim (loc, T_tx_rollup_l2_address, [], annot) ->
if Constants.tx_rollup_enable ctxt then
check_type_annot loc annot >|? fun () ->
(Ex_comparable_ty tx_rollup_l2_address_key, ctxt)
else error @@ Tx_rollup_addresses_disabled loc
| Prim
( loc,
(( T_unit | T_never | T_int | T_nat | T_string | T_bytes | T_mutez
| T_bool | T_key_hash | T_timestamp | T_address | T_chain_id
| T_signature | T_key ) as prim),
l,
_ ) ->
error (Invalid_arity (loc, prim, 0, List.length l))
| Prim (loc, T_pair, left :: right, annot) ->
check_type_annot loc annot >>? fun () ->
remove_field_annot left >>? fun left ->
(match right with
| [right] -> remove_field_annot right
| right ->
ok (Prim (loc, T_pair, right, [])))
>>? fun right ->
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt right
>>? fun (Ex_comparable_ty right, ctxt) ->
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt left
>>? fun (Ex_comparable_ty left, ctxt) ->
pair_key loc left right >|? fun ty -> (Ex_comparable_ty ty, ctxt)
| Prim (loc, T_or, [left; right], annot) ->
check_type_annot loc annot >>? fun () ->
remove_field_annot left >>? fun left ->
remove_field_annot right >>? fun right ->
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt right
>>? fun (Ex_comparable_ty right, ctxt) ->
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt left
>>? fun (Ex_comparable_ty left, ctxt) ->
union_key loc left right >|? fun ty -> (Ex_comparable_ty ty, ctxt)
| Prim (loc, ((T_pair | T_or) as prim), l, _) ->
error (Invalid_arity (loc, prim, 2, List.length l))
| Prim (loc, T_option, [t], annot) ->
check_type_annot loc annot >>? fun () ->
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt t
>>? fun (Ex_comparable_ty t, ctxt) ->
option_key loc t >|? fun ty -> (Ex_comparable_ty ty, ctxt)
| Prim (loc, T_option, l, _) ->
error (Invalid_arity (loc, T_option, 1, List.length l))
| Prim
( loc,
(T_set | T_map | T_list | T_lambda | T_contract | T_operation),
_,
_ ) ->
error (Comparable_type_expected (loc, Micheline.strip_locations ty))
| expr ->
error
@@ unexpected
expr
[]
Type_namespace
[
T_unit;
T_never;
T_int;
T_nat;
T_string;
T_bytes;
T_mutez;
T_bool;
T_key_hash;
T_timestamp;
T_address;
T_pair;
T_or;
T_option;
T_chain_id;
T_signature;
T_key;
]
type ex_ty = Ex_ty : ('a, _) ty -> ex_ty
type ex_parameter_ty_and_entrypoints_node =
| Ex_parameter_ty_and_entrypoints_node : {
arg_type : ('a, _) ty;
entrypoints : 'a entrypoints_node;
}
-> ex_parameter_ty_and_entrypoints_node
(** [parse_ty] can be used to parse regular types as well as parameter types
together with their entrypoints.
In the first case, use [~ret:Don't_parse_entrypoints], [parse_ty] will
return an [ex_ty].
In the second case, use [~ret:Parse_entrypoints], [parse_ty] will return
an [ex_parameter_ty_and_entrypoints_node].
*)
type ('ret, 'name) parse_ty_ret =
| Don't_parse_entrypoints : (ex_ty, unit) parse_ty_ret
| Parse_entrypoints
: (ex_parameter_ty_and_entrypoints_node, Entrypoint.t option) parse_ty_ret
let[@coq_axiom_with_reason "complex mutually recursive definition"] rec parse_ty :
type ret name.
context ->
stack_depth:int ->
legacy:bool ->
allow_lazy_storage:bool ->
allow_operation:bool ->
allow_contract:bool ->
allow_ticket:bool ->
ret:(ret, name) parse_ty_ret ->
Script.node ->
(ret * context) tzresult =
fun ctxt
~stack_depth
~legacy
~allow_lazy_storage
~allow_operation
~allow_contract
~allow_ticket
~ret
node ->
Gas.consume ctxt Typecheck_costs.parse_type_cycle >>? fun ctxt ->
if Compare.Int.(stack_depth > 10000) then
error Typechecking_too_many_recursive_calls
else
(match ret with
| Don't_parse_entrypoints -> ok (node, (() : name))
| Parse_entrypoints -> extract_entrypoint_annot node)
>>? fun (node, name) ->
let return ctxt ty : ret * context =
match ret with
| Don't_parse_entrypoints -> (Ex_ty ty, ctxt)
| Parse_entrypoints ->
let at_node =
Option.map (fun name -> {name; original_type_expr = node}) name
in
( Ex_parameter_ty_and_entrypoints_node
{
arg_type = ty;
entrypoints = {at_node; nested = Entrypoints_None};
},
ctxt )
in
match node with
| Prim (loc, T_unit, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt unit_t
| Prim (loc, T_int, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt int_t
| Prim (loc, T_nat, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt nat_t
| Prim (loc, T_string, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt string_t
| Prim (loc, T_bytes, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt bytes_t
| Prim (loc, T_mutez, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt mutez_t
| Prim (loc, T_bool, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt bool_t
| Prim (loc, T_key, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt key_t
| Prim (loc, T_key_hash, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt key_hash_t
| Prim (loc, T_chest_key, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt chest_key_t
| Prim (loc, T_chest, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt chest_t
| Prim (loc, T_timestamp, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt timestamp_t
| Prim (loc, T_address, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt address_t
| Prim (loc, T_tx_rollup_l2_address, [], annot) ->
if Constants.tx_rollup_enable ctxt then
check_type_annot loc annot >|? fun () ->
return ctxt tx_rollup_l2_address_t
else error @@ Tx_rollup_addresses_disabled loc
| Prim (loc, T_signature, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt signature_t
| Prim (loc, T_operation, [], annot) ->
if allow_operation then
check_type_annot loc annot >|? fun () -> return ctxt operation_t
else error (Unexpected_operation loc)
| Prim (loc, T_chain_id, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt chain_id_t
| Prim (loc, T_never, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt never_t
| Prim (loc, T_bls12_381_g1, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt bls12_381_g1_t
| Prim (loc, T_bls12_381_g2, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt bls12_381_g2_t
| Prim (loc, T_bls12_381_fr, [], annot) ->
check_type_annot loc annot >|? fun () -> return ctxt bls12_381_fr_t
| Prim (loc, T_contract, [utl], annot) ->
if allow_contract then
parse_passable_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
utl
~ret:Don't_parse_entrypoints
>>? fun (Ex_ty tl, ctxt) ->
check_type_annot loc annot >>? fun () ->
contract_t loc tl >|? fun ty -> return ctxt ty
else error (Unexpected_contract loc)
| Prim (loc, T_pair, utl :: utr, annot) ->
remove_field_annot utl >>? fun utl ->
parse_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
~allow_lazy_storage
~allow_operation
~allow_contract
~allow_ticket
~ret:Don't_parse_entrypoints
utl
>>? fun (Ex_ty tl, ctxt) ->
(match utr with
| [utr] -> remove_field_annot utr
| utr ->
ok (Prim (loc, T_pair, utr, [])))
>>? fun utr ->
parse_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
~allow_lazy_storage
~allow_operation
~allow_contract
~allow_ticket
~ret:Don't_parse_entrypoints
utr
>>? fun (Ex_ty tr, ctxt) ->
check_type_annot loc annot >>? fun () ->
pair_t loc tl tr >|? fun (Ty_ex_c ty) -> return ctxt ty
| Prim (loc, T_or, [utl; utr], annot) -> (
(match ret with
| Don't_parse_entrypoints ->
remove_field_annot utl >>? fun utl ->
remove_field_annot utr >|? fun utr -> (utl, utr)
| Parse_entrypoints -> ok (utl, utr))
>>? fun (utl, utr) ->
parse_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
~allow_lazy_storage
~allow_operation
~allow_contract
~allow_ticket
~ret
utl
>>? fun (parsed_l, ctxt) ->
parse_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
~allow_lazy_storage
~allow_operation
~allow_contract
~allow_ticket
~ret
utr
>>? fun (parsed_r, ctxt) ->
check_type_annot loc annot >>? fun () ->
match ret with
| Don't_parse_entrypoints ->
let (Ex_ty tl) = parsed_l in
let (Ex_ty tr) = parsed_r in
union_t loc tl tr >|? fun (Ty_ex_c ty) -> ((Ex_ty ty : ret), ctxt)
| Parse_entrypoints ->
let (Ex_parameter_ty_and_entrypoints_node
{arg_type = tl; entrypoints = left}) =
parsed_l
in
let (Ex_parameter_ty_and_entrypoints_node
{arg_type = tr; entrypoints = right}) =
parsed_r
in
union_t loc tl tr >|? fun (Ty_ex_c arg_type) ->
let entrypoints =
let at_node =
Option.map (fun name -> {name; original_type_expr = node}) name
in
{at_node; nested = Entrypoints_Union {left; right}}
in
(Ex_parameter_ty_and_entrypoints_node {arg_type; entrypoints}, ctxt)
)
| Prim (loc, T_lambda, [uta; utr], annot) ->
parse_any_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy uta
>>? fun (Ex_ty ta, ctxt) ->
parse_any_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy utr
>>? fun (Ex_ty tr, ctxt) ->
check_type_annot loc annot >>? fun () ->
lambda_t loc ta tr >|? fun ty -> return ctxt ty
| Prim (loc, T_option, [ut], annot) ->
(if legacy then
remove_field_annot ut >>? fun ut ->
check_composed_type_annot loc annot >>? fun () -> ok ut
else check_type_annot loc annot >>? fun () -> ok ut)
>>? fun ut ->
parse_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
~allow_lazy_storage
~allow_operation
~allow_contract
~allow_ticket
~ret:Don't_parse_entrypoints
ut
>>? fun (Ex_ty t, ctxt) ->
option_t loc t >|? fun ty -> return ctxt ty
| Prim (loc, T_list, [ut], annot) ->
parse_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
~allow_lazy_storage
~allow_operation
~allow_contract
~allow_ticket
~ret:Don't_parse_entrypoints
ut
>>? fun (Ex_ty t, ctxt) ->
check_type_annot loc annot >>? fun () ->
list_t loc t >|? fun ty -> return ctxt ty
| Prim (loc, T_ticket, [ut], annot) ->
if allow_ticket then
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt ut
>>? fun (Ex_comparable_ty t, ctxt) ->
check_type_annot loc annot >>? fun () ->
ticket_t loc t >|? fun ty -> return ctxt ty
else error (Unexpected_ticket loc)
| Prim (loc, T_set, [ut], annot) ->
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt ut
>>? fun (Ex_comparable_ty t, ctxt) ->
check_type_annot loc annot >>? fun () ->
set_t loc t >|? fun ty -> return ctxt ty
| Prim (loc, T_map, [uta; utr], annot) ->
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt uta
>>? fun (Ex_comparable_ty ta, ctxt) ->
parse_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
~allow_lazy_storage
~allow_operation
~allow_contract
~allow_ticket
~ret:Don't_parse_entrypoints
utr
>>? fun (Ex_ty tr, ctxt) ->
check_type_annot loc annot >>? fun () ->
map_t loc ta tr >|? fun ty -> return ctxt ty
| Prim (loc, T_sapling_transaction, [memo_size], annot) ->
check_type_annot loc annot >>? fun () ->
parse_memo_size memo_size >|? fun memo_size ->
return ctxt (sapling_transaction_t ~memo_size)
| Prim (loc, T_sapling_transaction_deprecated, [memo_size], annot) ->
if legacy then
check_type_annot loc annot >>? fun () ->
parse_memo_size memo_size >|? fun memo_size ->
return ctxt (sapling_transaction_deprecated_t ~memo_size)
else error (Deprecated_instruction T_sapling_transaction_deprecated)
| Prim (loc, T_big_map, args, annot) when allow_lazy_storage ->
parse_big_map_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
loc
args
annot
>|? fun (Ex_ty ty, ctxt) -> return ctxt ty
| Prim (loc, T_sapling_state, [memo_size], annot) when allow_lazy_storage ->
check_type_annot loc annot >>? fun () ->
parse_memo_size memo_size >|? fun memo_size ->
return ctxt (sapling_state_t ~memo_size)
| Prim (loc, (T_big_map | T_sapling_state), _, _) ->
error (Unexpected_lazy_storage loc)
| Prim
( loc,
(( T_unit | T_signature | T_int | T_nat | T_string | T_bytes | T_mutez
| T_bool | T_key | T_key_hash | T_timestamp | T_address
| T_tx_rollup_l2_address | T_chain_id | T_operation | T_never ) as
prim),
l,
_ ) ->
error (Invalid_arity (loc, prim, 0, List.length l))
| Prim
( loc,
((T_set | T_list | T_option | T_contract | T_ticket) as prim),
l,
_ ) ->
error (Invalid_arity (loc, prim, 1, List.length l))
| Prim (loc, ((T_pair | T_or | T_map | T_lambda) as prim), l, _) ->
error (Invalid_arity (loc, prim, 2, List.length l))
| expr ->
error
@@ unexpected
expr
[]
Type_namespace
[
T_pair;
T_or;
T_set;
T_map;
T_list;
T_option;
T_lambda;
T_unit;
T_signature;
T_contract;
T_int;
T_nat;
T_operation;
T_string;
T_bytes;
T_mutez;
T_bool;
T_key;
T_key_hash;
T_timestamp;
T_chain_id;
T_never;
T_bls12_381_g1;
T_bls12_381_g2;
T_bls12_381_fr;
T_ticket;
T_tx_rollup_l2_address;
]
and[@coq_axiom_with_reason "complex mutually recursive definition"] parse_passable_ty :
type ret name.
context ->
stack_depth:int ->
legacy:bool ->
ret:(ret, name) parse_ty_ret ->
Script.node ->
(ret * context) tzresult =
fun ctxt ~stack_depth ~legacy ->
(parse_ty [@tailcall])
ctxt
~stack_depth
~legacy
~allow_lazy_storage:true
~allow_operation:false
~allow_contract:true
~allow_ticket:true
and[@coq_axiom_with_reason "complex mutually recursive definition"] parse_any_ty
:
context ->
stack_depth:int ->
legacy:bool ->
Script.node ->
(ex_ty * context) tzresult =
fun ctxt ~stack_depth ~legacy ->
(parse_ty [@tailcall])
ctxt
~stack_depth
~legacy
~allow_lazy_storage:true
~allow_operation:true
~allow_contract:true
~allow_ticket:true
~ret:Don't_parse_entrypoints
and[@coq_axiom_with_reason "complex mutually recursive definition"] parse_big_map_ty
ctxt ~stack_depth ~legacy big_map_loc args map_annot =
Gas.consume ctxt Typecheck_costs.parse_type_cycle >>? fun ctxt ->
match args with
| [key_ty; value_ty] ->
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt key_ty
>>? fun (Ex_comparable_ty key_ty, ctxt) ->
parse_big_map_value_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
value_ty
>>? fun (Ex_ty value_ty, ctxt) ->
check_type_annot big_map_loc map_annot >>? fun () ->
big_map_t big_map_loc key_ty value_ty >|? fun big_map_ty ->
(Ex_ty big_map_ty, ctxt)
| args -> error @@ Invalid_arity (big_map_loc, T_big_map, 2, List.length args)
and[@coq_axiom_with_reason "complex mutually recursive definition"] parse_big_map_value_ty
ctxt ~stack_depth ~legacy value_ty =
(parse_ty [@tailcall])
ctxt
~stack_depth
~legacy
~allow_lazy_storage:false
~allow_operation:false
~allow_contract:legacy
~allow_ticket:true
~ret:Don't_parse_entrypoints
value_ty
let parse_packable_ty ctxt ~stack_depth ~legacy node =
(parse_ty [@tailcall])
ctxt
~stack_depth
~legacy
~allow_lazy_storage:false
~allow_operation:false
~allow_contract:
legacy
~allow_ticket:false
~ret:Don't_parse_entrypoints
node
let parse_view_input_ty ctxt ~stack_depth ~legacy node =
(parse_ty [@tailcall])
ctxt
~stack_depth
~legacy
~allow_lazy_storage:false
~allow_operation:false
~allow_contract:true
~allow_ticket:false
~ret:Don't_parse_entrypoints
node
let parse_view_output_ty ctxt ~stack_depth ~legacy node =
(parse_ty [@tailcall])
ctxt
~stack_depth
~legacy
~allow_lazy_storage:false
~allow_operation:false
~allow_contract:true
~allow_ticket:false
~ret:Don't_parse_entrypoints
node
let parse_normal_storage_ty ctxt ~stack_depth ~legacy node =
(parse_ty [@tailcall])
ctxt
~stack_depth
~legacy
~allow_lazy_storage:true
~allow_operation:false
~allow_contract:legacy
~allow_ticket:true
~ret:Don't_parse_entrypoints
node
let parse_storage_ty :
context ->
stack_depth:int ->
legacy:bool ->
Script.node ->
(ex_ty * context) tzresult =
fun ctxt ~stack_depth ~legacy node ->
match node with
| Prim
( loc,
T_pair,
[Prim (big_map_loc, T_big_map, args, map_annot); remaining_storage],
storage_annot )
when legacy -> (
match storage_annot with
| [] ->
(parse_normal_storage_ty [@tailcall]) ctxt ~stack_depth ~legacy node
| [single]
when Compare.Int.(String.length single > 0)
&& Compare.Char.(single.[0] = '%') ->
(parse_normal_storage_ty [@tailcall]) ctxt ~stack_depth ~legacy node
| _ ->
Gas.consume ctxt Typecheck_costs.parse_type_cycle >>? fun ctxt ->
parse_big_map_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
big_map_loc
args
map_annot
>>? fun (Ex_ty big_map_ty, ctxt) ->
parse_normal_storage_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
remaining_storage
>>? fun (Ex_ty remaining_storage, ctxt) ->
check_composed_type_annot loc storage_annot >>? fun () ->
pair_t loc big_map_ty remaining_storage >|? fun (Ty_ex_c ty) ->
(Ex_ty ty, ctxt))
| _ -> (parse_normal_storage_ty [@tailcall]) ctxt ~stack_depth ~legacy node
let check_packable ~legacy loc root =
let rec check : type t tc. (t, tc) ty -> unit tzresult = function
| Big_map_t _ -> error (Unexpected_lazy_storage loc)
| Sapling_state_t _ -> error (Unexpected_lazy_storage loc)
| Operation_t -> error (Unexpected_operation loc)
| Unit_t -> Result.return_unit
| Int_t -> Result.return_unit
| Nat_t -> Result.return_unit
| Signature_t -> Result.return_unit
| String_t -> Result.return_unit
| Bytes_t -> Result.return_unit
| Mutez_t -> Result.return_unit
| Key_hash_t -> Result.return_unit
| Key_t -> Result.return_unit
| Timestamp_t -> Result.return_unit
| Address_t -> Result.return_unit
| Tx_rollup_l2_address_t -> Result.return_unit
| Bool_t -> Result.return_unit
| Chain_id_t -> Result.return_unit
| Never_t -> Result.return_unit
| Set_t (_, _) -> Result.return_unit
| Ticket_t _ -> error (Unexpected_ticket loc)
| Lambda_t (_, _, _) -> Result.return_unit
| Bls12_381_g1_t -> Result.return_unit
| Bls12_381_g2_t -> Result.return_unit
| Bls12_381_fr_t -> Result.return_unit
| Pair_t (l_ty, r_ty, _, _) -> check l_ty >>? fun () -> check r_ty
| Union_t (l_ty, r_ty, _, _) -> check l_ty >>? fun () -> check r_ty
| Option_t (v_ty, _, _) -> check v_ty
| List_t (elt_ty, _) -> check elt_ty
| Map_t (_, elt_ty, _) -> check elt_ty
| Contract_t (_, _) when legacy -> Result.return_unit
| Contract_t (_, _) -> error (Unexpected_contract loc)
| Sapling_transaction_t _ -> ok ()
| Sapling_transaction_deprecated_t _ -> ok ()
| Chest_key_t -> Result.return_unit
| Chest_t -> Result.return_unit
in
check root
type toplevel = {
code_field : Script.node;
arg_type : Script.node;
storage_type : Script.node;
views : view_map;
}
type ('arg, 'storage) code =
| Code : {
code :
(('arg, 'storage) pair, (operation boxed_list, 'storage) pair) lambda;
arg_type : ('arg, _) ty;
storage_type : ('storage, _) ty;
views : view_map;
entrypoints : 'arg entrypoints;
code_size : Cache_memory_helpers.sint;
}
-> ('arg, 'storage) code
type ex_script = Ex_script : ('a, 'c) Script_typed_ir.script -> ex_script
type ex_code = Ex_code : ('a, 'c) code -> ex_code
type 'storage typed_view =
| Typed_view : {
input_ty : ('input, _) ty;
output_ty : ('output, _) ty;
kinstr : ('input * 'storage, end_of_stack, 'output, end_of_stack) kinstr;
original_code_expr : Script.node;
}
-> 'storage typed_view
type 'storage typed_view_map = (Script_string.t, 'storage typed_view) map
type (_, _) dig_proof_argument =
| Dig_proof_argument :
('x, 'a * 's, 'a, 's, 'b, 't, 'c, 'u) stack_prefix_preservation_witness
* ('x, _) ty
* ('c, 'u) stack_ty
-> ('b, 't) dig_proof_argument
type (_, _, _) dug_proof_argument =
| Dug_proof_argument :
(('a, 's, 'x, 'a * 's, 'b, 't, 'c, 'u) stack_prefix_preservation_witness
* ('c, 'u) stack_ty)
-> ('b, 't, 'x) dug_proof_argument
type (_, _) dipn_proof_argument =
| Dipn_proof_argument :
('fa, 'fs, 'fb, 'fu, 'a, 's, 'b, 'u) stack_prefix_preservation_witness
* context
* ('fa, 'fs, 'fb, 'fu) descr
* ('b, 'u) stack_ty
-> ('a, 's) dipn_proof_argument
type (_, _) dropn_proof_argument =
| Dropn_proof_argument :
('fa, 'fs, 'fa, 'fs, 'a, 's, 'a, 's) stack_prefix_preservation_witness
* ('fa, 'fs) stack_ty
-> ('a, 's) dropn_proof_argument
type 'before comb_proof_argument =
| Comb_proof_argument :
('a * 's, 'b * 'u) comb_gadt_witness * ('b, 'u) stack_ty
-> ('a * 's) comb_proof_argument
type 'before uncomb_proof_argument =
| Uncomb_proof_argument :
('a * 's, 'b * 'u) uncomb_gadt_witness * ('b, 'u) stack_ty
-> ('a * 's) uncomb_proof_argument
type 'before comb_get_proof_argument =
| Comb_get_proof_argument :
('before, 'after) comb_get_gadt_witness * ('after, _) ty
-> 'before comb_get_proof_argument
type ('rest, 'before) comb_set_proof_argument =
| Comb_set_proof_argument :
('rest, 'before, 'after) comb_set_gadt_witness * ('after, _) ty
-> ('rest, 'before) comb_set_proof_argument
type 'before dup_n_proof_argument =
| Dup_n_proof_argument :
('before, 'a) dup_n_gadt_witness * ('a, _) ty
-> 'before dup_n_proof_argument
let rec make_dug_proof_argument :
type a s x xc.
location ->
int ->
(x, xc) ty ->
(a, s) stack_ty ->
(a, s, x) dug_proof_argument option =
fun loc n x stk ->
match (n, stk) with
| (0, rest) -> Some (Dug_proof_argument (KRest, Item_t (x, rest)))
| (n, Item_t (v, rest)) ->
make_dug_proof_argument loc (n - 1) x rest
|> Option.map @@ fun (Dug_proof_argument (n', aft')) ->
let kinfo = {iloc = loc; kstack_ty = aft'} in
Dug_proof_argument (KPrefix (kinfo, n'), Item_t (v, aft'))
| (_, _) -> None
let rec make_comb_get_proof_argument :
type b bc. int -> (b, bc) ty -> b comb_get_proof_argument option =
fun n ty ->
match (n, ty) with
| (0, value_ty) -> Some (Comb_get_proof_argument (Comb_get_zero, value_ty))
| (1, Pair_t (hd_ty, _, _annot, _)) ->
Some (Comb_get_proof_argument (Comb_get_one, hd_ty))
| (n, Pair_t (_, tl_ty, _annot, _)) ->
make_comb_get_proof_argument (n - 2) tl_ty
|> Option.map
@@ fun (Comb_get_proof_argument (comb_get_left_witness, ty')) ->
Comb_get_proof_argument (Comb_get_plus_two comb_get_left_witness, ty')
| _ -> None
let rec make_comb_set_proof_argument :
type value valuec before beforec a s.
context ->
(a, s) stack_ty ->
location ->
int ->
(value, valuec) ty ->
(before, beforec) ty ->
(value, before) comb_set_proof_argument tzresult =
fun ctxt stack_ty loc n value_ty ty ->
match (n, ty) with
| (0, _) -> ok @@ Comb_set_proof_argument (Comb_set_zero, value_ty)
| (1, Pair_t (_hd_ty, tl_ty, _, _)) ->
pair_t loc value_ty tl_ty >|? fun (Ty_ex_c after_ty) ->
Comb_set_proof_argument (Comb_set_one, after_ty)
| (n, Pair_t (hd_ty, tl_ty, _, _)) ->
make_comb_set_proof_argument ctxt stack_ty loc (n - 2) value_ty tl_ty
>>? fun (Comb_set_proof_argument (comb_set_left_witness, tl_ty')) ->
pair_t loc hd_ty tl_ty' >|? fun (Ty_ex_c after_ty) ->
Comb_set_proof_argument (Comb_set_plus_two comb_set_left_witness, after_ty)
| _ ->
let whole_stack = serialize_stack_for_error ctxt stack_ty in
error (Bad_stack (loc, I_UPDATE, 2, whole_stack))
type 'a ex_ty_cstr =
| Ex_ty_cstr : {
ty : ('b, _) Script_typed_ir.ty;
construct : 'b -> 'a;
original_type_expr : Script.node;
}
-> 'a ex_ty_cstr
let find_entrypoint (type full fullc error_trace)
~(error_details : error_trace error_details) (full : (full, fullc) ty)
(entrypoints : full entrypoints) entrypoint :
(full ex_ty_cstr, error_trace) Gas_monad.t =
let open Gas_monad.Syntax in
let rec find_entrypoint :
type t tc.
(t, tc) ty ->
t entrypoints_node ->
Entrypoint.t ->
(t ex_ty_cstr, unit) Gas_monad.t =
fun ty entrypoints entrypoint ->
let* () = Gas_monad.consume_gas Typecheck_costs.find_entrypoint_cycle in
match (ty, entrypoints) with
| (_, {at_node = Some {name; original_type_expr}; _})
when Entrypoint.(name = entrypoint) ->
return (Ex_ty_cstr {ty; construct = (fun e -> e); original_type_expr})
| (Union_t (tl, tr, _, _), {nested = Entrypoints_Union {left; right}; _})
-> (
Gas_monad.bind_recover (find_entrypoint tl left entrypoint) @@ function
| Ok (Ex_ty_cstr {ty; construct; original_type_expr}) ->
return
(Ex_ty_cstr
{
ty;
construct = (fun e -> L (construct e));
original_type_expr;
})
| Error () ->
let+ (Ex_ty_cstr {ty; construct; original_type_expr}) =
find_entrypoint tr right entrypoint
in
Ex_ty_cstr
{ty; construct = (fun e -> R (construct e)); original_type_expr})
| (_, {nested = Entrypoints_None; _}) -> Gas_monad.of_result (Error ())
in
let {root; original_type_expr} = entrypoints in
Gas_monad.bind_recover (find_entrypoint full root entrypoint) @@ function
| Ok f_t -> return f_t
| Error () ->
if Entrypoint.is_default entrypoint then
return
(Ex_ty_cstr {ty = full; construct = (fun e -> e); original_type_expr})
else
Gas_monad.of_result
@@ Error
(match error_details with
| Fast -> (Inconsistent_types_fast : error_trace)
| Informative -> trace_of_error @@ No_such_entrypoint entrypoint)
let find_entrypoint_for_type (type full fullc exp expc error_trace)
~error_details ~(full : (full, fullc) ty) ~(expected : (exp, expc) ty)
entrypoints entrypoint loc :
(Entrypoint.t * (exp, expc) ty, error_trace) Gas_monad.t =
let open Gas_monad.Syntax in
let* res = find_entrypoint ~error_details full entrypoints entrypoint in
match res with
| Ex_ty_cstr {ty; _} -> (
match entrypoints.root.at_node with
| Some {name; original_type_expr = _}
when Entrypoint.is_root name && Entrypoint.is_default entrypoint ->
Gas_monad.bind_recover
(ty_eq ~error_details:Fast loc ty expected)
(function
| Ok Eq -> return (Entrypoint.default, (ty : (exp, expc) ty))
| Error Inconsistent_types_fast ->
let+ Eq = ty_eq ~error_details loc full expected in
(Entrypoint.root, (full : (exp, expc) ty)))
| _ ->
let+ Eq = ty_eq ~error_details loc ty expected in
(entrypoint, (ty : (exp, expc) ty)))
let well_formed_entrypoints (type full fullc) (full : (full, fullc) ty)
entrypoints =
let merge path (type t tc) (ty : (t, tc) ty)
(entrypoints : t entrypoints_node) reachable
((first_unreachable, all) as acc) =
match entrypoints.at_node with
| None ->
ok
( (if reachable then acc
else
match ty with
| Union_t _ -> acc
| _ -> (
match first_unreachable with
| None -> (Some (List.rev path), all)
| Some _ -> acc)),
reachable )
| Some {name; original_type_expr = _} ->
if Entrypoint.Set.mem name all then error (Duplicate_entrypoint name)
else ok ((first_unreachable, Entrypoint.Set.add name all), true)
in
let rec check :
type t tc.
(t, tc) ty ->
t entrypoints_node ->
prim list ->
bool ->
prim list option * Entrypoint.Set.t ->
(prim list option * Entrypoint.Set.t) tzresult =
fun t entrypoints path reachable acc ->
match (t, entrypoints) with
| (Union_t (tl, tr, _, _), {nested = Entrypoints_Union {left; right}; _}) ->
merge (D_Left :: path) tl left reachable acc
>>? fun (acc, l_reachable) ->
merge (D_Right :: path) tr right reachable acc
>>? fun (acc, r_reachable) ->
check tl left (D_Left :: path) l_reachable acc >>? fun acc ->
check tr right (D_Right :: path) r_reachable acc
| _ -> ok acc
in
let (init, reachable) =
match entrypoints.at_node with
| None -> (Entrypoint.Set.empty, false)
| Some {name; original_type_expr = _} ->
(Entrypoint.Set.singleton name, true)
in
check full entrypoints [] reachable (None, init)
>>? fun (first_unreachable, all) ->
if not (Entrypoint.Set.mem Entrypoint.default all) then Result.return_unit
else
match first_unreachable with
| None -> Result.return_unit
| Some path -> error (Unreachable_entrypoint path)
type ex_parameter_ty_and_entrypoints =
| Ex_parameter_ty_and_entrypoints : {
arg_type : ('a, _) ty;
entrypoints : 'a entrypoints;
}
-> ex_parameter_ty_and_entrypoints
let parse_parameter_ty_and_entrypoints :
context ->
stack_depth:int ->
legacy:bool ->
Script.node ->
(ex_parameter_ty_and_entrypoints * context) tzresult =
fun ctxt ~stack_depth ~legacy node ->
parse_passable_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
node
~ret:Parse_entrypoints
>>? fun (Ex_parameter_ty_and_entrypoints_node {arg_type; entrypoints}, ctxt)
->
(if legacy then Result.return_unit
else well_formed_entrypoints arg_type entrypoints)
>|? fun () ->
let entrypoints = {root = entrypoints; original_type_expr = node} in
(Ex_parameter_ty_and_entrypoints {arg_type; entrypoints}, ctxt)
let parse_passable_ty = parse_passable_ty ~ret:Don't_parse_entrypoints
let parse_uint ~nb_bits =
assert (Compare.Int.(nb_bits >= 0 && nb_bits <= 30)) ;
let max_int = (1 lsl nb_bits) - 1 in
let max_z = Z.of_int max_int in
function
| Micheline.Int (_, n) when Compare.Z.(Z.zero <= n) && Compare.Z.(n <= max_z)
->
ok (Z.to_int n)
| node ->
error
@@ Invalid_syntactic_constant
( location node,
strip_locations node,
"a positive " ^ string_of_int nb_bits
^ "-bit integer (between 0 and " ^ string_of_int max_int ^ ")" )
let parse_uint10 = parse_uint ~nb_bits:10
let parse_uint11 = parse_uint ~nb_bits:11
let opened_ticket_type loc ty = pair_3_key loc address_key ty nat_key
let parse_unit ctxt ~legacy = function
| Prim (loc, D_Unit, [], annot) ->
(if legacy then Result.return_unit else error_unexpected_annot loc annot)
>>? fun () ->
Gas.consume ctxt Typecheck_costs.unit >|? fun ctxt -> ((), ctxt)
| Prim (loc, D_Unit, l, _) ->
error @@ Invalid_arity (loc, D_Unit, 0, List.length l)
| expr -> error @@ unexpected expr [] Constant_namespace [D_Unit]
let parse_bool ctxt ~legacy = function
| Prim (loc, D_True, [], annot) ->
(if legacy then Result.return_unit else error_unexpected_annot loc annot)
>>? fun () ->
Gas.consume ctxt Typecheck_costs.bool >|? fun ctxt -> (true, ctxt)
| Prim (loc, D_False, [], annot) ->
(if legacy then Result.return_unit else error_unexpected_annot loc annot)
>>? fun () ->
Gas.consume ctxt Typecheck_costs.bool >|? fun ctxt -> (false, ctxt)
| Prim (loc, ((D_True | D_False) as c), l, _) ->
error @@ Invalid_arity (loc, c, 0, List.length l)
| expr -> error @@ unexpected expr [] Constant_namespace [D_True; D_False]
let parse_string ctxt : Script.node -> (Script_string.t * context) tzresult =
function
| String (loc, v) as expr ->
Gas.consume ctxt (Typecheck_costs.check_printable v) >>? fun ctxt ->
record_trace
(Invalid_syntactic_constant
(loc, strip_locations expr, "a printable ascii string"))
(Script_string.of_string v >|? fun s -> (s, ctxt))
| expr -> error @@ Invalid_kind (location expr, [String_kind], kind expr)
let parse_bytes ctxt = function
| Bytes (_, v) -> ok (v, ctxt)
| expr -> error @@ Invalid_kind (location expr, [Bytes_kind], kind expr)
let parse_int ctxt = function
| Int (_, v) -> ok (Script_int.of_zint v, ctxt)
| expr -> error @@ Invalid_kind (location expr, [Int_kind], kind expr)
let parse_nat ctxt :
Script.node -> (Script_int.n Script_int.num * context) tzresult = function
| Int (loc, v) as expr -> (
let v = Script_int.of_zint v in
match Script_int.is_nat v with
| Some nat -> ok (nat, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a non-negative integer"))
| expr -> error @@ Invalid_kind (location expr, [Int_kind], kind expr)
let parse_mutez ctxt : Script.node -> (Tez.t * context) tzresult = function
| Int (loc, v) as expr -> (
match
let open Option in
bind (catch (fun () -> Z.to_int64 v)) Tez.of_mutez
with
| Some tez -> Ok (tez, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a valid mutez amount"))
| expr -> error @@ Invalid_kind (location expr, [Int_kind], kind expr)
let parse_timestamp ctxt :
Script.node -> (Script_timestamp.t * context) tzresult = function
| Int (_, v)
->
ok (Script_timestamp.of_zint v, ctxt)
| String (loc, s) as expr -> (
Gas.consume ctxt (Typecheck_costs.timestamp_readable s) >>? fun ctxt ->
match Script_timestamp.of_string s with
| Some v -> ok (v, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a valid timestamp"))
| expr ->
error @@ Invalid_kind (location expr, [String_kind; Int_kind], kind expr)
let parse_key ctxt : Script.node -> (public_key * context) tzresult = function
| Bytes (loc, bytes) as expr -> (
Gas.consume ctxt Typecheck_costs.public_key_optimized
>>? fun ctxt ->
match
Data_encoding.Binary.of_bytes_opt Signature.Public_key.encoding bytes
with
| Some k -> ok (k, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a valid public key"))
| String (loc, s) as expr -> (
Gas.consume ctxt Typecheck_costs.public_key_readable
>>? fun ctxt ->
match Signature.Public_key.of_b58check_opt s with
| Some k -> ok (k, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a valid public key"))
| expr ->
error @@ Invalid_kind (location expr, [String_kind; Bytes_kind], kind expr)
let parse_key_hash ctxt : Script.node -> (public_key_hash * context) tzresult =
function
| Bytes (loc, bytes) as expr -> (
Gas.consume ctxt Typecheck_costs.key_hash_optimized
>>? fun ctxt ->
match
Data_encoding.Binary.of_bytes_opt
Signature.Public_key_hash.encoding
bytes
with
| Some k -> ok (k, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a valid key hash"))
| String (loc, s) as expr -> (
Gas.consume ctxt Typecheck_costs.key_hash_readable >>? fun ctxt ->
match Signature.Public_key_hash.of_b58check_opt s with
| Some k -> ok (k, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a valid key hash"))
| expr ->
error @@ Invalid_kind (location expr, [String_kind; Bytes_kind], kind expr)
let parse_signature ctxt : Script.node -> (signature * context) tzresult =
function
| Bytes (loc, bytes) as expr -> (
Gas.consume ctxt Typecheck_costs.signature_optimized >>? fun ctxt ->
match
Data_encoding.Binary.of_bytes_opt Script_signature.encoding bytes
with
| Some k -> ok (k, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a valid signature"))
| String (loc, s) as expr -> (
Gas.consume ctxt Typecheck_costs.signature_readable >>? fun ctxt ->
match Script_signature.of_b58check_opt s with
| Some s -> ok (s, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a valid signature"))
| expr ->
error @@ Invalid_kind (location expr, [String_kind; Bytes_kind], kind expr)
let parse_chain_id ctxt : Script.node -> (Script_chain_id.t * context) tzresult
= function
| Bytes (loc, bytes) as expr -> (
Gas.consume ctxt Typecheck_costs.chain_id_optimized >>? fun ctxt ->
match
Data_encoding.Binary.of_bytes_opt Script_chain_id.encoding bytes
with
| Some k -> ok (k, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a valid chain id"))
| String (loc, s) as expr -> (
Gas.consume ctxt Typecheck_costs.chain_id_readable >>? fun ctxt ->
match Script_chain_id.of_b58check_opt s with
| Some s -> ok (s, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a valid chain id"))
| expr ->
error @@ Invalid_kind (location expr, [String_kind; Bytes_kind], kind expr)
let parse_address ctxt : Script.node -> (address * context) tzresult =
let destination_allowed loc {destination; entrypoint} ctxt =
match destination with
| Destination.Tx_rollup _ when not (Constants.tx_rollup_enable ctxt) ->
error @@ Tx_rollup_addresses_disabled loc
| _ -> Ok ({destination; entrypoint}, ctxt)
in
function
| Bytes (loc, bytes) as expr -> (
Gas.consume ctxt Typecheck_costs.contract_optimized >>? fun ctxt ->
match
Data_encoding.Binary.of_bytes_opt
Data_encoding.(tup2 Destination.encoding Entrypoint.value_encoding)
bytes
with
| Some (destination, entrypoint) ->
destination_allowed loc {destination; entrypoint} ctxt
| None ->
error
@@ Invalid_syntactic_constant
(loc, strip_locations expr, "a valid address"))
| String (loc, s) ->
Gas.consume ctxt Typecheck_costs.contract_readable >>? fun ctxt ->
(match String.index_opt s '%' with
| None -> ok (s, Entrypoint.default)
| Some pos ->
let len = String.length s - pos - 1 in
let name = String.sub s (pos + 1) len in
Entrypoint.of_string_strict ~loc name >|? fun entrypoint ->
(String.sub s 0 pos, entrypoint))
>>? fun (addr, entrypoint) ->
Destination.of_b58check addr >>? fun destination ->
destination_allowed loc {destination; entrypoint} ctxt
| expr ->
error @@ Invalid_kind (location expr, [String_kind; Bytes_kind], kind expr)
let parse_tx_rollup_l2_address ctxt :
Script.node -> (tx_rollup_l2_address * context) tzresult = function
| Bytes (loc, bytes) as expr -> (
Gas.consume ctxt Typecheck_costs.tx_rollup_l2_address >>? fun ctxt ->
match Tx_rollup_l2_address.of_bytes_opt bytes with
| Some txa -> ok (Tx_rollup_l2_address.Indexable.value txa, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
( loc,
strip_locations expr,
"a valid transaction rollup L2 address" ))
| String (loc, str) as expr -> (
Gas.consume ctxt Typecheck_costs.tx_rollup_l2_address >>? fun ctxt ->
match Tx_rollup_l2_address.of_b58check_opt str with
| Some txa -> ok (Tx_rollup_l2_address.Indexable.value txa, ctxt)
| None ->
error
@@ Invalid_syntactic_constant
( loc,
strip_locations expr,
"a valid transaction rollup L2 address" ))
| expr ->
error @@ Invalid_kind (location expr, [String_kind; Bytes_kind], kind expr)
let parse_never expr : (never * context) tzresult =
error @@ Invalid_never_expr (location expr)
let parse_pair (type r) parse_l parse_r ctxt ~legacy
(r_comb_witness : (r, unit -> _) comb_witness) expr =
let parse_comb loc l rs =
parse_l ctxt l >>=? fun (l, ctxt) ->
(match (rs, r_comb_witness) with
| ([r], _) -> ok r
| ([], _) -> error @@ Invalid_arity (loc, D_Pair, 2, 1)
| (_ :: _, Comb_Pair _) ->
ok (Prim (loc, D_Pair, rs, []))
| _ -> error @@ Invalid_arity (loc, D_Pair, 2, 1 + List.length rs))
>>?= fun r ->
parse_r ctxt r >|=? fun (r, ctxt) -> ((l, r), ctxt)
in
match expr with
| Prim (loc, D_Pair, l :: rs, annot) ->
(if legacy then Result.return_unit else error_unexpected_annot loc annot)
>>?= fun () -> parse_comb loc l rs
| Prim (loc, D_Pair, l, _) ->
fail @@ Invalid_arity (loc, D_Pair, 2, List.length l)
| Seq (loc, l :: (_ :: _ as rs)) -> parse_comb loc l rs
| Seq (loc, l) -> fail @@ Invalid_seq_arity (loc, 2, List.length l)
| expr -> fail @@ unexpected expr [] Constant_namespace [D_Pair]
let parse_union parse_l parse_r ctxt ~legacy = function
| Prim (loc, D_Left, [v], annot) ->
(if legacy then Result.return_unit else error_unexpected_annot loc annot)
>>?= fun () ->
parse_l ctxt v >|=? fun (v, ctxt) -> (L v, ctxt)
| Prim (loc, D_Left, l, _) ->
fail @@ Invalid_arity (loc, D_Left, 1, List.length l)
| Prim (loc, D_Right, [v], annot) ->
(if legacy then Result.return_unit else error_unexpected_annot loc annot)
>>?= fun () ->
parse_r ctxt v >|=? fun (v, ctxt) -> (R v, ctxt)
| Prim (loc, D_Right, l, _) ->
fail @@ Invalid_arity (loc, D_Right, 1, List.length l)
| expr -> fail @@ unexpected expr [] Constant_namespace [D_Left; D_Right]
let parse_option parse_v ctxt ~legacy = function
| Prim (loc, D_Some, [v], annot) ->
(if legacy then Result.return_unit else error_unexpected_annot loc annot)
>>?= fun () ->
parse_v ctxt v >|=? fun (v, ctxt) -> (Some v, ctxt)
| Prim (loc, D_Some, l, _) ->
fail @@ Invalid_arity (loc, D_Some, 1, List.length l)
| Prim (loc, D_None, [], annot) ->
Lwt.return
( (if legacy then Result.return_unit
else error_unexpected_annot loc annot)
>|? fun () -> (None, ctxt) )
| Prim (loc, D_None, l, _) ->
fail @@ Invalid_arity (loc, D_None, 0, List.length l)
| expr -> fail @@ unexpected expr [] Constant_namespace [D_Some; D_None]
let comparable_comb_witness1 :
type t. t comparable_ty -> (t, unit -> unit) comb_witness = function
| Pair_t _ -> Comb_Pair Comb_Any
| _ -> Comb_Any
let[@coq_axiom_with_reason "gadt"] rec parse_comparable_data :
type a.
?type_logger:type_logger ->
context ->
a comparable_ty ->
Script.node ->
(a * context) tzresult Lwt.t =
fun ?type_logger ctxt ty script_data ->
let parse_data_error () =
let ty = serialize_ty_for_error (ty_of_comparable_ty ty) in
Invalid_constant (location script_data, strip_locations script_data, ty)
in
let traced_no_lwt body = record_trace_eval parse_data_error body in
let traced body = trace_eval parse_data_error body in
Gas.consume ctxt Typecheck_costs.parse_data_cycle
>>?=
fun ctxt ->
let legacy = false in
match (ty, script_data) with
| (Unit_t, expr) ->
Lwt.return @@ traced_no_lwt
@@ (parse_unit ctxt ~legacy expr : (a * context) tzresult)
| (Bool_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_bool ctxt ~legacy expr
| (String_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_string ctxt expr
| (Bytes_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_bytes ctxt expr
| (Int_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_int ctxt expr
| (Nat_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_nat ctxt expr
| (Mutez_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_mutez ctxt expr
| (Timestamp_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_timestamp ctxt expr
| (Key_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_key ctxt expr
| (Key_hash_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_key_hash ctxt expr
| (Signature_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_signature ctxt expr
| (Chain_id_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_chain_id ctxt expr
| (Address_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_address ctxt expr
| (Tx_rollup_l2_address_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_tx_rollup_l2_address ctxt expr
| (Pair_t (tl, tr, _, YesYes), expr) ->
let r_witness = comparable_comb_witness1 tr in
let parse_l ctxt v = parse_comparable_data ?type_logger ctxt tl v in
let parse_r ctxt v = parse_comparable_data ?type_logger ctxt tr v in
traced @@ parse_pair parse_l parse_r ctxt ~legacy r_witness expr
| (Union_t (tl, tr, _, YesYes), expr) ->
let parse_l ctxt v = parse_comparable_data ?type_logger ctxt tl v in
let parse_r ctxt v = parse_comparable_data ?type_logger ctxt tr v in
traced @@ parse_union parse_l parse_r ctxt ~legacy expr
| (Option_t (t, _, Yes), expr) ->
let parse_v ctxt v = parse_comparable_data ?type_logger ctxt t v in
traced @@ parse_option parse_v ctxt ~legacy expr
| (Never_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_never expr
let comb_witness1 : type t tc. (t, tc) ty -> (t, unit -> unit) comb_witness =
function
| Pair_t _ -> Comb_Pair Comb_Any
| _ -> Comb_Any
let[@coq_axiom_with_reason "gadt"] rec parse_data :
type a ac.
?type_logger:type_logger ->
stack_depth:int ->
context ->
legacy:bool ->
allow_forged:bool ->
(a, ac) ty ->
Script.node ->
(a * context) tzresult Lwt.t =
fun ?type_logger ~stack_depth ctxt ~legacy ~allow_forged ty script_data ->
Gas.consume ctxt Typecheck_costs.parse_data_cycle >>?= fun ctxt ->
let non_terminal_recursion ?type_logger ctxt ~legacy ty script_data =
if Compare.Int.(stack_depth > 10_000) then
fail Typechecking_too_many_recursive_calls
else
parse_data
?type_logger
~stack_depth:(stack_depth + 1)
ctxt
~legacy
~allow_forged
ty
script_data
in
let parse_data_error () =
let ty = serialize_ty_for_error ty in
Invalid_constant (location script_data, strip_locations script_data, ty)
in
let fail_parse_data () = fail (parse_data_error ()) in
let traced_no_lwt body = record_trace_eval parse_data_error body in
let traced body = trace_eval parse_data_error body in
let traced_fail err = Lwt.return @@ traced_no_lwt (error err) in
let parse_items ?type_logger ctxt expr key_type value_type items item_wrapper
=
List.fold_left_es
(fun (last_value, map, ctxt) item ->
match item with
| Prim (loc, D_Elt, [k; v], annot) ->
(if legacy then Result.return_unit
else error_unexpected_annot loc annot)
>>?= fun () ->
parse_comparable_data ?type_logger ctxt key_type k
>>=? fun (k, ctxt) ->
non_terminal_recursion ?type_logger ctxt ~legacy value_type v
>>=? fun (v, ctxt) ->
Lwt.return
( (match last_value with
| Some value ->
Gas.consume
ctxt
(Michelson_v1_gas.Cost_of.Interpreter.compare
key_type
value
k)
>>? fun ctxt ->
let c =
Script_comparable.compare_comparable key_type value k
in
if Compare.Int.(0 <= c) then
if Compare.Int.(0 = c) then
error (Duplicate_map_keys (loc, strip_locations expr))
else
error (Unordered_map_keys (loc, strip_locations expr))
else ok ctxt
| None -> ok ctxt)
>>? fun ctxt ->
Gas.consume
ctxt
(Michelson_v1_gas.Cost_of.Interpreter.map_update k map)
>|? fun ctxt ->
(Some k, Script_map.update k (Some (item_wrapper v)) map, ctxt)
)
| Prim (loc, D_Elt, l, _) ->
fail @@ Invalid_arity (loc, D_Elt, 2, List.length l)
| Prim (loc, name, _, _) ->
fail @@ Invalid_primitive (loc, [D_Elt], name)
| Int _ | String _ | Bytes _ | Seq _ -> fail_parse_data ())
(None, Script_map.empty key_type, ctxt)
items
|> traced
>|=? fun (_, items, ctxt) -> (items, ctxt)
in
let parse_big_map_items (type t) ?type_logger ctxt expr
(key_type : t comparable_ty) value_type items item_wrapper =
List.fold_left_es
(fun (last_key, {map; size}, ctxt) item ->
match item with
| Prim (loc, D_Elt, [k; v], annot) ->
(if legacy then Result.return_unit
else error_unexpected_annot loc annot)
>>?= fun () ->
parse_comparable_data ?type_logger ctxt key_type k
>>=? fun (k, ctxt) ->
hash_comparable_data ctxt key_type k >>=? fun (key_hash, ctxt) ->
non_terminal_recursion ?type_logger ctxt ~legacy value_type v
>>=? fun (v, ctxt) ->
Lwt.return
( (match last_key with
| Some last_key ->
Gas.consume
ctxt
(Michelson_v1_gas.Cost_of.Interpreter.compare
key_type
last_key
k)
>>? fun ctxt ->
let c =
Script_comparable.compare_comparable key_type last_key k
in
if Compare.Int.(0 <= c) then
if Compare.Int.(0 = c) then
error (Duplicate_map_keys (loc, strip_locations expr))
else
error (Unordered_map_keys (loc, strip_locations expr))
else ok ctxt
| None -> ok ctxt)
>>? fun ctxt ->
Gas.consume
ctxt
(Michelson_v1_gas.Cost_of.Interpreter.big_map_update
{map; size})
>>? fun ctxt ->
if Big_map_overlay.mem key_hash map then
error (Duplicate_map_keys (loc, strip_locations expr))
else
ok
( Some k,
{
map =
Big_map_overlay.add key_hash (k, item_wrapper v) map;
size = size + 1;
},
ctxt ) )
| Prim (loc, D_Elt, l, _) ->
fail @@ Invalid_arity (loc, D_Elt, 2, List.length l)
| Prim (loc, name, _, _) ->
fail @@ Invalid_primitive (loc, [D_Elt], name)
| Int _ | String _ | Bytes _ | Seq _ -> fail_parse_data ())
(None, {map = Big_map_overlay.empty; size = 0}, ctxt)
items
|> traced
>|=? fun (_, map, ctxt) -> (map, ctxt)
in
match (ty, script_data) with
| (Unit_t, expr) ->
Lwt.return @@ traced_no_lwt
@@ (parse_unit ctxt ~legacy expr : (a * context) tzresult)
| (Bool_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_bool ctxt ~legacy expr
| (String_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_string ctxt expr
| (Bytes_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_bytes ctxt expr
| (Int_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_int ctxt expr
| (Nat_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_nat ctxt expr
| (Mutez_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_mutez ctxt expr
| (Timestamp_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_timestamp ctxt expr
| (Key_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_key ctxt expr
| (Key_hash_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_key_hash ctxt expr
| (Signature_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_signature ctxt expr
| (Operation_t, _) ->
assert false
| (Chain_id_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_chain_id ctxt expr
| (Address_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_address ctxt expr
| (Tx_rollup_l2_address_t, expr) ->
Lwt.return @@ traced_no_lwt @@ parse_tx_rollup_l2_address ctxt expr
| (Contract_t (arg_ty, _), expr) ->
traced
( parse_address ctxt expr >>?= fun (address, ctxt) ->
let loc = location expr in
parse_contract
~stack_depth:(stack_depth + 1)
ctxt
loc
arg_ty
address.destination
~entrypoint:address.entrypoint
>|=? fun (ctxt, _) -> (Typed_contract {arg_ty; address}, ctxt) )
| (Pair_t (tl, tr, _, _), expr) ->
let r_witness = comb_witness1 tr in
let parse_l ctxt v =
non_terminal_recursion ?type_logger ctxt ~legacy tl v
in
let parse_r ctxt v =
non_terminal_recursion ?type_logger ctxt ~legacy tr v
in
traced @@ parse_pair parse_l parse_r ctxt ~legacy r_witness expr
| (Union_t (tl, tr, _, _), expr) ->
let parse_l ctxt v =
non_terminal_recursion ?type_logger ctxt ~legacy tl v
in
let parse_r ctxt v =
non_terminal_recursion ?type_logger ctxt ~legacy tr v
in
traced @@ parse_union parse_l parse_r ctxt ~legacy expr
| (Lambda_t (ta, tr, _ty_name), (Seq (_loc, _) as script_instr)) ->
traced
@@ parse_returning
Tc_context.data
?type_logger
~stack_depth:(stack_depth + 1)
ctxt
~legacy
ta
tr
script_instr
| (Lambda_t _, expr) ->
traced_fail (Invalid_kind (location expr, [Seq_kind], kind expr))
| (Option_t (t, _, _), expr) ->
let parse_v ctxt v =
non_terminal_recursion ?type_logger ctxt ~legacy t v
in
traced @@ parse_option parse_v ctxt ~legacy expr
| (List_t (t, _ty_name), Seq (_loc, items)) ->
traced
@@ List.fold_right_es
(fun v (rest, ctxt) ->
non_terminal_recursion ?type_logger ctxt ~legacy t v
>|=? fun (v, ctxt) -> (Script_list.cons v rest, ctxt))
items
(Script_list.empty, ctxt)
| (List_t _, expr) ->
traced_fail (Invalid_kind (location expr, [Seq_kind], kind expr))
| (Ticket_t (t, _ty_name), expr) ->
if allow_forged then
opened_ticket_type (location expr) t >>?= fun ty ->
parse_comparable_data ?type_logger ctxt ty expr
>>=? fun (({destination; entrypoint = _}, (contents, amount)), ctxt) ->
match destination with
| Contract ticketer -> return ({ticketer; contents; amount}, ctxt)
| Tx_rollup _ -> fail (Unexpected_ticket_owner destination)
else traced_fail (Unexpected_forged_value (location expr))
| (Set_t (t, _ty_name), (Seq (loc, vs) as expr)) ->
traced
@@ List.fold_left_es
(fun (last_value, set, ctxt) v ->
parse_comparable_data ?type_logger ctxt t v >>=? fun (v, ctxt) ->
Lwt.return
( (match last_value with
| Some value ->
Gas.consume
ctxt
(Michelson_v1_gas.Cost_of.Interpreter.compare t value v)
>>? fun ctxt ->
let c = Script_comparable.compare_comparable t value v in
if Compare.Int.(0 <= c) then
if Compare.Int.(0 = c) then
error
(Duplicate_set_values (loc, strip_locations expr))
else
error
(Unordered_set_values (loc, strip_locations expr))
else ok ctxt
| None -> ok ctxt)
>>? fun ctxt ->
Gas.consume
ctxt
(Michelson_v1_gas.Cost_of.Interpreter.set_update v set)
>|? fun ctxt -> (Some v, Script_set.update v true set, ctxt) ))
(None, Script_set.empty t, ctxt)
vs
>|=? fun (_, set, ctxt) -> (set, ctxt)
| (Set_t _, expr) ->
traced_fail (Invalid_kind (location expr, [Seq_kind], kind expr))
| (Map_t (tk, tv, _ty_name), (Seq (_, vs) as expr)) ->
parse_items ?type_logger ctxt expr tk tv vs (fun x -> x)
| (Map_t _, expr) ->
traced_fail (Invalid_kind (location expr, [Seq_kind], kind expr))
| (Big_map_t (tk, tv, _ty_name), expr) ->
(match expr with
| Int (loc, id) ->
return (Some (id, loc), {map = Big_map_overlay.empty; size = 0}, ctxt)
| Seq (_, vs) ->
parse_big_map_items ?type_logger ctxt expr tk tv vs (fun x -> Some x)
>|=? fun (diff, ctxt) -> (None, diff, ctxt)
| Prim (loc, D_Pair, [Int (loc_id, id); Seq (_, vs)], annot) ->
error_unexpected_annot loc annot >>?= fun () ->
option_t loc tv >>?= fun tv_opt ->
parse_big_map_items ?type_logger ctxt expr tk tv_opt vs (fun x -> x)
>|=? fun (diff, ctxt) -> (Some (id, loc_id), diff, ctxt)
| Prim (_, D_Pair, [Int _; expr], _) ->
traced_fail (Invalid_kind (location expr, [Seq_kind], kind expr))
| Prim (_, D_Pair, [expr; _], _) ->
traced_fail (Invalid_kind (location expr, [Int_kind], kind expr))
| Prim (loc, D_Pair, l, _) ->
traced_fail @@ Invalid_arity (loc, D_Pair, 2, List.length l)
| _ ->
traced_fail
(unexpected expr [Seq_kind; Int_kind] Constant_namespace [D_Pair]))
>>=? fun (id_opt, diff, ctxt) ->
(match id_opt with
| None -> return @@ (None, ctxt)
| Some (id, loc) ->
if allow_forged then
let id = Big_map.Id.parse_z id in
Big_map.exists ctxt id >>=? function
| (_, None) -> traced_fail (Invalid_big_map (loc, id))
| (ctxt, Some (btk, btv)) ->
Lwt.return
( parse_comparable_ty
~stack_depth:(stack_depth + 1)
ctxt
(Micheline.root btk)
>>? fun (Ex_comparable_ty btk, ctxt) ->
parse_big_map_value_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
(Micheline.root btv)
>>? fun (Ex_ty btv, ctxt) ->
(Gas_monad.run ctxt
@@
let open Gas_monad.Syntax in
let* Eq =
comparable_ty_eq ~error_details:Informative tk btk
in
ty_eq ~error_details:Informative loc tv btv)
>>? fun (eq, ctxt) ->
eq >|? fun Eq -> (Some id, ctxt) )
else traced_fail (Unexpected_forged_value loc))
>|=? fun (id, ctxt) ->
(Big_map {id; diff; key_type = tk; value_type = tv}, ctxt)
| (Never_t, expr) -> Lwt.return @@ traced_no_lwt @@ parse_never expr
| (Bls12_381_g1_t, Bytes (_, bs)) -> (
Gas.consume ctxt Typecheck_costs.bls12_381_g1 >>?= fun ctxt ->
match Script_bls.G1.of_bytes_opt bs with
| Some pt -> return (pt, ctxt)
| None -> fail_parse_data ())
| (Bls12_381_g1_t, expr) ->
traced_fail (Invalid_kind (location expr, [Bytes_kind], kind expr))
| (Bls12_381_g2_t, Bytes (_, bs)) -> (
Gas.consume ctxt Typecheck_costs.bls12_381_g2 >>?= fun ctxt ->
match Script_bls.G2.of_bytes_opt bs with
| Some pt -> return (pt, ctxt)
| None -> fail_parse_data ())
| (Bls12_381_g2_t, expr) ->
traced_fail (Invalid_kind (location expr, [Bytes_kind], kind expr))
| (Bls12_381_fr_t, Bytes (_, bs)) -> (
Gas.consume ctxt Typecheck_costs.bls12_381_fr >>?= fun ctxt ->
match Script_bls.Fr.of_bytes_opt bs with
| Some pt -> return (pt, ctxt)
| None -> fail_parse_data ())
| (Bls12_381_fr_t, Int (_, v)) ->
Gas.consume ctxt Typecheck_costs.bls12_381_fr >>?= fun ctxt ->
return (Script_bls.Fr.of_z v, ctxt)
| (Bls12_381_fr_t, expr) ->
traced_fail (Invalid_kind (location expr, [Bytes_kind], kind expr))
| (Sapling_transaction_t memo_size, Bytes (_, bytes)) -> (
match
Data_encoding.Binary.of_bytes_opt Sapling.transaction_encoding bytes
with
| Some transaction -> (
match Sapling.transaction_get_memo_size transaction with
| None -> return (transaction, ctxt)
| Some transac_memo_size ->
Lwt.return
( memo_size_eq
~error_details:Informative
memo_size
transac_memo_size
>|? fun () -> (transaction, ctxt) ))
| None -> fail_parse_data ())
| (Sapling_transaction_t _, expr) ->
traced_fail (Invalid_kind (location expr, [Bytes_kind], kind expr))
| (Sapling_transaction_deprecated_t memo_size, Bytes (_, bytes)) -> (
match
Data_encoding.Binary.of_bytes_opt
Sapling.Legacy.transaction_encoding
bytes
with
| Some transaction -> (
match Sapling.Legacy.transaction_get_memo_size transaction with
| None -> return (transaction, ctxt)
| Some transac_memo_size ->
Lwt.return
( memo_size_eq
~error_details:Informative
memo_size
transac_memo_size
>|? fun () -> (transaction, ctxt) ))
| None -> fail_parse_data ())
| (Sapling_transaction_deprecated_t _, expr) ->
traced_fail (Invalid_kind (location expr, [Bytes_kind], kind expr))
| (Sapling_state_t memo_size, Int (loc, id)) ->
if allow_forged then
let id = Sapling.Id.parse_z id in
Sapling.state_from_id ctxt id >>=? fun (state, ctxt) ->
Lwt.return
( traced_no_lwt
@@ memo_size_eq
~error_details:Informative
memo_size
state.Sapling.memo_size
>|? fun () -> (state, ctxt) )
else traced_fail (Unexpected_forged_value loc)
| (Sapling_state_t memo_size, Seq (_, [])) ->
return (Sapling.empty_state ~memo_size (), ctxt)
| (Sapling_state_t _, expr) ->
traced_fail
(Invalid_kind (location expr, [Int_kind; Seq_kind], kind expr))
| (Chest_key_t, Bytes (_, bytes)) -> (
Gas.consume ctxt Typecheck_costs.chest_key >>?= fun ctxt ->
match
Data_encoding.Binary.of_bytes_opt
Script_timelock.chest_key_encoding
bytes
with
| Some chest_key -> return (chest_key, ctxt)
| None -> fail_parse_data ())
| (Chest_key_t, expr) ->
traced_fail (Invalid_kind (location expr, [Bytes_kind], kind expr))
| (Chest_t, Bytes (_, bytes)) -> (
Gas.consume ctxt (Typecheck_costs.chest ~bytes:(Bytes.length bytes))
>>?= fun ctxt ->
match
Data_encoding.Binary.of_bytes_opt Script_timelock.chest_encoding bytes
with
| Some chest -> return (chest, ctxt)
| None -> fail_parse_data ())
| (Chest_t, expr) ->
traced_fail (Invalid_kind (location expr, [Bytes_kind], kind expr))
and parse_view :
type storage storagec.
?type_logger:type_logger ->
context ->
legacy:bool ->
(storage, storagec) ty ->
view ->
(storage typed_view * context) tzresult Lwt.t =
fun ?type_logger ctxt ~legacy storage_type {input_ty; output_ty; view_code} ->
let input_ty_loc = location input_ty in
record_trace_eval
(fun () ->
Ill_formed_type
(Some "arg of view", strip_locations input_ty, input_ty_loc))
(parse_view_input_ty ctxt ~stack_depth:0 ~legacy input_ty)
>>?= fun (Ex_ty input_ty, ctxt) ->
let output_ty_loc = location output_ty in
record_trace_eval
(fun () ->
Ill_formed_type
(Some "return of view", strip_locations output_ty, output_ty_loc))
(parse_view_output_ty ctxt ~stack_depth:0 ~legacy output_ty)
>>?= fun (Ex_ty output_ty, ctxt) ->
pair_t input_ty_loc input_ty storage_type >>?= fun (Ty_ex_c pair_ty) ->
parse_instr
?type_logger
~stack_depth:0
Tc_context.view
ctxt
~legacy
view_code
(Item_t (pair_ty, Bot_t))
>>=? fun (judgement, ctxt) ->
Lwt.return
@@
match judgement with
| Failed {descr} ->
let {kinstr; _} = close_descr (descr (Item_t (output_ty, Bot_t))) in
ok
( Typed_view
{input_ty; output_ty; kinstr; original_code_expr = view_code},
ctxt )
| Typed ({loc; aft; _} as descr) -> (
let ill_type_view loc stack_ty () =
let actual = serialize_stack_for_error ctxt stack_ty in
let expected_stack = Item_t (output_ty, Bot_t) in
let expected = serialize_stack_for_error ctxt expected_stack in
Ill_typed_view {loc; actual; expected}
in
match aft with
| Item_t (ty, Bot_t) ->
Gas_monad.run ctxt
@@ Gas_monad.record_trace_eval
~error_details:Informative
(ill_type_view loc aft : unit -> _)
@@ ty_eq ~error_details:Informative loc ty output_ty
>>? fun (eq, ctxt) ->
eq >|? fun Eq ->
let {kinstr; _} = close_descr descr in
( Typed_view
{input_ty; output_ty; kinstr; original_code_expr = view_code},
ctxt )
| _ -> error (ill_type_view loc aft ()))
and parse_views :
type storage storagec.
?type_logger:type_logger ->
context ->
legacy:bool ->
(storage, storagec) ty ->
view_map ->
(storage typed_view_map * context) tzresult Lwt.t =
fun ?type_logger ctxt ~legacy storage_type views ->
let aux ctxt name cur_view =
Gas.consume
ctxt
(Michelson_v1_gas.Cost_of.Interpreter.view_update name views)
>>?= fun ctxt -> parse_view ?type_logger ctxt ~legacy storage_type cur_view
in
Script_map.map_es_in_context aux ctxt views
and[@coq_axiom_with_reason "gadt"] parse_returning :
type arg argc ret retc.
?type_logger:type_logger ->
stack_depth:int ->
tc_context ->
context ->
legacy:bool ->
(arg, argc) ty ->
(ret, retc) ty ->
Script.node ->
((arg, ret) lambda * context) tzresult Lwt.t =
fun ?type_logger ~stack_depth tc_context ctxt ~legacy arg ret script_instr ->
parse_instr
?type_logger
tc_context
ctxt
~legacy
~stack_depth:(stack_depth + 1)
script_instr
(Item_t (arg, Bot_t))
>>=? function
| (Typed ({loc; aft = Item_t (ty, Bot_t) as stack_ty; _} as descr), ctxt) ->
Lwt.return
( Gas_monad.run ctxt
@@ Gas_monad.record_trace_eval ~error_details:Informative (fun () ->
let ret = serialize_ty_for_error ret in
let stack_ty = serialize_stack_for_error ctxt stack_ty in
Bad_return (loc, stack_ty, ret))
@@ ty_eq ~error_details:Informative loc ty ret
>>? fun (eq, ctxt) ->
eq >|? fun Eq ->
((Lam (close_descr descr, script_instr) : (arg, ret) lambda), ctxt) )
| (Typed {loc; aft = stack_ty; _}, ctxt) ->
let ret = serialize_ty_for_error ret in
let stack_ty = serialize_stack_for_error ctxt stack_ty in
fail @@ Bad_return (loc, stack_ty, ret)
| (Failed {descr}, ctxt) ->
return
( (Lam (close_descr (descr (Item_t (ret, Bot_t))), script_instr)
: (arg, ret) lambda),
ctxt )
and[@coq_axiom_with_reason "gadt"] parse_instr :
type a s.
?type_logger:type_logger ->
stack_depth:int ->
tc_context ->
context ->
legacy:bool ->
Script.node ->
(a, s) stack_ty ->
((a, s) judgement * context) tzresult Lwt.t =
fun ?type_logger ~stack_depth tc_context ctxt ~legacy script_instr stack_ty ->
let check_item_ty (type a ac b bc) ctxt (exp : (a, ac) ty) (got : (b, bc) ty)
loc name n m : ((a, b) eq * context) tzresult =
record_trace_eval (fun () ->
let stack_ty = serialize_stack_for_error ctxt stack_ty in
Bad_stack (loc, name, m, stack_ty))
@@ record_trace
(Bad_stack_item n)
( Gas_monad.run ctxt @@ ty_eq ~error_details:Informative loc exp got
>>? fun (eq, ctxt) ->
eq >|? fun Eq -> ((Eq : (a, b) eq), ctxt) )
in
let log_stack loc stack_ty aft =
match (type_logger, script_instr) with
| (None, _) | (Some _, (Int _ | String _ | Bytes _)) -> ()
| (Some log, (Prim _ | Seq _)) ->
let stack_ty_before = unparse_stack_uncarbonated stack_ty in
let stack_ty_after = unparse_stack_uncarbonated aft in
log loc ~stack_ty_before ~stack_ty_after
in
let typed_no_lwt ctxt loc instr aft =
log_stack loc stack_ty aft ;
let j = Typed {loc; instr; bef = stack_ty; aft} in
Ok (j, ctxt)
in
let typed ctxt loc instr aft =
Lwt.return @@ typed_no_lwt ctxt loc instr aft
in
Gas.consume ctxt Typecheck_costs.parse_instr_cycle >>?= fun ctxt ->
let non_terminal_recursion ?type_logger tc_context ctxt ~legacy script_instr
stack_ty =
if Compare.Int.(stack_depth > 10000) then
fail Typechecking_too_many_recursive_calls
else
parse_instr
?type_logger
tc_context
ctxt
~stack_depth:(stack_depth + 1)
~legacy
script_instr
stack_ty
in
match (script_instr, stack_ty) with
| (Prim (loc, I_DROP, [], annot), Item_t (_, rest)) ->
(error_unexpected_annot loc annot >>?= fun () ->
typed ctxt loc {apply = (fun kinfo k -> IDrop (kinfo, k))} rest
: ((a, s) judgement * context) tzresult Lwt.t)
| (Prim (loc, I_DROP, [n], result_annot), whole_stack) ->
parse_uint10 n >>?= fun whole_n ->
Gas.consume ctxt (Typecheck_costs.proof_argument whole_n) >>?= fun ctxt ->
let rec make_proof_argument :
type a s.
int -> (a, s) stack_ty -> (a, s) dropn_proof_argument tzresult =
fun n stk ->
match (Compare.Int.(n = 0), stk) with
| (true, rest) -> ok @@ Dropn_proof_argument (KRest, rest)
| (false, Item_t (_, rest)) ->
make_proof_argument (n - 1) rest
>|? fun (Dropn_proof_argument (n', stack_after_drops)) ->
let kinfo = {iloc = loc; kstack_ty = rest} in
Dropn_proof_argument (KPrefix (kinfo, n'), stack_after_drops)
| (_, _) ->
let whole_stack = serialize_stack_for_error ctxt whole_stack in
error (Bad_stack (loc, I_DROP, whole_n, whole_stack))
in
error_unexpected_annot loc result_annot >>?= fun () ->
make_proof_argument whole_n whole_stack
>>?= fun (Dropn_proof_argument (n', stack_after_drops)) ->
let kdropn kinfo k = IDropn (kinfo, whole_n, n', k) in
typed ctxt loc {apply = kdropn} stack_after_drops
| (Prim (loc, I_DROP, (_ :: _ :: _ as l), _), _) ->
fail (Invalid_arity (loc, I_DROP, 1, List.length l))
| (Prim (loc, I_DUP, [], annot), (Item_t (v, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
record_trace_eval
(fun () ->
let t = serialize_ty_for_error v in
Non_dupable_type (loc, t))
(check_dupable_ty ctxt loc v)
>>?= fun ctxt ->
let dup = {apply = (fun kinfo k -> IDup (kinfo, k))} in
typed ctxt loc dup (Item_t (v, stack))
| (Prim (loc, I_DUP, [n], v_annot), stack_ty) ->
check_var_annot loc v_annot >>?= fun () ->
let rec make_proof_argument :
type a s.
int -> (a, s) stack_ty -> (a * s) dup_n_proof_argument tzresult =
fun n (stack_ty : (a, s) stack_ty) ->
match (n, stack_ty) with
| (1, Item_t (hd_ty, _)) ->
ok @@ Dup_n_proof_argument (Dup_n_zero, hd_ty)
| (n, Item_t (_, tl_ty)) ->
make_proof_argument (n - 1) tl_ty
>|? fun (Dup_n_proof_argument (dup_n_witness, b_ty)) ->
Dup_n_proof_argument (Dup_n_succ dup_n_witness, b_ty)
| _ ->
let whole_stack = serialize_stack_for_error ctxt stack_ty in
error (Bad_stack (loc, I_DUP, 1, whole_stack))
in
parse_uint10 n >>?= fun n ->
Gas.consume ctxt (Typecheck_costs.proof_argument n) >>?= fun ctxt ->
error_unless (Compare.Int.( > ) n 0) (Dup_n_bad_argument loc)
>>?= fun () ->
record_trace (Dup_n_bad_stack loc) (make_proof_argument n stack_ty)
>>?= fun (Dup_n_proof_argument (witness, after_ty)) ->
record_trace_eval
(fun () ->
let t = serialize_ty_for_error after_ty in
Non_dupable_type (loc, t))
(check_dupable_ty ctxt loc after_ty)
>>?= fun ctxt ->
let dupn = {apply = (fun kinfo k -> IDup_n (kinfo, n, witness, k))} in
typed ctxt loc dupn (Item_t (after_ty, stack_ty))
| (Prim (loc, I_DIG, [n], result_annot), stack) ->
let rec make_proof_argument :
type a s. int -> (a, s) stack_ty -> (a, s) dig_proof_argument tzresult
=
fun n stk ->
match (Compare.Int.(n = 0), stk) with
| (true, Item_t (v, rest)) -> ok @@ Dig_proof_argument (KRest, v, rest)
| (false, Item_t (v, rest)) ->
make_proof_argument (n - 1) rest
>|? fun (Dig_proof_argument (n', x, aft')) ->
let kinfo = {iloc = loc; kstack_ty = aft'} in
Dig_proof_argument (KPrefix (kinfo, n'), x, Item_t (v, aft'))
| (_, _) ->
let whole_stack = serialize_stack_for_error ctxt stack in
error (Bad_stack (loc, I_DIG, 3, whole_stack))
in
parse_uint10 n >>?= fun n ->
Gas.consume ctxt (Typecheck_costs.proof_argument n) >>?= fun ctxt ->
error_unexpected_annot loc result_annot >>?= fun () ->
make_proof_argument n stack >>?= fun (Dig_proof_argument (n', x, aft)) ->
let dig = {apply = (fun kinfo k -> IDig (kinfo, n, n', k))} in
typed ctxt loc dig (Item_t (x, aft))
| (Prim (loc, I_DIG, (([] | _ :: _ :: _) as l), _), _) ->
fail (Invalid_arity (loc, I_DIG, 1, List.length l))
| (Prim (loc, I_DUG, [n], result_annot), Item_t (x, whole_stack)) -> (
parse_uint10 n >>?= fun whole_n ->
Gas.consume ctxt (Typecheck_costs.proof_argument whole_n) >>?= fun ctxt ->
error_unexpected_annot loc result_annot >>?= fun () ->
match make_dug_proof_argument loc whole_n x whole_stack with
| None ->
let whole_stack = serialize_stack_for_error ctxt whole_stack in
fail (Bad_stack (loc, I_DUG, whole_n, whole_stack))
| Some (Dug_proof_argument (n', aft)) ->
let dug = {apply = (fun kinfo k -> IDug (kinfo, whole_n, n', k))} in
typed ctxt loc dug aft)
| (Prim (loc, I_DUG, [_], result_annot), stack) ->
Lwt.return
( error_unexpected_annot loc result_annot >>? fun () ->
let stack = serialize_stack_for_error ctxt stack in
error (Bad_stack (loc, I_DUG, 1, stack)) )
| (Prim (loc, I_DUG, (([] | _ :: _ :: _) as l), _), _) ->
fail (Invalid_arity (loc, I_DUG, 1, List.length l))
| (Prim (loc, I_SWAP, [], annot), Item_t (v, Item_t (w, rest))) ->
error_unexpected_annot loc annot >>?= fun () ->
let swap = {apply = (fun kinfo k -> ISwap (kinfo, k))} in
let stack_ty = Item_t (w, Item_t (v, rest)) in
typed ctxt loc swap stack_ty
| (Prim (loc, I_PUSH, [t; d], annot), stack) ->
check_var_annot loc annot >>?= fun () ->
parse_packable_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy t
>>?= fun (Ex_ty t, ctxt) ->
parse_data
?type_logger
~stack_depth:(stack_depth + 1)
ctxt
~legacy
~allow_forged:false
t
d
>>=? fun (v, ctxt) ->
let const = {apply = (fun kinfo k -> IConst (kinfo, v, k))} in
typed ctxt loc const (Item_t (t, stack))
| (Prim (loc, I_UNIT, [], annot), stack) ->
check_var_type_annot loc annot >>?= fun () ->
let const = {apply = (fun kinfo k -> IConst (kinfo, (), k))} in
typed ctxt loc const (Item_t (unit_t, stack))
| (Prim (loc, I_SOME, [], annot), Item_t (t, rest)) ->
check_var_type_annot loc annot >>?= fun () ->
let cons_some = {apply = (fun kinfo k -> ICons_some (kinfo, k))} in
option_t loc t >>?= fun ty -> typed ctxt loc cons_some (Item_t (ty, rest))
| (Prim (loc, I_NONE, [t], annot), stack) ->
parse_any_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy t
>>?= fun (Ex_ty t, ctxt) ->
check_var_type_annot loc annot >>?= fun () ->
let cons_none = {apply = (fun kinfo k -> ICons_none (kinfo, k))} in
option_t loc t >>?= fun ty ->
let stack_ty = Item_t (ty, stack) in
typed ctxt loc cons_none stack_ty
| (Prim (loc, I_MAP, [body], annot), Item_t (Option_t (t, _, _), rest)) -> (
check_kind [Seq_kind] body >>?= fun () ->
check_var_type_annot loc annot >>?= fun () ->
non_terminal_recursion
?type_logger
~legacy
tc_context
ctxt
body
(Item_t (t, rest))
>>=? fun (judgement, ctxt) ->
Lwt.return
@@
match judgement with
| Typed ({loc; aft = Item_t (ret, aft_rest); _} as kibody) ->
let invalid_map_body () =
let aft = serialize_stack_for_error ctxt kibody.aft in
Invalid_map_body (loc, aft)
in
record_trace_eval
invalid_map_body
( stack_eq loc ctxt 1 aft_rest rest >>? fun (Eq, ctxt) ->
option_t loc ret >>? fun opt_ty ->
let final_stack = Item_t (opt_ty, rest) in
let hinfo = {iloc = loc; kstack_ty = Item_t (ret, aft_rest)} in
let cinfo = kinfo_of_descr kibody in
let body = kibody.instr.apply cinfo (IHalt hinfo) in
let apply kinfo k = IOpt_map {kinfo; body; k} in
typed_no_lwt ctxt loc {apply} final_stack )
| Typed {aft = Bot_t; _} ->
let aft = serialize_stack_for_error ctxt Bot_t in
error (Invalid_map_body (loc, aft))
| Failed _ -> error (Invalid_map_block_fail loc))
| ( Prim (loc, I_IF_NONE, [bt; bf], annot),
(Item_t (Option_t (t, _, _), rest) as bef) ) ->
check_kind [Seq_kind] bt >>?= fun () ->
check_kind [Seq_kind] bf >>?= fun () ->
error_unexpected_annot loc annot >>?= fun () ->
non_terminal_recursion ?type_logger tc_context ctxt ~legacy bt rest
>>=? fun (btr, ctxt) ->
let stack_ty = Item_t (t, rest) in
non_terminal_recursion ?type_logger tc_context ctxt ~legacy bf stack_ty
>>=? fun (bfr, ctxt) ->
let branch ibt ibf =
let ifnone =
{
apply =
(fun kinfo k ->
let hinfo = kinfo_of_kinstr k in
let btinfo = kinfo_of_descr ibt
and bfinfo = kinfo_of_descr ibf in
let branch_if_none = ibt.instr.apply btinfo (IHalt hinfo)
and branch_if_some = ibf.instr.apply bfinfo (IHalt hinfo) in
IIf_none {kinfo; branch_if_none; branch_if_some; k});
}
in
{loc; instr = ifnone; bef; aft = ibt.aft}
in
Lwt.return @@ merge_branches ctxt loc btr bfr {branch}
| (Prim (loc, I_PAIR, [], annot), Item_t (a, Item_t (b, rest))) ->
check_constr_annot loc annot >>?= fun () ->
pair_t loc a b >>?= fun (Ty_ex_c ty) ->
let stack_ty = Item_t (ty, rest) in
let cons_pair = {apply = (fun kinfo k -> ICons_pair (kinfo, k))} in
typed ctxt loc cons_pair stack_ty
| (Prim (loc, I_PAIR, [n], annot), stack_ty) ->
check_var_annot loc annot >>?= fun () ->
let rec make_proof_argument :
type a s.
int -> (a, s) stack_ty -> (a * s) comb_proof_argument tzresult =
fun n stack_ty ->
match (n, stack_ty) with
| (1, Item_t (a_ty, tl_ty)) ->
ok (Comb_proof_argument (Comb_one, Item_t (a_ty, tl_ty)))
| (n, Item_t (a_ty, tl_ty)) ->
make_proof_argument (n - 1) tl_ty
>>? fun (Comb_proof_argument (comb_witness, Item_t (b_ty, tl_ty')))
->
pair_t loc a_ty b_ty >|? fun (Ty_ex_c pair_t) ->
Comb_proof_argument (Comb_succ comb_witness, Item_t (pair_t, tl_ty'))
| _ ->
let whole_stack = serialize_stack_for_error ctxt stack_ty in
error (Bad_stack (loc, I_PAIR, 1, whole_stack))
in
parse_uint10 n >>?= fun n ->
Gas.consume ctxt (Typecheck_costs.proof_argument n) >>?= fun ctxt ->
error_unless (Compare.Int.( > ) n 1) (Pair_bad_argument loc)
>>?= fun () ->
make_proof_argument n stack_ty
>>?= fun (Comb_proof_argument (witness, after_ty)) ->
let comb = {apply = (fun kinfo k -> IComb (kinfo, n, witness, k))} in
typed ctxt loc comb after_ty
| (Prim (loc, I_UNPAIR, [n], annot), stack_ty) ->
error_unexpected_annot loc annot >>?= fun () ->
let rec make_proof_argument :
type a s.
int -> (a, s) stack_ty -> (a * s) uncomb_proof_argument tzresult =
fun n stack_ty ->
match (n, stack_ty) with
| (1, stack) -> ok @@ Uncomb_proof_argument (Uncomb_one, stack)
| (n, Item_t (Pair_t (a_ty, b_ty, _, _), tl_ty)) ->
make_proof_argument (n - 1) (Item_t (b_ty, tl_ty))
>|? fun (Uncomb_proof_argument (uncomb_witness, after_ty)) ->
Uncomb_proof_argument
(Uncomb_succ uncomb_witness, Item_t (a_ty, after_ty))
| _ ->
let whole_stack = serialize_stack_for_error ctxt stack_ty in
error (Bad_stack (loc, I_UNPAIR, 1, whole_stack))
in
parse_uint10 n >>?= fun n ->
Gas.consume ctxt (Typecheck_costs.proof_argument n) >>?= fun ctxt ->
error_unless (Compare.Int.( > ) n 1) (Unpair_bad_argument loc)
>>?= fun () ->
make_proof_argument n stack_ty
>>?= fun (Uncomb_proof_argument (witness, after_ty)) ->
let uncomb = {apply = (fun kinfo k -> IUncomb (kinfo, n, witness, k))} in
typed ctxt loc uncomb after_ty
| (Prim (loc, I_GET, [n], annot), Item_t (comb_ty, rest_ty)) -> (
check_var_annot loc annot >>?= fun () ->
parse_uint11 n >>?= fun n ->
Gas.consume ctxt (Typecheck_costs.proof_argument n) >>?= fun ctxt ->
match make_comb_get_proof_argument n comb_ty with
| None ->
let whole_stack = serialize_stack_for_error ctxt stack_ty in
fail (Bad_stack (loc, I_GET, 1, whole_stack))
| Some (Comb_get_proof_argument (witness, ty')) ->
let after_stack_ty = Item_t (ty', rest_ty) in
let comb_get =
{apply = (fun kinfo k -> IComb_get (kinfo, n, witness, k))}
in
typed ctxt loc comb_get after_stack_ty)
| ( Prim (loc, I_UPDATE, [n], annot),
Item_t (value_ty, Item_t (comb_ty, rest_ty)) ) ->
check_var_annot loc annot >>?= fun () ->
parse_uint11 n >>?= fun n ->
Gas.consume ctxt (Typecheck_costs.proof_argument n) >>?= fun ctxt ->
make_comb_set_proof_argument ctxt stack_ty loc n value_ty comb_ty
>>?= fun (Comb_set_proof_argument (witness, after_ty)) ->
let after_stack_ty = Item_t (after_ty, rest_ty) in
let comb_set =
{apply = (fun kinfo k -> IComb_set (kinfo, n, witness, k))}
in
typed ctxt loc comb_set after_stack_ty
| (Prim (loc, I_UNPAIR, [], annot), Item_t (Pair_t (a, b, _, _), rest)) ->
check_unpair_annot loc annot >>?= fun () ->
let unpair = {apply = (fun kinfo k -> IUnpair (kinfo, k))} in
typed ctxt loc unpair (Item_t (a, Item_t (b, rest)))
| (Prim (loc, I_CAR, [], annot), Item_t (Pair_t (a, _, _, _), rest)) ->
check_destr_annot loc annot >>?= fun () ->
let car = {apply = (fun kinfo k -> ICar (kinfo, k))} in
typed ctxt loc car (Item_t (a, rest))
| (Prim (loc, I_CDR, [], annot), Item_t (Pair_t (_, b, _, _), rest)) ->
check_destr_annot loc annot >>?= fun () ->
let cdr = {apply = (fun kinfo k -> ICdr (kinfo, k))} in
typed ctxt loc cdr (Item_t (b, rest))
| (Prim (loc, I_LEFT, [tr], annot), Item_t (tl, rest)) ->
parse_any_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy tr
>>?= fun (Ex_ty tr, ctxt) ->
check_constr_annot loc annot >>?= fun () ->
let cons_left = {apply = (fun kinfo k -> ICons_left (kinfo, k))} in
union_t loc tl tr >>?= fun (Ty_ex_c ty) ->
let stack_ty = Item_t (ty, rest) in
typed ctxt loc cons_left stack_ty
| (Prim (loc, I_RIGHT, [tl], annot), Item_t (tr, rest)) ->
parse_any_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy tl
>>?= fun (Ex_ty tl, ctxt) ->
check_constr_annot loc annot >>?= fun () ->
let cons_right = {apply = (fun kinfo k -> ICons_right (kinfo, k))} in
union_t loc tl tr >>?= fun (Ty_ex_c ty) ->
let stack_ty = Item_t (ty, rest) in
typed ctxt loc cons_right stack_ty
| ( Prim (loc, I_IF_LEFT, [bt; bf], annot),
(Item_t (Union_t (tl, tr, _, _), rest) as bef) ) ->
check_kind [Seq_kind] bt >>?= fun () ->
check_kind [Seq_kind] bf >>?= fun () ->
error_unexpected_annot loc annot >>?= fun () ->
non_terminal_recursion
?type_logger
tc_context
ctxt
~legacy
bt
(Item_t (tl, rest))
>>=? fun (btr, ctxt) ->
non_terminal_recursion
?type_logger
tc_context
ctxt
~legacy
bf
(Item_t (tr, rest))
>>=? fun (bfr, ctxt) ->
let branch ibt ibf =
let infobt = kinfo_of_descr ibt and infobf = kinfo_of_descr ibf in
let instr =
{
apply =
(fun kinfo k ->
let hinfo = kinfo_of_kinstr k in
let branch_if_left = ibt.instr.apply infobt (IHalt hinfo)
and branch_if_right = ibf.instr.apply infobf (IHalt hinfo) in
IIf_left {kinfo; branch_if_left; branch_if_right; k});
}
in
{loc; instr; bef; aft = ibt.aft}
in
Lwt.return @@ merge_branches ctxt loc btr bfr {branch}
| (Prim (loc, I_NIL, [t], annot), stack) ->
parse_any_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy t
>>?= fun (Ex_ty t, ctxt) ->
check_var_type_annot loc annot >>?= fun () ->
let nil = {apply = (fun kinfo k -> INil (kinfo, k))} in
list_t loc t >>?= fun ty -> typed ctxt loc nil (Item_t (ty, stack))
| ( Prim (loc, I_CONS, [], annot),
Item_t (tv, (Item_t (List_t (t, _), _) as stack)) ) ->
check_item_ty ctxt tv t loc I_CONS 1 2 >>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let cons_list = {apply = (fun kinfo k -> ICons_list (kinfo, k))} in
(typed ctxt loc cons_list stack
: ((a, s) judgement * context) tzresult Lwt.t)
| ( Prim (loc, I_IF_CONS, [bt; bf], annot),
(Item_t (List_t (t, _), rest) as bef) ) ->
check_kind [Seq_kind] bt >>?= fun () ->
check_kind [Seq_kind] bf >>?= fun () ->
error_unexpected_annot loc annot >>?= fun () ->
non_terminal_recursion
?type_logger
tc_context
ctxt
~legacy
bt
(Item_t (t, bef))
>>=? fun (btr, ctxt) ->
non_terminal_recursion ?type_logger tc_context ctxt ~legacy bf rest
>>=? fun (bfr, ctxt) ->
let branch ibt ibf =
let infobt = kinfo_of_descr ibt and infobf = kinfo_of_descr ibf in
let instr =
{
apply =
(fun kinfo k ->
let hinfo = kinfo_of_kinstr k in
let branch_if_cons = ibt.instr.apply infobt (IHalt hinfo)
and branch_if_nil = ibf.instr.apply infobf (IHalt hinfo) in
IIf_cons {kinfo; branch_if_nil; branch_if_cons; k});
}
in
{loc; instr; bef; aft = ibt.aft}
in
Lwt.return @@ merge_branches ctxt loc btr bfr {branch}
| (Prim (loc, I_SIZE, [], annot), Item_t (List_t _, rest)) ->
check_var_type_annot loc annot >>?= fun () ->
let list_size = {apply = (fun kinfo k -> IList_size (kinfo, k))} in
typed ctxt loc list_size (Item_t (nat_t, rest))
| (Prim (loc, I_MAP, [body], annot), Item_t (List_t (elt, _), starting_rest))
-> (
check_kind [Seq_kind] body >>?= fun () ->
check_var_type_annot loc annot >>?= fun () ->
non_terminal_recursion
?type_logger
tc_context
ctxt
~legacy
body
(Item_t (elt, starting_rest))
>>=? fun (judgement, ctxt) ->
Lwt.return
@@
match judgement with
| Typed ({aft = Item_t (ret, rest) as aft; _} as kibody) ->
let invalid_map_body () =
let aft = serialize_stack_for_error ctxt aft in
Invalid_map_body (loc, aft)
in
record_trace_eval
invalid_map_body
( stack_eq loc ctxt 1 rest starting_rest >>? fun (Eq, ctxt) ->
let binfo = kinfo_of_descr kibody in
let hinfo = {iloc = loc; kstack_ty = aft} in
let ibody = kibody.instr.apply binfo (IHalt hinfo) in
let list_map =
{apply = (fun kinfo k -> IList_map (kinfo, ibody, k))}
in
list_t loc ret >>? fun ty ->
let stack = Item_t (ty, rest) in
typed_no_lwt ctxt loc list_map stack )
| Typed {aft; _} ->
let aft = serialize_stack_for_error ctxt aft in
error (Invalid_map_body (loc, aft))
| Failed _ -> error (Invalid_map_block_fail loc))
| (Prim (loc, I_ITER, [body], annot), Item_t (List_t (elt, _), rest)) -> (
check_kind [Seq_kind] body >>?= fun () ->
error_unexpected_annot loc annot >>?= fun () ->
non_terminal_recursion
?type_logger
tc_context
ctxt
~legacy
body
(Item_t (elt, rest))
>>=? fun (judgement, ctxt) ->
let mk_list_iter ibody =
{
apply =
(fun kinfo k ->
let hinfo = {iloc = loc; kstack_ty = rest} in
let binfo = kinfo_of_descr ibody in
let ibody = ibody.instr.apply binfo (IHalt hinfo) in
IList_iter (kinfo, ibody, k));
}
in
Lwt.return
@@
match judgement with
| Typed ({aft; _} as ibody) ->
let invalid_iter_body () =
let aft = serialize_stack_for_error ctxt ibody.aft in
let rest = serialize_stack_for_error ctxt rest in
Invalid_iter_body (loc, rest, aft)
in
record_trace_eval
invalid_iter_body
( stack_eq loc ctxt 1 aft rest
>>? fun (Eq, ctxt) : ((a, s) judgement * context) tzresult ->
typed_no_lwt ctxt loc (mk_list_iter ibody) rest )
| Failed {descr} -> typed_no_lwt ctxt loc (mk_list_iter (descr rest)) rest
)
| (Prim (loc, I_EMPTY_SET, [t], annot), rest) ->
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt t
>>?= fun (Ex_comparable_ty t, ctxt) ->
check_var_type_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IEmpty_set (kinfo, t, k))} in
set_t loc t >>?= fun ty -> typed ctxt loc instr (Item_t (ty, rest))
| (Prim (loc, I_ITER, [body], annot), Item_t (Set_t (comp_elt, _), rest)) -> (
check_kind [Seq_kind] body >>?= fun () ->
error_unexpected_annot loc annot >>?= fun () ->
let elt = ty_of_comparable_ty comp_elt in
non_terminal_recursion
?type_logger
tc_context
ctxt
~legacy
body
(Item_t (elt, rest))
>>=? fun (judgement, ctxt) ->
let mk_iset_iter ibody =
{
apply =
(fun kinfo k ->
let hinfo = {iloc = loc; kstack_ty = rest} in
let binfo = kinfo_of_descr ibody in
let ibody = ibody.instr.apply binfo (IHalt hinfo) in
ISet_iter (kinfo, ibody, k));
}
in
Lwt.return
@@
match judgement with
| Typed ({aft; _} as ibody) ->
let invalid_iter_body () =
let aft = serialize_stack_for_error ctxt ibody.aft in
let rest = serialize_stack_for_error ctxt rest in
Invalid_iter_body (loc, rest, aft)
in
record_trace_eval
invalid_iter_body
( stack_eq loc ctxt 1 aft rest
>>? fun (Eq, ctxt) : ((a, s) judgement * context) tzresult ->
typed_no_lwt ctxt loc (mk_iset_iter ibody) rest )
| Failed {descr} -> typed_no_lwt ctxt loc (mk_iset_iter (descr rest)) rest
)
| (Prim (loc, I_MEM, [], annot), Item_t (v, Item_t (Set_t (elt, _), rest))) ->
let elt = ty_of_comparable_ty elt in
check_var_type_annot loc annot >>?= fun () ->
check_item_ty ctxt elt v loc I_MEM 1 2 >>?= fun (Eq, ctxt) ->
let instr = {apply = (fun kinfo k -> ISet_mem (kinfo, k))} in
(typed ctxt loc instr (Item_t (bool_t, rest))
: ((a, s) judgement * context) tzresult Lwt.t)
| ( Prim (loc, I_UPDATE, [], annot),
Item_t (v, Item_t (Bool_t, (Item_t (Set_t (elt, _), _) as stack))) ) ->
check_item_ty ctxt (ty_of_comparable_ty elt) v loc I_UPDATE 1 3
>>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISet_update (kinfo, k))} in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| (Prim (loc, I_SIZE, [], annot), Item_t (Set_t _, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISet_size (kinfo, k))} in
typed ctxt loc instr (Item_t (nat_t, rest))
| (Prim (loc, I_EMPTY_MAP, [tk; tv], annot), stack) ->
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt tk
>>?= fun (Ex_comparable_ty tk, ctxt) ->
parse_any_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy tv
>>?= fun (Ex_ty tv, ctxt) ->
check_var_type_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IEmpty_map (kinfo, tk, k))} in
map_t loc tk tv >>?= fun ty -> typed ctxt loc instr (Item_t (ty, stack))
| ( Prim (loc, I_MAP, [body], annot),
Item_t (Map_t (ck, elt, _), starting_rest) ) -> (
let k = ty_of_comparable_ty ck in
check_kind [Seq_kind] body >>?= fun () ->
check_var_type_annot loc annot >>?= fun () ->
pair_t loc k elt >>?= fun (Ty_ex_c ty) ->
non_terminal_recursion
?type_logger
tc_context
ctxt
~legacy
body
(Item_t (ty, starting_rest))
>>=? fun (judgement, ctxt) ->
Lwt.return
@@
match judgement with
| Typed ({aft = Item_t (ret, rest) as aft; _} as ibody) ->
let invalid_map_body () =
let aft = serialize_stack_for_error ctxt aft in
Invalid_map_body (loc, aft)
in
record_trace_eval
invalid_map_body
( stack_eq loc ctxt 1 rest starting_rest >>? fun (Eq, ctxt) ->
let instr =
{
apply =
(fun kinfo k ->
let binfo = kinfo_of_descr ibody in
let hinfo = {iloc = loc; kstack_ty = aft} in
let ibody = ibody.instr.apply binfo (IHalt hinfo) in
IMap_map (kinfo, ibody, k));
}
in
map_t loc ck ret >>? fun ty ->
let stack = Item_t (ty, rest) in
typed_no_lwt ctxt loc instr stack )
| Typed {aft; _} ->
let aft = serialize_stack_for_error ctxt aft in
error (Invalid_map_body (loc, aft))
| Failed _ -> error (Invalid_map_block_fail loc))
| ( Prim (loc, I_ITER, [body], annot),
Item_t (Map_t (comp_elt, element_ty, _), rest) ) -> (
check_kind [Seq_kind] body >>?= fun () ->
error_unexpected_annot loc annot >>?= fun () ->
let key = ty_of_comparable_ty comp_elt in
pair_t loc key element_ty >>?= fun (Ty_ex_c ty) ->
non_terminal_recursion
?type_logger
tc_context
ctxt
~legacy
body
(Item_t (ty, rest))
>>=? fun (judgement, ctxt) ->
let make_instr ibody =
{
apply =
(fun kinfo k ->
let hinfo = {iloc = loc; kstack_ty = rest} in
let binfo = kinfo_of_descr ibody in
let ibody = ibody.instr.apply binfo (IHalt hinfo) in
IMap_iter (kinfo, ibody, k));
}
in
Lwt.return
@@
match judgement with
| Typed ({aft; _} as ibody) ->
let invalid_iter_body () =
let aft = serialize_stack_for_error ctxt ibody.aft in
let rest = serialize_stack_for_error ctxt rest in
Invalid_iter_body (loc, rest, aft)
in
record_trace_eval
invalid_iter_body
( stack_eq loc ctxt 1 aft rest
>>? fun (Eq, ctxt) : ((a, s) judgement * context) tzresult ->
typed_no_lwt ctxt loc (make_instr ibody) rest )
| Failed {descr} -> typed_no_lwt ctxt loc (make_instr (descr rest)) rest)
| (Prim (loc, I_MEM, [], annot), Item_t (vk, Item_t (Map_t (ck, _, _), rest)))
->
let k = ty_of_comparable_ty ck in
check_item_ty ctxt vk k loc I_MEM 1 2 >>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMap_mem (kinfo, k))} in
(typed ctxt loc instr (Item_t (bool_t, rest))
: ((a, s) judgement * context) tzresult Lwt.t)
| ( Prim (loc, I_GET, [], annot),
Item_t (vk, Item_t (Map_t (ck, elt, _), rest)) ) ->
let k = ty_of_comparable_ty ck in
check_item_ty ctxt vk k loc I_GET 1 2 >>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMap_get (kinfo, k))} in
option_t loc elt
>>?= fun ty : ((a, s) judgement * context) tzresult Lwt.t ->
typed ctxt loc instr (Item_t (ty, rest))
| ( Prim (loc, I_UPDATE, [], annot),
Item_t
( vk,
Item_t (Option_t (vv, _, _), (Item_t (Map_t (ck, v, _), _) as stack))
) ) ->
let k = ty_of_comparable_ty ck in
check_item_ty ctxt vk k loc I_UPDATE 1 3 >>?= fun (Eq, ctxt) ->
check_item_ty ctxt vv v loc I_UPDATE 2 3 >>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMap_update (kinfo, k))} in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| ( Prim (loc, I_GET_AND_UPDATE, [], annot),
Item_t
( vk,
(Item_t (Option_t (vv, _, _), Item_t (Map_t (ck, v, _), _)) as stack)
) ) ->
let k = ty_of_comparable_ty ck in
check_item_ty ctxt vk k loc I_GET_AND_UPDATE 1 3 >>?= fun (Eq, ctxt) ->
check_item_ty ctxt vv v loc I_GET_AND_UPDATE 2 3 >>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMap_get_and_update (kinfo, k))} in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| (Prim (loc, I_SIZE, [], annot), Item_t (Map_t (_, _, _), rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMap_size (kinfo, k))} in
typed ctxt loc instr (Item_t (nat_t, rest))
| (Prim (loc, I_EMPTY_BIG_MAP, [tk; tv], annot), stack) ->
parse_comparable_ty ~stack_depth:(stack_depth + 1) ctxt tk
>>?= fun (Ex_comparable_ty tk, ctxt) ->
parse_big_map_value_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy tv
>>?= fun (Ex_ty tv, ctxt) ->
check_var_type_annot loc annot >>?= fun () ->
let instr =
{apply = (fun kinfo k -> IEmpty_big_map (kinfo, tk, tv, k))}
in
big_map_t loc tk tv >>?= fun ty ->
let stack = Item_t (ty, stack) in
typed ctxt loc instr stack
| ( Prim (loc, I_MEM, [], annot),
Item_t (set_key, Item_t (Big_map_t (map_key, _, _), rest)) ) ->
let k = ty_of_comparable_ty map_key in
check_item_ty ctxt set_key k loc I_MEM 1 2 >>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IBig_map_mem (kinfo, k))} in
let stack = Item_t (bool_t, rest) in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| ( Prim (loc, I_GET, [], annot),
Item_t (vk, Item_t (Big_map_t (ck, elt, _), rest)) ) ->
let k = ty_of_comparable_ty ck in
check_item_ty ctxt vk k loc I_GET 1 2 >>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IBig_map_get (kinfo, k))} in
option_t loc elt >>?= fun ty ->
let stack = Item_t (ty, rest) in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| ( Prim (loc, I_UPDATE, [], annot),
Item_t
( set_key,
Item_t
( Option_t (set_value, _, _),
(Item_t (Big_map_t (map_key, map_value, _), _) as stack) ) ) ) ->
let k = ty_of_comparable_ty map_key in
check_item_ty ctxt set_key k loc I_UPDATE 1 3 >>?= fun (Eq, ctxt) ->
check_item_ty ctxt set_value map_value loc I_UPDATE 2 3
>>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IBig_map_update (kinfo, k))} in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| ( Prim (loc, I_GET_AND_UPDATE, [], annot),
Item_t
( vk,
(Item_t (Option_t (vv, _, _), Item_t (Big_map_t (ck, v, _), _)) as
stack) ) ) ->
let k = ty_of_comparable_ty ck in
check_item_ty ctxt vk k loc I_GET_AND_UPDATE 1 3 >>?= fun (Eq, ctxt) ->
check_item_ty ctxt vv v loc I_GET_AND_UPDATE 2 3 >>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr =
{apply = (fun kinfo k -> IBig_map_get_and_update (kinfo, k))}
in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| (Prim (loc, I_SAPLING_EMPTY_STATE, [memo_size], annot), rest) ->
parse_memo_size memo_size >>?= fun memo_size ->
check_var_annot loc annot >>?= fun () ->
let instr =
{apply = (fun kinfo k -> ISapling_empty_state (kinfo, memo_size, k))}
in
let stack = Item_t (sapling_state_t ~memo_size, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_SAPLING_VERIFY_UPDATE, [], _),
Item_t
( Sapling_transaction_deprecated_t transaction_memo_size,
Item_t ((Sapling_state_t state_memo_size as state_ty), rest) ) ) ->
if legacy then
memo_size_eq
~error_details:Informative
state_memo_size
transaction_memo_size
>>?= fun () ->
let instr =
{
apply = (fun kinfo k -> ISapling_verify_update_deprecated (kinfo, k));
}
in
pair_t loc int_t state_ty >>?= fun (Ty_ex_c pair_ty) ->
option_t loc pair_ty >>?= fun ty ->
let stack = Item_t (ty, rest) in
typed ctxt loc instr stack
else fail (Deprecated_instruction T_sapling_transaction_deprecated)
| ( Prim (loc, I_SAPLING_VERIFY_UPDATE, [], _),
Item_t
( Sapling_transaction_t transaction_memo_size,
Item_t ((Sapling_state_t state_memo_size as state_ty), rest) ) ) ->
memo_size_eq
~error_details:Informative
state_memo_size
transaction_memo_size
>>?= fun () ->
let instr =
{apply = (fun kinfo k -> ISapling_verify_update (kinfo, k))}
in
pair_t loc int_t state_ty >>?= fun (Ty_ex_c pair_ty) ->
pair_t loc bytes_t pair_ty >>?= fun (Ty_ex_c pair_ty) ->
option_t loc pair_ty >>?= fun ty ->
let stack = Item_t (ty, rest) in
typed ctxt loc instr stack
| (Seq (loc, []), stack) ->
let instr = {apply = (fun _kinfo k -> k)} in
typed ctxt loc instr stack
| (Seq (_, [single]), stack) ->
non_terminal_recursion ?type_logger tc_context ctxt ~legacy single stack
| (Seq (loc, hd :: tl), stack) -> (
non_terminal_recursion ?type_logger tc_context ctxt ~legacy hd stack
>>=? fun (judgement, ctxt) ->
match judgement with
| Failed _ -> fail (Fail_not_in_tail_position (Micheline.location hd))
| Typed ({aft = middle; _} as ihd) ->
non_terminal_recursion
?type_logger
tc_context
ctxt
~legacy
(Seq (Micheline.dummy_location, tl))
middle
>|=? fun (judgement, ctxt) ->
let judgement =
match judgement with
| Failed {descr} ->
let descr ret = compose_descr loc ihd (descr ret) in
Failed {descr}
| Typed itl -> Typed (compose_descr loc ihd itl)
in
(judgement, ctxt))
| (Prim (loc, I_IF, [bt; bf], annot), (Item_t (Bool_t, rest) as bef)) ->
check_kind [Seq_kind] bt >>?= fun () ->
check_kind [Seq_kind] bf >>?= fun () ->
error_unexpected_annot loc annot >>?= fun () ->
non_terminal_recursion ?type_logger tc_context ctxt ~legacy bt rest
>>=? fun (btr, ctxt) ->
non_terminal_recursion ?type_logger tc_context ctxt ~legacy bf rest
>>=? fun (bfr, ctxt) ->
let branch ibt ibf =
let infobt = kinfo_of_descr ibt and infobf = kinfo_of_descr ibf in
let instr =
{
apply =
(fun kinfo k ->
let hinfo = kinfo_of_kinstr k in
let branch_if_true = ibt.instr.apply infobt (IHalt hinfo)
and branch_if_false = ibf.instr.apply infobf (IHalt hinfo) in
IIf {kinfo; branch_if_true; branch_if_false; k});
}
in
{loc; instr; bef; aft = ibt.aft}
in
Lwt.return @@ merge_branches ctxt loc btr bfr {branch}
| (Prim (loc, I_LOOP, [body], annot), (Item_t (Bool_t, rest) as stack)) -> (
check_kind [Seq_kind] body >>?= fun () ->
error_unexpected_annot loc annot >>?= fun () ->
non_terminal_recursion ?type_logger tc_context ctxt ~legacy body rest
>>=? fun (judgement, ctxt) ->
Lwt.return
@@
match judgement with
| Typed ibody ->
let unmatched_branches () =
let aft = serialize_stack_for_error ctxt ibody.aft in
let stack = serialize_stack_for_error ctxt stack in
Unmatched_branches (loc, aft, stack)
in
record_trace_eval
unmatched_branches
( stack_eq loc ctxt 1 ibody.aft stack >>? fun (Eq, ctxt) ->
let instr =
{
apply =
(fun kinfo k ->
let ibody =
ibody.instr.apply (kinfo_of_descr ibody) (IHalt kinfo)
in
ILoop (kinfo, ibody, k));
}
in
typed_no_lwt ctxt loc instr rest )
| Failed {descr} ->
let instr =
{
apply =
(fun kinfo k ->
let ibody = descr stack in
let ibody =
ibody.instr.apply (kinfo_of_descr ibody) (IHalt kinfo)
in
ILoop (kinfo, ibody, k));
}
in
typed_no_lwt ctxt loc instr rest)
| ( Prim (loc, I_LOOP_LEFT, [body], annot),
(Item_t (Union_t (tl, tr, _, _), rest) as stack) ) -> (
check_kind [Seq_kind] body >>?= fun () ->
check_var_annot loc annot >>?= fun () ->
non_terminal_recursion
?type_logger
tc_context
ctxt
~legacy
body
(Item_t (tl, rest))
>>=? fun (judgement, ctxt) ->
Lwt.return
@@
match judgement with
| Typed ibody ->
let unmatched_branches () =
let aft = serialize_stack_for_error ctxt ibody.aft in
let stack = serialize_stack_for_error ctxt stack in
Unmatched_branches (loc, aft, stack)
in
record_trace_eval
unmatched_branches
( stack_eq loc ctxt 1 ibody.aft stack >>? fun (Eq, ctxt) ->
let instr =
{
apply =
(fun kinfo k ->
let ibody =
ibody.instr.apply (kinfo_of_descr ibody) (IHalt kinfo)
in
ILoop_left (kinfo, ibody, k));
}
in
let stack = Item_t (tr, rest) in
typed_no_lwt ctxt loc instr stack )
| Failed {descr} ->
let instr =
{
apply =
(fun kinfo k ->
let ibody = descr stack in
let ibody =
ibody.instr.apply (kinfo_of_descr ibody) (IHalt kinfo)
in
ILoop_left (kinfo, ibody, k));
}
in
let stack = Item_t (tr, rest) in
typed_no_lwt ctxt loc instr stack)
| (Prim (loc, I_LAMBDA, [arg; ret; code], annot), stack) ->
parse_any_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy arg
>>?= fun (Ex_ty arg, ctxt) ->
parse_any_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy ret
>>?= fun (Ex_ty ret, ctxt) ->
check_kind [Seq_kind] code >>?= fun () ->
check_var_annot loc annot >>?= fun () ->
parse_returning
(Tc_context.add_lambda tc_context)
?type_logger
~stack_depth:(stack_depth + 1)
ctxt
~legacy
arg
ret
code
>>=? fun (lambda, ctxt) ->
let instr = {apply = (fun kinfo k -> ILambda (kinfo, lambda, k))} in
lambda_t loc arg ret >>?= fun ty ->
let stack = Item_t (ty, stack) in
typed ctxt loc instr stack
| ( Prim (loc, I_EXEC, [], annot),
Item_t (arg, Item_t (Lambda_t (param, ret, _), rest)) ) ->
check_item_ty ctxt arg param loc I_EXEC 1 2 >>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IExec (kinfo, k))} in
let stack = Item_t (ret, rest) in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| ( Prim (loc, I_APPLY, [], annot),
Item_t
( capture,
Item_t (Lambda_t (Pair_t (capture_ty, arg_ty, _, _), ret, _), rest) )
) ->
check_packable ~legacy:false loc capture_ty >>?= fun () ->
check_item_ty ctxt capture capture_ty loc I_APPLY 1 2
>>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IApply (kinfo, capture_ty, k))} in
lambda_t loc arg_ty ret
>>?=
fun res_ty ->
let stack = Item_t (res_ty, rest) in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| (Prim (loc, I_DIP, [code], annot), Item_t (v, rest)) -> (
error_unexpected_annot loc annot >>?= fun () ->
check_kind [Seq_kind] code >>?= fun () ->
non_terminal_recursion ?type_logger tc_context ctxt ~legacy code rest
>>=? fun (judgement, ctxt) ->
match judgement with
| Typed descr ->
let instr =
{
apply =
(fun kinfo k ->
let binfo = {iloc = descr.loc; kstack_ty = descr.bef} in
let kinfoh = {iloc = descr.loc; kstack_ty = descr.aft} in
let b = descr.instr.apply binfo (IHalt kinfoh) in
IDip (kinfo, b, k));
}
in
let stack = Item_t (v, descr.aft) in
typed ctxt loc instr stack
| Failed _ -> fail (Fail_not_in_tail_position loc))
| (Prim (loc, I_DIP, [n; code], result_annot), stack) ->
parse_uint10 n >>?= fun n ->
Gas.consume ctxt (Typecheck_costs.proof_argument n) >>?= fun ctxt ->
let rec make_proof_argument :
type a s.
int -> (a, s) stack_ty -> (a, s) dipn_proof_argument tzresult Lwt.t =
fun n stk ->
match (Compare.Int.(n = 0), stk) with
| (true, rest) -> (
non_terminal_recursion
?type_logger
tc_context
ctxt
~legacy
code
rest
>>=? fun (judgement, ctxt) ->
Lwt.return
@@
match judgement with
| Typed descr ->
ok
(Dipn_proof_argument (KRest, ctxt, descr, descr.aft)
: (a, s) dipn_proof_argument)
| Failed _ -> error (Fail_not_in_tail_position loc))
| (false, Item_t (v, rest)) ->
make_proof_argument (n - 1) rest
>|=? fun (Dipn_proof_argument (n', ctxt, descr, aft')) ->
let kinfo' = {iloc = loc; kstack_ty = aft'} in
let w = KPrefix (kinfo', n') in
Dipn_proof_argument (w, ctxt, descr, Item_t (v, aft'))
| (_, _) ->
Lwt.return
(let whole_stack = serialize_stack_for_error ctxt stack in
error (Bad_stack (loc, I_DIP, 1, whole_stack)))
in
error_unexpected_annot loc result_annot >>?= fun () ->
make_proof_argument n stack
>>=? fun (Dipn_proof_argument (n', ctxt, descr, aft)) ->
let kinfo = {iloc = descr.loc; kstack_ty = descr.bef} in
let kinfoh = {iloc = descr.loc; kstack_ty = descr.aft} in
let b = descr.instr.apply kinfo (IHalt kinfoh) in
let res = {apply = (fun kinfo k -> IDipn (kinfo, n, n', b, k))} in
typed ctxt loc res aft
| (Prim (loc, I_DIP, (([] | _ :: _ :: _ :: _) as l), _), _) ->
fail (Invalid_arity (loc, I_DIP, 2, List.length l))
| (Prim (loc, I_FAILWITH, [], annot), Item_t (v, _rest)) ->
Lwt.return
( error_unexpected_annot loc annot >>? fun () ->
(if legacy then Result.return_unit
else check_packable ~legacy:false loc v)
>|? fun () ->
let instr = {apply = (fun kinfo _k -> IFailwith (kinfo, loc, v))} in
let descr aft = {loc; instr; bef = stack_ty; aft} in
log_stack loc stack_ty Bot_t ;
(Failed {descr}, ctxt) )
| (Prim (loc, I_NEVER, [], annot), Item_t (Never_t, _rest)) ->
Lwt.return
( error_unexpected_annot loc annot >|? fun () ->
let instr = {apply = (fun kinfo _k -> INever kinfo)} in
let descr aft = {loc; instr; bef = stack_ty; aft} in
log_stack loc stack_ty Bot_t ;
(Failed {descr}, ctxt) )
| (Prim (loc, I_ADD, [], annot), Item_t (Timestamp_t, Item_t (Int_t, rest)))
->
check_var_annot loc annot >>?= fun () ->
let instr =
{apply = (fun kinfo k -> IAdd_timestamp_to_seconds (kinfo, k))}
in
typed ctxt loc instr (Item_t (Timestamp_t, rest))
| ( Prim (loc, I_ADD, [], annot),
Item_t (Int_t, (Item_t (Timestamp_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr =
{apply = (fun kinfo k -> IAdd_seconds_to_timestamp (kinfo, k))}
in
typed ctxt loc instr stack
| (Prim (loc, I_SUB, [], annot), Item_t (Timestamp_t, Item_t (Int_t, rest)))
->
check_var_annot loc annot >>?= fun () ->
let instr =
{apply = (fun kinfo k -> ISub_timestamp_seconds (kinfo, k))}
in
let stack = Item_t (Timestamp_t, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_SUB, [], annot),
Item_t (Timestamp_t, Item_t (Timestamp_t, rest)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IDiff_timestamps (kinfo, k))} in
let stack = Item_t (int_t, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_CONCAT, [], annot),
Item_t (String_t, (Item_t (String_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IConcat_string_pair (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_CONCAT, [], annot), Item_t (List_t (String_t, _), rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IConcat_string (kinfo, k))} in
typed ctxt loc instr (Item_t (String_t, rest))
| ( Prim (loc, I_SLICE, [], annot),
Item_t (Nat_t, Item_t (Nat_t, Item_t (String_t, rest))) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISlice_string (kinfo, k))} in
let stack = Item_t (option_string_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_SIZE, [], annot), Item_t (String_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IString_size (kinfo, k))} in
let stack = Item_t (nat_t, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_CONCAT, [], annot),
Item_t (Bytes_t, (Item_t (Bytes_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IConcat_bytes_pair (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_CONCAT, [], annot), Item_t (List_t (Bytes_t, _), rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IConcat_bytes (kinfo, k))} in
let stack = Item_t (Bytes_t, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_SLICE, [], annot),
Item_t (Nat_t, Item_t (Nat_t, Item_t (Bytes_t, rest))) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISlice_bytes (kinfo, k))} in
let stack = Item_t (option_bytes_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_SIZE, [], annot), Item_t (Bytes_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IBytes_size (kinfo, k))} in
let stack = Item_t (nat_t, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_ADD, [], annot),
Item_t (Mutez_t, (Item_t (Mutez_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAdd_tez (kinfo, k))} in
typed ctxt loc instr stack
| ( Prim (loc, I_SUB, [], annot),
Item_t (Mutez_t, (Item_t (Mutez_t, _) as stack)) ) ->
if legacy then
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISub_tez_legacy (kinfo, k))} in
typed ctxt loc instr stack
else fail (Deprecated_instruction I_SUB)
| ( Prim (loc, I_SUB_MUTEZ, [], annot),
Item_t (Mutez_t, Item_t (Mutez_t, rest)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISub_tez (kinfo, k))} in
let stack = Item_t (option_mutez_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_MUL, [], annot), Item_t (Mutez_t, Item_t (Nat_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_teznat (kinfo, k))} in
let stack = Item_t (Mutez_t, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_MUL, [], annot),
Item_t (Nat_t, (Item_t (Mutez_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_nattez (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_OR, [], annot), Item_t (Bool_t, (Item_t (Bool_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IOr (kinfo, k))} in
typed ctxt loc instr stack
| ( Prim (loc, I_AND, [], annot),
Item_t (Bool_t, (Item_t (Bool_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAnd (kinfo, k))} in
typed ctxt loc instr stack
| ( Prim (loc, I_XOR, [], annot),
Item_t (Bool_t, (Item_t (Bool_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IXor (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_NOT, [], annot), (Item_t (Bool_t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> INot (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_ABS, [], annot), Item_t (Int_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAbs_int (kinfo, k))} in
let stack = Item_t (nat_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_ISNAT, [], annot), Item_t (Int_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IIs_nat (kinfo, k))} in
let stack = Item_t (option_nat_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_INT, [], annot), Item_t (Nat_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IInt_nat (kinfo, k))} in
let stack = Item_t (int_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_NEG, [], annot), (Item_t (Int_t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> INeg (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_NEG, [], annot), Item_t (Nat_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> INeg (kinfo, k))} in
let stack = Item_t (int_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_ADD, [], annot), Item_t (Int_t, (Item_t (Int_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAdd_int (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_ADD, [], annot), Item_t (Int_t, Item_t (Nat_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAdd_int (kinfo, k))} in
let stack = Item_t (Int_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_ADD, [], annot), Item_t (Nat_t, (Item_t (Int_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAdd_int (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_ADD, [], annot), Item_t (Nat_t, (Item_t (Nat_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAdd_nat (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_SUB, [], annot), Item_t (Int_t, (Item_t (Int_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISub_int (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_SUB, [], annot), Item_t (Int_t, Item_t (Nat_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISub_int (kinfo, k))} in
let stack = Item_t (Int_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_SUB, [], annot), Item_t (Nat_t, (Item_t (Int_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISub_int (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_SUB, [], annot), Item_t (Nat_t, Item_t (Nat_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISub_int (kinfo, k))} in
let stack = Item_t (int_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_MUL, [], annot), Item_t (Int_t, (Item_t (Int_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_int (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_MUL, [], annot), Item_t (Int_t, Item_t (Nat_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_int (kinfo, k))} in
let stack = Item_t (Int_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_MUL, [], annot), Item_t (Nat_t, (Item_t (Int_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_nat (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_MUL, [], annot), Item_t (Nat_t, (Item_t (Nat_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_nat (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_EDIV, [], annot), Item_t (Mutez_t, Item_t (Nat_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IEdiv_teznat (kinfo, k))} in
let stack = Item_t (option_pair_mutez_mutez_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_EDIV, [], annot), Item_t (Mutez_t, Item_t (Mutez_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IEdiv_tez (kinfo, k))} in
let stack = Item_t (option_pair_nat_mutez_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_EDIV, [], annot), Item_t (Int_t, Item_t (Int_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IEdiv_int (kinfo, k))} in
let stack = Item_t (option_pair_int_nat_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_EDIV, [], annot), Item_t (Int_t, Item_t (Nat_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IEdiv_int (kinfo, k))} in
let stack = Item_t (option_pair_int_nat_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_EDIV, [], annot), Item_t (Nat_t, Item_t (Int_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IEdiv_nat (kinfo, k))} in
let stack = Item_t (option_pair_int_nat_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_EDIV, [], annot), Item_t (Nat_t, Item_t (Nat_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IEdiv_nat (kinfo, k))} in
let stack = Item_t (option_pair_nat_nat_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_LSL, [], annot), Item_t (Nat_t, (Item_t (Nat_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ILsl_nat (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_LSR, [], annot), Item_t (Nat_t, (Item_t (Nat_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ILsr_nat (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_OR, [], annot), Item_t (Nat_t, (Item_t (Nat_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IOr_nat (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_AND, [], annot), Item_t (Nat_t, (Item_t (Nat_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAnd_nat (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_AND, [], annot), Item_t (Int_t, (Item_t (Nat_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAnd_int_nat (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_XOR, [], annot), Item_t (Nat_t, (Item_t (Nat_t, _) as stack)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IXor_nat (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_NOT, [], annot), (Item_t (Int_t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> INot_int (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_NOT, [], annot), Item_t (Nat_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> INot_int (kinfo, k))} in
let stack = Item_t (int_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_COMPARE, [], annot), Item_t (t1, Item_t (t2, rest))) ->
check_var_annot loc annot >>?= fun () ->
check_item_ty ctxt t1 t2 loc I_COMPARE 1 2 >>?= fun (Eq, ctxt) ->
comparable_ty_of_ty ctxt loc t1 >>?= fun (key, ctxt) ->
let instr = {apply = (fun kinfo k -> ICompare (kinfo, key, k))} in
let stack = Item_t (int_t, rest) in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| (Prim (loc, I_EQ, [], annot), Item_t (Int_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IEq (kinfo, k))} in
let stack = Item_t (bool_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_NEQ, [], annot), Item_t (Int_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> INeq (kinfo, k))} in
let stack = Item_t (bool_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_LT, [], annot), Item_t (Int_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ILt (kinfo, k))} in
let stack = Item_t (bool_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_GT, [], annot), Item_t (Int_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IGt (kinfo, k))} in
let stack = Item_t (bool_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_LE, [], annot), Item_t (Int_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ILe (kinfo, k))} in
let stack = Item_t (bool_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_GE, [], annot), Item_t (Int_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IGe (kinfo, k))} in
let stack = Item_t (bool_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_CAST, [cast_t], annot), (Item_t (t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
parse_any_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy cast_t
>>?= fun (Ex_ty cast_t, ctxt) ->
Gas_monad.run ctxt @@ ty_eq ~error_details:Informative loc cast_t t
>>?= fun (eq, ctxt) ->
eq >>?= fun Eq ->
let instr = {apply = (fun _ k -> k)} in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| (Prim (loc, I_RENAME, [], annot), (Item_t _ as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun _ k -> k)} in
typed ctxt loc instr stack
| (Prim (loc, I_PACK, [], annot), Item_t (t, rest)) ->
check_packable
~legacy:true
loc
t
>>?= fun () ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IPack (kinfo, t, k))} in
let stack = Item_t (bytes_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_UNPACK, [ty], annot), Item_t (Bytes_t, rest)) ->
parse_packable_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy ty
>>?= fun (Ex_ty t, ctxt) ->
check_var_type_annot loc annot >>?= fun () ->
option_t loc t >>?= fun res_ty ->
let instr = {apply = (fun kinfo k -> IUnpack (kinfo, t, k))} in
let stack = Item_t (res_ty, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_ADDRESS, [], annot), Item_t (Contract_t _, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAddress (kinfo, k))} in
let stack = Item_t (address_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_CONTRACT, [ty], annot), Item_t (Address_t, rest)) ->
parse_passable_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy ty
>>?= fun (Ex_ty t, ctxt) ->
contract_t loc t >>?= fun contract_ty ->
option_t loc contract_ty >>?= fun res_ty ->
parse_entrypoint_annot_strict loc annot >>?= fun entrypoint ->
let instr =
{apply = (fun kinfo k -> IContract (kinfo, t, entrypoint, k))}
in
let stack = Item_t (res_ty, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_VIEW, [name; output_ty], annot),
Item_t (input_ty, Item_t (Address_t, rest)) ) ->
let output_ty_loc = location output_ty in
parse_view_name ctxt name >>?= fun (name, ctxt) ->
parse_view_output_ty ctxt ~stack_depth:0 ~legacy output_ty
>>?= fun (Ex_ty output_ty, ctxt) ->
option_t output_ty_loc output_ty >>?= fun res_ty ->
check_var_annot loc annot >>?= fun () ->
let instr =
{
apply =
(fun kinfo k ->
IView (kinfo, View_signature {name; input_ty; output_ty}, k));
}
in
let stack = Item_t (res_ty, rest) in
typed ctxt loc instr stack
| ( Prim (loc, (I_TRANSFER_TOKENS as prim), [], annot),
Item_t (p, Item_t (Mutez_t, Item_t (Contract_t (cp, _), rest))) ) ->
Tc_context.check_not_in_view loc ~legacy tc_context prim >>?= fun () ->
check_item_ty ctxt p cp loc prim 1 4 >>?= fun (Eq, ctxt) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ITransfer_tokens (kinfo, k))} in
let stack = Item_t (operation_t, rest) in
(typed ctxt loc instr stack : ((a, s) judgement * context) tzresult Lwt.t)
| ( Prim (loc, (I_SET_DELEGATE as prim), [], annot),
Item_t (Option_t (Key_hash_t, _, _), rest) ) ->
Tc_context.check_not_in_view loc ~legacy tc_context prim >>?= fun () ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISet_delegate (kinfo, k))} in
let stack = Item_t (operation_t, rest) in
typed ctxt loc instr stack
| (Prim (_, I_CREATE_ACCOUNT, _, _), _) ->
fail (Deprecated_instruction I_CREATE_ACCOUNT)
| (Prim (loc, I_IMPLICIT_ACCOUNT, [], annot), Item_t (Key_hash_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IImplicit_account (kinfo, k))} in
let stack = Item_t (contract_unit_t, rest) in
typed ctxt loc instr stack
| ( Prim (loc, (I_CREATE_CONTRACT as prim), [(Seq _ as code)], annot),
Item_t
(Option_t (Key_hash_t, _, _), Item_t (Mutez_t, Item_t (ginit, rest))) )
->
Tc_context.check_not_in_view ~legacy loc tc_context prim >>?= fun () ->
check_two_var_annot loc annot >>?= fun () ->
let canonical_code = Micheline.strip_locations code in
parse_toplevel ctxt ~legacy canonical_code
>>?= fun ({arg_type; storage_type; code_field; views}, ctxt) ->
record_trace
(Ill_formed_type (Some "parameter", canonical_code, location arg_type))
(parse_parameter_ty_and_entrypoints
ctxt
~stack_depth:(stack_depth + 1)
~legacy
arg_type)
>>?= fun (Ex_parameter_ty_and_entrypoints {arg_type; entrypoints}, ctxt)
->
record_trace
(Ill_formed_type (Some "storage", canonical_code, location storage_type))
(parse_storage_ty
ctxt
~stack_depth:(stack_depth + 1)
~legacy
storage_type)
>>?= fun (Ex_ty storage_type, ctxt) ->
pair_t loc arg_type storage_type >>?= fun (Ty_ex_c arg_type_full) ->
pair_t loc list_operation_t storage_type
>>?= fun (Ty_ex_c ret_type_full) ->
trace
(Ill_typed_contract (canonical_code, []))
(parse_returning
(Tc_context.toplevel ~storage_type ~param_type:arg_type ~entrypoints)
ctxt
~legacy
?type_logger
~stack_depth:(stack_depth + 1)
arg_type_full
ret_type_full
code_field)
>>=? fun ( Lam
( {kbef = Item_t (arg, Bot_t); kaft = Item_t (ret, Bot_t); _},
_ ),
ctxt ) ->
let views_result =
parse_views ctxt ?type_logger ~legacy storage_type views
in
trace (Ill_typed_contract (canonical_code, [])) views_result
>>=? fun (_typed_views, ctxt) ->
(Gas_monad.run ctxt
@@
let open Gas_monad.Syntax in
let* Eq = ty_eq ~error_details:Informative loc arg arg_type_full in
let* Eq = ty_eq ~error_details:Informative loc ret ret_type_full in
ty_eq ~error_details:Informative loc storage_type ginit)
>>?= fun (storage_eq, ctxt) ->
storage_eq >>?= fun Eq ->
let instr =
{
apply =
(fun kinfo k ->
ICreate_contract {kinfo; storage_type; code = canonical_code; k});
}
in
let stack = Item_t (operation_t, Item_t (address_t, rest)) in
typed ctxt loc instr stack
| (Prim (loc, I_NOW, [], annot), stack) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> INow (kinfo, k))} in
let stack = Item_t (timestamp_t, stack) in
typed ctxt loc instr stack
| (Prim (loc, I_MIN_BLOCK_TIME, [], _), stack) ->
typed
ctxt
loc
{apply = (fun kinfo k -> IMin_block_time (kinfo, k))}
(Item_t (nat_t, stack))
| (Prim (loc, I_AMOUNT, [], annot), stack) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAmount (kinfo, k))} in
let stack = Item_t (mutez_t, stack) in
typed ctxt loc instr stack
| (Prim (loc, I_CHAIN_ID, [], annot), stack) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IChainId (kinfo, k))} in
let stack = Item_t (chain_id_t, stack) in
typed ctxt loc instr stack
| (Prim (loc, I_BALANCE, [], annot), stack) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IBalance (kinfo, k))} in
let stack = Item_t (mutez_t, stack) in
typed ctxt loc instr stack
| (Prim (loc, I_LEVEL, [], annot), stack) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ILevel (kinfo, k))} in
let stack = Item_t (nat_t, stack) in
typed ctxt loc instr stack
| (Prim (loc, I_VOTING_POWER, [], annot), Item_t (Key_hash_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IVoting_power (kinfo, k))} in
let stack = Item_t (nat_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_TOTAL_VOTING_POWER, [], annot), stack) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ITotal_voting_power (kinfo, k))} in
let stack = Item_t (nat_t, stack) in
typed ctxt loc instr stack
| (Prim (_, I_STEPS_TO_QUOTA, _, _), _) ->
fail (Deprecated_instruction I_STEPS_TO_QUOTA)
| (Prim (loc, I_SOURCE, [], annot), stack) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISource (kinfo, k))} in
let stack = Item_t (address_t, stack) in
typed ctxt loc instr stack
| (Prim (loc, I_SENDER, [], annot), stack) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISender (kinfo, k))} in
let stack = Item_t (address_t, stack) in
typed ctxt loc instr stack
| (Prim (loc, (I_SELF as prim), [], annot), stack) ->
Lwt.return
( parse_entrypoint_annot_lax loc annot >>? fun entrypoint ->
let open Tc_context in
match tc_context.callsite with
| _ when is_in_lambda tc_context ->
error
(Forbidden_instr_in_context (loc, Script_tc_errors.Lambda, prim))
| Data ->
error
(Forbidden_instr_in_context (loc, Script_tc_errors.Lambda, prim))
| View ->
error
(Forbidden_instr_in_context (loc, Script_tc_errors.View, prim))
| Toplevel {param_type; entrypoints; storage_type = _} ->
Gas_monad.run ctxt
@@ find_entrypoint
~error_details:Informative
param_type
entrypoints
entrypoint
>>? fun (r, ctxt) ->
r >>? fun (Ex_ty_cstr {ty = param_type; _}) ->
contract_t loc param_type >>? fun res_ty ->
let instr =
{
apply =
(fun kinfo k -> ISelf (kinfo, param_type, entrypoint, k));
}
in
let stack = Item_t (res_ty, stack) in
typed_no_lwt ctxt loc instr stack )
| (Prim (loc, I_SELF_ADDRESS, [], annot), stack) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISelf_address (kinfo, k))} in
let stack = Item_t (address_t, stack) in
typed ctxt loc instr stack
| (Prim (loc, I_HASH_KEY, [], annot), Item_t (Key_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IHash_key (kinfo, k))} in
let stack = Item_t (key_hash_t, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_CHECK_SIGNATURE, [], annot),
Item_t (Key_t, Item_t (Signature_t, Item_t (Bytes_t, rest))) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ICheck_signature (kinfo, k))} in
let stack = Item_t (bool_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_BLAKE2B, [], annot), (Item_t (Bytes_t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IBlake2b (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_SHA256, [], annot), (Item_t (Bytes_t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISha256 (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_SHA512, [], annot), (Item_t (Bytes_t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISha512 (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_KECCAK, [], annot), (Item_t (Bytes_t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IKeccak (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_SHA3, [], annot), (Item_t (Bytes_t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> ISha3 (kinfo, k))} in
typed ctxt loc instr stack
| ( Prim (loc, I_ADD, [], annot),
Item_t (Bls12_381_g1_t, (Item_t (Bls12_381_g1_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAdd_bls12_381_g1 (kinfo, k))} in
typed ctxt loc instr stack
| ( Prim (loc, I_ADD, [], annot),
Item_t (Bls12_381_g2_t, (Item_t (Bls12_381_g2_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAdd_bls12_381_g2 (kinfo, k))} in
typed ctxt loc instr stack
| ( Prim (loc, I_ADD, [], annot),
Item_t (Bls12_381_fr_t, (Item_t (Bls12_381_fr_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IAdd_bls12_381_fr (kinfo, k))} in
typed ctxt loc instr stack
| ( Prim (loc, I_MUL, [], annot),
Item_t (Bls12_381_g1_t, Item_t (Bls12_381_fr_t, rest)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_bls12_381_g1 (kinfo, k))} in
let stack = Item_t (Bls12_381_g1_t, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_MUL, [], annot),
Item_t (Bls12_381_g2_t, Item_t (Bls12_381_fr_t, rest)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_bls12_381_g2 (kinfo, k))} in
let stack = Item_t (Bls12_381_g2_t, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_MUL, [], annot),
Item_t (Bls12_381_fr_t, (Item_t (Bls12_381_fr_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_bls12_381_fr (kinfo, k))} in
typed ctxt loc instr stack
| ( Prim (loc, I_MUL, [], annot),
Item_t (Nat_t, (Item_t (Bls12_381_fr_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_bls12_381_fr_z (kinfo, k))} in
typed ctxt loc instr stack
| ( Prim (loc, I_MUL, [], annot),
Item_t (Int_t, (Item_t (Bls12_381_fr_t, _) as stack)) ) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_bls12_381_fr_z (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_MUL, [], annot), Item_t (Bls12_381_fr_t, Item_t (Int_t, rest)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_bls12_381_z_fr (kinfo, k))} in
let stack = Item_t (Bls12_381_fr_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_MUL, [], annot), Item_t (Bls12_381_fr_t, Item_t (Nat_t, rest)))
->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IMul_bls12_381_z_fr (kinfo, k))} in
let stack = Item_t (Bls12_381_fr_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_INT, [], annot), Item_t (Bls12_381_fr_t, rest)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> IInt_bls12_381_fr (kinfo, k))} in
let stack = Item_t (int_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_NEG, [], annot), (Item_t (Bls12_381_g1_t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> INeg_bls12_381_g1 (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_NEG, [], annot), (Item_t (Bls12_381_g2_t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> INeg_bls12_381_g2 (kinfo, k))} in
typed ctxt loc instr stack
| (Prim (loc, I_NEG, [], annot), (Item_t (Bls12_381_fr_t, _) as stack)) ->
check_var_annot loc annot >>?= fun () ->
let instr = {apply = (fun kinfo k -> INeg_bls12_381_fr (kinfo, k))} in
typed ctxt loc instr stack
| ( Prim (loc, I_PAIRING_CHECK, [], annot),
Item_t (List_t (Pair_t (Bls12_381_g1_t, Bls12_381_g2_t, _, _), _), rest)
) ->
check_var_annot loc annot >>?= fun () ->
let instr =
{apply = (fun kinfo k -> IPairing_check_bls12_381 (kinfo, k))}
in
let stack = Item_t (bool_t, rest) in
typed ctxt loc instr stack
| (Prim (loc, I_TICKET, [], annot), Item_t (t, Item_t (Nat_t, rest))) ->
check_var_annot loc annot >>?= fun () ->
comparable_ty_of_ty ctxt loc t >>?= fun (ty, ctxt) ->
ticket_t loc ty >>?= fun res_ty ->
let instr = {apply = (fun kinfo k -> ITicket (kinfo, k))} in
let stack = Item_t (res_ty, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_READ_TICKET, [], annot),
(Item_t (Ticket_t (t, _), _) as full_stack) ) ->
check_var_annot loc annot >>?= fun () ->
let () = check_dupable_comparable_ty t in
opened_ticket_type loc t >>?= fun opened_ticket_ty ->
let result = ty_of_comparable_ty opened_ticket_ty in
let instr = {apply = (fun kinfo k -> IRead_ticket (kinfo, k))} in
let stack = Item_t (result, full_stack) in
typed ctxt loc instr stack
| ( Prim (loc, I_SPLIT_TICKET, [], annot),
Item_t
( (Ticket_t (t, _) as ticket_t),
Item_t (Pair_t (Nat_t, Nat_t, _, _), rest) ) ) ->
check_var_annot loc annot >>?= fun () ->
let () = check_dupable_comparable_ty t in
pair_t loc ticket_t ticket_t >>?= fun (Ty_ex_c pair_tickets_ty) ->
option_t loc pair_tickets_ty >>?= fun res_ty ->
let instr = {apply = (fun kinfo k -> ISplit_ticket (kinfo, k))} in
let stack = Item_t (res_ty, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_JOIN_TICKETS, [], annot),
Item_t
( Pair_t
( (Ticket_t (contents_ty_a, _) as ty_a),
Ticket_t (contents_ty_b, _),
_,
_ ),
rest ) ) ->
check_var_annot loc annot >>?= fun () ->
Gas_monad.run ctxt
@@ comparable_ty_eq ~error_details:Informative contents_ty_a contents_ty_b
>>?= fun (eq, ctxt) ->
eq >>?= fun Eq ->
option_t loc ty_a >>?= fun res_ty ->
let instr =
{apply = (fun kinfo k -> IJoin_tickets (kinfo, contents_ty_a, k))}
in
let stack = Item_t (res_ty, rest) in
typed ctxt loc instr stack
| ( Prim (loc, I_OPEN_CHEST, [], _),
Item_t (Chest_key_t, Item_t (Chest_t, Item_t (Nat_t, rest))) ) ->
let instr = {apply = (fun kinfo k -> IOpen_chest (kinfo, k))} in
typed ctxt loc instr (Item_t (union_bytes_bool_t, rest))
| ( Prim
( loc,
(( I_DUP | I_SWAP | I_SOME | I_UNIT | I_PAIR | I_UNPAIR | I_CAR
| I_CDR | I_CONS | I_CONCAT | I_SLICE | I_MEM | I_UPDATE | I_GET
| I_EXEC | I_FAILWITH | I_SIZE | I_ADD | I_SUB | I_SUB_MUTEZ | I_MUL
| I_EDIV | I_OR | I_AND | I_XOR | I_NOT | I_ABS | I_NEG | I_LSL
| I_LSR | I_COMPARE | I_EQ | I_NEQ | I_LT | I_GT | I_LE | I_GE
| I_TRANSFER_TOKENS | I_SET_DELEGATE | I_NOW | I_MIN_BLOCK_TIME
| I_IMPLICIT_ACCOUNT | I_AMOUNT | I_BALANCE | I_LEVEL
| I_CHECK_SIGNATURE | I_HASH_KEY | I_SOURCE | I_SENDER | I_BLAKE2B
| I_SHA256 | I_SHA512 | I_ADDRESS | I_RENAME | I_PACK | I_ISNAT
| I_INT | I_SELF | I_CHAIN_ID | I_NEVER | I_VOTING_POWER
| I_TOTAL_VOTING_POWER | I_KECCAK | I_SHA3 | I_PAIRING_CHECK
| I_TICKET | I_READ_TICKET | I_SPLIT_TICKET | I_JOIN_TICKETS
| I_OPEN_CHEST ) as name),
(_ :: _ as l),
_ ),
_ ) ->
fail (Invalid_arity (loc, name, 0, List.length l))
| ( Prim
( loc,
(( I_NONE | I_LEFT | I_RIGHT | I_NIL | I_MAP | I_ITER | I_EMPTY_SET
| I_LOOP | I_LOOP_LEFT | I_CONTRACT | I_CAST | I_UNPACK
| I_CREATE_CONTRACT ) as name),
(([] | _ :: _ :: _) as l),
_ ),
_ ) ->
fail (Invalid_arity (loc, name, 1, List.length l))
| ( Prim
( loc,
(( I_PUSH | I_VIEW | I_IF_NONE | I_IF_LEFT | I_IF_CONS | I_EMPTY_MAP
| I_EMPTY_BIG_MAP | I_IF ) as name),
(([] | [_] | _ :: _ :: _ :: _) as l),
_ ),
_ ) ->
fail (Invalid_arity (loc, name, 2, List.length l))
| ( Prim (loc, I_LAMBDA, (([] | [_] | [_; _] | _ :: _ :: _ :: _ :: _) as l), _),
_ ) ->
fail (Invalid_arity (loc, I_LAMBDA, 3, List.length l))
| ( Prim
( loc,
(( I_ADD | I_SUB | I_SUB_MUTEZ | I_MUL | I_EDIV | I_AND | I_OR | I_XOR
| I_LSL | I_LSR | I_CONCAT | I_PAIRING_CHECK ) as name),
[],
_ ),
Item_t (ta, Item_t (tb, _)) ) ->
let ta = serialize_ty_for_error ta in
let tb = serialize_ty_for_error tb in
fail (Undefined_binop (loc, name, ta, tb))
| ( Prim
( loc,
(( I_NEG | I_ABS | I_NOT | I_SIZE | I_EQ | I_NEQ | I_LT | I_GT | I_LE
| I_GE
| I_CONCAT ) as name),
[],
_ ),
Item_t (t, _) ) ->
let t = serialize_ty_for_error t in
fail (Undefined_unop (loc, name, t))
| (Prim (loc, ((I_UPDATE | I_SLICE | I_OPEN_CHEST) as name), [], _), stack) ->
Lwt.return
(let stack = serialize_stack_for_error ctxt stack in
error (Bad_stack (loc, name, 3, stack)))
| (Prim (loc, I_CREATE_CONTRACT, _, _), stack) ->
let stack = serialize_stack_for_error ctxt stack in
fail (Bad_stack (loc, I_CREATE_CONTRACT, 7, stack))
| (Prim (loc, I_TRANSFER_TOKENS, [], _), stack) ->
Lwt.return
(let stack = serialize_stack_for_error ctxt stack in
error (Bad_stack (loc, I_TRANSFER_TOKENS, 4, stack)))
| ( Prim
( loc,
(( I_DROP | I_DUP | I_CAR | I_CDR | I_UNPAIR | I_SOME | I_BLAKE2B
| I_SHA256 | I_SHA512 | I_DIP | I_IF_NONE | I_LEFT | I_RIGHT
| I_IF_LEFT | I_IF | I_LOOP | I_IF_CONS | I_IMPLICIT_ACCOUNT | I_NEG
| I_ABS | I_INT | I_NOT | I_HASH_KEY | I_EQ | I_NEQ | I_LT | I_GT
| I_LE | I_GE | I_SIZE | I_FAILWITH | I_RENAME | I_PACK | I_ISNAT
| I_ADDRESS | I_SET_DELEGATE | I_CAST | I_MAP | I_ITER | I_LOOP_LEFT
| I_UNPACK | I_CONTRACT | I_NEVER | I_KECCAK | I_SHA3 | I_READ_TICKET
| I_JOIN_TICKETS ) as name),
_,
_ ),
stack ) ->
Lwt.return
(let stack = serialize_stack_for_error ctxt stack in
error (Bad_stack (loc, name, 1, stack)))
| ( Prim
( loc,
(( I_SWAP | I_PAIR | I_CONS | I_GET | I_MEM | I_EXEC
| I_CHECK_SIGNATURE | I_ADD | I_SUB | I_SUB_MUTEZ | I_MUL | I_EDIV
| I_AND | I_OR | I_XOR | I_LSL | I_LSR | I_COMPARE | I_PAIRING_CHECK
| I_TICKET | I_SPLIT_TICKET ) as name),
_,
_ ),
stack ) ->
Lwt.return
(let stack = serialize_stack_for_error ctxt stack in
error (Bad_stack (loc, name, 2, stack)))
| (expr, _) ->
fail
@@ unexpected
expr
[Seq_kind]
Instr_namespace
[
I_DROP;
I_DUP;
I_DIG;
I_DUG;
I_VIEW;
I_SWAP;
I_SOME;
I_UNIT;
I_PAIR;
I_UNPAIR;
I_CAR;
I_CDR;
I_CONS;
I_MEM;
I_UPDATE;
I_MAP;
I_ITER;
I_GET;
I_GET_AND_UPDATE;
I_EXEC;
I_FAILWITH;
I_SIZE;
I_CONCAT;
I_ADD;
I_SUB;
I_SUB_MUTEZ;
I_MUL;
I_EDIV;
I_OR;
I_AND;
I_XOR;
I_NOT;
I_ABS;
I_INT;
I_NEG;
I_LSL;
I_LSR;
I_COMPARE;
I_EQ;
I_NEQ;
I_LT;
I_GT;
I_LE;
I_GE;
I_TRANSFER_TOKENS;
I_CREATE_CONTRACT;
I_NOW;
I_MIN_BLOCK_TIME;
I_AMOUNT;
I_BALANCE;
I_LEVEL;
I_IMPLICIT_ACCOUNT;
I_CHECK_SIGNATURE;
I_BLAKE2B;
I_SHA256;
I_SHA512;
I_HASH_KEY;
I_PUSH;
I_NONE;
I_LEFT;
I_RIGHT;
I_NIL;
I_EMPTY_SET;
I_DIP;
I_LOOP;
I_IF_NONE;
I_IF_LEFT;
I_IF_CONS;
I_EMPTY_MAP;
I_EMPTY_BIG_MAP;
I_IF;
I_SOURCE;
I_SENDER;
I_SELF;
I_SELF_ADDRESS;
I_LAMBDA;
I_NEVER;
I_VOTING_POWER;
I_TOTAL_VOTING_POWER;
I_KECCAK;
I_SHA3;
I_PAIRING_CHECK;
I_SAPLING_EMPTY_STATE;
I_SAPLING_VERIFY_UPDATE;
I_TICKET;
I_READ_TICKET;
I_SPLIT_TICKET;
I_JOIN_TICKETS;
I_OPEN_CHEST;
]
and[@coq_axiom_with_reason "complex mutually recursive definition"] parse_contract :
type arg argc.
stack_depth:int ->
context ->
Script.location ->
(arg, argc) ty ->
Destination.t ->
entrypoint:Entrypoint.t ->
(context * arg typed_contract) tzresult Lwt.t =
fun ~stack_depth ctxt loc arg destination ~entrypoint ->
match destination with
| Contract contract -> (
match Contract.is_implicit contract with
| Some _ ->
if Entrypoint.is_default entrypoint then
Lwt.return
( Gas_monad.run ctxt
@@ ty_eq ~error_details:Informative loc arg unit_t
>>? fun (eq, ctxt) ->
eq >|? fun Eq ->
let destination : Destination.t = Contract contract in
let address = {destination; entrypoint} in
(ctxt, Typed_contract {arg_ty = arg; address}) )
else fail (No_such_entrypoint entrypoint)
| None -> (
trace (Invalid_contract (loc, contract))
@@ Contract.get_script_code ctxt contract
>>=? fun (ctxt, code) ->
match code with
| None -> fail (Invalid_contract (loc, contract))
| Some code ->
Lwt.return
( Script.force_decode_in_context
~consume_deserialization_gas:When_needed
ctxt
code
>>? fun (code, ctxt) ->
parse_toplevel ctxt ~legacy:true code
>>? fun ({arg_type; _}, ctxt) ->
parse_parameter_ty_and_entrypoints
ctxt
~stack_depth:(stack_depth + 1)
~legacy:true
arg_type
>>? fun ( Ex_parameter_ty_and_entrypoints
{arg_type = targ; entrypoints},
ctxt ) ->
Gas_monad.run ctxt
@@ find_entrypoint_for_type
~error_details:Informative
~full:targ
~expected:arg
entrypoints
entrypoint
loc
>>? fun (entrypoint_arg, ctxt) ->
entrypoint_arg >|? fun (entrypoint, arg_ty) ->
let address = {destination; entrypoint} in
(ctxt, Typed_contract {arg_ty; address}) )))
| Tx_rollup tx_rollup ->
Tx_rollup_state.assert_exist ctxt tx_rollup >>=? fun ctxt ->
if Entrypoint.(entrypoint = Tx_rollup.deposit_entrypoint) then
match arg with
| Pair_t (Ticket_t (_, _), Tx_rollup_l2_address_t, _, _) ->
let address = {destination; entrypoint} in
return (ctxt, Typed_contract {arg_ty = arg; address})
| _ ->
fail
@@ Tx_rollup_bad_deposit_parameter (loc, serialize_ty_for_error arg)
else fail (No_such_entrypoint entrypoint)
and parse_view_name ctxt : Script.node -> (Script_string.t * context) tzresult =
function
| String (loc, v) as expr ->
if Compare.Int.(String.length v > 31) then error (View_name_too_long v)
else
let rec check_char i =
if Compare.Int.(i < 0) then ok v
else if Script_ir_annot.is_allowed_char v.[i] then check_char (i - 1)
else error (Bad_view_name loc)
in
Gas.consume ctxt (Typecheck_costs.check_printable v) >>? fun ctxt ->
record_trace
(Invalid_syntactic_constant
( loc,
strip_locations expr,
"string [a-zA-Z0-9_.%@] and the maximum string length of 31 \
characters" ))
( check_char (String.length v - 1) >>? fun v ->
Script_string.of_string v >|? fun s -> (s, ctxt) )
| expr -> error @@ Invalid_kind (location expr, [String_kind], kind expr)
and parse_toplevel :
context -> legacy:bool -> Script.expr -> (toplevel * context) tzresult =
fun ctxt ~legacy toplevel ->
record_trace (Ill_typed_contract (toplevel, []))
@@
match root toplevel with
| Int (loc, _) -> error (Invalid_kind (loc, [Seq_kind], Int_kind))
| String (loc, _) -> error (Invalid_kind (loc, [Seq_kind], String_kind))
| Bytes (loc, _) -> error (Invalid_kind (loc, [Seq_kind], Bytes_kind))
| Prim (loc, _, _, _) -> error (Invalid_kind (loc, [Seq_kind], Prim_kind))
| Seq (_, fields) -> (
let rec find_fields ctxt p s c views fields =
match fields with
| [] -> ok (ctxt, (p, s, c, views))
| Int (loc, _) :: _ -> error (Invalid_kind (loc, [Prim_kind], Int_kind))
| String (loc, _) :: _ ->
error (Invalid_kind (loc, [Prim_kind], String_kind))
| Bytes (loc, _) :: _ ->
error (Invalid_kind (loc, [Prim_kind], Bytes_kind))
| Seq (loc, _) :: _ -> error (Invalid_kind (loc, [Prim_kind], Seq_kind))
| Prim (loc, K_parameter, [arg], annot) :: rest -> (
match p with
| None -> find_fields ctxt (Some (arg, loc, annot)) s c views rest
| Some _ -> error (Duplicate_field (loc, K_parameter)))
| Prim (loc, K_storage, [arg], annot) :: rest -> (
match s with
| None -> find_fields ctxt p (Some (arg, loc, annot)) c views rest
| Some _ -> error (Duplicate_field (loc, K_storage)))
| Prim (loc, K_code, [arg], annot) :: rest -> (
match c with
| None -> find_fields ctxt p s (Some (arg, loc, annot)) views rest
| Some _ -> error (Duplicate_field (loc, K_code)))
| Prim (loc, ((K_parameter | K_storage | K_code) as name), args, _) :: _
->
error (Invalid_arity (loc, name, 1, List.length args))
| Prim (loc, K_view, [name; input_ty; output_ty; view_code], _) :: rest
->
parse_view_name ctxt name >>? fun (str, ctxt) ->
Gas.consume
ctxt
(Michelson_v1_gas.Cost_of.Interpreter.view_update str views)
>>? fun ctxt ->
if Script_map.mem str views then error (Duplicated_view_name loc)
else
let views' =
Script_map.update
str
(Some {input_ty; output_ty; view_code})
views
in
find_fields ctxt p s c views' rest
| Prim (loc, K_view, args, _) :: _ ->
error (Invalid_arity (loc, K_view, 4, List.length args))
| Prim (loc, name, _, _) :: _ ->
let allowed = [K_parameter; K_storage; K_code; K_view] in
error (Invalid_primitive (loc, allowed, name))
in
find_fields ctxt None None None (Script_map.empty string_key) fields
>>? fun (ctxt, toplevel) ->
match toplevel with
| (None, _, _, _) -> error (Missing_field K_parameter)
| (Some _, None, _, _) -> error (Missing_field K_storage)
| (Some _, Some _, None, _) -> error (Missing_field K_code)
| ( Some (p, ploc, pannot),
Some (s, sloc, sannot),
Some (c, cloc, cannot),
views ) ->
let p_pannot =
Script_ir_annot.has_field_annot p >>? function
| true -> ok (p, pannot)
| false -> (
match pannot with
| [single] when legacy -> (
is_field_annot ploc single >|? fun is_field_annot ->
match (is_field_annot, p) with
| (true, Prim (loc, prim, args, annots)) ->
(Prim (loc, prim, args, single :: annots), [])
| _ -> (p, []))
| _ -> ok (p, pannot))
in
p_pannot >>? fun (arg_type, pannot) ->
Script_ir_annot.error_unexpected_annot ploc pannot >>? fun () ->
Script_ir_annot.error_unexpected_annot cloc cannot >>? fun () ->
Script_ir_annot.error_unexpected_annot sloc sannot >|? fun () ->
({code_field = c; arg_type; views; storage_type = s}, ctxt))
let parse_contract_for_script :
type arg argc.
context ->
Script.location ->
(arg, argc) ty ->
Destination.t ->
entrypoint:Entrypoint.t ->
(context * arg typed_contract option) tzresult Lwt.t =
fun ctxt loc arg contract ~entrypoint ->
match contract with
| Contract contract -> (
match Contract.is_implicit contract with
| Some _ ->
if Entrypoint.is_default entrypoint then
Lwt.return
( Gas_monad.run ctxt @@ ty_eq ~error_details:Fast loc arg unit_t
>|? fun (eq, ctxt) ->
match eq with
| Ok Eq ->
let destination : Destination.t = Contract contract in
let address = {destination; entrypoint} in
let contract = Typed_contract {arg_ty = arg; address} in
(ctxt, Some contract)
| Error Inconsistent_types_fast -> (ctxt, None) )
else
Lwt.return
( Gas.consume ctxt Typecheck_costs.parse_instr_cycle
>|? fun ctxt ->
(ctxt, None) )
| None -> (
trace (Invalid_contract (loc, contract))
@@ Contract.get_script_code ctxt contract
>>=? fun (ctxt, code) ->
match code with
| None -> return (ctxt, None)
| Some code ->
Lwt.return
( Script.force_decode_in_context
~consume_deserialization_gas:When_needed
ctxt
code
>>? fun (code, ctxt) ->
match parse_toplevel ctxt ~legacy:true code with
| Error _ -> error (Invalid_contract (loc, contract))
| Ok ({arg_type; _}, ctxt) -> (
match
parse_parameter_ty_and_entrypoints
ctxt
~stack_depth:0
~legacy:true
arg_type
with
| Error _ -> error (Invalid_contract (loc, contract))
| Ok
( Ex_parameter_ty_and_entrypoints
{arg_type = targ; entrypoints},
ctxt ) -> (
Gas_monad.run ctxt
@@ find_entrypoint_for_type
~error_details:Fast
~full:targ
~expected:arg
entrypoints
entrypoint
loc
>|? fun (entrypoint_arg, ctxt) ->
match entrypoint_arg with
| Ok (entrypoint, arg_ty) ->
let destination = Destination.Contract contract in
let address = {destination; entrypoint} in
let contract = Typed_contract {arg_ty; address} in
(ctxt, Some contract)
| Error Inconsistent_types_fast -> (ctxt, None))) )))
| Tx_rollup tx_rollup -> (
match arg with
| Pair_t (Ticket_t (_, _), Tx_rollup_l2_address_t, _, _)
when Entrypoint.(
entrypoint = Alpha_context.Tx_rollup.deposit_entrypoint) -> (
Tx_rollup_state.find ctxt tx_rollup >|=? function
| (ctxt, Some _) ->
let address = {destination = contract; entrypoint} in
(ctxt, Some (Typed_contract {arg_ty = arg; address}))
| (ctxt, None) -> (ctxt, None))
| _ -> return (ctxt, None))
let view_size view =
let open Script_typed_ir_size in
node_size view.view_code ++ node_size view.input_ty
++ node_size view.output_ty
let code_size ctxt code views =
let open Script_typed_ir_size in
let views_size = Script_map.fold (fun _ v s -> view_size v ++ s) views zero in
let ir_size = lambda_size code in
let (nodes, code_size) = views_size ++ ir_size in
Gas.consume ctxt (Script_typed_ir_size_costs.nodes_cost ~nodes)
>|? fun ctxt -> (code_size, ctxt)
let parse_code :
?type_logger:type_logger ->
context ->
legacy:bool ->
code:lazy_expr ->
(ex_code * context) tzresult Lwt.t =
fun ?type_logger ctxt ~legacy ~code ->
Script.force_decode_in_context
~consume_deserialization_gas:When_needed
ctxt
code
>>?= fun (code, ctxt) ->
Global_constants_storage.expand ctxt code >>=? fun (ctxt, code) ->
parse_toplevel ctxt ~legacy code
>>?= fun ({arg_type; storage_type; code_field; views}, ctxt) ->
let arg_type_loc = location arg_type in
record_trace
(Ill_formed_type (Some "parameter", code, arg_type_loc))
(parse_parameter_ty_and_entrypoints ctxt ~stack_depth:0 ~legacy arg_type)
>>?= fun (Ex_parameter_ty_and_entrypoints {arg_type; entrypoints}, ctxt) ->
let storage_type_loc = location storage_type in
record_trace
(Ill_formed_type (Some "storage", code, storage_type_loc))
(parse_storage_ty ctxt ~stack_depth:0 ~legacy storage_type)
>>?= fun (Ex_ty storage_type, ctxt) ->
pair_t storage_type_loc arg_type storage_type
>>?= fun (Ty_ex_c arg_type_full) ->
pair_t storage_type_loc list_operation_t storage_type
>>?= fun (Ty_ex_c ret_type_full) ->
trace
(Ill_typed_contract (code, []))
(parse_returning
Tc_context.(toplevel ~storage_type ~param_type:arg_type ~entrypoints)
ctxt
~legacy
~stack_depth:0
?type_logger
arg_type_full
ret_type_full
code_field)
>>=? fun (code, ctxt) ->
Lwt.return
( code_size ctxt code views >>? fun (code_size, ctxt) ->
ok
( Ex_code
(Code {code; arg_type; storage_type; views; entrypoints; code_size}),
ctxt ) )
let parse_storage :
?type_logger:type_logger ->
context ->
legacy:bool ->
allow_forged:bool ->
('storage, _) ty ->
storage:lazy_expr ->
('storage * context) tzresult Lwt.t =
fun ?type_logger ctxt ~legacy ~allow_forged storage_type ~storage ->
Script.force_decode_in_context
~consume_deserialization_gas:When_needed
ctxt
storage
>>?= fun (storage, ctxt) ->
trace_eval
(fun () ->
let storage_type = serialize_ty_for_error storage_type in
Ill_typed_data (None, storage, storage_type))
(parse_data
?type_logger
~stack_depth:0
ctxt
~legacy
~allow_forged
storage_type
(root storage))
let[@coq_axiom_with_reason "gadt"] parse_script :
?type_logger:type_logger ->
context ->
legacy:bool ->
allow_forged_in_storage:bool ->
Script.t ->
(ex_script * context) tzresult Lwt.t =
fun ?type_logger ctxt ~legacy ~allow_forged_in_storage {code; storage} ->
parse_code ~legacy ctxt ?type_logger ~code
>>=? fun ( Ex_code
(Code
{code; arg_type; storage_type; views; entrypoints; code_size}),
ctxt ) ->
parse_storage
?type_logger
ctxt
~legacy
~allow_forged:allow_forged_in_storage
storage_type
~storage
>|=? fun (storage, ctxt) ->
( Ex_script
(Script
{code_size; code; arg_type; storage; storage_type; views; entrypoints}),
ctxt )
type typechecked_code_internal =
| Typechecked_code_internal : {
toplevel : toplevel;
arg_type : ('arg, _) ty;
storage_type : ('storage, _) ty;
entrypoints : 'arg entrypoints;
typed_views : 'storage typed_view_map;
type_map : type_map;
}
-> typechecked_code_internal
let typecheck_code :
legacy:bool ->
show_types:bool ->
context ->
Script.expr ->
(typechecked_code_internal * context) tzresult Lwt.t =
fun ~legacy ~show_types ctxt code ->
Global_constants_storage.expand ctxt code >>=? fun (ctxt, code) ->
parse_toplevel ctxt ~legacy code >>?= fun (toplevel, ctxt) ->
let {arg_type; storage_type; code_field; views} = toplevel in
let type_map = ref [] in
let arg_type_loc = location arg_type in
record_trace
(Ill_formed_type (Some "parameter", code, arg_type_loc))
(parse_parameter_ty_and_entrypoints ctxt ~stack_depth:0 ~legacy arg_type)
>>?= fun (Ex_parameter_ty_and_entrypoints {arg_type; entrypoints}, ctxt) ->
let storage_type_loc = location storage_type in
record_trace
(Ill_formed_type (Some "storage", code, storage_type_loc))
(parse_storage_ty ctxt ~stack_depth:0 ~legacy storage_type)
>>?= fun (ex_storage_type, ctxt) ->
let (Ex_ty storage_type) = ex_storage_type in
pair_t storage_type_loc arg_type storage_type
>>?= fun (Ty_ex_c arg_type_full) ->
pair_t storage_type_loc list_operation_t storage_type
>>?= fun (Ty_ex_c ret_type_full) ->
let type_logger loc ~stack_ty_before ~stack_ty_after =
type_map := (loc, (stack_ty_before, stack_ty_after)) :: !type_map
in
let type_logger = if show_types then Some type_logger else None in
let result =
parse_returning
(Tc_context.toplevel ~storage_type ~param_type:arg_type ~entrypoints)
ctxt
~legacy
~stack_depth:0
?type_logger
arg_type_full
ret_type_full
code_field
in
trace (Ill_typed_contract (code, !type_map)) result >>=? fun (Lam _, ctxt) ->
let views_result = parse_views ctxt ?type_logger ~legacy storage_type views in
trace (Ill_typed_contract (code, !type_map)) views_result
>|=? fun (typed_views, ctxt) ->
( Typechecked_code_internal
{
toplevel;
arg_type;
storage_type;
entrypoints;
typed_views;
type_map = !type_map;
},
ctxt )
let list_entrypoints_uncarbonated (type full fullc) (full : (full, fullc) ty)
(entrypoints : full entrypoints) =
let merge path (type t tc) (ty : (t, tc) ty)
(entrypoints : t entrypoints_node) reachable ((unreachables, all) as acc)
=
match entrypoints.at_node with
| None ->
( (if reachable then acc
else
match ty with
| Union_t _ -> acc
| _ -> (List.rev path :: unreachables, all)),
reachable )
| Some {name; original_type_expr} ->
( (if Entrypoint.Map.mem name all then
(List.rev path :: unreachables, all)
else
( unreachables,
Entrypoint.Map.add name (Ex_ty ty, original_type_expr) all )),
true )
in
let rec fold_tree :
type t tc.
(t, tc) ty ->
t entrypoints_node ->
prim list ->
bool ->
prim list list * (ex_ty * Script.node) Entrypoint.Map.t ->
prim list list * (ex_ty * Script.node) Entrypoint.Map.t =
fun t entrypoints path reachable acc ->
match (t, entrypoints) with
| (Union_t (tl, tr, _, _), {nested = Entrypoints_Union {left; right}; _}) ->
let (acc, l_reachable) = merge (D_Left :: path) tl left reachable acc in
let (acc, r_reachable) =
merge (D_Right :: path) tr right reachable acc
in
let acc = fold_tree tl left (D_Left :: path) l_reachable acc in
fold_tree tr right (D_Right :: path) r_reachable acc
| _ -> acc
in
let (init, reachable) =
match entrypoints.root.at_node with
| None -> (Entrypoint.Map.empty, false)
| Some {name; original_type_expr} ->
(Entrypoint.Map.singleton name (Ex_ty full, original_type_expr), true)
in
fold_tree full entrypoints.root [] reachable ([], init)
[@@coq_axiom_with_reason "unsupported syntax"]
let comb_witness2 :
type t tc. (t, tc) ty -> (t, unit -> unit -> unit) comb_witness = function
| Pair_t (_, Pair_t _, _, _) -> Comb_Pair (Comb_Pair Comb_Any)
| Pair_t _ -> Comb_Pair Comb_Any
| _ -> Comb_Any
let[@coq_axiom_with_reason "gadt"] rec unparse_data :
type a ac.
context ->
stack_depth:int ->
unparsing_mode ->
(a, ac) ty ->
a ->
(Script.node * context) tzresult Lwt.t =
fun ctxt ~stack_depth mode ty a ->
Gas.consume ctxt Unparse_costs.unparse_data_cycle >>?= fun ctxt ->
let non_terminal_recursion ctxt mode ty a =
if Compare.Int.(stack_depth > 10_000) then
fail Unparsing_too_many_recursive_calls
else unparse_data ctxt ~stack_depth:(stack_depth + 1) mode ty a
in
let loc = Micheline.dummy_location in
match (ty, a) with
| (Unit_t, v) -> Lwt.return @@ unparse_unit ~loc ctxt v
| (Int_t, v) -> Lwt.return @@ unparse_int ~loc ctxt v
| (Nat_t, v) -> Lwt.return @@ unparse_nat ~loc ctxt v
| (String_t, s) -> Lwt.return @@ unparse_string ~loc ctxt s
| (Bytes_t, s) -> Lwt.return @@ unparse_bytes ~loc ctxt s
| (Bool_t, b) -> Lwt.return @@ unparse_bool ~loc ctxt b
| (Timestamp_t, t) -> Lwt.return @@ unparse_timestamp ~loc ctxt mode t
| (Address_t, address) -> Lwt.return @@ unparse_address ~loc ctxt mode address
| (Tx_rollup_l2_address_t, address) ->
Lwt.return @@ unparse_tx_rollup_l2_address ~loc ctxt mode address
| (Contract_t _, contract) ->
Lwt.return @@ unparse_contract ~loc ctxt mode contract
| (Signature_t, s) -> Lwt.return @@ unparse_signature ~loc ctxt mode s
| (Mutez_t, v) -> Lwt.return @@ unparse_mutez ~loc ctxt v
| (Key_t, k) -> Lwt.return @@ unparse_key ~loc ctxt mode k
| (Key_hash_t, k) -> Lwt.return @@ unparse_key_hash ~loc ctxt mode k
| (Operation_t, operation) ->
Lwt.return @@ unparse_operation ~loc ctxt operation
| (Chain_id_t, chain_id) ->
Lwt.return @@ unparse_chain_id ~loc ctxt mode chain_id
| (Bls12_381_g1_t, x) -> Lwt.return @@ unparse_bls12_381_g1 ~loc ctxt x
| (Bls12_381_g2_t, x) -> Lwt.return @@ unparse_bls12_381_g2 ~loc ctxt x
| (Bls12_381_fr_t, x) -> Lwt.return @@ unparse_bls12_381_fr ~loc ctxt x
| (Pair_t (tl, tr, _, _), pair) ->
let r_witness = comb_witness2 tr in
let unparse_l ctxt v = non_terminal_recursion ctxt mode tl v in
let unparse_r ctxt v = non_terminal_recursion ctxt mode tr v in
unparse_pair ~loc unparse_l unparse_r ctxt mode r_witness pair
| (Union_t (tl, tr, _, _), v) ->
let unparse_l ctxt v = non_terminal_recursion ctxt mode tl v in
let unparse_r ctxt v = non_terminal_recursion ctxt mode tr v in
unparse_union ~loc unparse_l unparse_r ctxt v
| (Option_t (t, _, _), v) ->
let unparse_v ctxt v = non_terminal_recursion ctxt mode t v in
unparse_option ~loc unparse_v ctxt v
| (List_t (t, _), items) ->
List.fold_left_es
(fun (l, ctxt) element ->
non_terminal_recursion ctxt mode t element
>|=? fun (unparsed, ctxt) -> (unparsed :: l, ctxt))
([], ctxt)
items.elements
>|=? fun (items, ctxt) -> (Micheline.Seq (loc, List.rev items), ctxt)
| (Ticket_t (t, _), {ticketer; contents; amount}) ->
opened_ticket_type loc t >>?= fun opened_ticket_ty ->
let t = ty_of_comparable_ty opened_ticket_ty in
let destination : Destination.t = Contract ticketer in
let addr = {destination; entrypoint = Entrypoint.default} in
(unparse_data [@tailcall])
ctxt
~stack_depth
mode
t
(addr, (contents, amount))
| (Set_t (t, _), set) ->
List.fold_left_es
(fun (l, ctxt) item ->
unparse_comparable_data ~loc ctxt mode t item >|=? fun (item, ctxt) ->
(item :: l, ctxt))
([], ctxt)
(Script_set.fold (fun e acc -> e :: acc) set [])
>|=? fun (items, ctxt) -> (Micheline.Seq (loc, items), ctxt)
| (Map_t (kt, vt, _), map) ->
let items = Script_map.fold (fun k v acc -> (k, v) :: acc) map [] in
unparse_items ctxt ~stack_depth:(stack_depth + 1) mode kt vt items
>|=? fun (items, ctxt) -> (Micheline.Seq (loc, items), ctxt)
| (Big_map_t (_kt, _vt, _), Big_map {id = Some id; diff = {size; _}; _})
when Compare.Int.( = ) size 0 ->
return (Micheline.Int (loc, Big_map.Id.unparse_to_z id), ctxt)
| (Big_map_t (kt, vt, _), Big_map {id = Some id; diff = {map; _}; _}) ->
let items =
Big_map_overlay.fold (fun _ (k, v) acc -> (k, v) :: acc) map []
in
let items =
List.sort
(fun (a, _) (b, _) -> Script_comparable.compare_comparable kt b a)
items
in
option_t loc vt >>?= fun vt ->
unparse_items ctxt ~stack_depth:(stack_depth + 1) mode kt vt items
>|=? fun (items, ctxt) ->
( Micheline.Prim
( loc,
D_Pair,
[Int (loc, Big_map.Id.unparse_to_z id); Seq (loc, items)],
[] ),
ctxt )
| (Big_map_t (kt, vt, _), Big_map {id = None; diff = {map; _}; _}) ->
let items =
Big_map_overlay.fold
(fun _ (k, v) acc ->
match v with None -> acc | Some v -> (k, v) :: acc)
map
[]
in
let items =
List.sort
(fun (a, _) (b, _) -> Script_comparable.compare_comparable kt b a)
items
in
unparse_items ctxt ~stack_depth:(stack_depth + 1) mode kt vt items
>|=? fun (items, ctxt) -> (Micheline.Seq (loc, items), ctxt)
| (Lambda_t _, Lam (_, original_code)) ->
unparse_code ctxt ~stack_depth:(stack_depth + 1) mode original_code
| (Never_t, _) -> .
| (Sapling_transaction_t _, s) ->
Lwt.return
( Gas.consume ctxt (Unparse_costs.sapling_transaction s) >|? fun ctxt ->
let bytes =
Data_encoding.Binary.to_bytes_exn Sapling.transaction_encoding s
in
(Bytes (loc, bytes), ctxt) )
| (Sapling_transaction_deprecated_t _, s) ->
Lwt.return
( Gas.consume ctxt (Unparse_costs.sapling_transaction_deprecated s)
>|? fun ctxt ->
let bytes =
Data_encoding.Binary.to_bytes_exn
Sapling.Legacy.transaction_encoding
s
in
(Bytes (loc, bytes), ctxt) )
| (Sapling_state_t _, {id; diff; _}) ->
Lwt.return
( Gas.consume ctxt (Unparse_costs.sapling_diff diff) >|? fun ctxt ->
( (match diff with
| {commitments_and_ciphertexts = []; nullifiers = []} -> (
match id with
| None -> Micheline.Seq (loc, [])
| Some id ->
let id = Sapling.Id.unparse_to_z id in
Micheline.Int (loc, id))
| diff -> (
let diff_bytes =
Data_encoding.Binary.to_bytes_exn Sapling.diff_encoding diff
in
let unparsed_diff = Bytes (loc, diff_bytes) in
match id with
| None -> unparsed_diff
| Some id ->
let id = Sapling.Id.unparse_to_z id in
Micheline.Prim
(loc, D_Pair, [Int (loc, id); unparsed_diff], []))),
ctxt ) )
| (Chest_key_t, s) ->
unparse_with_data_encoding
~loc
ctxt
s
Unparse_costs.chest_key
Script_timelock.chest_key_encoding
| (Chest_t, s) ->
unparse_with_data_encoding
~loc
ctxt
s
(Unparse_costs.chest
~plaintext_size:(Script_timelock.get_plaintext_size s))
Script_timelock.chest_encoding
and unparse_items :
type k v vc.
context ->
stack_depth:int ->
unparsing_mode ->
k comparable_ty ->
(v, vc) ty ->
(k * v) list ->
(Script.node list * context) tzresult Lwt.t =
fun ctxt ~stack_depth mode kt vt items ->
List.fold_left_es
(fun (l, ctxt) (k, v) ->
let loc = Micheline.dummy_location in
unparse_comparable_data ~loc ctxt mode kt k >>=? fun (key, ctxt) ->
unparse_data ctxt ~stack_depth:(stack_depth + 1) mode vt v
>|=? fun (value, ctxt) -> (Prim (loc, D_Elt, [key; value], []) :: l, ctxt))
([], ctxt)
items
and[@coq_axiom_with_reason "gadt"] unparse_code ctxt ~stack_depth mode code =
let legacy = true in
Gas.consume ctxt Unparse_costs.unparse_instr_cycle >>?= fun ctxt ->
let non_terminal_recursion ctxt mode code =
if Compare.Int.(stack_depth > 10_000) then
fail Unparsing_too_many_recursive_calls
else unparse_code ctxt ~stack_depth:(stack_depth + 1) mode code
in
match code with
| Prim (loc, I_PUSH, [ty; data], annot) ->
parse_packable_ty ctxt ~stack_depth:(stack_depth + 1) ~legacy ty
>>?= fun (Ex_ty t, ctxt) ->
let allow_forged =
false
in
parse_data
ctxt
~stack_depth:(stack_depth + 1)
~legacy
~allow_forged
t
data
>>=? fun (data, ctxt) ->
unparse_data ctxt ~stack_depth:(stack_depth + 1) mode t data
>>=? fun (data, ctxt) ->
return (Prim (loc, I_PUSH, [ty; data], annot), ctxt)
| Seq (loc, items) ->
List.fold_left_es
(fun (l, ctxt) item ->
non_terminal_recursion ctxt mode item >|=? fun (item, ctxt) ->
(item :: l, ctxt))
([], ctxt)
items
>>=? fun (items, ctxt) ->
return (Micheline.Seq (loc, List.rev items), ctxt)
| Prim (loc, prim, items, annot) ->
List.fold_left_es
(fun (l, ctxt) item ->
non_terminal_recursion ctxt mode item >|=? fun (item, ctxt) ->
(item :: l, ctxt))
([], ctxt)
items
>>=? fun (items, ctxt) ->
return (Prim (loc, prim, List.rev items, annot), ctxt)
| (Int _ | String _ | Bytes _) as atom -> return (atom, ctxt)
let parse_and_unparse_script_unaccounted ctxt ~legacy ~allow_forged_in_storage
mode ~normalize_types {code; storage} =
Script.force_decode_in_context
~consume_deserialization_gas:When_needed
ctxt
code
>>?= fun (code, ctxt) ->
typecheck_code ~legacy ~show_types:false ctxt code
>>=? fun ( Typechecked_code_internal
{
toplevel =
{
code_field;
arg_type = original_arg_type_expr;
storage_type = original_storage_type_expr;
views;
};
arg_type;
storage_type;
entrypoints;
typed_views;
type_map = _;
},
ctxt ) ->
parse_storage
ctxt
~legacy
~allow_forged:allow_forged_in_storage
storage_type
~storage
>>=? fun (storage, ctxt) ->
unparse_code ctxt ~stack_depth:0 mode code_field >>=? fun (code, ctxt) ->
unparse_data ctxt ~stack_depth:0 mode storage_type storage
>>=? fun (storage, ctxt) ->
let loc = Micheline.dummy_location in
(if normalize_types then
unparse_parameter_ty ~loc ctxt arg_type ~entrypoints
>>?= fun (arg_type, ctxt) ->
unparse_ty ~loc ctxt storage_type >>?= fun (storage_type, ctxt) ->
Script_map.map_es_in_context
(fun ctxt
_name
(Typed_view {input_ty; output_ty; kinstr = _; original_code_expr}) ->
Lwt.return
( unparse_ty ~loc ctxt input_ty >>? fun (input_ty, ctxt) ->
unparse_ty ~loc ctxt output_ty >|? fun (output_ty, ctxt) ->
({input_ty; output_ty; view_code = original_code_expr}, ctxt) ))
ctxt
typed_views
>|=? fun (views, ctxt) -> (arg_type, storage_type, views, ctxt)
else return (original_arg_type_expr, original_storage_type_expr, views, ctxt))
>>=? fun (arg_type, storage_type, views, ctxt) ->
Script_map.map_es_in_context
(fun ctxt _name {input_ty; output_ty; view_code} ->
unparse_code ctxt ~stack_depth:0 mode view_code
>|=? fun (view_code, ctxt) -> ({input_ty; output_ty; view_code}, ctxt))
ctxt
views
>>=? fun (views, ctxt) ->
let open Micheline in
let unparse_view_unaccounted name {input_ty; output_ty; view_code} views =
Prim
( loc,
K_view,
[
String (loc, Script_string.to_string name);
input_ty;
output_ty;
view_code;
],
[] )
:: views
in
let views = Script_map.fold unparse_view_unaccounted views [] |> List.rev in
let code =
Seq
( loc,
[
Prim (loc, K_parameter, [arg_type], []);
Prim (loc, K_storage, [storage_type], []);
Prim (loc, K_code, [code], []);
]
@ views )
in
return
( {
code = lazy_expr (strip_locations code);
storage = lazy_expr (strip_locations storage);
},
ctxt )
let pack_data_with_mode ctxt ty data ~mode =
unparse_data ~stack_depth:0 ctxt mode ty data >>=? fun (unparsed, ctxt) ->
Lwt.return @@ pack_node unparsed ctxt
let hash_data ctxt ty data =
pack_data_with_mode ctxt ty data ~mode:Optimized_legacy
>>=? fun (bytes, ctxt) -> Lwt.return @@ hash_bytes ctxt bytes
let pack_data ctxt ty data =
pack_data_with_mode ctxt ty data ~mode:Optimized_legacy
let empty_big_map key_type value_type =
Big_map
{
id = None;
diff = {map = Big_map_overlay.empty; size = 0};
key_type;
value_type;
}
let big_map_mem ctxt key (Big_map {id; diff; key_type; _}) =
hash_comparable_data ctxt key_type key >>=? fun (key, ctxt) ->
match (Big_map_overlay.find key diff.map, id) with
| (None, None) -> return (false, ctxt)
| (None, Some id) ->
Alpha_context.Big_map.mem ctxt id key >|=? fun (ctxt, res) -> (res, ctxt)
| (Some (_, None), _) -> return (false, ctxt)
| (Some (_, Some _), _) -> return (true, ctxt)
let big_map_get_by_hash ctxt key (Big_map {id; diff; value_type; _}) =
match (Big_map_overlay.find key diff.map, id) with
| (Some (_, x), _) -> return (x, ctxt)
| (None, None) -> return (None, ctxt)
| (None, Some id) -> (
Alpha_context.Big_map.get_opt ctxt id key >>=? function
| (ctxt, None) -> return (None, ctxt)
| (ctxt, Some value) ->
parse_data
~stack_depth:0
ctxt
~legacy:true
~allow_forged:true
value_type
(Micheline.root value)
>|=? fun (x, ctxt) -> (Some x, ctxt))
let big_map_get ctxt key (Big_map {key_type; _} as map) =
hash_comparable_data ctxt key_type key >>=? fun (key_hash, ctxt) ->
big_map_get_by_hash ctxt key_hash map
let big_map_update_by_hash ctxt key_hash key value (Big_map map) =
let contains = Big_map_overlay.mem key_hash map.diff.map in
return
( Big_map
{
map with
diff =
{
map = Big_map_overlay.add key_hash (key, value) map.diff.map;
size = (if contains then map.diff.size else map.diff.size + 1);
};
},
ctxt )
let big_map_update ctxt key value (Big_map {key_type; _} as map) =
hash_comparable_data ctxt key_type key >>=? fun (key_hash, ctxt) ->
big_map_update_by_hash ctxt key_hash key value map
let big_map_get_and_update ctxt key value (Big_map {key_type; _} as map) =
hash_comparable_data ctxt key_type key >>=? fun (key_hash, ctxt) ->
big_map_update_by_hash ctxt key_hash key value map >>=? fun (map', ctxt) ->
big_map_get_by_hash ctxt key_hash map >>=? fun (old_value, ctxt) ->
return ((old_value, map'), ctxt)
type lazy_storage_ids = Lazy_storage.IdSet.t
let no_lazy_storage_id = Lazy_storage.IdSet.empty
let diff_of_big_map ctxt mode ~temporary ~ids_to_copy
(Big_map {id; key_type; value_type; diff}) =
(match id with
| Some id ->
if Lazy_storage.IdSet.mem Big_map id ids_to_copy then
Big_map.fresh ~temporary ctxt >|=? fun (ctxt, duplicate) ->
(ctxt, Lazy_storage.Copy {src = id}, duplicate)
else
return (ctxt, Lazy_storage.Existing, id)
| None ->
Big_map.fresh ~temporary ctxt >>=? fun (ctxt, id) ->
Lwt.return
(let kt = unparse_comparable_ty_uncarbonated ~loc:() key_type in
Gas.consume ctxt (Script.strip_locations_cost kt) >>? fun ctxt ->
unparse_ty ~loc:() ctxt value_type >>? fun (kv, ctxt) ->
Gas.consume ctxt (Script.strip_locations_cost kv) >|? fun ctxt ->
let key_type = Micheline.strip_locations kt in
let value_type = Micheline.strip_locations kv in
(ctxt, Lazy_storage.(Alloc Big_map.{key_type; value_type}), id)))
>>=? fun (ctxt, init, id) ->
let pairs =
Big_map_overlay.fold
(fun key_hash (key, value) acc -> (key_hash, key, value) :: acc)
diff.map
[]
in
List.fold_left_es
(fun (acc, ctxt) (key_hash, key, value) ->
Gas.consume ctxt Typecheck_costs.parse_instr_cycle >>?= fun ctxt ->
unparse_comparable_data ~loc:() ctxt mode key_type key
>>=? fun (key_node, ctxt) ->
Gas.consume ctxt (Script.strip_locations_cost key_node) >>?= fun ctxt ->
let key = Micheline.strip_locations key_node in
(match value with
| None -> return (None, ctxt)
| Some x ->
unparse_data ~stack_depth:0 ctxt mode value_type x
>>=? fun (node, ctxt) ->
Lwt.return
( Gas.consume ctxt (Script.strip_locations_cost node) >|? fun ctxt ->
(Some (Micheline.strip_locations node), ctxt) ))
>|=? fun (value, ctxt) ->
let diff_item = Big_map.{key; key_hash; value} in
(diff_item :: acc, ctxt))
([], ctxt)
(List.rev pairs)
>|=? fun (updates, ctxt) -> (Lazy_storage.Update {init; updates}, id, ctxt)
let diff_of_sapling_state ctxt ~temporary ~ids_to_copy
({id; diff; memo_size} : Sapling.state) =
(match id with
| Some id ->
if Lazy_storage.IdSet.mem Sapling_state id ids_to_copy then
Sapling.fresh ~temporary ctxt >|=? fun (ctxt, duplicate) ->
(ctxt, Lazy_storage.Copy {src = id}, duplicate)
else return (ctxt, Lazy_storage.Existing, id)
| None ->
Sapling.fresh ~temporary ctxt >|=? fun (ctxt, id) ->
(ctxt, Lazy_storage.Alloc Sapling.{memo_size}, id))
>|=? fun (ctxt, init, id) ->
(Lazy_storage.Update {init; updates = diff}, id, ctxt)
(**
Witness flag for whether a type can be populated by a value containing a
lazy storage.
[False_f] must be used only when a value of the type cannot contain a lazy
storage.
This flag is built in [has_lazy_storage] and used only in
[extract_lazy_storage_updates] and [collect_lazy_storage].
This flag is necessary to avoid these two functions to have a quadratic
complexity in the size of the type.
Add new lazy storage kinds here.
Please keep the usage of this GADT local.
*)
type 'ty has_lazy_storage =
| Big_map_f : ('a, 'b) big_map has_lazy_storage
| Sapling_state_f : Sapling.state has_lazy_storage
| False_f : _ has_lazy_storage
| Pair_f :
'a has_lazy_storage * 'b has_lazy_storage
-> ('a, 'b) pair has_lazy_storage
| Union_f :
'a has_lazy_storage * 'b has_lazy_storage
-> ('a, 'b) union has_lazy_storage
| Option_f : 'a has_lazy_storage -> 'a option has_lazy_storage
| List_f : 'a has_lazy_storage -> 'a boxed_list has_lazy_storage
| Map_f : 'v has_lazy_storage -> (_, 'v) map has_lazy_storage
(**
This function is called only on storage and parameter types of contracts,
once per typechecked contract. It has a complexity linear in the size of
the types, which happen to be literally written types, so the gas for them
has already been paid.
*)
let rec has_lazy_storage : type t tc. (t, tc) ty -> t has_lazy_storage =
fun ty ->
let aux1 cons t =
match has_lazy_storage t with False_f -> False_f | h -> cons h
in
let aux2 cons t1 t2 =
match (has_lazy_storage t1, has_lazy_storage t2) with
| (False_f, False_f) -> False_f
| (h1, h2) -> cons h1 h2
in
match ty with
| Big_map_t (_, _, _) -> Big_map_f
| Sapling_state_t _ -> Sapling_state_f
| Unit_t -> False_f
| Int_t -> False_f
| Nat_t -> False_f
| Signature_t -> False_f
| String_t -> False_f
| Bytes_t -> False_f
| Mutez_t -> False_f
| Key_hash_t -> False_f
| Key_t -> False_f
| Timestamp_t -> False_f
| Address_t -> False_f
| Tx_rollup_l2_address_t -> False_f
| Bool_t -> False_f
| Lambda_t (_, _, _) -> False_f
| Set_t (_, _) -> False_f
| Contract_t (_, _) -> False_f
| Operation_t -> False_f
| Chain_id_t -> False_f
| Never_t -> False_f
| Bls12_381_g1_t -> False_f
| Bls12_381_g2_t -> False_f
| Bls12_381_fr_t -> False_f
| Sapling_transaction_t _ -> False_f
| Sapling_transaction_deprecated_t _ -> False_f
| Ticket_t _ -> False_f
| Chest_key_t -> False_f
| Chest_t -> False_f
| Pair_t (l, r, _, _) -> aux2 (fun l r -> Pair_f (l, r)) l r
| Union_t (l, r, _, _) -> aux2 (fun l r -> Union_f (l, r)) l r
| Option_t (t, _, _) -> aux1 (fun h -> Option_f h) t
| List_t (t, _) -> aux1 (fun h -> List_f h) t
| Map_t (_, t, _) -> aux1 (fun h -> Map_f h) t
(**
Transforms a value potentially containing lazy storage in an intermediary
state to a value containing lazy storage only represented by identifiers.
Returns the updated value, the updated set of ids to copy, and the lazy
storage diff to show on the receipt and apply on the storage.
*)
let[@coq_axiom_with_reason "gadt"] ctxt mode
~temporary ids_to_copy acc ty x =
let rec aux :
type a ac.
context ->
unparsing_mode ->
temporary:bool ->
Lazy_storage.IdSet.t ->
Lazy_storage.diffs ->
(a, ac) ty ->
a ->
has_lazy_storage:a has_lazy_storage ->
(context * a * Lazy_storage.IdSet.t * Lazy_storage.diffs) tzresult Lwt.t =
fun ctxt mode ~temporary ids_to_copy acc ty x ~has_lazy_storage ->
Gas.consume ctxt Typecheck_costs.parse_instr_cycle >>?= fun ctxt ->
match (has_lazy_storage, ty, x) with
| (False_f, _, _) -> return (ctxt, x, ids_to_copy, acc)
| (Big_map_f, Big_map_t (_, _, _), map) ->
diff_of_big_map ctxt mode ~temporary ~ids_to_copy map
>|=? fun (diff, id, ctxt) ->
let map =
let (Big_map map) = map in
Big_map
{
map with
diff = {map = Big_map_overlay.empty; size = 0};
id = Some id;
}
in
let diff = Lazy_storage.make Big_map id diff in
let ids_to_copy = Lazy_storage.IdSet.add Big_map id ids_to_copy in
(ctxt, map, ids_to_copy, diff :: acc)
| (Sapling_state_f, Sapling_state_t _, sapling_state) ->
diff_of_sapling_state ctxt ~temporary ~ids_to_copy sapling_state
>|=? fun (diff, id, ctxt) ->
let sapling_state =
Sapling.empty_state ~id ~memo_size:sapling_state.memo_size ()
in
let diff = Lazy_storage.make Sapling_state id diff in
let ids_to_copy = Lazy_storage.IdSet.add Sapling_state id ids_to_copy in
(ctxt, sapling_state, ids_to_copy, diff :: acc)
| (Pair_f (hl, hr), Pair_t (tyl, tyr, _, _), (xl, xr)) ->
aux ctxt mode ~temporary ids_to_copy acc tyl xl ~has_lazy_storage:hl
>>=? fun (ctxt, xl, ids_to_copy, acc) ->
aux ctxt mode ~temporary ids_to_copy acc tyr xr ~has_lazy_storage:hr
>|=? fun (ctxt, xr, ids_to_copy, acc) ->
(ctxt, (xl, xr), ids_to_copy, acc)
| (Union_f (has_lazy_storage, _), Union_t (ty, _, _, _), L x) ->
aux ctxt mode ~temporary ids_to_copy acc ty x ~has_lazy_storage
>|=? fun (ctxt, x, ids_to_copy, acc) -> (ctxt, L x, ids_to_copy, acc)
| (Union_f (_, has_lazy_storage), Union_t (_, ty, _, _), R x) ->
aux ctxt mode ~temporary ids_to_copy acc ty x ~has_lazy_storage
>|=? fun (ctxt, x, ids_to_copy, acc) -> (ctxt, R x, ids_to_copy, acc)
| (Option_f has_lazy_storage, Option_t (ty, _, _), Some x) ->
aux ctxt mode ~temporary ids_to_copy acc ty x ~has_lazy_storage
>|=? fun (ctxt, x, ids_to_copy, acc) -> (ctxt, Some x, ids_to_copy, acc)
| (List_f has_lazy_storage, List_t (ty, _), l) ->
List.fold_left_es
(fun (ctxt, l, ids_to_copy, acc) x ->
aux ctxt mode ~temporary ids_to_copy acc ty x ~has_lazy_storage
>|=? fun (ctxt, x, ids_to_copy, acc) ->
(ctxt, Script_list.cons x l, ids_to_copy, acc))
(ctxt, Script_list.empty, ids_to_copy, acc)
l.elements
>|=? fun (ctxt, l, ids_to_copy, acc) ->
let reversed = {length = l.length; elements = List.rev l.elements} in
(ctxt, reversed, ids_to_copy, acc)
| (Map_f has_lazy_storage, Map_t (_, ty, _), map) ->
let (module M) = Script_map.get_module map in
let bindings m = M.OPS.fold (fun k v bs -> (k, v) :: bs) m [] in
List.fold_left_es
(fun (ctxt, m, ids_to_copy, acc) (k, x) ->
aux ctxt mode ~temporary ids_to_copy acc ty x ~has_lazy_storage
>|=? fun (ctxt, x, ids_to_copy, acc) ->
(ctxt, M.OPS.add k x m, ids_to_copy, acc))
(ctxt, M.OPS.empty, ids_to_copy, acc)
(bindings M.boxed)
>|=? fun (ctxt, m, ids_to_copy, acc) ->
let module M = struct
module OPS = M.OPS
type key = M.key
type value = M.value
let boxed = m
let size = M.size
end in
( ctxt,
Script_map.make
(module M : Boxed_map
with type key = M.key
and type value = M.value),
ids_to_copy,
acc )
| (_, Option_t (_, _, _), None) -> return (ctxt, None, ids_to_copy, acc)
in
let has_lazy_storage = has_lazy_storage ty in
aux ctxt mode ~temporary ids_to_copy acc ty x ~has_lazy_storage
(** We namespace an error type for [fold_lazy_storage]. The error case is only
available when the ['error] parameter is equal to unit. *)
module Fold_lazy_storage = struct
type ('acc, 'error) result =
| Ok : 'acc -> ('acc, 'error) result
| Error : ('acc, unit) result
end
(** Prematurely abort if [f] generates an error. Use this function without the
[unit] type for [error] if you are in a case where errors are impossible.
*)
let[@coq_axiom_with_reason "gadt"] rec fold_lazy_storage :
type a ac error.
f:('acc, error) Fold_lazy_storage.result Lazy_storage.IdSet.fold_f ->
init:'acc ->
context ->
(a, ac) ty ->
a ->
has_lazy_storage:a has_lazy_storage ->
(('acc, error) Fold_lazy_storage.result * context) tzresult =
fun ~f ~init ctxt ty x ~has_lazy_storage ->
Gas.consume ctxt Typecheck_costs.parse_instr_cycle >>? fun ctxt ->
match (has_lazy_storage, ty, x) with
| (Big_map_f, Big_map_t (_, _, _), Big_map {id = Some id; _}) ->
Gas.consume ctxt Typecheck_costs.parse_instr_cycle >>? fun ctxt ->
ok (f.f Big_map id (Fold_lazy_storage.Ok init), ctxt)
| (Sapling_state_f, Sapling_state_t _, {id = Some id; _}) ->
Gas.consume ctxt Typecheck_costs.parse_instr_cycle >>? fun ctxt ->
ok (f.f Sapling_state id (Fold_lazy_storage.Ok init), ctxt)
| (False_f, _, _) -> ok (Fold_lazy_storage.Ok init, ctxt)
| (Big_map_f, Big_map_t (_, _, _), Big_map {id = None; _}) ->
ok (Fold_lazy_storage.Ok init, ctxt)
| (Sapling_state_f, Sapling_state_t _, {id = None; _}) ->
ok (Fold_lazy_storage.Ok init, ctxt)
| (Pair_f (hl, hr), Pair_t (tyl, tyr, _, _), (xl, xr)) -> (
fold_lazy_storage ~f ~init ctxt tyl xl ~has_lazy_storage:hl
>>? fun (init, ctxt) ->
match init with
| Fold_lazy_storage.Ok init ->
fold_lazy_storage ~f ~init ctxt tyr xr ~has_lazy_storage:hr
| Fold_lazy_storage.Error -> ok (init, ctxt))
| (Union_f (has_lazy_storage, _), Union_t (ty, _, _, _), L x) ->
fold_lazy_storage ~f ~init ctxt ty x ~has_lazy_storage
| (Union_f (_, has_lazy_storage), Union_t (_, ty, _, _), R x) ->
fold_lazy_storage ~f ~init ctxt ty x ~has_lazy_storage
| (_, Option_t (_, _, _), None) -> ok (Fold_lazy_storage.Ok init, ctxt)
| (Option_f has_lazy_storage, Option_t (ty, _, _), Some x) ->
fold_lazy_storage ~f ~init ctxt ty x ~has_lazy_storage
| (List_f has_lazy_storage, List_t (ty, _), l) ->
List.fold_left_e
(fun ((init, ctxt) : ('acc, error) Fold_lazy_storage.result * context) x ->
match init with
| Fold_lazy_storage.Ok init ->
fold_lazy_storage ~f ~init ctxt ty x ~has_lazy_storage
| Fold_lazy_storage.Error -> ok (init, ctxt))
(Fold_lazy_storage.Ok init, ctxt)
l.elements
| (Map_f has_lazy_storage, Map_t (_, ty, _), m) ->
Script_map.fold
(fun _
v
(acc : (('acc, error) Fold_lazy_storage.result * context) tzresult) ->
acc >>? fun (init, ctxt) ->
match init with
| Fold_lazy_storage.Ok init ->
fold_lazy_storage ~f ~init ctxt ty v ~has_lazy_storage
| Fold_lazy_storage.Error -> ok (init, ctxt))
m
(ok (Fold_lazy_storage.Ok init, ctxt))
let[@coq_axiom_with_reason "gadt"] collect_lazy_storage ctxt ty x =
let has_lazy_storage = has_lazy_storage ty in
let f kind id (acc : (_, never) Fold_lazy_storage.result) =
let acc = match acc with Fold_lazy_storage.Ok acc -> acc in
Fold_lazy_storage.Ok (Lazy_storage.IdSet.add kind id acc)
in
fold_lazy_storage ~f:{f} ~init:no_lazy_storage_id ctxt ty x ~has_lazy_storage
>>? fun (ids, ctxt) ->
match ids with Fold_lazy_storage.Ok ids -> ok (ids, ctxt)
let[@coq_axiom_with_reason "gadt"] ctxt mode
~temporary ~to_duplicate ~to_update ty v =
let to_duplicate = Lazy_storage.IdSet.diff to_duplicate to_update in
extract_lazy_storage_updates ctxt mode ~temporary to_duplicate [] ty v
>|=? fun (ctxt, v, alive, diffs) ->
let diffs =
if temporary then diffs
else
let dead = Lazy_storage.IdSet.diff to_update alive in
Lazy_storage.IdSet.fold_all
{f = (fun kind id acc -> Lazy_storage.make kind id Remove :: acc)}
dead
diffs
in
match diffs with
| [] -> (v, None, ctxt)
| diffs -> (v, Some diffs , ctxt)
let list_of_big_map_ids ids =
Lazy_storage.IdSet.fold Big_map (fun id acc -> id :: acc) ids []
let parse_data = parse_data ~stack_depth:0
let parse_instr :
type a s.
?type_logger:type_logger ->
tc_context ->
context ->
legacy:bool ->
Script.node ->
(a, s) stack_ty ->
((a, s) judgement * context) tzresult Lwt.t =
fun ?type_logger tc_context ctxt ~legacy script_instr stack_ty ->
parse_instr
~stack_depth:0
?type_logger
tc_context
ctxt
~legacy
script_instr
stack_ty
let unparse_data = unparse_data ~stack_depth:0
let unparse_code ctxt mode code =
Global_constants_storage.expand ctxt (strip_locations code)
>>=? fun (ctxt, code) -> unparse_code ~stack_depth:0 ctxt mode (root code)
let parse_contract context loc arg_ty contract ~entrypoint =
parse_contract ~stack_depth:0 context loc arg_ty contract ~entrypoint
let parse_toplevel ctxt ~legacy toplevel =
Global_constants_storage.expand ctxt toplevel >>=? fun (ctxt, toplevel) ->
Lwt.return @@ parse_toplevel ctxt ~legacy toplevel
let parse_comparable_ty = parse_comparable_ty ~stack_depth:0
let parse_big_map_value_ty = parse_big_map_value_ty ~stack_depth:0
let parse_packable_ty = parse_packable_ty ~stack_depth:0
let parse_passable_ty = parse_passable_ty ~stack_depth:0
let parse_any_ty = parse_any_ty ~stack_depth:0
let parse_ty = parse_ty ~stack_depth:0 ~ret:Don't_parse_entrypoints
let parse_parameter_ty_and_entrypoints =
parse_parameter_ty_and_entrypoints ~stack_depth:0
let[@coq_axiom_with_reason "gadt"] get_single_sapling_state ctxt ty x =
let has_lazy_storage = has_lazy_storage ty in
let f (type i a u) (kind : (i, a, u) Lazy_storage.Kind.t) (id : i)
single_id_opt : (Sapling.Id.t option, unit) Fold_lazy_storage.result =
match kind with
| Lazy_storage.Kind.Sapling_state -> (
match single_id_opt with
| Fold_lazy_storage.Ok None -> Fold_lazy_storage.Ok (Some id)
| Fold_lazy_storage.Ok (Some _) ->
Fold_lazy_storage.Error
| Fold_lazy_storage.Error -> single_id_opt)
| _ -> single_id_opt
in
fold_lazy_storage ~f:{f} ~init:None ctxt ty x ~has_lazy_storage
>>? fun (id, ctxt) ->
match id with
| Fold_lazy_storage.Ok (Some id) -> ok (Some id, ctxt)
| Fold_lazy_storage.Ok None | Fold_lazy_storage.Error -> ok (None, ctxt)
let script_size
(Ex_script
(Script
{
code_size;
code = _;
arg_type = _;
storage;
storage_type;
entrypoints = _;
views = _;
})) =
let (nodes, storage_size) =
Script_typed_ir_size.value_size storage_type storage
in
let cost = Script_typed_ir_size_costs.nodes_cost ~nodes in
(Saturation_repr.(add code_size storage_size |> to_int), cost)
let typecheck_code ~legacy ~show_types ctxt code =
typecheck_code ~legacy ~show_types ctxt code
>|=? fun (Typechecked_code_internal {type_map; _}, ctxt) -> (type_map, ctxt)