summaryrefslogtreecommitdiff
path: root/vm_method.c
blob: 7fc3f8db974df2402bdc278303f3579830ce7d1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
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
/*
 * This file is included by vm.c
 */

#include "id_table.h"

#define METHOD_DEBUG 0

static int vm_redefinition_check_flag(VALUE klass);
static void rb_vm_check_redefinition_opt_method(const rb_method_entry_t *me, VALUE klass);
static inline rb_method_entry_t *lookup_method_table(VALUE klass, ID id);

#define object_id           idObject_id
#define added               idMethod_added
#define singleton_added     idSingleton_method_added
#define removed             idMethod_removed
#define singleton_removed   idSingleton_method_removed
#define undefined           idMethod_undefined
#define singleton_undefined idSingleton_method_undefined
#define attached            id__attached__

#define ruby_running (GET_VM()->running)
/* int ruby_running = 0; */

static enum rb_id_table_iterator_result
vm_ccs_dump_i(ID mid, VALUE val, void *data)
{
    const struct rb_class_cc_entries *ccs = (struct rb_class_cc_entries *)val;
    fprintf(stderr,     "  | %s (len:%d) ", rb_id2name(mid), ccs->len);
    rp(ccs->cme);

    for (int i=0; i<ccs->len; i++) {
        fprintf(stderr, "  | [%d]\t", i); vm_ci_dump(ccs->entries[i].ci);
        rp_m(           "  |   \t", ccs->entries[i].cc);
    }

    return ID_TABLE_CONTINUE;
}

static void
vm_ccs_dump(VALUE klass, ID target_mid)
{
    struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
    if (cc_tbl) {
        const struct rb_class_cc_entries *ccs;
        if (target_mid) {
            if (rb_id_table_lookup(cc_tbl, target_mid, (VALUE *)&ccs)) {
                fprintf(stderr, "  [CCTB] %p\n", (void *)cc_tbl);
                vm_ccs_dump_i(target_mid, (VALUE)ccs, NULL);
            }
        }
        else {
            fprintf(stderr, "  [CCTB] %p\n", (void *)cc_tbl);
            rb_id_table_foreach(cc_tbl, vm_ccs_dump_i, (void *)target_mid);
        }
    }
}

static enum rb_id_table_iterator_result
vm_cme_dump_i(ID mid, VALUE val, void *data)
{
    ID target_mid = (ID)data;
    if (target_mid == 0 || mid == target_mid) {
        rp_m("  > ", val);
    }
    return ID_TABLE_CONTINUE;
}

static VALUE
vm_mtbl_dump(VALUE klass, ID target_mid)
{
    fprintf(stderr, "# vm_mtbl\n");
    while (klass) {
        rp_m("  -> ", klass);
        rb_method_entry_t *me;

        if (RCLASS_M_TBL(klass)) {
            if (target_mid != 0) {
                if (rb_id_table_lookup(RCLASS_M_TBL(klass), target_mid, (VALUE *)&me)) {
                    rp_m("  [MTBL] ", me);
                }
            }
            else {
                fprintf(stderr, "  ## RCLASS_M_TBL (%p)\n", (void *)RCLASS_M_TBL(klass));
                rb_id_table_foreach(RCLASS_M_TBL(klass), vm_cme_dump_i, NULL);
            }
        }
        else {
            fprintf(stderr, "    MTBL: NULL\n");
        }
        if (RCLASS_CALLABLE_M_TBL(klass)) {
            if (target_mid != 0) {
                if (rb_id_table_lookup(RCLASS_CALLABLE_M_TBL(klass), target_mid, (VALUE *)&me)) {
                    rp_m("  [CM**] ", me);
                }
            }
            else {
                fprintf(stderr, "  ## RCLASS_CALLABLE_M_TBL\n");
                rb_id_table_foreach(RCLASS_CALLABLE_M_TBL(klass), vm_cme_dump_i, NULL);
            }
        }
        if (RCLASS_CC_TBL(klass)) {
            vm_ccs_dump(klass, target_mid);
        }
        klass = RCLASS_SUPER(klass);
    }
    return Qnil;
}

void
rb_vm_mtbl_dump(const char *msg, VALUE klass, ID target_mid)
{
    fprintf(stderr, "[%s] ", msg);
    vm_mtbl_dump(klass, target_mid);
}

static inline void
vm_me_invalidate_cache(rb_callable_method_entry_t *cme)
{
    VM_ASSERT(IMEMO_TYPE_P(cme, imemo_ment));
    VM_ASSERT(callable_method_entry_p(cme));
    METHOD_ENTRY_INVALIDATED_SET(cme);
    RB_DEBUG_COUNTER_INC(cc_cme_invalidate);
}

void
rb_clear_constant_cache(void)
{
    INC_GLOBAL_CONSTANT_STATE();
}

static rb_method_entry_t *rb_method_entry_alloc(ID called_id, VALUE owner, VALUE defined_class, const rb_method_definition_t *def);
const rb_method_entry_t * rb_method_entry_clone(const rb_method_entry_t *src_me);
static const rb_callable_method_entry_t *complemented_callable_method_entry(VALUE klass, ID id);

static void
clear_method_cache_by_id_in_class(VALUE klass, ID mid)
{
    VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_ICLASS));
    if (rb_objspace_garbage_object_p(klass)) return;

    if (LIKELY(RCLASS_EXT(klass)->subclasses == NULL)) {
        // no subclasses
        // check only current class

        struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
        struct rb_class_cc_entries *ccs;

        // invalidate CCs
        if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, (VALUE *)&ccs)) {
            rb_vm_ccs_free(ccs);
            rb_id_table_delete(cc_tbl, mid);
            RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_ccs);
        }

        // remove from callable_m_tbl, if exists
        struct rb_id_table *cm_tbl;
        if ((cm_tbl = RCLASS_CALLABLE_M_TBL(klass)) != NULL) {
            rb_id_table_delete(cm_tbl, mid);
            RB_DEBUG_COUNTER_INC(cc_invalidate_leaf_callable);
        }
        RB_DEBUG_COUNTER_INC(cc_invalidate_leaf);
    }
    else {
        const rb_callable_method_entry_t *cme = complemented_callable_method_entry(klass, mid);

        if (cme) {
            // invalidate cme if found to invalidate the inline method cache.
            if (METHOD_ENTRY_CACHED(cme)) {
                if (METHOD_ENTRY_COMPLEMENTED(cme)) {
                    // do nothing
                }
                else {
                    // invalidate cc by invalidating cc->cme
                    VALUE owner = cme->owner;
                    VM_ASSERT(BUILTIN_TYPE(owner) == T_CLASS);
                    VALUE klass_housing_cme;
                    if (cme->def->type == VM_METHOD_TYPE_REFINED && !cme->def->body.refined.orig_me) {
                        klass_housing_cme = owner;
                    }
                    else {
                        klass_housing_cme = RCLASS_ORIGIN(owner);
                    }
                    // replace the cme that will be invalid
                    VM_ASSERT(lookup_method_table(klass_housing_cme, mid) == (const rb_method_entry_t *)cme);
                    const rb_method_entry_t *new_cme = rb_method_entry_clone((const rb_method_entry_t *)cme);
                    rb_method_table_insert(klass_housing_cme, RCLASS_M_TBL(klass_housing_cme), mid, new_cme);
                }

                vm_me_invalidate_cache((rb_callable_method_entry_t *)cme);
                RB_DEBUG_COUNTER_INC(cc_invalidate_tree_cme);
            }

            // invalidate complement tbl
            if (METHOD_ENTRY_COMPLEMENTED(cme)) {
                VALUE defined_class = cme->defined_class;
                struct rb_id_table *cm_tbl = RCLASS_CALLABLE_M_TBL(defined_class);
                VM_ASSERT(cm_tbl != NULL);
                int r = rb_id_table_delete(cm_tbl, mid);
                VM_ASSERT(r == TRUE); (void)r;
                RB_DEBUG_COUNTER_INC(cc_invalidate_tree_callable);
            }

            RB_DEBUG_COUNTER_INC(cc_invalidate_tree);
        }
        else {
            rb_vm_t *vm = GET_VM();
            if (rb_id_table_lookup(vm->negative_cme_table, mid, (VALUE *)&cme)) {
                rb_id_table_delete(vm->negative_cme_table, mid);
                vm_me_invalidate_cache((rb_callable_method_entry_t *)cme);

                RB_DEBUG_COUNTER_INC(cc_invalidate_negative);
            }
        }
    }
}

static void
clear_iclass_method_cache_by_id(VALUE iclass, VALUE d)
{
    VM_ASSERT(RB_TYPE_P(iclass, T_ICLASS));
    ID mid = (ID)d;
    clear_method_cache_by_id_in_class(iclass, mid);
}

static void
clear_iclass_method_cache_by_id_for_refinements(VALUE klass, VALUE d)
{
    if (RB_TYPE_P(klass, T_ICLASS)) {
        ID mid = (ID)d;
        clear_method_cache_by_id_in_class(klass, mid);
    }
}

void
rb_clear_method_cache(VALUE klass_or_module, ID mid)
{
    if (RB_TYPE_P(klass_or_module, T_MODULE)) {
        VALUE module = klass_or_module; // alias

        if (FL_TEST(module, RMODULE_IS_REFINEMENT)) {
            VALUE refined_class = rb_refinement_module_get_refined_class(module);
            rb_clear_method_cache(refined_class, mid);
            rb_class_foreach_subclass(refined_class, clear_iclass_method_cache_by_id_for_refinements, mid);
        }
        rb_class_foreach_subclass(module, clear_iclass_method_cache_by_id, mid);
    }
    else {
        clear_method_cache_by_id_in_class(klass_or_module, mid);
    }
}

// gc.c
void rb_cc_table_free(VALUE klass);

static int
invalidate_all_cc(void *vstart, void *vend, size_t stride, void *data)
{
    VALUE v = (VALUE)vstart;
    for (; v != (VALUE)vend; v += stride) {
        void *ptr = asan_poisoned_object_p(v);
        asan_unpoison_object(v, false);
        if (RBASIC(v)->flags) { // liveness check
            if (RB_TYPE_P(v, T_CLASS) ||
                RB_TYPE_P(v, T_ICLASS)) {
                if (RCLASS_CC_TBL(v)) {
                    rb_cc_table_free(v);
                }
                RCLASS_CC_TBL(v) = NULL;
            }
        }
        if (ptr) {
            asan_poison_object(v);
        }
    }
    return 0; // continue to iteration
}

void
rb_clear_method_cache_all(void)
{
    rb_objspace_each_objects(invalidate_all_cc, NULL);
}

void
rb_method_table_insert(VALUE klass, struct rb_id_table *table, ID method_id, const rb_method_entry_t *me)
{
    VALUE table_owner = klass;
    if (RB_TYPE_P(klass, T_ICLASS) && !RICLASS_OWNS_M_TBL_P(klass)) {
        table_owner = RBASIC(table_owner)->klass;
    }
    VM_ASSERT(RB_TYPE_P(table_owner, T_CLASS) || RB_TYPE_P(table_owner, T_ICLASS) || RB_TYPE_P(table_owner, T_MODULE));
    VM_ASSERT(table == RCLASS_M_TBL(table_owner));
    rb_id_table_insert(table, method_id, (VALUE)me);
    RB_OBJ_WRITTEN(table_owner, Qundef, (VALUE)me);
}

VALUE
rb_f_notimplement(int argc, const VALUE *argv, VALUE obj, VALUE marker)
{
    rb_notimplement();

    UNREACHABLE_RETURN(Qnil);
}

static void
rb_define_notimplement_method_id(VALUE mod, ID id, rb_method_visibility_t visi)
{
    rb_add_method(mod, id, VM_METHOD_TYPE_NOTIMPLEMENTED, (void *)1, visi);
}

void
rb_add_method_cfunc(VALUE klass, ID mid, VALUE (*func)(ANYARGS), int argc, rb_method_visibility_t visi)
{
    if (argc < -2 || 15 < argc) rb_raise(rb_eArgError, "arity out of range: %d for -2..15", argc);
    if (func != rb_f_notimplement) {
	rb_method_cfunc_t opt;
	opt.func = func;
	opt.argc = argc;
        rb_add_method(klass, mid, VM_METHOD_TYPE_CFUNC, &opt, visi);
    }
    else {
	rb_define_notimplement_method_id(klass, mid, visi);
    }
}

static void
rb_method_definition_release(rb_method_definition_t *def, int complemented)
{
    if (def != NULL) {
	const int alias_count = def->alias_count;
	const int complemented_count = def->complemented_count;
	VM_ASSERT(alias_count >= 0);
	VM_ASSERT(complemented_count >= 0);

	if (alias_count + complemented_count == 0) {
            if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:%d,%d (remove)\n", (void *)def,
                                      rb_id2name(def->original_id), alias_count, complemented_count);
            VM_ASSERT(def->type == VM_METHOD_TYPE_BMETHOD ? def->body.bmethod.hooks == NULL : TRUE);
	    xfree(def);
	}
	else {
            if (complemented) {
                VM_ASSERT(def->complemented_count > 0);
                def->complemented_count--;
            }
            else if (def->alias_count > 0) {
                def->alias_count--;
            }

	    if (METHOD_DEBUG) fprintf(stderr, "-%p-%s:%d->%d,%d->%d (dec)\n", (void *)def, rb_id2name(def->original_id),
				      alias_count, def->alias_count, complemented_count, def->complemented_count);
	}
    }
}

void
rb_free_method_entry(const rb_method_entry_t *me)
{
    rb_method_definition_release(me->def, METHOD_ENTRY_COMPLEMENTED(me));
}

static inline rb_method_entry_t *search_method(VALUE klass, ID id, VALUE *defined_class_ptr);
extern int rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2);

static VALUE
(*call_cfunc_invoker_func(int argc))(VALUE recv, int argc, const VALUE *, VALUE (*func)(ANYARGS))
{
    if (!GET_THREAD()->ext_config.ractor_safe) {
        switch (argc) {
          case -2: return &call_cfunc_m2;
          case -1: return &call_cfunc_m1;
          case 0: return &call_cfunc_0;
          case 1: return &call_cfunc_1;
          case 2: return &call_cfunc_2;
          case 3: return &call_cfunc_3;
          case 4: return &call_cfunc_4;
          case 5: return &call_cfunc_5;
          case 6: return &call_cfunc_6;
          case 7: return &call_cfunc_7;
          case 8: return &call_cfunc_8;
          case 9: return &call_cfunc_9;
          case 10: return &call_cfunc_10;
          case 11: return &call_cfunc_11;
          case 12: return &call_cfunc_12;
          case 13: return &call_cfunc_13;
          case 14: return &call_cfunc_14;
          case 15: return &call_cfunc_15;
          default:
            rb_bug("unsupported length: %d", argc);
        }
    }
    else {
        switch (argc) {
          case -2: return &ractor_safe_call_cfunc_m2;
          case -1: return &ractor_safe_call_cfunc_m1;
          case 0: return  &ractor_safe_call_cfunc_0;
          case 1: return  &ractor_safe_call_cfunc_1;
          case 2: return  &ractor_safe_call_cfunc_2;
          case 3: return  &ractor_safe_call_cfunc_3;
          case 4: return  &ractor_safe_call_cfunc_4;
          case 5: return  &ractor_safe_call_cfunc_5;
          case 6: return  &ractor_safe_call_cfunc_6;
          case 7: return  &ractor_safe_call_cfunc_7;
          case 8: return  &ractor_safe_call_cfunc_8;
          case 9: return  &ractor_safe_call_cfunc_9;
          case 10: return &ractor_safe_call_cfunc_10;
          case 11: return &ractor_safe_call_cfunc_11;
          case 12: return &ractor_safe_call_cfunc_12;
          case 13: return &ractor_safe_call_cfunc_13;
          case 14: return &ractor_safe_call_cfunc_14;
          case 15: return &ractor_safe_call_cfunc_15;
          default:
            rb_bug("unsupported length: %d", argc);
        }
    }
}

static void
setup_method_cfunc_struct(rb_method_cfunc_t *cfunc, VALUE (*func)(), int argc)
{
    cfunc->func = func;
    cfunc->argc = argc;
    cfunc->invoker = call_cfunc_invoker_func(argc);
}

MJIT_FUNC_EXPORTED void
rb_method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *def, void *opts)
{
    *(rb_method_definition_t **)&me->def = def;

    if (opts != NULL) {
	switch (def->type) {
	  case VM_METHOD_TYPE_ISEQ:
	    {
		rb_method_iseq_t *iseq_body = (rb_method_iseq_t *)opts;
		rb_cref_t *method_cref, *cref = iseq_body->cref;

		/* setup iseq first (before invoking GC) */
		RB_OBJ_WRITE(me, &def->body.iseq.iseqptr, iseq_body->iseqptr);

		if (0) vm_cref_dump("rb_method_definition_create", cref);

		if (cref) {
		    method_cref = cref;
		}
		else {
		    method_cref = vm_cref_new_toplevel(GET_EC()); /* TODO: can we reuse? */
		}

		RB_OBJ_WRITE(me, &def->body.iseq.cref, method_cref);
		return;
	    }
	  case VM_METHOD_TYPE_CFUNC:
	    {
		rb_method_cfunc_t *cfunc = (rb_method_cfunc_t *)opts;
		setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), cfunc->func, cfunc->argc);
		return;
	    }
	  case VM_METHOD_TYPE_ATTRSET:
	  case VM_METHOD_TYPE_IVAR:
	    {
		const rb_execution_context_t *ec = GET_EC();
		rb_control_frame_t *cfp;
		int line;

		def->body.attr.id = (ID)(VALUE)opts;

		cfp = rb_vm_get_ruby_level_next_cfp(ec, ec->cfp);

		if (cfp && (line = rb_vm_get_sourceline(cfp))) {
		    VALUE location = rb_ary_new3(2, rb_iseq_path(cfp->iseq), INT2FIX(line));
		    RB_OBJ_WRITE(me, &def->body.attr.location, rb_ary_freeze(location));
		}
		else {
		    VM_ASSERT(def->body.attr.location == 0);
		}
		return;
	    }
	  case VM_METHOD_TYPE_BMETHOD:
            RB_OBJ_WRITE(me, &def->body.bmethod.proc, (VALUE)opts);
            RB_OBJ_WRITE(me, &def->body.bmethod.defined_ractor, rb_ractor_self(GET_RACTOR()));
	    return;
	  case VM_METHOD_TYPE_NOTIMPLEMENTED:
	    setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), rb_f_notimplement, -1);
	    return;
	  case VM_METHOD_TYPE_OPTIMIZED:
            def->body.optimize_type = (enum method_optimized_type)(intptr_t)opts;
	    return;
	  case VM_METHOD_TYPE_REFINED:
	    {
		const rb_method_refined_t *refined = (rb_method_refined_t *)opts;
		RB_OBJ_WRITE(me, &def->body.refined.orig_me, refined->orig_me);
		RB_OBJ_WRITE(me, &def->body.refined.owner, refined->owner);
		return;
	    }
	  case VM_METHOD_TYPE_ALIAS:
	    RB_OBJ_WRITE(me, &def->body.alias.original_me, (rb_method_entry_t *)opts);
	    return;
	  case VM_METHOD_TYPE_ZSUPER:
	  case VM_METHOD_TYPE_UNDEF:
	  case VM_METHOD_TYPE_MISSING:
	    return;
	}
    }
}

static void
method_definition_reset(const rb_method_entry_t *me)
{
    rb_method_definition_t *def = me->def;

    switch(def->type) {
      case VM_METHOD_TYPE_ISEQ:
	RB_OBJ_WRITTEN(me, Qundef, def->body.iseq.iseqptr);
	RB_OBJ_WRITTEN(me, Qundef, def->body.iseq.cref);
	break;
      case VM_METHOD_TYPE_ATTRSET:
      case VM_METHOD_TYPE_IVAR:
	RB_OBJ_WRITTEN(me, Qundef, def->body.attr.location);
	break;
      case VM_METHOD_TYPE_BMETHOD:
        RB_OBJ_WRITTEN(me, Qundef, def->body.bmethod.proc);
        RB_OBJ_WRITTEN(me, Qundef, def->body.bmethod.defined_ractor);
        /* give up to check all in a list */
        if (def->body.bmethod.hooks) rb_gc_writebarrier_remember((VALUE)me);
	break;
      case VM_METHOD_TYPE_REFINED:
	RB_OBJ_WRITTEN(me, Qundef, def->body.refined.orig_me);
	RB_OBJ_WRITTEN(me, Qundef, def->body.refined.owner);
	break;
      case VM_METHOD_TYPE_ALIAS:
	RB_OBJ_WRITTEN(me, Qundef, def->body.alias.original_me);
	break;
      case VM_METHOD_TYPE_CFUNC:
      case VM_METHOD_TYPE_ZSUPER:
      case VM_METHOD_TYPE_MISSING:
      case VM_METHOD_TYPE_OPTIMIZED:
      case VM_METHOD_TYPE_UNDEF:
      case VM_METHOD_TYPE_NOTIMPLEMENTED:
	break;
    }
}

MJIT_FUNC_EXPORTED rb_method_definition_t *
rb_method_definition_create(rb_method_type_t type, ID mid)
{
    rb_method_definition_t *def;
    def = ZALLOC(rb_method_definition_t);
    def->type = type;
    def->original_id = mid;
    static uintptr_t method_serial = 1;
    def->method_serial = method_serial++;
    return def;
}

static rb_method_definition_t *
method_definition_addref(rb_method_definition_t *def)
{
    def->alias_count++;
    if (METHOD_DEBUG) fprintf(stderr, "+%p-%s:%d\n", (void *)def, rb_id2name(def->original_id), def->alias_count);
    return def;
}

static rb_method_definition_t *
method_definition_addref_complement(rb_method_definition_t *def)
{
    def->complemented_count++;
    if (METHOD_DEBUG) fprintf(stderr, "+%p-%s:%d\n", (void *)def, rb_id2name(def->original_id), def->complemented_count);
    return def;
}

static rb_method_entry_t *
rb_method_entry_alloc(ID called_id, VALUE owner, VALUE defined_class, const rb_method_definition_t *def)
{
    rb_method_entry_t *me = (rb_method_entry_t *)rb_imemo_new(imemo_ment, (VALUE)def, (VALUE)called_id, owner, defined_class);
    return me;
}

static VALUE
filter_defined_class(VALUE klass)
{
    switch (BUILTIN_TYPE(klass)) {
      case T_CLASS:
	return klass;
      case T_MODULE:
	return 0;
      case T_ICLASS:
	break;
      default:
        break;
    }
    rb_bug("filter_defined_class: %s", rb_obj_info(klass));
}

rb_method_entry_t *
rb_method_entry_create(ID called_id, VALUE klass, rb_method_visibility_t visi, const rb_method_definition_t *def)
{
    rb_method_entry_t *me = rb_method_entry_alloc(called_id, klass, filter_defined_class(klass), def);
    METHOD_ENTRY_FLAGS_SET(me, visi, ruby_running ? FALSE : TRUE);
    if (def != NULL) method_definition_reset(me);
    return me;
}

const rb_method_entry_t *
rb_method_entry_clone(const rb_method_entry_t *src_me)
{
    rb_method_entry_t *me = rb_method_entry_alloc(src_me->called_id, src_me->owner, src_me->defined_class,
                                                  method_definition_addref(src_me->def));
    if (METHOD_ENTRY_COMPLEMENTED(src_me)) {
        method_definition_addref_complement(src_me->def);
    }

    METHOD_ENTRY_FLAGS_COPY(me, src_me);
    return me;
}

MJIT_FUNC_EXPORTED const rb_callable_method_entry_t *
rb_method_entry_complement_defined_class(const rb_method_entry_t *src_me, ID called_id, VALUE defined_class)
{
    rb_method_definition_t *def = src_me->def;
    rb_method_entry_t *me;
    struct {
	const struct rb_method_entry_struct *orig_me;
	VALUE owner;
    } refined = {0};

    if (!src_me->defined_class &&
	def->type == VM_METHOD_TYPE_REFINED &&
	def->body.refined.orig_me) {
	const rb_method_entry_t *orig_me =
	    rb_method_entry_clone(def->body.refined.orig_me);
	RB_OBJ_WRITE((VALUE)orig_me, &orig_me->defined_class, defined_class);
	refined.orig_me = orig_me;
	refined.owner = orig_me->owner;
	def = NULL;
    }
    else {
	def = method_definition_addref_complement(def);
    }
    me = rb_method_entry_alloc(called_id, src_me->owner, defined_class, def);
    METHOD_ENTRY_FLAGS_COPY(me, src_me);
    METHOD_ENTRY_COMPLEMENTED_SET(me);
    if (!def) {
	def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, called_id);
	rb_method_definition_set(me, def, &refined);
    }

    VM_ASSERT(RB_TYPE_P(me->owner, T_MODULE));

    return (rb_callable_method_entry_t *)me;
}

void
rb_method_entry_copy(rb_method_entry_t *dst, const rb_method_entry_t *src)
{
    *(rb_method_definition_t **)&dst->def = method_definition_addref(src->def);
    method_definition_reset(dst);
    dst->called_id = src->called_id;
    RB_OBJ_WRITE((VALUE)dst, &dst->owner, src->owner);
    RB_OBJ_WRITE((VALUE)dst, &dst->defined_class, src->defined_class);
    METHOD_ENTRY_FLAGS_COPY(dst, src);
}

static void
make_method_entry_refined(VALUE owner, rb_method_entry_t *me)
{
    if (me->def->type == VM_METHOD_TYPE_REFINED) {
	return;
    }
    else {
	struct {
	    struct rb_method_entry_struct *orig_me;
	    VALUE owner;
	} refined;
	rb_method_definition_t *def;

	rb_vm_check_redefinition_opt_method(me, me->owner);

	refined.orig_me =
	    rb_method_entry_alloc(me->called_id, me->owner,
				  me->defined_class ?
				  me->defined_class : owner,
				  method_definition_addref(me->def));
	METHOD_ENTRY_FLAGS_COPY(refined.orig_me, me);
	refined.owner = owner;

	def = rb_method_definition_create(VM_METHOD_TYPE_REFINED, me->called_id);
	rb_method_definition_set(me, def, (void *)&refined);
	METHOD_ENTRY_VISI_SET(me, METHOD_VISI_PUBLIC);
    }
}

static inline rb_method_entry_t *
lookup_method_table(VALUE klass, ID id)
{
    st_data_t body;
    struct rb_id_table *m_tbl = RCLASS_M_TBL(klass);

    if (rb_id_table_lookup(m_tbl, id, &body)) {
	return (rb_method_entry_t *) body;
    }
    else {
	return 0;
    }
}

void
rb_add_refined_method_entry(VALUE refined_class, ID mid)
{
    rb_method_entry_t *me = lookup_method_table(refined_class, mid);

    if (me) {
	make_method_entry_refined(refined_class, me);
	rb_clear_method_cache(refined_class, mid);
    }
    else {
	rb_add_method(refined_class, mid, VM_METHOD_TYPE_REFINED, 0, METHOD_VISI_PUBLIC);
    }
}

static void
check_override_opt_method_i(VALUE klass, VALUE arg)
{
    ID mid = (ID)arg;
    const rb_method_entry_t *me, *newme;

    if (vm_redefinition_check_flag(klass)) {
	me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
	if (me) {
	    newme = rb_method_entry(klass, mid);
	    if (newme != me) rb_vm_check_redefinition_opt_method(me, me->owner);
	}
    }
    rb_class_foreach_subclass(klass, check_override_opt_method_i, (VALUE)mid);
}

static void
check_override_opt_method(VALUE klass, VALUE mid)
{
    if (rb_vm_check_optimizable_mid(mid)) {
        check_override_opt_method_i(klass, mid);
    }
}

/*
 * klass->method_table[mid] = method_entry(defined_class, visi, def)
 *
 * If def is given (!= NULL), then just use it and ignore original_id and otps.
 * If not given, then make a new def with original_id and opts.
 */
static rb_method_entry_t *
rb_method_entry_make(VALUE klass, ID mid, VALUE defined_class, rb_method_visibility_t visi,
		     rb_method_type_t type, rb_method_definition_t *def, ID original_id, void *opts)
{
    rb_method_entry_t *me;
    struct rb_id_table *mtbl;
    st_data_t data;
    int make_refined = 0;
    VALUE orig_klass;

    if (NIL_P(klass)) {
	klass = rb_cObject;
    }
    orig_klass = klass;

    if (!FL_TEST(klass, FL_SINGLETON) &&
	type != VM_METHOD_TYPE_NOTIMPLEMENTED &&
	type != VM_METHOD_TYPE_ZSUPER) {
	switch (mid) {
	  case idInitialize:
	  case idInitialize_copy:
	  case idInitialize_clone:
	  case idInitialize_dup:
	  case idRespond_to_missing:
	    visi = METHOD_VISI_PRIVATE;
	}
    }

    if (type != VM_METHOD_TYPE_REFINED) {
       rb_class_modify_check(klass);
    }

    if (FL_TEST(klass, RMODULE_IS_REFINEMENT)) {
	VALUE refined_class = rb_refinement_module_get_refined_class(klass);
	rb_add_refined_method_entry(refined_class, mid);
    }
    if (type == VM_METHOD_TYPE_REFINED) {
	rb_method_entry_t *old_me = lookup_method_table(RCLASS_ORIGIN(klass), mid);
	if (old_me) rb_vm_check_redefinition_opt_method(old_me, klass);
    }
    else {
	klass = RCLASS_ORIGIN(klass);
        if (klass != orig_klass) {
            rb_clear_method_cache(orig_klass, mid);
        }
    }
    mtbl = RCLASS_M_TBL(klass);

    /* check re-definition */
    if (rb_id_table_lookup(mtbl, mid, &data)) {
	rb_method_entry_t *old_me = (rb_method_entry_t *)data;
	rb_method_definition_t *old_def = old_me->def;

	if (rb_method_definition_eq(old_def, def)) return old_me;
	rb_vm_check_redefinition_opt_method(old_me, klass);

	if (old_def->type == VM_METHOD_TYPE_REFINED) make_refined = 1;

	if (RTEST(ruby_verbose) &&
	    type != VM_METHOD_TYPE_UNDEF &&
	    (old_def->alias_count == 0) &&
	    !make_refined &&
	    old_def->type != VM_METHOD_TYPE_UNDEF &&
	    old_def->type != VM_METHOD_TYPE_ZSUPER &&
	    old_def->type != VM_METHOD_TYPE_ALIAS) {
	    const rb_iseq_t *iseq = 0;

	    rb_warning("method redefined; discarding old %"PRIsVALUE, rb_id2str(mid));
	    switch (old_def->type) {
	      case VM_METHOD_TYPE_ISEQ:
		iseq = def_iseq_ptr(old_def);
		break;
	      case VM_METHOD_TYPE_BMETHOD:
                iseq = rb_proc_get_iseq(old_def->body.bmethod.proc, 0);
		break;
	      default:
		break;
	    }
	    if (iseq) {
		rb_compile_warning(RSTRING_PTR(rb_iseq_path(iseq)),
				   FIX2INT(iseq->body->location.first_lineno),
				   "previous definition of %"PRIsVALUE" was here",
				   rb_id2str(old_def->original_id));
	    }
	}
    }

    /* create method entry */
    me = rb_method_entry_create(mid, defined_class, visi, NULL);
    if (def == NULL) def = rb_method_definition_create(type, original_id);
    rb_method_definition_set(me, def, opts);

    rb_clear_method_cache(klass, mid);

    /* check mid */
    if (klass == rb_cObject) {
        switch (mid) {
          case idInitialize:
          case idRespond_to_missing:
          case idMethodMissing:
          case idRespond_to:
            rb_warn("redefining Object#%s may cause infinite loop", rb_id2name(mid));
        }
    }
    /* check mid */
    if (mid == object_id || mid == id__send__) {
	if (type == VM_METHOD_TYPE_ISEQ && search_method(klass, mid, 0)) {
	    rb_warn("redefining `%s' may cause serious problems", rb_id2name(mid));
	}
    }

    if (make_refined) {
	make_method_entry_refined(klass, me);
    }

    rb_method_table_insert(klass, mtbl, mid, me);

    VM_ASSERT(me->def != NULL);

    /* check optimized method override by a prepended module */
    if (RB_TYPE_P(orig_klass, T_MODULE)) {
	check_override_opt_method(klass, (VALUE)mid);
    }

    return me;
}

#define CALL_METHOD_HOOK(klass, hook, mid) do {		\
	const VALUE arg = ID2SYM(mid);			\
	VALUE recv_class = (klass);			\
	ID hook_id = (hook);				\
	if (FL_TEST((klass), FL_SINGLETON)) {		\
	    recv_class = rb_ivar_get((klass), attached);	\
	    hook_id = singleton_##hook;			\
	}						\
	rb_funcallv(recv_class, hook_id, 1, &arg);	\
    } while (0)

static void
method_added(VALUE klass, ID mid)
{
    if (ruby_running) {
	CALL_METHOD_HOOK(klass, added, mid);
    }
}

void
rb_add_method(VALUE klass, ID mid, rb_method_type_t type, void *opts, rb_method_visibility_t visi)
{
    rb_method_entry_make(klass, mid, klass, visi, type, NULL, mid, opts);

    if (type != VM_METHOD_TYPE_UNDEF && type != VM_METHOD_TYPE_REFINED) {
	method_added(klass, mid);
    }
}

MJIT_FUNC_EXPORTED void
rb_add_method_iseq(VALUE klass, ID mid, const rb_iseq_t *iseq, rb_cref_t *cref, rb_method_visibility_t visi)
{
    struct { /* should be same fields with rb_method_iseq_struct */
        const rb_iseq_t *iseqptr;
        rb_cref_t *cref;
    } iseq_body;

    iseq_body.iseqptr = iseq;
    iseq_body.cref = cref;
    rb_add_method(klass, mid, VM_METHOD_TYPE_ISEQ, &iseq_body, visi);
}

static rb_method_entry_t *
method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me,
		 rb_method_visibility_t visi, VALUE defined_class)
{
    rb_method_entry_t *newme = rb_method_entry_make(klass, mid, defined_class, visi,
						    me->def->type, method_definition_addref(me->def), 0, NULL);
    method_added(klass, mid);
    return newme;
}

rb_method_entry_t *
rb_method_entry_set(VALUE klass, ID mid, const rb_method_entry_t *me, rb_method_visibility_t visi)
{
    return method_entry_set(klass, mid, me, visi, klass);
}

#define UNDEF_ALLOC_FUNC ((rb_alloc_func_t)-1)

void
rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE))
{
    Check_Type(klass, T_CLASS);
    RCLASS_EXT(klass)->allocator = func;
}

void
rb_undef_alloc_func(VALUE klass)
{
    rb_define_alloc_func(klass, UNDEF_ALLOC_FUNC);
}

rb_alloc_func_t
rb_get_alloc_func(VALUE klass)
{
    Check_Type(klass, T_CLASS);

    for (; klass; klass = RCLASS_SUPER(klass)) {
	rb_alloc_func_t allocator = RCLASS_EXT(klass)->allocator;
	if (allocator == UNDEF_ALLOC_FUNC) break;
	if (allocator) return allocator;
    }
    return 0;
}

const rb_method_entry_t *
rb_method_entry_at(VALUE klass, ID id)
{
    return lookup_method_table(klass, id);
}

static inline rb_method_entry_t*
search_method0(VALUE klass, ID id, VALUE *defined_class_ptr, bool skip_refined)
{
    rb_method_entry_t *me = NULL;

    RB_DEBUG_COUNTER_INC(mc_search);

    for (; klass; klass = RCLASS_SUPER(klass)) {
	RB_DEBUG_COUNTER_INC(mc_search_super);
        if ((me = lookup_method_table(klass, id)) != 0) {
            if (!skip_refined || me->def->type != VM_METHOD_TYPE_REFINED ||
                    me->def->body.refined.orig_me) {
                break;
            }
        }
    }

    if (defined_class_ptr) *defined_class_ptr = klass;

    if (me == NULL) RB_DEBUG_COUNTER_INC(mc_search_notfound);

    VM_ASSERT(me == NULL || !METHOD_ENTRY_INVALIDATED(me));
    return me;
}

static inline rb_method_entry_t*
search_method(VALUE klass, ID id, VALUE *defined_class_ptr)
{
    return search_method0(klass, id, defined_class_ptr, false);
}

static rb_method_entry_t *
search_method_protect(VALUE klass, ID id, VALUE *defined_class_ptr)
{
    rb_method_entry_t *me = search_method(klass, id, defined_class_ptr);

    if (!UNDEFINED_METHOD_ENTRY_P(me)) {
        return me;
    }
    else {
        return NULL;
    }
}

MJIT_FUNC_EXPORTED const rb_method_entry_t *
rb_method_entry(VALUE klass, ID id)
{
    return search_method_protect(klass, id, NULL);
}

static inline const rb_callable_method_entry_t *
prepare_callable_method_entry(VALUE defined_class, ID id, const rb_method_entry_t * const me, int create)
{
    struct rb_id_table *mtbl;
    const rb_callable_method_entry_t *cme;

    if (me) {
        if (me->defined_class == 0) {
            RB_DEBUG_COUNTER_INC(mc_cme_complement);
            VM_ASSERT(RB_TYPE_P(defined_class, T_ICLASS) || RB_TYPE_P(defined_class, T_MODULE));
            VM_ASSERT(me->defined_class == 0);

            mtbl = RCLASS_CALLABLE_M_TBL(defined_class);

            if (mtbl && rb_id_table_lookup(mtbl, id, (VALUE *)&cme)) {
                RB_DEBUG_COUNTER_INC(mc_cme_complement_hit);
                VM_ASSERT(callable_method_entry_p(cme));
                VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
            }
            else if (create) {
                if (!mtbl) {
                    mtbl = RCLASS_EXT(defined_class)->callable_m_tbl = rb_id_table_create(0);
                }
                cme = rb_method_entry_complement_defined_class(me, me->called_id, defined_class);
                rb_id_table_insert(mtbl, id, (VALUE)cme);
                RB_OBJ_WRITTEN(defined_class, Qundef, (VALUE)cme);
                VM_ASSERT(callable_method_entry_p(cme));
            }
            else {
                return NULL;
            }
        }
        else {
            cme = (const rb_callable_method_entry_t *)me;
            VM_ASSERT(callable_method_entry_p(cme));
            VM_ASSERT(!METHOD_ENTRY_INVALIDATED(cme));
        }
        return cme;
    }
    else {
        return NULL;
    }
}

static const rb_callable_method_entry_t *
complemented_callable_method_entry(VALUE klass, ID id)
{
    VALUE defined_class;
    rb_method_entry_t *me = search_method(klass, id, &defined_class);
    return prepare_callable_method_entry(defined_class, id, me, FALSE);
}

static const rb_callable_method_entry_t *
cached_callable_method_entry(VALUE klass, ID mid)
{
    ASSERT_vm_locking();

    struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
    struct rb_class_cc_entries *ccs;

    if (cc_tbl && rb_id_table_lookup(cc_tbl, mid, (VALUE *)&ccs)) {
        VM_ASSERT(vm_ccs_p(ccs));

        if (LIKELY(!METHOD_ENTRY_INVALIDATED(ccs->cme))) {
            VM_ASSERT(ccs->cme->called_id == mid);
            RB_DEBUG_COUNTER_INC(ccs_found);
            return ccs->cme;
        }
        else {
            rb_vm_ccs_free(ccs);
            rb_id_table_delete(cc_tbl, mid);
        }
    }

    RB_DEBUG_COUNTER_INC(ccs_not_found);
    return NULL;
}

static void
cache_callable_method_entry(VALUE klass, ID mid, const rb_callable_method_entry_t *cme)
{
    ASSERT_vm_locking();
    VM_ASSERT(cme != NULL);

    struct rb_id_table *cc_tbl = RCLASS_CC_TBL(klass);
    struct rb_class_cc_entries *ccs;

    if (!cc_tbl) {
        cc_tbl = RCLASS_CC_TBL(klass) = rb_id_table_create(2);
    }

    if (rb_id_table_lookup(cc_tbl, mid, (VALUE *)&ccs)) {
        VM_ASSERT(ccs->cme == cme);
    }
    else {
        ccs = vm_ccs_create(klass, cme);
        rb_id_table_insert(cc_tbl, mid, (VALUE)ccs);
    }
}

static const rb_callable_method_entry_t *
negative_cme(ID mid)
{
    rb_vm_t *vm = GET_VM();
    const rb_callable_method_entry_t *cme;

    if (!rb_id_table_lookup(vm->negative_cme_table, mid, (VALUE *)&cme)) {
        cme = (rb_callable_method_entry_t *)rb_method_entry_alloc(mid, Qnil, Qnil, NULL);
        rb_id_table_insert(vm->negative_cme_table, mid, (VALUE)cme);
    }

    VM_ASSERT(cme != NULL);
    return cme;
}

static const rb_callable_method_entry_t *
callable_method_entry(VALUE klass, ID mid, VALUE *defined_class_ptr)
{
    const rb_callable_method_entry_t *cme;

    VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_ICLASS));
    RB_VM_LOCK_ENTER();
    {
        cme = cached_callable_method_entry(klass, mid);

        if (cme) {
            if (defined_class_ptr != NULL) *defined_class_ptr = cme->defined_class;
        }
        else {
            VALUE defined_class;
            rb_method_entry_t *me = search_method(klass, mid, &defined_class);
            if (defined_class_ptr) *defined_class_ptr = defined_class;

            if (me != NULL) {
                cme = prepare_callable_method_entry(defined_class, mid, me, TRUE);
            }
            else {
                cme = negative_cme(mid);
            }

            cache_callable_method_entry(klass, mid, cme);
        }
    }
    RB_VM_LOCK_LEAVE();

    return !UNDEFINED_METHOD_ENTRY_P(cme) ? cme : NULL;
}

MJIT_FUNC_EXPORTED const rb_callable_method_entry_t *
rb_callable_method_entry(VALUE klass, ID mid)
{
    return callable_method_entry(klass, mid, NULL);
}

static const rb_method_entry_t *resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr);

static const rb_method_entry_t *
method_entry_resolve_refinement(VALUE klass, ID id, int with_refinement, VALUE *defined_class_ptr)
{
    const rb_method_entry_t *me = search_method_protect(klass, id, defined_class_ptr);

    if (me) {
	if (me->def->type == VM_METHOD_TYPE_REFINED) {
	    if (with_refinement) {
		const rb_cref_t *cref = rb_vm_cref();
		VALUE refinements = cref ? CREF_REFINEMENTS(cref) : Qnil;
		me = resolve_refined_method(refinements, me, defined_class_ptr);
	    }
	    else {
		me = resolve_refined_method(Qnil, me, defined_class_ptr);
	    }

	    if (UNDEFINED_METHOD_ENTRY_P(me)) me = NULL;
	}
    }

    return me;
}

MJIT_FUNC_EXPORTED const rb_method_entry_t *
rb_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
{
    return method_entry_resolve_refinement(klass, id, TRUE, defined_class_ptr);
}

static const rb_callable_method_entry_t *
callable_method_entry_refeinements0(VALUE klass, ID id, VALUE *defined_class_ptr, bool with_refinements,
                                    const rb_callable_method_entry_t *cme)
{
    if (cme == NULL || LIKELY(cme->def->type != VM_METHOD_TYPE_REFINED)) {
        return cme;
    }
    else {
        VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
        const rb_method_entry_t *me = method_entry_resolve_refinement(klass, id, with_refinements, dcp);
        return prepare_callable_method_entry(*dcp, id, me, TRUE);
    }
}

static const rb_callable_method_entry_t *
callable_method_entry_refeinements(VALUE klass, ID id, VALUE *defined_class_ptr, bool with_refinements)
{
    const rb_callable_method_entry_t *cme = callable_method_entry(klass, id, defined_class_ptr);
    return callable_method_entry_refeinements0(klass, id, defined_class_ptr, with_refinements, cme);
}

MJIT_FUNC_EXPORTED const rb_callable_method_entry_t *
rb_callable_method_entry_with_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
{
    return callable_method_entry_refeinements(klass, id, defined_class_ptr, true);
}

static const rb_callable_method_entry_t *
callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
{
    return callable_method_entry_refeinements(klass, id, defined_class_ptr, false);
}

const rb_method_entry_t *
rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
{
    return method_entry_resolve_refinement(klass, id, FALSE, defined_class_ptr);
}

MJIT_FUNC_EXPORTED const rb_callable_method_entry_t *
rb_callable_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class_ptr)
{
    VALUE defined_class, *dcp = defined_class_ptr ? defined_class_ptr : &defined_class;
    const rb_method_entry_t *me = method_entry_resolve_refinement(klass, id, FALSE, dcp);
    return prepare_callable_method_entry(*dcp, id, me, TRUE);
}

static const rb_method_entry_t *
resolve_refined_method(VALUE refinements, const rb_method_entry_t *me, VALUE *defined_class_ptr)
{
    while (me && me->def->type == VM_METHOD_TYPE_REFINED) {
	VALUE refinement;
        const rb_method_entry_t *tmp_me;
        VALUE super;

	refinement = find_refinement(refinements, me->owner);
        if (!NIL_P(refinement)) {
	    tmp_me = search_method_protect(refinement, me->called_id, defined_class_ptr);

	    if (tmp_me && tmp_me->def->type != VM_METHOD_TYPE_REFINED) {
		return tmp_me;
	    }
	}

        tmp_me = me->def->body.refined.orig_me;
        if (tmp_me) {
            if (defined_class_ptr) *defined_class_ptr = tmp_me->defined_class;
            return tmp_me;
        }

        super = RCLASS_SUPER(me->owner);
        if (!super) {
            return 0;
        }

        me = search_method_protect(super, me->called_id, defined_class_ptr);
    }
    return me;
}

const rb_method_entry_t *
rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me)
{
    return resolve_refined_method(refinements, me, NULL);
}

MJIT_FUNC_EXPORTED
const rb_callable_method_entry_t *
rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me)
{
    VALUE defined_class = me->defined_class;
    const rb_method_entry_t *resolved_me = resolve_refined_method(refinements, (const rb_method_entry_t *)me, &defined_class);

    if (resolved_me && resolved_me->defined_class == 0) {
	return rb_method_entry_complement_defined_class(resolved_me, me->called_id, defined_class);
    }
    else {
	return (const rb_callable_method_entry_t *)resolved_me;
    }
}

static void
remove_method(VALUE klass, ID mid)
{
    VALUE data;
    rb_method_entry_t *me = 0;
    VALUE self = klass;

    klass = RCLASS_ORIGIN(klass);
    rb_class_modify_check(klass);
    if (mid == object_id || mid == id__send__ || mid == idInitialize) {
	rb_warn("removing `%s' may cause serious problems", rb_id2name(mid));
    }

    if (!rb_id_table_lookup(RCLASS_M_TBL(klass), mid, &data) ||
	!(me = (rb_method_entry_t *)data) ||
	(!me->def || me->def->type == VM_METHOD_TYPE_UNDEF) ||
        UNDEFINED_REFINED_METHOD_P(me->def)) {
	rb_name_err_raise("method `%1$s' not defined in %2$s",
			  klass, ID2SYM(mid));
    }

    if (klass != self) {
        rb_clear_method_cache(self, mid);
    }
    rb_clear_method_cache(klass, mid);
    rb_id_table_delete(RCLASS_M_TBL(klass), mid);

    rb_vm_check_redefinition_opt_method(me, klass);

    if (me->def->type == VM_METHOD_TYPE_REFINED) {
	rb_add_refined_method_entry(klass, mid);
    }

    CALL_METHOD_HOOK(self, removed, mid);
}

void
rb_remove_method_id(VALUE klass, ID mid)
{
    remove_method(klass, mid);
}

void
rb_remove_method(VALUE klass, const char *name)
{
    remove_method(klass, rb_intern(name));
}

/*
 *  call-seq:
 *     remove_method(symbol)   -> self
 *     remove_method(string)   -> self
 *
 *  Removes the method identified by _symbol_ from the current
 *  class. For an example, see Module#undef_method.
 *  String arguments are converted to symbols.
 */

static VALUE
rb_mod_remove_method(int argc, VALUE *argv, VALUE mod)
{
    int i;

    for (i = 0; i < argc; i++) {
	VALUE v = argv[i];
	ID id = rb_check_id(&v);
	if (!id) {
	    rb_name_err_raise("method `%1$s' not defined in %2$s",
			      mod, v);
	}
	remove_method(mod, id);
    }
    return mod;
}

static void
rb_export_method(VALUE klass, ID name, rb_method_visibility_t visi)
{
    rb_method_entry_t *me;
    VALUE defined_class;
    VALUE origin_class = RCLASS_ORIGIN(klass);

    me = search_method0(origin_class, name, &defined_class, true);

    if (!me && RB_TYPE_P(klass, T_MODULE)) {
	me = search_method(rb_cObject, name, &defined_class);
    }

    if (UNDEFINED_METHOD_ENTRY_P(me) ||
	UNDEFINED_REFINED_METHOD_P(me->def)) {
	rb_print_undef(klass, name, METHOD_VISI_UNDEF);
    }

    if (METHOD_ENTRY_VISI(me) != visi) {
	rb_vm_check_redefinition_opt_method(me, klass);

	if (klass == defined_class || origin_class == defined_class) {
            if (me->def->type == VM_METHOD_TYPE_REFINED) {
                // Refinement method entries should always be public because the refinement
                // search is always performed.
                if (me->def->body.refined.orig_me) {
                    METHOD_ENTRY_VISI_SET((rb_method_entry_t *)me->def->body.refined.orig_me, visi);
                }
            }
            else {
                METHOD_ENTRY_VISI_SET(me, visi);
            }
            rb_clear_method_cache(klass, name);
	}
	else {
	    rb_add_method(klass, name, VM_METHOD_TYPE_ZSUPER, 0, visi);
	}
    }
}

#define BOUND_PRIVATE  0x01
#define BOUND_RESPONDS 0x02

static int
method_boundp(VALUE klass, ID id, int ex)
{
    const rb_callable_method_entry_t *cme;

    VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_ICLASS));

    if (ex & BOUND_RESPONDS) {
        cme = rb_callable_method_entry_with_refinements(klass, id, NULL);
    }
    else {
        cme = callable_method_entry_without_refinements(klass, id, NULL);
    }

    if (cme != NULL) {
        if (ex & ~BOUND_RESPONDS) {
            switch (METHOD_ENTRY_VISI(cme)) {
              case METHOD_VISI_PRIVATE:
                return 0;
              case METHOD_VISI_PROTECTED:
                if (ex & BOUND_RESPONDS) return 0;
              default:
                break;
            }
	}

	if (cme->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) {
	    if (ex & BOUND_RESPONDS) return 2;
	    return 0;
	}
	return 1;
    }
    return 0;
}

// deprecated
int
rb_method_boundp(VALUE klass, ID id, int ex)
{
    return method_boundp(klass, id, ex);
}

static void
vm_cref_set_visibility(rb_method_visibility_t method_visi, int module_func)
{
    rb_scope_visibility_t *scope_visi = (rb_scope_visibility_t *)&rb_vm_cref()->scope_visi;
    scope_visi->method_visi = method_visi;
    scope_visi->module_func = module_func;
}

void
rb_scope_visibility_set(rb_method_visibility_t visi)
{
    vm_cref_set_visibility(visi, FALSE);
}

static void
scope_visibility_check(void)
{
    /* Check for public/protected/private/module_function called inside a method */
    rb_control_frame_t *cfp = GET_EC()->cfp+1;
    if (cfp && cfp->iseq && cfp->iseq->body->type == ISEQ_TYPE_METHOD) {
        rb_warn("calling %s without arguments inside a method may not have the intended effect",
            rb_id2name(rb_frame_this_func()));
    }
}

static void
rb_scope_module_func_set(void)
{
    scope_visibility_check();
    vm_cref_set_visibility(METHOD_VISI_PRIVATE, TRUE);
}

const rb_cref_t *rb_vm_cref_in_context(VALUE self, VALUE cbase);
void
rb_attr(VALUE klass, ID id, int read, int write, int ex)
{
    ID attriv;
    rb_method_visibility_t visi;
    const rb_execution_context_t *ec = GET_EC();
    const rb_cref_t *cref = rb_vm_cref_in_context(klass, klass);

    if (!ex || !cref) {
	visi = METHOD_VISI_PUBLIC;
    }
    else {
        switch (vm_scope_visibility_get(ec)) {
	  case METHOD_VISI_PRIVATE:
            if (vm_scope_module_func_check(ec)) {
		rb_warning("attribute accessor as module_function");
	    }
	    visi = METHOD_VISI_PRIVATE;
	    break;
	  case METHOD_VISI_PROTECTED:
	    visi = METHOD_VISI_PROTECTED;
	    break;
	  default:
	    visi = METHOD_VISI_PUBLIC;
	    break;
	}
    }

    attriv = rb_intern_str(rb_sprintf("@%"PRIsVALUE, rb_id2str(id)));
    if (read) {
	rb_add_method(klass, id, VM_METHOD_TYPE_IVAR, (void *)attriv, visi);
    }
    if (write) {
	rb_add_method(klass, rb_id_attrset(id), VM_METHOD_TYPE_ATTRSET, (void *)attriv, visi);
    }
}

void
rb_undef(VALUE klass, ID id)
{
    const rb_method_entry_t *me;

    if (NIL_P(klass)) {
	rb_raise(rb_eTypeError, "no class to undef method");
    }
    rb_class_modify_check(klass);
    if (id == object_id || id == id__send__ || id == idInitialize) {
	rb_warn("undefining `%s' may cause serious problems", rb_id2name(id));
    }

    me = search_method(klass, id, 0);
    if (me && me->def->type == VM_METHOD_TYPE_REFINED) {
	me = rb_resolve_refined_method(Qnil, me);
    }

    if (UNDEFINED_METHOD_ENTRY_P(me) ||
	UNDEFINED_REFINED_METHOD_P(me->def)) {
	rb_method_name_error(klass, rb_id2str(id));
    }

    rb_add_method(klass, id, VM_METHOD_TYPE_UNDEF, 0, METHOD_VISI_PUBLIC);

    CALL_METHOD_HOOK(klass, undefined, id);
}

/*
 *  call-seq:
 *     undef_method(symbol)    -> self
 *     undef_method(string)    -> self
 *
 *  Prevents the current class from responding to calls to the named
 *  method. Contrast this with <code>remove_method</code>, which deletes
 *  the method from the particular class; Ruby will still search
 *  superclasses and mixed-in modules for a possible receiver.
 *  String arguments are converted to symbols.
 *
 *     class Parent
 *       def hello
 *         puts "In parent"
 *       end
 *     end
 *     class Child < Parent
 *       def hello
 *         puts "In child"
 *       end
 *     end
 *
 *
 *     c = Child.new
 *     c.hello
 *
 *
 *     class Child
 *       remove_method :hello  # remove from child, still in parent
 *     end
 *     c.hello
 *
 *
 *     class Child
 *       undef_method :hello   # prevent any calls to 'hello'
 *     end
 *     c.hello
 *
 *  <em>produces:</em>
 *
 *     In child
 *     In parent
 *     prog.rb:23: undefined method `hello' for #<Child:0x401b3bb4> (NoMethodError)
 */

static VALUE
rb_mod_undef_method(int argc, VALUE *argv, VALUE mod)
{
    int i;
    for (i = 0; i < argc; i++) {
	VALUE v = argv[i];
	ID id = rb_check_id(&v);
	if (!id) {
	    rb_method_name_error(mod, v);
	}
	rb_undef(mod, id);
    }
    return mod;
}

static rb_method_visibility_t
check_definition_visibility(VALUE mod, int argc, VALUE *argv)
{
    const rb_method_entry_t *me;
    VALUE mid, include_super, lookup_mod = mod;
    int inc_super;
    ID id;

    rb_scan_args(argc, argv, "11", &mid, &include_super);
    id = rb_check_id(&mid);
    if (!id) return METHOD_VISI_UNDEF;

    if (argc == 1) {
	inc_super = 1;
    }
    else {
	inc_super = RTEST(include_super);
	if (!inc_super) {
	    lookup_mod = RCLASS_ORIGIN(mod);
	}
    }

    me = rb_method_entry_without_refinements(lookup_mod, id, NULL);
    if (me) {
	if (me->def->type == VM_METHOD_TYPE_NOTIMPLEMENTED) return METHOD_VISI_UNDEF;
	if (!inc_super && me->owner != mod) return METHOD_VISI_UNDEF;
	return METHOD_ENTRY_VISI(me);
    }
    return METHOD_VISI_UNDEF;
}

/*
 *  call-seq:
 *     mod.method_defined?(symbol, inherit=true)    -> true or false
 *     mod.method_defined?(string, inherit=true)    -> true or false
 *
 *  Returns +true+ if the named method is defined by
 *  _mod_.  If _inherit_ is set, the lookup will also search _mod_'s
 *  ancestors. Public and protected methods are matched.
 *  String arguments are converted to symbols.
 *
 *     module A
 *       def method1()  end
 *       def protected_method1()  end
 *       protected :protected_method1
 *     end
 *     class B
 *       def method2()  end
 *       def private_method2()  end
 *       private :private_method2
 *     end
 *     class C < B
 *       include A
 *       def method3()  end
 *     end
 *
 *     A.method_defined? :method1              #=> true
 *     C.method_defined? "method1"             #=> true
 *     C.method_defined? "method2"             #=> true
 *     C.method_defined? "method2", true       #=> true
 *     C.method_defined? "method2", false      #=> false
 *     C.method_defined? "method3"             #=> true
 *     C.method_defined? "protected_method1"   #=> true
 *     C.method_defined? "method4"             #=> false
 *     C.method_defined? "private_method2"     #=> false
 */

static VALUE
rb_mod_method_defined(int argc, VALUE *argv, VALUE mod)
{
    rb_method_visibility_t visi = check_definition_visibility(mod, argc, argv);
    return (visi == METHOD_VISI_PUBLIC || visi == METHOD_VISI_PROTECTED) ? Qtrue : Qfalse;
}

static VALUE
check_definition(VALUE mod, int argc, VALUE *argv, rb_method_visibility_t visi)
{
    return (check_definition_visibility(mod, argc, argv) == visi) ? Qtrue : Qfalse;
}

/*
 *  call-seq:
 *     mod.public_method_defined?(symbol, inherit=true)   -> true or false
 *     mod.public_method_defined?(string, inherit=true)   -> true or false
 *
 *  Returns +true+ if the named public method is defined by
 *  _mod_.  If _inherit_ is set, the lookup will also search _mod_'s
 *  ancestors.
 *  String arguments are converted to symbols.
 *
 *     module A
 *       def method1()  end
 *     end
 *     class B
 *       protected
 *       def method2()  end
 *     end
 *     class C < B
 *       include A
 *       def method3()  end
 *     end
 *
 *     A.method_defined? :method1                 #=> true
 *     C.public_method_defined? "method1"         #=> true
 *     C.public_method_defined? "method1", true   #=> true
 *     C.public_method_defined? "method1", false  #=> true
 *     C.public_method_defined? "method2"         #=> false
 *     C.method_defined? "method2"                #=> true
 */

static VALUE
rb_mod_public_method_defined(int argc, VALUE *argv, VALUE mod)
{
    return check_definition(mod, argc, argv, METHOD_VISI_PUBLIC);
}

/*
 *  call-seq:
 *     mod.private_method_defined?(symbol, inherit=true)    -> true or false
 *     mod.private_method_defined?(string, inherit=true)    -> true or false
 *
 *  Returns +true+ if the named private method is defined by
 *  _mod_.  If _inherit_ is set, the lookup will also search _mod_'s
 *  ancestors.
 *  String arguments are converted to symbols.
 *
 *     module A
 *       def method1()  end
 *     end
 *     class B
 *       private
 *       def method2()  end
 *     end
 *     class C < B
 *       include A
 *       def method3()  end
 *     end
 *
 *     A.method_defined? :method1                   #=> true
 *     C.private_method_defined? "method1"          #=> false
 *     C.private_method_defined? "method2"          #=> true
 *     C.private_method_defined? "method2", true    #=> true
 *     C.private_method_defined? "method2", false   #=> false
 *     C.method_defined? "method2"                  #=> false
 */

static VALUE
rb_mod_private_method_defined(int argc, VALUE *argv, VALUE mod)
{
    return check_definition(mod, argc, argv, METHOD_VISI_PRIVATE);
}

/*
 *  call-seq:
 *     mod.protected_method_defined?(symbol, inherit=true)   -> true or false
 *     mod.protected_method_defined?(string, inherit=true)   -> true or false
 *
 *  Returns +true+ if the named protected method is defined
 *  _mod_.  If _inherit_ is set, the lookup will also search _mod_'s
 *  ancestors.
 *  String arguments are converted to symbols.
 *
 *     module A
 *       def method1()  end
 *     end
 *     class B
 *       protected
 *       def method2()  end
 *     end
 *     class C < B
 *       include A
 *       def method3()  end
 *     end
 *
 *     A.method_defined? :method1                    #=> true
 *     C.protected_method_defined? "method1"         #=> false
 *     C.protected_method_defined? "method2"         #=> true
 *     C.protected_method_defined? "method2", true   #=> true
 *     C.protected_method_defined? "method2", false  #=> false
 *     C.method_defined? "method2"                   #=> true
 */

static VALUE
rb_mod_protected_method_defined(int argc, VALUE *argv, VALUE mod)
{
    return check_definition(mod, argc, argv, METHOD_VISI_PROTECTED);
}

int
rb_method_entry_eq(const rb_method_entry_t *m1, const rb_method_entry_t *m2)
{
    return rb_method_definition_eq(m1->def, m2->def);
}

static const rb_method_definition_t *
original_method_definition(const rb_method_definition_t *def)
{
  again:
    if (def) {
	switch (def->type) {
	  case VM_METHOD_TYPE_REFINED:
	    if (def->body.refined.orig_me) {
		def = def->body.refined.orig_me->def;
		goto again;
	    }
	    break;
	  case VM_METHOD_TYPE_ALIAS:
	    def = def->body.alias.original_me->def;
	    goto again;
	  default:
	    break;
	}
    }
    return def;
}

MJIT_FUNC_EXPORTED int
rb_method_definition_eq(const rb_method_definition_t *d1, const rb_method_definition_t *d2)
{
    d1 = original_method_definition(d1);
    d2 = original_method_definition(d2);

    if (d1 == d2) return 1;
    if (!d1 || !d2) return 0;
    if (d1->type != d2->type) return 0;

    switch (d1->type) {
      case VM_METHOD_TYPE_ISEQ:
	return d1->body.iseq.iseqptr == d2->body.iseq.iseqptr;
      case VM_METHOD_TYPE_CFUNC:
	return
	  d1->body.cfunc.func == d2->body.cfunc.func &&
	  d1->body.cfunc.argc == d2->body.cfunc.argc;
      case VM_METHOD_TYPE_ATTRSET:
      case VM_METHOD_TYPE_IVAR:
	return d1->body.attr.id == d2->body.attr.id;
      case VM_METHOD_TYPE_BMETHOD:
        return RTEST(rb_equal(d1->body.bmethod.proc, d2->body.bmethod.proc));
      case VM_METHOD_TYPE_MISSING:
	return d1->original_id == d2->original_id;
      case VM_METHOD_TYPE_ZSUPER:
      case VM_METHOD_TYPE_NOTIMPLEMENTED:
      case VM_METHOD_TYPE_UNDEF:
	return 1;
      case VM_METHOD_TYPE_OPTIMIZED:
	return d1->body.optimize_type == d2->body.optimize_type;
      case VM_METHOD_TYPE_REFINED:
      case VM_METHOD_TYPE_ALIAS:
	break;
    }
    rb_bug("rb_method_definition_eq: unsupported type: %d\n", d1->type);
}

static st_index_t
rb_hash_method_definition(st_index_t hash, const rb_method_definition_t *def)
{
    hash = rb_hash_uint(hash, def->type);
    def = original_method_definition(def);

    if (!def) return hash;

    switch (def->type) {
      case VM_METHOD_TYPE_ISEQ:
	return rb_hash_uint(hash, (st_index_t)def->body.iseq.iseqptr);
      case VM_METHOD_TYPE_CFUNC:
	hash = rb_hash_uint(hash, (st_index_t)def->body.cfunc.func);
	return rb_hash_uint(hash, def->body.cfunc.argc);
      case VM_METHOD_TYPE_ATTRSET:
      case VM_METHOD_TYPE_IVAR:
	return rb_hash_uint(hash, def->body.attr.id);
      case VM_METHOD_TYPE_BMETHOD:
        return rb_hash_proc(hash, def->body.bmethod.proc);
      case VM_METHOD_TYPE_MISSING:
	return rb_hash_uint(hash, def->original_id);
      case VM_METHOD_TYPE_ZSUPER:
      case VM_METHOD_TYPE_NOTIMPLEMENTED:
      case VM_METHOD_TYPE_UNDEF:
	return hash;
      case VM_METHOD_TYPE_OPTIMIZED:
	return rb_hash_uint(hash, def->body.optimize_type);
      case VM_METHOD_TYPE_REFINED:
      case VM_METHOD_TYPE_ALIAS:
	break; /* unreachable */
	}
	rb_bug("rb_hash_method_definition: unsupported method type (%d)\n", def->type);
    }

st_index_t
rb_hash_method_entry(st_index_t hash, const rb_method_entry_t *me)
{
    return rb_hash_method_definition(hash, me->def);
}

void
rb_alias(VALUE klass, ID alias_name, ID original_name)
{
    const VALUE target_klass = klass;
    VALUE defined_class;
    const rb_method_entry_t *orig_me;
    rb_method_visibility_t visi = METHOD_VISI_UNDEF;

    if (NIL_P(klass)) {
	rb_raise(rb_eTypeError, "no class to make alias");
    }

    rb_class_modify_check(klass);

  again:
    orig_me = search_method(klass, original_name, &defined_class);

    if (orig_me && orig_me->def->type == VM_METHOD_TYPE_REFINED) {
	orig_me = rb_resolve_refined_method(Qnil, orig_me);
    }

    if (UNDEFINED_METHOD_ENTRY_P(orig_me) ||
	UNDEFINED_REFINED_METHOD_P(orig_me->def)) {
	if ((!RB_TYPE_P(klass, T_MODULE)) ||
	    (orig_me = search_method(rb_cObject, original_name, &defined_class),
	     UNDEFINED_METHOD_ENTRY_P(orig_me))) {
	    rb_print_undef(klass, original_name, METHOD_VISI_UNDEF);
	}
    }

    if (orig_me->def->type == VM_METHOD_TYPE_ZSUPER) {
	klass = RCLASS_SUPER(klass);
	original_name = orig_me->def->original_id;
	visi = METHOD_ENTRY_VISI(orig_me);
	goto again;
    }

    if (visi == METHOD_VISI_UNDEF) visi = METHOD_ENTRY_VISI(orig_me);

    if (orig_me->defined_class == 0) {
	rb_method_entry_make(target_klass, alias_name, target_klass, visi,
			     VM_METHOD_TYPE_ALIAS, NULL, orig_me->called_id,
			     (void *)rb_method_entry_clone(orig_me));
	method_added(target_klass, alias_name);
    }
    else {
	rb_method_entry_t *alias_me;

	alias_me = method_entry_set(target_klass, alias_name, orig_me, visi, orig_me->owner);
	RB_OBJ_WRITE(alias_me, &alias_me->owner, target_klass);
        RB_OBJ_WRITE(alias_me, &alias_me->defined_class, orig_me->defined_class);
    }
}

/*
 *  call-seq:
 *     alias_method(new_name, old_name)   -> symbol
 *
 *  Makes <i>new_name</i> a new copy of the method <i>old_name</i>. This can
 *  be used to retain access to methods that are overridden.
 *
 *     module Mod
 *       alias_method :orig_exit, :exit #=> :orig_exit
 *       def exit(code=0)
 *         puts "Exiting with code #{code}"
 *         orig_exit(code)
 *       end
 *     end
 *     include Mod
 *     exit(99)
 *
 *  <em>produces:</em>
 *
 *     Exiting with code 99
 */

static VALUE
rb_mod_alias_method(VALUE mod, VALUE newname, VALUE oldname)
{
    ID oldid = rb_check_id(&oldname);
    if (!oldid) {
	rb_print_undef_str(mod, oldname);
    }
    VALUE id = rb_to_id(newname);
    rb_alias(mod, id, oldid);
    return ID2SYM(id);
}

static void
check_and_export_method(VALUE self, VALUE name, rb_method_visibility_t visi)
{
    ID id = rb_check_id(&name);
    if (!id) {
	rb_print_undef_str(self, name);
    }
    rb_export_method(self, id, visi);
}

static void
set_method_visibility(VALUE self, int argc, const VALUE *argv, rb_method_visibility_t visi)
{
    int i;

    rb_check_frozen(self);
    if (argc == 0) {
	rb_warning("%"PRIsVALUE" with no argument is just ignored",
		   QUOTE_ID(rb_frame_callee()));
	return;
    }


    VALUE v;

    if (argc == 1 && (v = rb_check_array_type(argv[0])) != Qnil) {
        long j;

	for (j = 0; j < RARRAY_LEN(v); j++) {
	    check_and_export_method(self, RARRAY_AREF(v, j), visi);
	}
    } else {
        for (i = 0; i < argc; i++) {
            check_and_export_method(self, argv[i], visi);
        }
    }
}

static VALUE
set_visibility(int argc, const VALUE *argv, VALUE module, rb_method_visibility_t visi)
{
    if (argc == 0) {
        scope_visibility_check();
        rb_scope_visibility_set(visi);
    }
    else {
	set_method_visibility(module, argc, argv, visi);
    }
    return module;
}

/*
 *  call-seq:
 *     public                 -> self
 *     public(symbol, ...)    -> self
 *     public(string, ...)    -> self
 *     public(array)          -> self
 *
 *  With no arguments, sets the default visibility for subsequently
 *  defined methods to public. With arguments, sets the named methods to
 *  have public visibility.
 *  String arguments are converted to symbols.
 *  An Array of Symbols and/or Strings are also accepted.
 */

static VALUE
rb_mod_public(int argc, VALUE *argv, VALUE module)
{
    return set_visibility(argc, argv, module, METHOD_VISI_PUBLIC);
}

/*
 *  call-seq:
 *     protected                -> self
 *     protected(symbol, ...)   -> self
 *     protected(string, ...)   -> self
 *     protected(array)         -> self
 *
 *  With no arguments, sets the default visibility for subsequently
 *  defined methods to protected. With arguments, sets the named methods
 *  to have protected visibility.
 *  String arguments are converted to symbols.
 *  An Array of Symbols and/or Strings are also accepted.
 *
 *  If a method has protected visibility, it is callable only where
 *  <code>self</code> of the context is the same as the method.
 *  (method definition or instance_eval). This behavior is different from
 *  Java's protected method. Usually <code>private</code> should be used.
 *
 *  Note that a protected method is slow because it can't use inline cache.
 *
 *  To show a private method on RDoc, use <code>:doc:</code> instead of this.
 */

static VALUE
rb_mod_protected(int argc, VALUE *argv, VALUE module)
{
    return set_visibility(argc, argv, module, METHOD_VISI_PROTECTED);
}

/*
 *  call-seq:
 *     private                 -> self
 *     private(symbol, ...)    -> self
 *     private(string, ...)    -> self
 *     private(array)          -> self
 *
 *  With no arguments, sets the default visibility for subsequently
 *  defined methods to private. With arguments, sets the named methods
 *  to have private visibility.
 *  String arguments are converted to symbols.
 *  An Array of Symbols and/or Strings are also accepted.
 *
 *     module Mod
 *       def a()  end
 *       def b()  end
 *       private
 *       def c()  end
 *       private :a
 *     end
 *     Mod.private_instance_methods   #=> [:a, :c]
 *
 *  Note that to show a private method on RDoc, use <code>:doc:</code>.
 */

static VALUE
rb_mod_private(int argc, VALUE *argv, VALUE module)
{
    return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
}

/*
 *  call-seq:
 *     ruby2_keywords(method_name, ...)    -> nil
 *
 *  For the given method names, marks the method as passing keywords through
 *  a normal argument splat.  This should only be called on methods that
 *  accept an argument splat (<tt>*args</tt>) but not explicit keywords or
 *  a keyword splat.  It marks the method such that if the method is called
 *  with keyword arguments, the final hash argument is marked with a special
 *  flag such that if it is the final element of a normal argument splat to
 *  another method call, and that method call does not include explicit
 *  keywords or a keyword splat, the final element is interpreted as keywords.
 *  In other words, keywords will be passed through the method to other
 *  methods.
 *
 *  This should only be used for methods that delegate keywords to another
 *  method, and only for backwards compatibility with Ruby versions before
 *  2.7.
 *
 *  This method will probably be removed at some point, as it exists only
 *  for backwards compatibility. As it does not exist in Ruby versions
 *  before 2.7, check that the module responds to this method before calling
 *  it. Also, be aware that if this method is removed, the behavior of the
 *  method will change so that it does not pass through keywords.
 *
 *    module Mod
 *      def foo(meth, *args, &block)
 *        send(:"do_#{meth}", *args, &block)
 *      end
 *      ruby2_keywords(:foo) if respond_to?(:ruby2_keywords, true)
 *    end
 */

static VALUE
rb_mod_ruby2_keywords(int argc, VALUE *argv, VALUE module)
{
    int i;
    VALUE origin_class = RCLASS_ORIGIN(module);

    rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
    rb_check_frozen(module);

    for (i = 0; i < argc; i++) {
        VALUE v = argv[i];
        ID name = rb_check_id(&v);
        rb_method_entry_t *me;
        VALUE defined_class;

        if (!name) {
            rb_print_undef_str(module, v);
        }

        me = search_method(origin_class, name, &defined_class);
        if (!me && RB_TYPE_P(module, T_MODULE)) {
            me = search_method(rb_cObject, name, &defined_class);
        }

        if (UNDEFINED_METHOD_ENTRY_P(me) ||
            UNDEFINED_REFINED_METHOD_P(me->def)) {
            rb_print_undef(module, name, METHOD_VISI_UNDEF);
        }

        if (module == defined_class || origin_class == defined_class) {
            switch (me->def->type) {
              case VM_METHOD_TYPE_ISEQ:
                if (me->def->body.iseq.iseqptr->body->param.flags.has_rest &&
                        !me->def->body.iseq.iseqptr->body->param.flags.has_kw &&
                        !me->def->body.iseq.iseqptr->body->param.flags.has_kwrest) {
                    me->def->body.iseq.iseqptr->body->param.flags.ruby2_keywords = 1;
                    rb_clear_method_cache(module, name);
                }
                else {
                    rb_warn("Skipping set of ruby2_keywords flag for %s (method accepts keywords or method does not accept argument splat)", rb_id2name(name));
                }
                break;
              case VM_METHOD_TYPE_BMETHOD: {
                VALUE procval = me->def->body.bmethod.proc;
                if (vm_block_handler_type(procval) == block_handler_type_proc) {
                    procval = vm_proc_to_block_handler(VM_BH_TO_PROC(procval));
                }

                if (vm_block_handler_type(procval) == block_handler_type_iseq) {
                    const struct rb_captured_block *captured = VM_BH_TO_ISEQ_BLOCK(procval);
                    const rb_iseq_t *iseq = rb_iseq_check(captured->code.iseq);
                    if (iseq->body->param.flags.has_rest &&
                            !iseq->body->param.flags.has_kw &&
                            !iseq->body->param.flags.has_kwrest) {
                        iseq->body->param.flags.ruby2_keywords = 1;
                        rb_clear_method_cache(module, name);
                    }
                    else {
                        rb_warn("Skipping set of ruby2_keywords flag for %s (method accepts keywords or method does not accept argument splat)", rb_id2name(name));
                    }
                    break;
                }
              }
              /* fallthrough */
              default:
                rb_warn("Skipping set of ruby2_keywords flag for %s (method not defined in Ruby)", rb_id2name(name));
                break;
            }
        }
        else {
            rb_warn("Skipping set of ruby2_keywords flag for %s (can only set in method defining module)", rb_id2name(name));
        }
    }
    return Qnil;
}

/*
 *  call-seq:
 *     mod.public_class_method(symbol, ...)    -> mod
 *     mod.public_class_method(string, ...)    -> mod
 *     mod.public_class_method(array)          -> mod
 *
 *  Makes a list of existing class methods public.
 *
 *  String arguments are converted to symbols.
 *  An Array of Symbols and/or Strings are also accepted.
 */

static VALUE
rb_mod_public_method(int argc, VALUE *argv, VALUE obj)
{
    set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PUBLIC);
    return obj;
}

/*
 *  call-seq:
 *     mod.private_class_method(symbol, ...)   -> mod
 *     mod.private_class_method(string, ...)   -> mod
 *     mod.private_class_method(array)         -> mod
 *
 *  Makes existing class methods private. Often used to hide the default
 *  constructor <code>new</code>.
 *
 *  String arguments are converted to symbols.
 *  An Array of Symbols and/or Strings are also accepted.
 *
 *     class SimpleSingleton  # Not thread safe
 *       private_class_method :new
 *       def SimpleSingleton.create(*args, &block)
 *         @me = new(*args, &block) if ! @me
 *         @me
 *       end
 *     end
 */

static VALUE
rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
{
    set_method_visibility(rb_singleton_class(obj), argc, argv, METHOD_VISI_PRIVATE);
    return obj;
}

/*
 *  call-seq:
 *     public
 *     public(symbol, ...)
 *     public(string, ...)
 *     public(array)
 *
 *  With no arguments, sets the default visibility for subsequently
 *  defined methods to public. With arguments, sets the named methods to
 *  have public visibility.
 *
 *  String arguments are converted to symbols.
 *  An Array of Symbols and/or Strings are also accepted.
 */

static VALUE
top_public(int argc, VALUE *argv, VALUE _)
{
    return rb_mod_public(argc, argv, rb_cObject);
}

/*
 *  call-seq:
 *     private
 *     private(symbol, ...)
 *     private(string, ...)
 *     private(array)
 *
 *  With no arguments, sets the default visibility for subsequently
 *  defined methods to private. With arguments, sets the named methods to
 *  have private visibility.
 *
 *  String arguments are converted to symbols.
 *  An Array of Symbols and/or Strings are also accepted.
 */
static VALUE
top_private(int argc, VALUE *argv, VALUE _)
{
    return rb_mod_private(argc, argv, rb_cObject);
}

/*
 *  call-seq:
 *     ruby2_keywords(method_name, ...) -> self
 *
 *  For the given method names, marks the method as passing keywords through
 *  a normal argument splat.  See Module#ruby2_keywords in detail.
 */
static VALUE
top_ruby2_keywords(int argc, VALUE *argv, VALUE module)
{
    return rb_mod_ruby2_keywords(argc, argv, rb_cObject);
}

/*
 *  call-seq:
 *     module_function(symbol, ...)    -> self
 *     module_function(string, ...)    -> self
 *
 *  Creates module functions for the named methods. These functions may
 *  be called with the module as a receiver, and also become available
 *  as instance methods to classes that mix in the module. Module
 *  functions are copies of the original, and so may be changed
 *  independently. The instance-method versions are made private. If
 *  used with no arguments, subsequently defined methods become module
 *  functions.
 *  String arguments are converted to symbols.
 *
 *     module Mod
 *       def one
 *         "This is one"
 *       end
 *       module_function :one
 *     end
 *     class Cls
 *       include Mod
 *       def call_one
 *         one
 *       end
 *     end
 *     Mod.one     #=> "This is one"
 *     c = Cls.new
 *     c.call_one  #=> "This is one"
 *     module Mod
 *       def one
 *         "This is the new one"
 *       end
 *     end
 *     Mod.one     #=> "This is one"
 *     c.call_one  #=> "This is the new one"
 */

static VALUE
rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
{
    int i;
    ID id;
    const rb_method_entry_t *me;

    if (!RB_TYPE_P(module, T_MODULE)) {
	rb_raise(rb_eTypeError, "module_function must be called for modules");
    }

    if (argc == 0) {
	rb_scope_module_func_set();
	return module;
    }

    set_method_visibility(module, argc, argv, METHOD_VISI_PRIVATE);

    for (i = 0; i < argc; i++) {
	VALUE m = module;

	id = rb_to_id(argv[i]);
	for (;;) {
	    me = search_method(m, id, 0);
	    if (me == 0) {
		me = search_method(rb_cObject, id, 0);
	    }
	    if (UNDEFINED_METHOD_ENTRY_P(me)) {
		rb_print_undef(module, id, METHOD_VISI_UNDEF);
	    }
	    if (me->def->type != VM_METHOD_TYPE_ZSUPER) {
		break; /* normal case: need not to follow 'super' link */
	    }
	    m = RCLASS_SUPER(m);
	    if (!m)
		break;
	}
	rb_method_entry_set(rb_singleton_class(module), id, me, METHOD_VISI_PUBLIC);
    }
    return module;
}

#ifdef __GNUC__
#pragma push_macro("rb_method_basic_definition_p")
#undef rb_method_basic_definition_p
#endif
int
rb_method_basic_definition_p(VALUE klass, ID id)
{
    const rb_callable_method_entry_t *cme;
    if (!klass) return TRUE; /* hidden object cannot be overridden */
    cme = rb_callable_method_entry(klass, id);
    return (cme && METHOD_ENTRY_BASIC(cme)) ? TRUE : FALSE;
}
#ifdef __GNUC__
#pragma pop_macro("rb_method_basic_definition_p")
#endif

static VALUE
call_method_entry(rb_execution_context_t *ec, VALUE defined_class, VALUE obj, ID id,
                  const rb_callable_method_entry_t *cme, int argc, const VALUE *argv, int kw_splat)
{
    VALUE passed_block_handler = vm_passed_block_handler(ec);
    VALUE result = rb_vm_call_kw(ec, obj, id, argc, argv, cme, kw_splat);
    vm_passed_block_handler_set(ec, passed_block_handler);
    return result;
}

static VALUE
basic_obj_respond_to_missing(rb_execution_context_t *ec, VALUE klass, VALUE obj,
			     VALUE mid, VALUE priv)
{
    VALUE defined_class, args[2];
    const ID rtmid = idRespond_to_missing;
    const rb_callable_method_entry_t *const cme = callable_method_entry(klass, rtmid, &defined_class);

    if (!cme || METHOD_ENTRY_BASIC(cme)) return Qundef;
    args[0] = mid;
    args[1] = priv;
    return call_method_entry(ec, defined_class, obj, rtmid, cme, 2, args, RB_NO_KEYWORDS);
}

static inline int
basic_obj_respond_to(rb_execution_context_t *ec, VALUE obj, ID id, int pub)
{
    VALUE klass = CLASS_OF(obj);
    VALUE ret;

    switch (method_boundp(klass, id, pub|BOUND_RESPONDS)) {
      case 2:
	return FALSE;
      case 0:
	ret = basic_obj_respond_to_missing(ec, klass, obj, ID2SYM(id),
					   pub ? Qfalse : Qtrue);
	return RTEST(ret) && ret != Qundef;
      default:
	return TRUE;
    }
}

static int
vm_respond_to(rb_execution_context_t *ec, VALUE klass, VALUE obj, ID id, int priv)
{
    VALUE defined_class;
    const ID resid = idRespond_to;
    const rb_callable_method_entry_t *const cme = callable_method_entry(klass, resid, &defined_class);

    if (!cme) return -1;
    if (METHOD_ENTRY_BASIC(cme)) {
	return -1;
    }
    else {
	int argc = 1;
	VALUE args[2];
	VALUE result;

	args[0] = ID2SYM(id);
	args[1] = Qtrue;
	if (priv) {
            argc = rb_method_entry_arity((const rb_method_entry_t *)cme);
	    if (argc > 2) {
		rb_raise(rb_eArgError,
			 "respond_to? must accept 1 or 2 arguments (requires %d)",
			 argc);
	    }
	    if (argc != 1) {
		argc = 2;
	    }
	    else if (!NIL_P(ruby_verbose)) {
		VALUE location = rb_method_entry_location((const rb_method_entry_t *)cme);
                rb_category_warn(RB_WARN_CATEGORY_DEPRECATED,
                        "%"PRIsVALUE"%c""respond_to?(:%"PRIsVALUE") uses"
			" the deprecated method signature, which takes one parameter",
			(FL_TEST(klass, FL_SINGLETON) ? obj : klass),
			(FL_TEST(klass, FL_SINGLETON) ? '.' : '#'),
			QUOTE_ID(id));
		if (!NIL_P(location)) {
		    VALUE path = RARRAY_AREF(location, 0);
		    VALUE line = RARRAY_AREF(location, 1);
		    if (!NIL_P(path)) {
			rb_category_compile_warn(RB_WARN_CATEGORY_DEPRECATED,
					RSTRING_PTR(path), NUM2INT(line),
					"respond_to? is defined here");
		    }
		}
	    }
	}
        result = call_method_entry(ec, defined_class, obj, resid, cme, argc, args, RB_NO_KEYWORDS);
	return RTEST(result);
    }
}

int
rb_obj_respond_to(VALUE obj, ID id, int priv)
{
    rb_execution_context_t *ec = GET_EC();
    return rb_ec_obj_respond_to(ec, obj, id, priv);
}

int
rb_ec_obj_respond_to(rb_execution_context_t *ec, VALUE obj, ID id, int priv)
{
    VALUE klass = CLASS_OF(obj);
    int ret = vm_respond_to(ec, klass, obj, id, priv);
    if (ret == -1) ret = basic_obj_respond_to(ec, obj, id, !priv);
    return ret;
}

int
rb_respond_to(VALUE obj, ID id)
{
    return rb_obj_respond_to(obj, id, FALSE);
}


/*
 *  call-seq:
 *     obj.respond_to?(symbol, include_all=false) -> true or false
 *     obj.respond_to?(string, include_all=false) -> true or false
 *
 *  Returns +true+ if _obj_ responds to the given method.  Private and
 *  protected methods are included in the search only if the optional
 *  second parameter evaluates to +true+.
 *
 *  If the method is not implemented,
 *  as Process.fork on Windows, File.lchmod on GNU/Linux, etc.,
 *  false is returned.
 *
 *  If the method is not defined, <code>respond_to_missing?</code>
 *  method is called and the result is returned.
 *
 *  When the method name parameter is given as a string, the string is
 *  converted to a symbol.
 */

static VALUE
obj_respond_to(int argc, VALUE *argv, VALUE obj)
{
    VALUE mid, priv;
    ID id;
    rb_execution_context_t *ec = GET_EC();

    rb_scan_args(argc, argv, "11", &mid, &priv);
    if (!(id = rb_check_id(&mid))) {
	VALUE ret = basic_obj_respond_to_missing(ec, CLASS_OF(obj), obj,
						 rb_to_symbol(mid), priv);
	if (ret == Qundef) ret = Qfalse;
	return ret;
    }
    if (basic_obj_respond_to(ec, obj, id, !RTEST(priv)))
	return Qtrue;
    return Qfalse;
}

/*
 *  call-seq:
 *     obj.respond_to_missing?(symbol, include_all) -> true or false
 *     obj.respond_to_missing?(string, include_all) -> true or false
 *
 *  DO NOT USE THIS DIRECTLY.
 *
 *  Hook method to return whether the _obj_ can respond to _id_ method
 *  or not.
 *
 *  When the method name parameter is given as a string, the string is
 *  converted to a symbol.
 *
 *  See #respond_to?, and the example of BasicObject.
 */
static VALUE
obj_respond_to_missing(VALUE obj, VALUE mid, VALUE priv)
{
    return Qfalse;
}

void
Init_Method(void)
{
    //
}

void
Init_eval_method(void)
{
    rb_define_method(rb_mKernel, "respond_to?", obj_respond_to, -1);
    rb_define_method(rb_mKernel, "respond_to_missing?", obj_respond_to_missing, 2);

    rb_define_method(rb_cModule, "remove_method", rb_mod_remove_method, -1);
    rb_define_method(rb_cModule, "undef_method", rb_mod_undef_method, -1);
    rb_define_method(rb_cModule, "alias_method", rb_mod_alias_method, 2);
    rb_define_private_method(rb_cModule, "public", rb_mod_public, -1);
    rb_define_private_method(rb_cModule, "protected", rb_mod_protected, -1);
    rb_define_private_method(rb_cModule, "private", rb_mod_private, -1);
    rb_define_private_method(rb_cModule, "module_function", rb_mod_modfunc, -1);
    rb_define_private_method(rb_cModule, "ruby2_keywords", rb_mod_ruby2_keywords, -1);

    rb_define_method(rb_cModule, "method_defined?", rb_mod_method_defined, -1);
    rb_define_method(rb_cModule, "public_method_defined?", rb_mod_public_method_defined, -1);
    rb_define_method(rb_cModule, "private_method_defined?", rb_mod_private_method_defined, -1);
    rb_define_method(rb_cModule, "protected_method_defined?", rb_mod_protected_method_defined, -1);
    rb_define_method(rb_cModule, "public_class_method", rb_mod_public_method, -1);
    rb_define_method(rb_cModule, "private_class_method", rb_mod_private_method, -1);

    rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
			     "public", top_public, -1);
    rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
			     "private", top_private, -1);
    rb_define_private_method(rb_singleton_class(rb_vm_top_self()),
			     "ruby2_keywords", top_ruby2_keywords, -1);

    {
#define REPLICATE_METHOD(klass, id) do { \
	    const rb_method_entry_t *me = rb_method_entry((klass), (id)); \
	    rb_method_entry_set((klass), (id), me, METHOD_ENTRY_VISI(me)); \
	} while (0)

	REPLICATE_METHOD(rb_eException, idMethodMissing);
	REPLICATE_METHOD(rb_eException, idRespond_to);
	REPLICATE_METHOD(rb_eException, idRespond_to_missing);
    }
}