summaryrefslogtreecommitdiff
path: root/Source/Modules/ruby.cxx
blob: 6aeaae5a190bc63e456815eb92e52d68664f6b47 (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
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
/* ----------------------------------------------------------------------------- 
 * This file is part of SWIG, which is licensed as a whole under version 3 
 * (or any later version) of the GNU General Public License. Some additional
 * terms also apply to certain portions of SWIG. The full details of the SWIG
 * license and copyrights can be found in the LICENSE and COPYRIGHT files
 * included with the SWIG source code as distributed by the SWIG developers
 * and at http://www.swig.org/legal.html.
 *
 * ruby.cxx
 *
 * Ruby language module for SWIG.
 * ----------------------------------------------------------------------------- */

#include "swigmod.h"
#include "cparse.h"
static int treduce = SWIG_cparse_template_reduce(0);

#define SWIG_PROTECTED_TARGET_METHODS 1

#include <ctype.h>
#include <string.h>
#include <limits.h>		/* for INT_MAX */

class RClass {
private:
  String *temp;

public:
  String *name;			/* class name (renamed) */
  String *cname;		/* original C class/struct name */
  String *mname;		/* Mangled name */

  /**
   * The C variable name used in the SWIG-generated wrapper code to refer to
   * this class; usually it is of the form "SwigClassXXX.klass", where SwigClassXXX
   * is a swig_class struct instance and klass is a member of that struct.
   */
  String *vname;

  /**
   * The C variable name used in the SWIG-generated wrapper code to refer to
   * the module that implements this class's methods (when we're trying to
   * support C++ multiple inheritance). Usually it is of the form
   * "SwigClassClassName.mImpl", where SwigClassXXX is a swig_class struct instance
   * and mImpl is a member of that struct.
   */
  String *mImpl;

  String *type;
  String *prefix;
  String *init;


  int constructor_defined;
  int destructor_defined;

   RClass() {
    temp = NewString("");
    name = NewString("");
    cname = NewString("");
    mname = NewString("");
    vname = NewString("");
    mImpl = NewString("");
    type = NewString("");
    prefix = NewString("");
    init = NewString("");
    constructor_defined = 0;
    destructor_defined = 0;
  }
  
  ~RClass() {
    Delete(name);
    Delete(cname);
    Delete(vname);
    Delete(mImpl);
    Delete(mname);
    Delete(type);
    Delete(prefix);
    Delete(init);
    Delete(temp);
  }

  void set_name(const_String_or_char_ptr cn, const_String_or_char_ptr rn, const_String_or_char_ptr valn) {
    /* Original C/C++ class (or struct) name */
    Clear(cname);
    Append(cname, cn);

    /* Mangled name */
    Delete(mname);
    mname = Swig_name_mangle(cname);

    /* Renamed class name */
    Clear(name);
    Append(name, valn);

    /* Variable name for the VALUE that refers to the Ruby Class object */
    Clear(vname);
    Printf(vname, "SwigClass%s.klass", name);

    /* Variable name for the VALUE that refers to the Ruby Class object */
    Clear(mImpl);
    Printf(mImpl, "SwigClass%s.mImpl", name);

    /* Prefix */
    Clear(prefix);
    Printv(prefix, (rn ? rn : cn), "_", NIL);
  }

  char *strip(const_String_or_char_ptr s) {
    Clear(temp);
    Append(temp, s);
    if (Strncmp(s, prefix, Len(prefix)) == 0) {
      Replaceall(temp, prefix, "");
    }
    return Char(temp);
  }
};


/* flags for the make_autodoc function */
enum autodoc_t {
  AUTODOC_CLASS,
  AUTODOC_CTOR,
  AUTODOC_DTOR,
  AUTODOC_STATICFUNC,
  AUTODOC_FUNC,
  AUTODOC_METHOD,
  AUTODOC_GETTER,
  AUTODOC_SETTER,
  AUTODOC_NONE
};

static const char *usage = "\
Ruby Options (available with -ruby)\n\
     -autorename     - Enable renaming of classes and methods to follow Ruby coding standards\n\
     -cppcast        - Enable C++ casting operators (default)\n\
     -globalmodule   - Wrap everything into the global module\n\
     -initname <name>- Set entry function to Init_<name> (used by `require')\n\
     -minherit       - Attempt to support multiple inheritance\n\
     -noautorename   - Disable renaming of classes and methods (default)\n\
     -nocppcast      - Disable C++ casting operators, useful for generating bugs\n\
     -prefix <name>  - Set a prefix <name> to be prepended to all names\n\
";


#define RCLASS(hash, name) (RClass*)(Getattr(hash, name) ? Data(Getattr(hash, name)) : 0)
#define SET_RCLASS(hash, name, klass) Setattr(hash, name, NewVoid(klass, 0))


class RUBY:public Language {
private:

  String *module;
  String *modvar;
  String *feature;
  String *prefix;
  int current;
  Hash *classes;		/* key=cname val=RClass */
  RClass *klass;		/* Currently processing class */
  Hash *special_methods;	/* Python style special method name table */

  File *f_directors;
  File *f_directors_h;
  File *f_directors_helpers;
  File *f_begin;
  File *f_runtime;
  File *f_runtime_h;
  File *f_header;
  File *f_wrappers;
  File *f_init;
  File *f_initbeforefunc;

  bool useGlobalModule;
  bool multipleInheritance;

  // Wrap modes
  enum WrapperMode {
    NO_CPP,
    MEMBER_FUNC,
    CONSTRUCTOR_ALLOCATE,
    CONSTRUCTOR_INITIALIZE,
    DESTRUCTOR,
    MEMBER_VAR,
    CLASS_CONST,
    STATIC_FUNC,
    STATIC_VAR
  };

  /* ------------------------------------------------------------
   * autodoc level declarations
   * ------------------------------------------------------------ */

  enum autodoc_l {
    NO_AUTODOC = -2,		// no autodoc
    STRING_AUTODOC = -1,	// use provided string
    NAMES_AUTODOC = 0,		// only parameter names
    TYPES_AUTODOC = 1,		// parameter names and types
    EXTEND_AUTODOC = 2,		// extended documentation and parameter names
    EXTEND_TYPES_AUTODOC = 3	// extended documentation and parameter types + names
  };

  autodoc_t last_mode;
  String*   last_autodoc;

  autodoc_l autodoc_level(String *autodoc) {
    autodoc_l dlevel = NO_AUTODOC;
    char *c = Char(autodoc);
    if (c) {
      if (isdigit(c[0])) {
	dlevel = (autodoc_l) atoi(c);
      } else {
	if (strcmp(c, "extended") == 0) {
	  dlevel = EXTEND_AUTODOC;
	} else {
	  dlevel = STRING_AUTODOC;
	}
      }
    }
    return dlevel;
  }



  /* ------------------------------------------------------------
   * have_docstring()
   *    Check if there is a docstring directive and it has text,
   *    or there is an autodoc flag set
   * ------------------------------------------------------------ */

  bool have_docstring(Node *n) {
    String *str = Getattr(n, "feature:docstring");
    return (str && Len(str) > 0) || (Getattr(n, "feature:autodoc") && !GetFlag(n, "feature:noautodoc"));
  }

  /* ------------------------------------------------------------
   * docstring()
   *    Get the docstring text, stripping off {} if neccessary,
   *    and enclose in triple double quotes.  If autodoc is also
   *    set then it will build a combined docstring.
   * ------------------------------------------------------------ */

  String *docstring(Node *n, autodoc_t ad_type) {

    String *str = Getattr(n, "feature:docstring");
    bool have_ds = (str && Len(str) > 0);
    bool have_auto = (Getattr(n, "feature:autodoc") && !GetFlag(n, "feature:noautodoc"));
    String *autodoc = NULL;
    String *doc = NULL;

    if (have_ds) {
      char *t = Char(str);
      if (*t == '{') {
	Delitem(str, 0);
	Delitem(str, DOH_END);
      }
    }

    if (have_auto) {
      autodoc = make_autodoc(n, ad_type);
      have_auto = (autodoc && Len(autodoc) > 0);
    }
    // If there is more than one line then make docstrings like this:
    //
    //      This is line1
    //      And here is line2 followed by the rest of them
    //
    // otherwise, put it all on a single line
    //
    if (have_auto && have_ds) {	// Both autodoc and docstring are present
      doc = NewString("");
      Printv(doc, "\n", autodoc, "\n", str, NIL);
    } else if (!have_auto && have_ds) {	// only docstring
      if (Strchr(str, '\n') == 0) {
	doc = NewString(str);
      } else {
	doc = NewString("");
	Printv(doc, str, NIL);
      }
    } else if (have_auto && !have_ds) {	// only autodoc
      if (Strchr(autodoc, '\n') == 0) {
	doc = NewStringf("%s", autodoc);
      } else {
	doc = NewString("");
	Printv(doc, "\n", autodoc, NIL);
      }
    } else
      doc = NewString("");

    // Save the generated strings in the parse tree in case they are used later
    // by post processing tools
    Setattr(n, "ruby:docstring", doc);
    Setattr(n, "ruby:autodoc", autodoc);
    return doc;
  }

  /* -----------------------------------------------------------------------------
   * addMissingParameterNames()
   *  For functions that have not had nameless parameters set in the Language class.
   *
   * Inputs: 
   *   plist - entire parameter list
   *   arg_offset - argument number for first parameter
   * Side effects:
   *   The "lname" attribute in each parameter in plist will be contain a parameter name
   * ----------------------------------------------------------------------------- */

  void addMissingParameterNames(ParmList *plist, int arg_offset) {
    Parm *p = plist;
    int i = arg_offset;
    while (p) {
      if (!Getattr(p, "lname")) {
	String *pname = Swig_cparm_name(p, i);
	Delete(pname);
      }
      i++;
      p = nextSibling(p);
    }
  }

  /* ------------------------------------------------------------
   * make_autodocParmList()
   *   Generate the documentation for the function parameters
   * ------------------------------------------------------------ */

  String *make_autodocParmList(Node *n, bool showTypes) {
    String *doc = NewString("");
    String *pdocs = 0;
    ParmList *plist = CopyParmList(Getattr(n, "parms"));
    Parm *p;
    Parm *pnext;
    int lines = 0;
    int start_arg_num = is_wrapping_class() ? 1 : 0;
    const int maxwidth = 80;

    addMissingParameterNames(plist, start_arg_num); // for $1_name substitutions done in Swig_typemap_attach_parms

    Swig_typemap_attach_parms("in", plist, 0);
    Swig_typemap_attach_parms("doc", plist, 0);

    if (Strcmp(ParmList_protostr(plist), "void") == 0) {
      //No parameters actually
      return doc;
    }

    for (p = plist; p; p = pnext) {

      String *tm = Getattr(p, "tmap:in");
      if (tm) {
	pnext = Getattr(p, "tmap:in:next");
	if (checkAttribute(p, "tmap:in:numinputs", "0")) {
	  continue;
	}
      } else {
	pnext = nextSibling(p);
      }

      String *name = 0;
      String *type = 0;
      String *value = 0;
      String *pdoc = Getattr(p, "tmap:doc");
      if (pdoc) {
	name = Getattr(p, "tmap:doc:name");
	type = Getattr(p, "tmap:doc:type");
	value = Getattr(p, "tmap:doc:value");
      }

      // Note: the generated name should be consistent with that in kwnames[]
      name = name ? name : Getattr(p, "name");
      name = name ? name : Getattr(p, "lname");
      name = Swig_name_make(p, 0, name, 0, 0); // rename parameter if a keyword

      type = type ? type : Getattr(p, "type");
      value = value ? value : Getattr(p, "value");

      if (SwigType_isvarargs(type))
	break;

      // Skip the 'self' parameter which in ruby is implicit
      if ( Cmp(name, "self") == 0 )
	continue;

      // Make __p parameters just p (as used in STL)
      Replace( name, "__", "", DOH_REPLACE_FIRST );

      if (Len(doc)) {
	// add a comma to the previous one if any
	Append(doc, ", ");

	// Do we need to wrap a long line?
	if ((Len(doc) - lines * maxwidth) > maxwidth) {
	  Printf(doc, "\n%s", tab4);
	  lines += 1;
	}
      }

      // Do the param type too?
      Node *nn = classLookup(Getattr(p, "type"));
      String *type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0);
      if (showTypes)
	Printf(doc, "%s ", type_str);

      Append(doc, name);
      if (pdoc) {
	if (!pdocs)
	  pdocs = NewString("Parameters:\n");
	Printf(pdocs, "    %s.\n", pdoc);
      }

      if (value) {
	String *new_value = convertValue(value, Getattr(p, "type"));
	if (new_value) {
	  value = new_value;
	} else {
	  Node *lookup = Swig_symbol_clookup(value, 0);
	  if (lookup)
	    value = Getattr(lookup, "sym:name");
	}
	Printf(doc, "=%s", value);
      }
      Delete(type_str);
      Delete(name);
    }
    if (pdocs)
      Setattr(n, "feature:pdocs", pdocs);
    Delete(plist);
    return doc;
  }

  /* ------------------------------------------------------------
   * make_autodoc()
   *    Build a docstring for the node, using parameter and other
   *    info in the parse tree.  If the value of the autodoc
   *    attribute is "0" then do not include parameter types, if
   *    it is "1" (the default) then do.  If it has some other
   *    value then assume it is supplied by the extension writer
   *    and use it directly.
   * ------------------------------------------------------------ */

  String *make_autodoc(Node *n, autodoc_t ad_type) {
    int extended = 0;
    // If the function is overloaded then this funciton is called
    // for the last one.  Rewind to the first so the docstrings are
    // in order.
    while (Getattr(n, "sym:previousSibling"))
      n = Getattr(n, "sym:previousSibling");

    Node *pn = Swig_methodclass(n);
    String* super_names = NewString(""); 
    String* class_name = Getattr(pn, "sym:name") ; 

    if ( !class_name ) {
      class_name = NewString("");
    } else {
      class_name = Copy(class_name);
      List *baselist = Getattr(pn, "bases");
      if (baselist && Len(baselist)) {
	Iterator base = First(baselist);
	while (base.item && GetFlag(base.item, "feature:ignore")) {
	  base = Next(base);
	}

	int count = 0;
	for ( ;base.item; ++count) {
	  if ( count ) Append(super_names, ", ");
	  String *basename = Getattr(base.item, "sym:name");

	  String* basenamestr = NewString(basename);
	  Node* parent = parentNode(base.item);
	  while (parent)
	  {
	    String *parent_name = Copy( Getattr(parent, "sym:name") );
	    if ( !parent_name ) {
	      Node* mod = Getattr(parent, "module");
	      if ( mod )
		parent_name = Copy( Getattr(mod, "name") );
	      if ( parent_name )
		(Char(parent_name))[0] = (char)toupper((Char(parent_name))[0]);
	    }
	    if ( parent_name ) {
	      Insert(basenamestr, 0, "::");
	      Insert(basenamestr, 0, parent_name);
	      Delete(parent_name);
	    }
	    parent = parentNode(parent);
	  }

	  Append(super_names, basenamestr );
	  Delete(basenamestr);
	  base = Next(base);
	}
      }
    }
    String* full_name;
    if ( module ) {
      full_name = NewString(module);
      if (Len(class_name) > 0)
       	Append(full_name, "::");
    }
    else
      full_name = NewString("");
    Append(full_name, class_name);

    String* symname = Getattr(n, "sym:name");
    if ( Getattr( special_methods, symname ) )
      symname = Getattr( special_methods, symname );

    String* methodName = NewString(full_name);
    Append(methodName, symname);


    // Each overloaded function will try to get documented,
    // so we keep the name of the last overloaded function and its type.
    // Documenting just from functionWrapper() is not possible as
    // sym:name has already been changed to include the class name
    if ( last_mode == ad_type && Cmp(methodName, last_autodoc) == 0 ) {
      Delete(full_name);
      Delete(class_name);
      Delete(super_names);
      Delete(methodName);
      return NewString("");
    }


    last_mode    = ad_type;
    last_autodoc = Copy(methodName);

    String *doc = NewString("/*\n");
    int counter = 0;
    bool skipAuto = false;
    Node* on = n;
    for ( ; n; ++counter ) {
      String *type_str = NULL;
      skipAuto = false;
      bool showTypes = false;
      String *autodoc = Getattr(n, "feature:autodoc");
      autodoc_l dlevel = autodoc_level(autodoc);
      switch (dlevel) {
      case NO_AUTODOC:
	break;
      case NAMES_AUTODOC:
	showTypes = false;
	break;
      case TYPES_AUTODOC:
	showTypes = true;
	break;
      case EXTEND_AUTODOC:
	extended = 1;
	showTypes = false;
	break;
      case EXTEND_TYPES_AUTODOC:
	extended = 1;
	showTypes = true;
	break;
      case STRING_AUTODOC:
	skipAuto = true;
	break;
      }

      SwigType *type = Getattr(n, "type");

      if (type) {
	if (Strcmp(type, "void") == 0) {
	  type_str = NULL;
	} else {
	  SwigType *qt = SwigType_typedef_resolve_all(type);
	  if (SwigType_isenum(qt)) {
	    type_str = NewString("int");
	  } else {
	    Node *nn = classLookup(type);
	    type_str = nn ? Copy(Getattr(nn, "sym:name")) : SwigType_str(type, 0);
	  }
	}
      }

      if (counter == 0) {
	switch (ad_type) {
	case AUTODOC_CLASS:
	  Printf(doc, "  Document-class: %s", full_name);
	  if ( Len(super_names) > 0 )
	    Printf( doc, " < %s", super_names);
	  Append(doc, "\n\n");
	  break;
	case AUTODOC_CTOR:
 	  Printf(doc, "  Document-method: %s.new\n\n", full_name);
	  break;

	case AUTODOC_DTOR:
	  break;

	case AUTODOC_STATICFUNC:
 	  Printf(doc, "  Document-method: %s.%s\n\n", full_name, symname);
	  break;

	case AUTODOC_FUNC:
	case AUTODOC_METHOD:
	case AUTODOC_GETTER:
 	  Printf(doc, "  Document-method: %s.%s\n\n", full_name, symname);
	  break;
	case AUTODOC_SETTER:
 	  Printf(doc, "  Document-method: %s.%s=\n\n", full_name, symname);
	  break;
	case AUTODOC_NONE:
	  break;
	}
      }

      if (skipAuto) {
	if ( counter == 0 ) Printf(doc, "  call-seq:\n");
	switch( ad_type )
	  {
	  case AUTODOC_STATICFUNC:
	  case AUTODOC_FUNC:
	  case AUTODOC_METHOD:
	  case AUTODOC_GETTER:
	    {
	      String *paramList = make_autodocParmList(n, showTypes);
	      if (Len(paramList))
		Printf(doc, "    %s(%s)", symname, paramList);
	      else
		Printf(doc, "    %s", symname);
	      if (type_str)
		Printf(doc, " -> %s", type_str);
	      break;
	    }
	  case AUTODOC_SETTER:
	    {
	      Printf(doc, "    %s=(x)", symname);
	      if (type_str)
	       	Printf(doc, " -> %s", type_str);
	      break;
	    }
	  default:
	    break;
	  }
      } else {
	switch (ad_type) {
	case AUTODOC_CLASS:
	  {
	    // Only do the autodoc if there isn't a docstring for the class
	    String *str = Getattr(n, "feature:docstring");
	    if (counter == 0 && (str == 0 || Len(str) == 0)) {
	      if (CPlusPlus) {
		Printf(doc, "  Proxy of C++ %s class", full_name);
	      } else {
		Printf(doc, "  Proxy of C %s struct", full_name);
	      }
	    }
	  }
	  break;
	case AUTODOC_CTOR:
	  if (counter == 0)
	    Printf(doc, "  call-seq:\n");
	  if (Strcmp(class_name, symname) == 0) {
	    String *paramList = make_autodocParmList(n, showTypes);
	    if (Len(paramList))
	      Printf(doc, "    %s.new(%s)", class_name, paramList);
	    else
	      Printf(doc, "    %s.new", class_name);
	  } else {
	    Printf(doc, "    %s.new(%s)", class_name, make_autodocParmList(n, showTypes));
	  }
	  break;

	case AUTODOC_DTOR:
	  break;

	case AUTODOC_STATICFUNC:
	case AUTODOC_FUNC:
	case AUTODOC_METHOD:
	case AUTODOC_GETTER:
	  {
	    if (counter == 0)
	      Printf(doc, "  call-seq:\n");
	    String *paramList = make_autodocParmList(n, showTypes);
	    if (Len(paramList))
	      Printf(doc, "    %s(%s)", symname, paramList);
	    else
	      Printf(doc, "    %s", symname);
	    if (type_str)
	      Printf(doc, " -> %s", type_str);
	    break;
	  }
	case AUTODOC_SETTER:
	  {
	    Printf(doc, "  call-seq:\n");
	    Printf(doc, "    %s=(x)", symname);
	    if (type_str)
	      Printf(doc, " -> %s", type_str);
	    break;
	  }
	case AUTODOC_NONE:
	  break;
	}
      }

      // if it's overloaded then get the next decl and loop around again
      n = Getattr(n, "sym:nextSibling");
      if (n)
	Append(doc, "\n");
      Delete(type_str);
    }

    Printf(doc, "\n\n");
    if (!skipAuto) {
      switch (ad_type) {
      case AUTODOC_CLASS:
      case AUTODOC_DTOR:
	break;
      case AUTODOC_CTOR:
	Printf(doc, "Class constructor.\n");
	break;
      case AUTODOC_STATICFUNC:
	Printf(doc, "A class method.\n");
	break;
      case AUTODOC_FUNC:
	Printf(doc, "A module function.\n");
	break;
      case AUTODOC_METHOD:
	Printf(doc, "An instance method.\n");
	break;
      case AUTODOC_GETTER:
	Printf(doc, "Get value of attribute.\n");
	break;
      case AUTODOC_SETTER:
	Printf(doc, "Set new value for attribute.\n");
	break;
      case AUTODOC_NONE:
	break;
      }
    }


    n = on;
    while ( n ) {
      String *autodoc = Getattr(n, "feature:autodoc");
      autodoc_l dlevel = autodoc_level(autodoc);

      switch (dlevel) {
      case NO_AUTODOC:
      case NAMES_AUTODOC:
      case TYPES_AUTODOC:
	extended = 0;
	break;
      case STRING_AUTODOC:
	extended = 2;
	Replaceall( autodoc, "$class", class_name );
	Printv(doc, autodoc, ".", NIL);
	break;
      case EXTEND_AUTODOC:
      case EXTEND_TYPES_AUTODOC:
	extended = 1;
	break;
      }


      if (extended) {
	String *pdocs = Getattr(n, "feature:pdocs");
	if (pdocs) {
	  Printv(doc, "\n\n", pdocs, NULL);
	  break;
	}
	if ( extended == 2 ) break;
      }
      n = Getattr(n, "sym:nextSibling");
    }

    Append(doc, "\n*/\n");
    Delete(full_name);
    Delete(class_name);
    Delete(super_names);
    Delete(methodName);

    return doc;
  }

  /* ------------------------------------------------------------
   * convertValue()
   *    Check if string v can be a Ruby value literal,
   *    (eg. number or string), or translate it to a Ruby literal.
   * ------------------------------------------------------------ */
  String *convertValue(String *v, SwigType *t) {
    if (v && Len(v) > 0) {
      char fc = (Char(v))[0];
      if (('0' <= fc && fc <= '9') || '\'' == fc || '"' == fc) {
	/* number or string (or maybe NULL pointer) */
	if (SwigType_ispointer(t) && Strcmp(v, "0") == 0)
	  return NewString("None");
	else
	  return v;
      }
      if (Strcmp(v, "NULL") == 0 || Strcmp(v, "nullptr") == 0)
	return SwigType_ispointer(t) ? NewString("nil") : NewString("0");
      if (Strcmp(v, "true") == 0 || Strcmp(v, "TRUE") == 0)
	return NewString("True");
      if (Strcmp(v, "false") == 0 || Strcmp(v, "FALSE") == 0)
	return NewString("False");
    }
    return 0;
  }

public:

  /* ---------------------------------------------------------------------
   * RUBY()
   *
   * Initialize member data
   * --------------------------------------------------------------------- */
  RUBY() :
    module(0),
    modvar(0),
    feature(0),
    prefix(0),
    current(0),
    classes(0),
    klass(0),
    special_methods(0),
    f_directors(0),
    f_directors_h(0),
    f_directors_helpers(0),
    f_begin(0),
    f_runtime(0),
    f_runtime_h(0),
    f_header(0),
    f_wrappers(0),
    f_init(0),
    f_initbeforefunc(0),
    useGlobalModule(false),
    multipleInheritance(false),
    last_mode(AUTODOC_NONE),
    last_autodoc(NewString("")) {
      current = NO_CPP;
      director_prot_ctor_code = NewString("");
      Printv(director_prot_ctor_code,
          "if ( $comparison ) { /* subclassed */\n",
          "  $director_new \n",
          "} else {\n", "  rb_raise(rb_eRuntimeError,\"accessing abstract class or protected constructor\"); \n", "  return Qnil;\n", "}\n", NIL);
      director_multiple_inheritance = 0;
      director_language = 1;
    }

  /* ---------------------------------------------------------------------
   * main()
   *
   * Parse command line options and initializes variables.
   * --------------------------------------------------------------------- */
  
  virtual void main(int argc, char *argv[]) {

    int cppcast = 1;
    int autorename = 0;

    /* Set location of SWIG library */
    SWIG_library_directory("ruby");

    /* Look for certain command line options */
    for (int i = 1; i < argc; i++) {
      if (argv[i]) {
	if (strcmp(argv[i], "-initname") == 0) {
	  if (argv[i + 1]) {
	    char *name = argv[i + 1];
	    feature = NewString(name);
	    Swig_mark_arg(i);
	    Swig_mark_arg(i + 1);
	    i++;
	  } else {
	    Swig_arg_error();
	  }
	}
	else if (strcmp(argv[i], "-feature") == 0) {
	  fprintf( stderr, "Warning: Ruby -feature option is deprecated, "
		   "please use -initname instead.\n");
	  if (argv[i + 1]) {
	    char *name = argv[i + 1];
	    feature = NewString(name);
	    Swig_mark_arg(i);
	    Swig_mark_arg(i + 1);
	    i++;
	  } else {
	    Swig_arg_error();
	  }
	} else if (strcmp(argv[i], "-globalmodule") == 0) {
	  useGlobalModule = true;
	  Swig_mark_arg(i);
	} else if (strcmp(argv[i], "-minherit") == 0) {
	  multipleInheritance = true;
	  director_multiple_inheritance = 1;
	  Swig_mark_arg(i);
	} else if (strcmp(argv[i], "-cppcast") == 0) {
	  cppcast = 1;
	  Swig_mark_arg(i);
	} else if (strcmp(argv[i], "-nocppcast") == 0) {
	  cppcast = 0;
	  Swig_mark_arg(i);
	} else if (strcmp(argv[i], "-autorename") == 0) {
	  autorename = 1;
	  Swig_mark_arg(i);
	} else if (strcmp(argv[i], "-noautorename") == 0) {
	  autorename = 0;
	  Swig_mark_arg(i);
	} else if (strcmp(argv[i], "-prefix") == 0) {
	  if (argv[i + 1]) {
	    char *name = argv[i + 1];
	    prefix = NewString(name);
	    Swig_mark_arg(i);
	    Swig_mark_arg(i + 1);
	    i++;
	  } else {
	    Swig_arg_error();
	  }
	} else if (strcmp(argv[i], "-help") == 0) {
	  Printf(stdout, "%s\n", usage);
	}
      }
    }

    if (cppcast) {
      /* Turn on cppcast mode */
      Preprocessor_define((DOH *) "SWIG_CPLUSPLUS_CAST", 0);
    }

    if (autorename) {
      /* Turn on the autorename mode */
      Preprocessor_define((DOH *) "SWIG_RUBY_AUTORENAME", 0);
    }

    /* Add a symbol to the parser for conditional compilation */
    Preprocessor_define("SWIGRUBY 1", 0);

    /* Add typemap definitions */
    SWIG_typemap_lang("ruby");
    SWIG_config_file("ruby.swg");
    allow_overloading();
  }

  /**
   * Generate initialization code to define the Ruby module(s),
   * accounting for nested modules as necessary.
   */
  void defineRubyModule() {
    List *modules = Split(module, ':', INT_MAX);
    if (modules != 0 && Len(modules) > 0) {
      String *mv = 0;
      Iterator m;
      m = First(modules);
      while (m.item) {
	if (Len(m.item) > 0) {
	  if (mv != 0) {
	    Printv(f_init, tab4, modvar, " = rb_define_module_under(", modvar, ", \"", m.item, "\");\n", NIL);
	  } else {
	    Printv(f_init, tab4, modvar, " = rb_define_module(\"", m.item, "\");\n", NIL);
	    mv = NewString(modvar);
	  }
	}
	m = Next(m);
      }
      Delete(mv);
      Delete(modules);
    }
  }

  void registerMagicMethods() {

    special_methods = NewHash();

    /* Python->Ruby style special method name. */
    /* Basic */
    Setattr(special_methods, "__repr__", "inspect");
    Setattr(special_methods, "__str__", "to_s");
    Setattr(special_methods, "__cmp__", "<=>");
    Setattr(special_methods, "__hash__", "hash");
    Setattr(special_methods, "__nonzero__", "nonzero?");

    /* Callable */
    Setattr(special_methods, "__call__", "call");

    /* Collection */
    Setattr(special_methods, "__len__", "length");
    Setattr(special_methods, "__getitem__", "[]");
    Setattr(special_methods, "__setitem__", "[]=");

    /* Operators */
    Setattr(special_methods, "__add__", "+");
    Setattr(special_methods, "__pos__", "+@");
    Setattr(special_methods, "__sub__", "-");
    Setattr(special_methods, "__neg__", "-@");
    Setattr(special_methods, "__mul__", "*");
    Setattr(special_methods, "__div__", "/");
    Setattr(special_methods, "__mod__", "%");
    Setattr(special_methods, "__lshift__", "<<");
    Setattr(special_methods, "__rshift__", ">>");
    Setattr(special_methods, "__and__", "&");
    Setattr(special_methods, "__or__", "|");
    Setattr(special_methods, "__xor__", "^");
    Setattr(special_methods, "__invert__", "~");
    Setattr(special_methods, "__lt__", "<");
    Setattr(special_methods, "__le__", "<=");
    Setattr(special_methods, "__gt__", ">");
    Setattr(special_methods, "__ge__", ">=");
    Setattr(special_methods, "__eq__", "==");

    /* Other numeric */
    Setattr(special_methods, "__divmod__", "divmod");
    Setattr(special_methods, "__pow__", "**");
    Setattr(special_methods, "__abs__", "abs");
    Setattr(special_methods, "__int__", "to_i");
    Setattr(special_methods, "__float__", "to_f");
    Setattr(special_methods, "__coerce__", "coerce");
  }

  /* ---------------------------------------------------------------------
   * top()
   * --------------------------------------------------------------------- */

  virtual int top(Node *n) {

    /**
     * See if any Ruby module options have been specified as options
     * to the %module directive.
     */
    Node *swigModule = Getattr(n, "module");
    if (swigModule) {
      Node *options = Getattr(swigModule, "options");
      if (options) {
	if (Getattr(options, "directors")) {
	  allow_directors();
	}
	if (Getattr(options, "dirprot")) {
	  allow_dirprot();
	}
	if (Getattr(options, "ruby_globalmodule")) {
	  useGlobalModule = true;
	}
	if (Getattr(options, "ruby_minherit")) {
	  multipleInheritance = true;
	  director_multiple_inheritance = 1;
	}
      }
    }

    /* Set comparison with none for ConstructorToFunction */


    setSubclassInstanceCheck(NewStringf("strcmp(rb_obj_classname(self), classname) != 0"));
    // setSubclassInstanceCheck(NewString("CLASS_OF(self) != cFoo.klass"));

    /* Initialize all of the output files */
    String *outfile = Getattr(n, "outfile");
    String *outfile_h = Getattr(n, "outfile_h");

    if (!outfile) {
      Printf(stderr, "Unable to determine outfile\n");
      SWIG_exit(EXIT_FAILURE);
    }

    f_begin = NewFile(outfile, "w", SWIG_output_files());
    if (!f_begin) {
      FileErrorDisplay(outfile);
      SWIG_exit(EXIT_FAILURE);
    }

    f_runtime = NewString("");
    f_init = NewString("");
    f_header = NewString("");
    f_wrappers = NewString("");
    f_directors_h = NewString("");
    f_directors = NewString("");
    f_directors_helpers = NewString("");
    f_initbeforefunc = NewString("");

    if (directorsEnabled()) {
      if (!outfile_h) {
        Printf(stderr, "Unable to determine outfile_h\n");
        SWIG_exit(EXIT_FAILURE);
      }
      f_runtime_h = NewFile(outfile_h, "w", SWIG_output_files());
      if (!f_runtime_h) {
	FileErrorDisplay(outfile_h);
	SWIG_exit(EXIT_FAILURE);
      }
    }

    /* Register file targets with the SWIG file handler */
    Swig_register_filebyname("header", f_header);
    Swig_register_filebyname("wrapper", f_wrappers);
    Swig_register_filebyname("begin", f_begin);
    Swig_register_filebyname("runtime", f_runtime);
    Swig_register_filebyname("init", f_init);
    Swig_register_filebyname("director", f_directors);
    Swig_register_filebyname("director_h", f_directors_h);
    Swig_register_filebyname("director_helpers", f_directors_helpers);
    Swig_register_filebyname("initbeforefunc", f_initbeforefunc);

    modvar = 0;
    current = NO_CPP;
    klass = 0;
    classes = NewHash();

    registerMagicMethods();

    Swig_banner(f_begin);

    Printf(f_runtime, "\n");
    Printf(f_runtime, "#define SWIGRUBY\n");

    if (directorsEnabled()) {
      Printf(f_runtime, "#define SWIG_DIRECTORS\n");
    }

    Printf(f_runtime, "\n");

    /* typedef void *VALUE */
    SwigType *value = NewSwigType(T_VOID);
    SwigType_add_pointer(value);
    SwigType_typedef(value, "VALUE");
    Delete(value);

    /* Set module name */
    set_module(Char(Getattr(n, "name")));

    if (directorsEnabled()) {
      /* Build a version of the module name for use in a C macro name. */
      String *module_macro = Copy(module);
      Replaceall(module_macro, "::", "__");

      Swig_banner(f_directors_h);
      Printf(f_directors_h, "\n");
      Printf(f_directors_h, "#ifndef SWIG_%s_WRAP_H_\n", module_macro);
      Printf(f_directors_h, "#define SWIG_%s_WRAP_H_\n\n", module_macro);
      Printf(f_directors_h, "namespace Swig {\n");
      Printf(f_directors_h, "  class Director;\n");
      Printf(f_directors_h, "}\n\n");

      Printf(f_directors_helpers, "/* ---------------------------------------------------\n");
      Printf(f_directors_helpers, " * C++ director class helpers\n");
      Printf(f_directors_helpers, " * --------------------------------------------------- */\n\n");

      Printf(f_directors, "\n\n");
      Printf(f_directors, "/* ---------------------------------------------------\n");
      Printf(f_directors, " * C++ director class methods\n");
      Printf(f_directors, " * --------------------------------------------------- */\n\n");
      if (outfile_h) {
	String *filename = Swig_file_filename(outfile_h);
	Printf(f_directors, "#include \"%s\"\n\n", filename);
	Delete(filename);
      }

      Delete(module_macro);
    }

    Printf(f_header, "#define SWIG_init    Init_%s\n", feature);
    Printf(f_header, "#define SWIG_name    \"%s\"\n\n", module);
    Printf(f_header, "static VALUE %s;\n", modvar);

    /* Start generating the initialization function */
    String* docs = docstring(n, AUTODOC_CLASS);
    Printf(f_init, "/*\n%s\n*/", docs );
    Printv(f_init, "\n", "#ifdef __cplusplus\n", "extern \"C\"\n", "#endif\n", "SWIGEXPORT void Init_", feature, "(void) {\n", "size_t i;\n", "\n", NIL);

    Printv(f_init, tab4, "SWIG_InitRuntime();\n", NIL);

    if (!useGlobalModule)
      defineRubyModule();

    Printv(f_init, "\n", "SWIG_InitializeModule(0);\n", "for (i = 0; i < swig_module.size; i++) {\n", "SWIG_define_class(swig_module.types[i]);\n", "}\n", NIL);
    Printf(f_init, "\n");

    /* Initialize code to keep track of objects */
    Printf(f_init, "SWIG_RubyInitializeTrackings();\n");

    Language::top(n);

    if (directorsEnabled()) {
      // Insert director runtime into the f_runtime file (make it occur before %header section)
      Swig_insert_file("director.swg", f_runtime);
    }

    /* Finish off our init function */
    Printf(f_init, "}\n");
    SwigType_emit_type_table(f_runtime, f_wrappers);

    /* Close all of the files */
    Dump(f_runtime, f_begin);
    Dump(f_header, f_begin);

    if (directorsEnabled()) {
      Dump(f_directors_helpers, f_begin);
      Dump(f_directors, f_begin);
      Dump(f_directors_h, f_runtime_h);
      Printf(f_runtime_h, "\n");
      Printf(f_runtime_h, "#endif\n");
      Delete(f_runtime_h);
    }

    Dump(f_wrappers, f_begin);
    Dump(f_initbeforefunc, f_begin);
    Wrapper_pretty_print(f_init, f_begin);

    Delete(f_header);
    Delete(f_wrappers);
    Delete(f_init);
    Delete(f_initbeforefunc);
    Delete(f_runtime);
    Delete(f_begin);

    return SWIG_OK;
  }

  /* -----------------------------------------------------------------------------
   * importDirective()
   * ----------------------------------------------------------------------------- */

  virtual int importDirective(Node *n) {
    String *modname = Getattr(n, "module");
    if (modname) {
      if (prefix) {
	Insert(modname, 0, prefix);
      }

      List *modules = Split(modname, ':', INT_MAX);
      if (modules && Len(modules) > 0) {
	modname = NewString("");
	String *last = NULL;
	Iterator m = First(modules);
	while (m.item) {
	  if (Len(m.item) > 0) {
	    if (last) {
	      Append(modname, "/");
	    }
	    Append(modname, m.item);
	    last = m.item;
	  }
	  m = Next(m);
	}
	Printf(f_init, "rb_require(\"%s\");\n", modname);
	Delete(modname);
      }
      Delete(modules);
    }
    return Language::importDirective(n);
  }

  /* ---------------------------------------------------------------------
   * set_module(const char *mod_name)
   *
   * Sets the module name.  Does nothing if it's already set (so it can
   * be overridden as a command line option).
   *---------------------------------------------------------------------- */

  void set_module(const char *s) {
    String *mod_name = NewString(s);
    if (module == 0) {
      /* Start with the empty string */
      module = NewString("");

      if (prefix) {
	Insert(mod_name, 0, prefix);
      }

      /* Account for nested modules */
      List *modules = Split(mod_name, ':', INT_MAX);
      if (modules != 0 && Len(modules) > 0) {
	String *last = 0;
	Iterator m = First(modules);
	while (m.item) {
	  if (Len(m.item) > 0) {
	    String *cap = NewString(m.item);
	    (Char(cap))[0] = (char)toupper((Char(cap))[0]);
	    if (last != 0) {
	      Append(module, "::");
	    }
	    Append(module, cap);
	    last = m.item;
	  }
	  m = Next(m);
	}
	if (last) {
	  if (feature == 0) {
	    feature = Copy(last);
	  }
	  (Char(last))[0] = (char)toupper((Char(last))[0]);
	  modvar = NewStringf("m%s", last);
	}
      }
      Delete(modules);
    }
    Delete(mod_name);
  }

  /* --------------------------------------------------------------------------
   * nativeWrapper()
   * -------------------------------------------------------------------------- */
  virtual int nativeWrapper(Node *n) {
    String *funcname = Getattr(n, "wrap:name");
    Swig_warning(WARN_LANG_NATIVE_UNIMPL, input_file, line_number, "Adding native function %s not supported (ignored).\n", funcname);
    return SWIG_NOWRAP;
  }

  /**
   * Process the comma-separated list of aliases (if any).
   */
  void defineAliases(Node *n, const_String_or_char_ptr iname) {
    String *aliasv = Getattr(n, "feature:alias");
    if (aliasv) {
      List *aliases = Split(aliasv, ',', INT_MAX);
      if (aliases && Len(aliases) > 0) {
	Iterator alias = First(aliases);
	while (alias.item) {
	  if (Len(alias.item) > 0) {
	    if (multipleInheritance) {
	      Printv(klass->init, tab4, "rb_define_alias(", klass->mImpl, ", \"", alias.item, "\", \"", iname, "\");\n", NIL);
	    } else {
	      Printv(klass->init, tab4, "rb_define_alias(", klass->vname, ", \"", alias.item, "\", \"", iname, "\");\n", NIL);
	    }
	  }
	  alias = Next(alias);
	}
      }
      Delete(aliases);
    }
  }

  /* ---------------------------------------------------------------------
   * create_command(Node *n, char *iname)
   *
   * Creates a new command from a C function.
   *              iname = Name of function in scripting language
   *
   * A note about what "protected" and "private" mean in Ruby:
   *
   * A private method is accessible only within the class or its subclasses,
   * and it is callable only in "function form", with 'self' (implicit or
   * explicit) as a receiver.
   *
   * A protected method is callable only from within its class, but unlike
   * a private method, it can be called with a receiver other than self, such
   * as another instance of the same class.
   * --------------------------------------------------------------------- */

  void create_command(Node *n, const_String_or_char_ptr iname) {

    String *alloc_func = Swig_name_wrapper(iname);
    String *wname = Swig_name_wrapper(iname);
    if (CPlusPlus) {
      Insert(wname, 0, "VALUEFUNC(");
      Append(wname, ")");
    }
    if (current != NO_CPP)
      iname = klass->strip(iname);
    if (Getattr(special_methods, iname)) {
      iname = GetChar(special_methods, iname);
    }

    String *s = NewString("");
    String *temp = NewString("");

#ifdef SWIG_PROTECTED_TARGET_METHODS
    const char *rb_define_method = is_public(n) ? "rb_define_method" : "rb_define_protected_method";
#else
    const char *rb_define_method = "rb_define_method";
#endif
    switch (current) {
    case MEMBER_FUNC:
      {
	if (multipleInheritance) {
	  Printv(klass->init, tab4, rb_define_method, "(", klass->mImpl, ", \"", iname, "\", ", wname, ", -1);\n", NIL);
	} else {
	  Printv(klass->init, tab4, rb_define_method, "(", klass->vname, ", \"", iname, "\", ", wname, ", -1);\n", NIL);
	}
      }
      break;
    case CONSTRUCTOR_ALLOCATE:
      Printv(s, tab4, "rb_define_alloc_func(", klass->vname, ", ", alloc_func, ");\n", NIL);
      Replaceall(klass->init, "$allocator", s);
      break;
    case CONSTRUCTOR_INITIALIZE:
      Printv(s, tab4, rb_define_method, "(", klass->vname, ", \"initialize\", ", wname, ", -1);\n", NIL);
      Replaceall(klass->init, "$initializer", s);
      break;
    case MEMBER_VAR:
      Append(temp, iname);
      /* Check for _set or _get at the end of the name. */
      if (Len(temp) > 4) {
	const char *p = Char(temp) + (Len(temp) - 4);
	if (strcmp(p, "_set") == 0) {
	  Delslice(temp, Len(temp) - 4, DOH_END);
	  Append(temp, "=");
	} else if (strcmp(p, "_get") == 0) {
	  Delslice(temp, Len(temp) - 4, DOH_END);
	}
      }
      if (multipleInheritance) {
	Printv(klass->init, tab4, "rb_define_method(", klass->mImpl, ", \"", temp, "\", ", wname, ", -1);\n", NIL);
      } else {
	Printv(klass->init, tab4, "rb_define_method(", klass->vname, ", \"", temp, "\", ", wname, ", -1);\n", NIL);
      }
      break;
    case STATIC_FUNC:
      Printv(klass->init, tab4, "rb_define_singleton_method(", klass->vname, ", \"", iname, "\", ", wname, ", -1);\n", NIL);
      break;
    case NO_CPP:
      if (!useGlobalModule) {
	Printv(s, tab4, "rb_define_module_function(", modvar, ", \"", iname, "\", ", wname, ", -1);\n", NIL);
	Printv(f_init, s, NIL);
      } else {
	Printv(s, tab4, "rb_define_global_function(\"", iname, "\", ", wname, ", -1);\n", NIL);
	Printv(f_init, s, NIL);
      }
      break;
    case DESTRUCTOR:
    case CLASS_CONST:
    case STATIC_VAR:
      assert(false);		// Should not have gotten here for these types
    default:
      assert(false);
    }

    defineAliases(n, iname);

    Delete(temp);
    Delete(s);
    Delete(wname);
    Delete(alloc_func);
  }

  /* ---------------------------------------------------------------------
   * applyInputTypemap()
   *
   * Look up the appropriate "in" typemap for this parameter (p),
   * substitute the correct strings for the $target and $input typemap
   * parameters, and dump the resulting code to the wrapper file.
   * --------------------------------------------------------------------- */

  Parm *applyInputTypemap(Parm *p, String *ln, String *source, Wrapper *f, String *symname) {
    String *tm;
    SwigType *pt = Getattr(p, "type");
    if ((tm = Getattr(p, "tmap:in"))) {
      Replaceall(tm, "$target", ln);
      Replaceall(tm, "$source", source);
      Replaceall(tm, "$input", source);
      Replaceall(tm, "$symname", symname);

      if (Getattr(p, "wrap:disown") || (Getattr(p, "tmap:in:disown"))) {
	Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN");
      } else {
	Replaceall(tm, "$disown", "0");
      }

      Setattr(p, "emit:input", Copy(source));
      Printf(f->code, "%s\n", tm);
      p = Getattr(p, "tmap:in:next");
    } else {
      Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0));
      p = nextSibling(p);
    }
    return p;
  }

  Parm *skipIgnoredArgs(Parm *p) {
    while (checkAttribute(p, "tmap:in:numinputs", "0")) {
      p = Getattr(p, "tmap:in:next");
    }
    return p;
  }

  /* ---------------------------------------------------------------------
   * marshalInputArgs()
   *
   * Process all of the arguments passed into the scripting language
   * method and convert them into C/C++ function arguments using the
   * supplied typemaps.
   * --------------------------------------------------------------------- */

  void marshalInputArgs(Node *n, ParmList *l, int numarg, int numreq, String *kwargs, bool allow_kwargs, Wrapper *f) {
    int i;
    Parm *p;
    String *tm;
    String *source;
    String *target;

    source = NewString("");
    target = NewString("");

    bool ctor_director = (current == CONSTRUCTOR_INITIALIZE && Swig_directorclass(n));

    /**
     * The 'start' value indicates which of the C/C++ function arguments
     * produced here corresponds to the first value in Ruby's argv[] array.
     * The value of start is either zero or one. If start is zero, then
     * the first argument (with name arg1) is based on the value of argv[0].
     * If start is one, then arg1 is based on the value of argv[1].
     */
    int start = (current == MEMBER_FUNC || current == MEMBER_VAR || ctor_director) ? 1 : 0;

    int varargs = emit_isvarargs(l);

    Printf(kwargs, "{ ");
    for (i = 0, p = l; i < numarg; i++) {

      p = skipIgnoredArgs(p);

      String *pn = Getattr(p, "name");
      String *ln = Getattr(p, "lname");

      /* Produce string representation of source argument */
      Clear(source);

      /* First argument is a special case */
      if (i == 0) {
	Printv(source, (start == 0) ? "argv[0]" : "self", NIL);
      } else {
	Printf(source, "argv[%d]", i - start);
      }

      /* Produce string representation of target argument */
      Clear(target);
      Printf(target, "%s", Char(ln));

      if (i >= (numreq)) {	/* Check if parsing an optional argument */
	Printf(f->code, "    if (argc > %d) {\n", i - start);
      }

      /* Record argument name for keyword argument handling */
      if (Len(pn)) {
	Printf(kwargs, "\"%s\",", pn);
      } else {
	Printf(kwargs, "\"arg%d\",", i + 1);
      }

      /* Look for an input typemap */
      p = applyInputTypemap(p, ln, source, f, Getattr(n, "name"));
      if (i >= numreq) {
	Printf(f->code, "}\n");
      }
    }

    /* Finish argument marshalling */
    Printf(kwargs, " NULL }");
    if (allow_kwargs) {
      Printv(f->locals, tab4, "const char *kwnames[] = ", kwargs, ";\n", NIL);
    }

    /* Trailing varargs */
    if (varargs) {
      if (p && (tm = Getattr(p, "tmap:in"))) {
	Clear(source);
	Printf(source, "argv[%d]", i - start);
	Replaceall(tm, "$input", source);
	Setattr(p, "emit:input", Copy(source));
	Printf(f->code, "if (argc > %d) {\n", i - start);
	Printv(f->code, tm, "\n", NIL);
	Printf(f->code, "}\n");
      }
    }

    Delete(source);
    Delete(target);
  }

  /* ---------------------------------------------------------------------
   * insertConstraintCheckingCode(ParmList *l, Wrapper *f)
   *
   * Checks each of the parameters in the parameter list for a "check"
   * typemap and (if it finds one) inserts the typemapping code into
   * the function wrapper.
   * --------------------------------------------------------------------- */

  void insertConstraintCheckingCode(ParmList *l, Wrapper *f) {
    Parm *p;
    String *tm;
    for (p = l; p;) {
      if ((tm = Getattr(p, "tmap:check"))) {
	Replaceall(tm, "$target", Getattr(p, "lname"));
	Printv(f->code, tm, "\n", NIL);
	p = Getattr(p, "tmap:check:next");
      } else {
	p = nextSibling(p);
      }
    }
  }

  /* ---------------------------------------------------------------------
   * insertCleanupCode(ParmList *l, String *cleanup)
   *
   * Checks each of the parameters in the parameter list for a "freearg"
   * typemap and (if it finds one) inserts the typemapping code into
   * the function wrapper.
   * --------------------------------------------------------------------- */

  void insertCleanupCode(ParmList *l, String *cleanup) {
    String *tm;
    for (Parm *p = l; p;) {
      if ((tm = Getattr(p, "tmap:freearg"))) {
	if (Len(tm) != 0) {
	  Replaceall(tm, "$source", Getattr(p, "lname"));
	  Printv(cleanup, tm, "\n", NIL);
	}
	p = Getattr(p, "tmap:freearg:next");
      } else {
	p = nextSibling(p);
      }
    }
  }

  /* ---------------------------------------------------------------------
   * insertArgOutputCode(ParmList *l, String *outarg, int& need_result)
   *
   * Checks each of the parameters in the parameter list for a "argout"
   * typemap and (if it finds one) inserts the typemapping code into
   * the function wrapper.
   * --------------------------------------------------------------------- */

  void insertArgOutputCode(ParmList *l, String *outarg, int &need_result) {
    String *tm;
    for (Parm *p = l; p;) {
      if ((tm = Getattr(p, "tmap:argout"))) {
	Replaceall(tm, "$source", Getattr(p, "lname"));
	Replaceall(tm, "$target", "vresult");
	Replaceall(tm, "$result", "vresult");
	Replaceall(tm, "$arg", Getattr(p, "emit:input"));
	Replaceall(tm, "$input", Getattr(p, "emit:input"));

	Printv(outarg, tm, "\n", NIL);
	need_result += 1;
	p = Getattr(p, "tmap:argout:next");
      } else {
	p = nextSibling(p);
      }
    }
  }

  /* ---------------------------------------------------------------------
   * validIdentifier()
   *
   * Is this a valid identifier in the scripting language?
   * Ruby method names can include any combination of letters, numbers
   * and underscores. A Ruby method name may optionally end with
   * a question mark ("?"), exclamation point ("!") or equals sign ("=").
   *
   * Methods whose names end with question marks are, by convention,
   * predicate methods that return true or false (e.g. Array#empty?).
   *
   * Methods whose names end with exclamation points are, by convention,
   * called bang methods that modify the instance in place (e.g. Array#sort!).
   *
   * Methods whose names end with an equals sign are attribute setters
   * (e.g. Thread#critical=).
   * --------------------------------------------------------------------- */

  virtual int validIdentifier(String *s) {
    char *c = Char(s);
    while (*c) {
      if (!(isalnum(*c) || (*c == '_') || (*c == '?') || (*c == '!') || (*c == '=')))
	return 0;
      c++;
    }
    return 1;
  }

  /* ---------------------------------------------------------------------
   * functionWrapper()
   *
   * Create a function declaration and register it with the interpreter.
   * --------------------------------------------------------------------- */

  virtual int functionWrapper(Node *n) {

    String *nodeType;
    bool destructor;

    String *symname = Copy(Getattr(n, "sym:name"));
    SwigType *t = Getattr(n, "type");
    ParmList *l = Getattr(n, "parms");
    int director_method = 0;
    String *tm;

    int need_result = 0;

    /* Ruby needs no destructor wrapper */
    if (current == DESTRUCTOR)
      return SWIG_NOWRAP;

    nodeType = Getattr(n, "nodeType");
    destructor = (!Cmp(nodeType, "destructor"));

    /* If the C++ class constructor is overloaded, we only want to
     * write out the "new" singleton method once since it is always
     * the same. (It's the "initialize" method that will handle the
     * overloading). */

    if (current == CONSTRUCTOR_ALLOCATE && Swig_symbol_isoverloaded(n) && Getattr(n, "sym:nextSibling") != 0)
      return SWIG_OK;

    String *overname = 0;
    if (Getattr(n, "sym:overloaded")) {
      overname = Getattr(n, "sym:overname");
    } else {
      if (!addSymbol(symname, n))
	return SWIG_ERROR;
    }

    String *cleanup = NewString("");
    String *outarg = NewString("");
    String *kwargs = NewString("");
    Wrapper *f = NewWrapper();

    /* Rename predicate methods */
    if (GetFlag(n, "feature:predicate")) {
      Append(symname, "?");
    }

    /* Rename bang methods */
    if (GetFlag(n, "feature:bang")) {
      Append(symname, "!");
    }

    /* Determine the name of the SWIG wrapper function */
    String *wname = Swig_name_wrapper(symname);
    if (overname && current != CONSTRUCTOR_ALLOCATE) {
      Append(wname, overname);
    }

    /* Emit arguments */
    if (current != CONSTRUCTOR_ALLOCATE) {
      emit_parameter_variables(l, f);
    }

    /* Attach standard typemaps */
    if (current != CONSTRUCTOR_ALLOCATE) {
      emit_attach_parmmaps(l, f);
    }
    Setattr(n, "wrap:parms", l);

    /* Get number of arguments */
    int numarg = emit_num_arguments(l);
    int numreq = emit_num_required(l);
    int varargs = emit_isvarargs(l);
    bool allow_kwargs = GetFlag(n, "feature:kwargs") ? true : false;

    bool ctor_director = (current == CONSTRUCTOR_INITIALIZE && Swig_directorclass(n));
    int start = (current == MEMBER_FUNC || current == MEMBER_VAR || ctor_director) ? 1 : 0;

    /* Now write the wrapper function itself */
    if (current == CONSTRUCTOR_ALLOCATE) {
      Printf(f->def, "#ifdef HAVE_RB_DEFINE_ALLOC_FUNC\n");
      Printv(f->def, "SWIGINTERN VALUE\n", wname, "(VALUE self) {", NIL);
      Printf(f->def, "#else\n");
      Printv(f->def, "SWIGINTERN VALUE\n", wname, "(int argc, VALUE *argv, VALUE self) {", NIL);
      Printf(f->def, "#endif\n");
    } else if (current == CONSTRUCTOR_INITIALIZE) {
      Printv(f->def, "SWIGINTERN VALUE\n", wname, "(int argc, VALUE *argv, VALUE self) {", NIL);
      if (!varargs) {
	Printf(f->code, "if ((argc < %d) || (argc > %d)) ", numreq - start, numarg - start);
      } else {
	Printf(f->code, "if (argc < %d) ", numreq - start);
      }
      Printf(f->code, "{rb_raise(rb_eArgError, \"wrong # of arguments(%%d for %d)\",argc); SWIG_fail;}\n", numreq - start);
    } else {

      if ( current == NO_CPP )
	{
	  String* docs = docstring(n, AUTODOC_FUNC);
	  Printf(f_wrappers, "%s", docs);
	  Delete(docs);
	}

      Printv(f->def, "SWIGINTERN VALUE\n", wname, "(int argc, VALUE *argv, VALUE self) {", NIL);
      if (!varargs) {
	Printf(f->code, "if ((argc < %d) || (argc > %d)) ", numreq - start, numarg - start);
      } else {
	Printf(f->code, "if (argc < %d) ", numreq - start);
      }
      Printf(f->code, "{rb_raise(rb_eArgError, \"wrong # of arguments(%%d for %d)\",argc); SWIG_fail;}\n", numreq - start);
    }

    /* Now walk the function parameter list and generate code */
    /* to get arguments */
    if (current != CONSTRUCTOR_ALLOCATE) {
      marshalInputArgs(n, l, numarg, numreq, kwargs, allow_kwargs, f);
    }
    // FIXME?
    if (ctor_director) {
      numarg--;
      numreq--;
    }

    /* Insert constraint checking code */
    insertConstraintCheckingCode(l, f);

    /* Insert cleanup code */
    insertCleanupCode(l, cleanup);

    /* Insert argument output code */
    insertArgOutputCode(l, outarg, need_result);

    /* if the object is a director, and the method call originated from its
     * underlying Ruby object, resolve the call by going up the c++ 
     * inheritance chain.  otherwise try to resolve the method in python.  
     * without this check an infinite loop is set up between the director and 
     * shadow class method calls.
     */

    // NOTE: this code should only be inserted if this class is the
    // base class of a director class.  however, in general we haven't
    // yet analyzed all classes derived from this one to see if they are
    // directors.  furthermore, this class may be used as the base of
    // a director class defined in a completely different module at a
    // later time, so this test must be included whether or not directorbase
    // is true.  we do skip this code if directors have not been enabled
    // at the command line to preserve source-level compatibility with
    // non-polymorphic swig.  also, if this wrapper is for a smart-pointer
    // method, there is no need to perform the test since the calling object
    // (the smart-pointer) and the director object (the "pointee") are
    // distinct.

    director_method = is_member_director(n) && !is_smart_pointer() && !destructor;
    if (director_method) {
      Wrapper_add_local(f, "director", "Swig::Director *director = 0");
      Printf(f->code, "director = dynamic_cast<Swig::Director *>(arg1);\n");
      Wrapper_add_local(f, "upcall", "bool upcall = false");
      Append(f->code, "upcall = (director && (director->swig_get_self() == self));\n");
    }

    /* Now write code to make the function call */
    if (current != CONSTRUCTOR_ALLOCATE) {
      if (current == CONSTRUCTOR_INITIALIZE) {
	Node *pn = Swig_methodclass(n);
	String *symname = Getattr(pn, "sym:name");
	String *action = Getattr(n, "wrap:action");
	if (directorsEnabled()) {
	  String *classname = NewStringf("const char *classname SWIGUNUSED = \"%s::%s\"", module, symname);
	  Wrapper_add_local(f, "classname", classname);
	}
	if (action) {
	  Printf(action, "\nDATA_PTR(self) = %s;", Swig_cresult_name());
	  if (GetFlag(pn, "feature:trackobjects")) {
	    Printf(action, "\nSWIG_RubyAddTracking(%s, self);", Swig_cresult_name());
	  }
	}
      }

      /* Emit the function call */
      if (director_method) {
	Printf(f->code, "try {\n");
      }

      Setattr(n, "wrap:name", wname);

      Swig_director_emit_dynamic_cast(n, f);
      String *actioncode = emit_action(n);

      if (director_method) {
	Printf(actioncode, "} catch (Swig::DirectorException& e) {\n");
	Printf(actioncode, "  rb_exc_raise(e.getError());\n");
	Printf(actioncode, "  SWIG_fail;\n");
	Printf(actioncode, "}\n");
      }

      /* Return value if necessary */
      if (SwigType_type(t) != T_VOID && current != CONSTRUCTOR_INITIALIZE) {
        need_result = 1;
        if (GetFlag(n, "feature:predicate")) {
          Printv(actioncode, tab4, "vresult = (", Swig_cresult_name(), " ? Qtrue : Qfalse);\n", NIL);
        } else {
          tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode);
          actioncode = 0;
          if (tm) {
            Replaceall(tm, "$result", "vresult");
            Replaceall(tm, "$source", Swig_cresult_name());
            Replaceall(tm, "$target", "vresult");

            if (GetFlag(n, "feature:new"))
              Replaceall(tm, "$owner", "SWIG_POINTER_OWN");
            else
              Replaceall(tm, "$owner", "0");

#if 1
            // FIXME: this will not try to unwrap directors returned as non-director
            //        base class pointers!

            /* New addition to unwrap director return values so that the original
             * Ruby object is returned instead. 
             */
            bool unwrap = false;
            String *decl = Getattr(n, "decl");
            int is_pointer = SwigType_ispointer_return(decl);
            int is_reference = SwigType_isreference_return(decl);
            if (is_pointer || is_reference) {
              String *type = Getattr(n, "type");
              Node *parent = Swig_methodclass(n);
              Node *modname = Getattr(parent, "module");
              Node *target = Swig_directormap(modname, type);
              if (target)
                unwrap = true;
            }
            if (unwrap) {
              Wrapper_add_local(f, "director", "Swig::Director *director = 0");
              Printf(f->code, "director = dynamic_cast<Swig::Director *>(%s);\n", Swig_cresult_name());
              Printf(f->code, "if (director) {\n");
              Printf(f->code, "  vresult = director->swig_get_self();\n");
              Printf(f->code, "} else {\n");
              Printf(f->code, "%s\n", tm);
              Printf(f->code, "}\n");
              director_method = 0;
            } else {
              Printf(f->code, "%s\n", tm);
            }
#else
            Printf(f->code, "%s\n", tm);
#endif
            Delete(tm);
          } else {
            Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s.\n", SwigType_str(t, 0));
          }
        }
      }
      if (actioncode) {
        Append(f->code, actioncode);
        Delete(actioncode);
      }
      emit_return_variable(n, t, f);
    }

    /* Extra code needed for new and initialize methods */
    if (current == CONSTRUCTOR_ALLOCATE) {
      need_result = 1;
      Printf(f->code, "VALUE vresult = SWIG_NewClassInstance(self, SWIGTYPE%s);\n", Char(SwigType_manglestr(t)));
      Printf(f->code, "#ifndef HAVE_RB_DEFINE_ALLOC_FUNC\n");
      Printf(f->code, "rb_obj_call_init(vresult, argc, argv);\n");
      Printf(f->code, "#endif\n");
    } else if (current == CONSTRUCTOR_INITIALIZE) {
      need_result = 1;
    }
    else
      {
	if ( need_result > 1 ) {
	  if ( SwigType_type(t) == T_VOID )
	    Printf(f->code, "vresult = rb_ary_new();\n");
	  else
	    {
	      Printf(f->code, "if (vresult == Qnil) vresult = rb_ary_new();\n");
	      Printf(f->code, "else vresult = SWIG_Ruby_AppendOutput( "
		     "rb_ary_new(), vresult);\n");
	    }
	}
      }

    /* Dump argument output code; */
    Printv(f->code, outarg, NIL);

    /* Dump the argument cleanup code */
    int need_cleanup = (current != CONSTRUCTOR_ALLOCATE) && (Len(cleanup) != 0);
    if (need_cleanup) {
      Printv(f->code, cleanup, NIL);
    }


    /* Look for any remaining cleanup.  This processes the %new directive */
    if (current != CONSTRUCTOR_ALLOCATE && GetFlag(n, "feature:new")) {
      tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0);
      if (tm) {
	Replaceall(tm, "$source", Swig_cresult_name());
	Printv(f->code, tm, "\n", NIL);
	Delete(tm);
      }
    }

    /* Special processing on return value. */
    tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0);
    if (tm) {
      Replaceall(tm, "$source", Swig_cresult_name());
      Printv(f->code, tm, NIL);
      Delete(tm);
    }

    if (director_method) {
      if ((tm = Swig_typemap_lookup("directorfree", n, Swig_cresult_name(), 0))) {
	Replaceall(tm, "$input", Swig_cresult_name());
	Replaceall(tm, "$result", "vresult");
	Printf(f->code, "%s\n", tm);
      }
    }


    /* Wrap things up (in a manner of speaking) */
    if (need_result) {
      if (current == CONSTRUCTOR_ALLOCATE) {
	Printv(f->code, tab4, "return vresult;\n", NIL);
      } else if (current == CONSTRUCTOR_INITIALIZE) {
	Printv(f->code, tab4, "return self;\n", NIL);
	Printv(f->code, "fail:\n", NIL);
	if (need_cleanup) {
	  Printv(f->code, cleanup, NIL);
	}
	Printv(f->code, tab4, "return Qnil;\n", NIL);
      } else {
	Wrapper_add_local(f, "vresult", "VALUE vresult = Qnil");
	Printv(f->code, tab4, "return vresult;\n", NIL);
	Printv(f->code, "fail:\n", NIL);
	if (need_cleanup) {
	  Printv(f->code, cleanup, NIL);
	}
	Printv(f->code, tab4, "return Qnil;\n", NIL);
      }
    } else {
      Printv(f->code, tab4, "return Qnil;\n", NIL);
      Printv(f->code, "fail:\n", NIL);
      if (need_cleanup) {
	Printv(f->code, cleanup, NIL);
      }
      Printv(f->code, tab4, "return Qnil;\n", NIL);
    }

    Printf(f->code, "}\n");

    /* Substitute the cleanup code */
    Replaceall(f->code, "$cleanup", cleanup);

    /* Substitute the function name */
    Replaceall(f->code, "$symname", symname);

    /* Emit the function */
    Wrapper_print(f, f_wrappers);

    /* Now register the function with the interpreter */
    if (!Swig_symbol_isoverloaded(n)) {
      create_command(n, symname);
    } else {
      if (current == CONSTRUCTOR_ALLOCATE) {
	create_command(n, symname);
      } else {
	if (!Getattr(n, "sym:nextSibling"))
	  dispatchFunction(n);
      }
    }

    Delete(kwargs);
    Delete(cleanup);
    Delete(outarg);
    DelWrapper(f);
    Delete(symname);

    return SWIG_OK;
  }

  /* ------------------------------------------------------------
   * dispatchFunction()
   * ------------------------------------------------------------ */

  void dispatchFunction(Node *n) {
    /* Last node in overloaded chain */

    int maxargs;
    String *tmp = NewString("");
    String *dispatch = Swig_overload_dispatch(n, "return %s(nargs, args, self);", &maxargs);

    /* Generate a dispatch wrapper for all overloaded functions */

    Wrapper *f = NewWrapper();
    String *symname = Getattr(n, "sym:name");
    String *wname = Swig_name_wrapper(symname);

    Printv(f->def, "SWIGINTERN VALUE ", wname, "(int nargs, VALUE *args, VALUE self) {", NIL);

    Wrapper_add_local(f, "argc", "int argc");
    bool ctor_director = (current == CONSTRUCTOR_INITIALIZE && Swig_directorclass(n));
    if (current == MEMBER_FUNC || current == MEMBER_VAR || ctor_director) {
      Printf(tmp, "VALUE argv[%d]", maxargs + 1);
    } else {
      Printf(tmp, "VALUE argv[%d]", maxargs);
    }
    Wrapper_add_local(f, "argv", tmp);
    Wrapper_add_local(f, "ii", "int ii");

    if (current == MEMBER_FUNC || current == MEMBER_VAR || ctor_director) {
      maxargs += 1;
      Printf(f->code, "argc = nargs + 1;\n");
      Printf(f->code, "argv[0] = self;\n");
      Printf(f->code, "if (argc > %d) SWIG_fail;\n", maxargs);
      Printf(f->code, "for (ii = 1; (ii < argc); ++ii) {\n");
      Printf(f->code, "argv[ii] = args[ii-1];\n");
      Printf(f->code, "}\n");
    } else {
      Printf(f->code, "argc = nargs;\n");
      Printf(f->code, "if (argc > %d) SWIG_fail;\n", maxargs);
      Printf(f->code, "for (ii = 0; (ii < argc); ++ii) {\n");
      Printf(f->code, "argv[ii] = args[ii];\n");
      Printf(f->code, "}\n");
    }

    Replaceall(dispatch, "$args", "nargs, args, self");
    Printv(f->code, dispatch, "\n", NIL);


    
    // Generate prototype list, go to first node
    Node *sibl = n;

    String* type = SwigType_str(Getattr(sibl,"type"),NULL);

    while (Getattr(sibl, "sym:previousSibling"))
      sibl = Getattr(sibl, "sym:previousSibling");	// go all the way up

    // Constructors will be treated specially
    const bool isCtor = Cmp(Getattr(sibl,"feature:new"), "1") == 0;
    const bool isMethod = ( Cmp(Getattr(sibl, "ismember"), "1") == 0 &&
			    (!isCtor) );

    // Construct real method name
    String* methodName = NewString("");
    if ( isMethod ) {
      // Sometimes a method node has no parent (SF#3034054).
      // This value is used in an exception message, so just skip the class
      // name in this case so at least we don't segfault.  This is probably
      // just working around a problem elsewhere though.
      Node *parent_node = parentNode(sibl);
      if (parent_node)
	Printv( methodName, Getattr(parent_node,"sym:name"), ".", NIL );
    }
    Append( methodName, Getattr(sibl,"sym:name" ) );
    if ( isCtor ) Append( methodName, ".new" ); 

    // Generate prototype list
    String *protoTypes = NewString("");
    do {
      Append( protoTypes, "\n\"    ");
      if ( !isCtor )  Printv( protoTypes, type, " ", NIL );
      Printv(protoTypes, methodName, NIL );
      Parm* p = Getattr(sibl, "wrap:parms");
      if (p && (current == MEMBER_FUNC || current == MEMBER_VAR || 
		ctor_director) )
	p = nextSibling(p); // skip self
      Append( protoTypes, "(" );
      while(p)
	{
 	  Append( protoTypes, SwigType_str(Getattr(p,"type"), Getattr(p,"name")) );
	  if ( ( p = nextSibling(p)) ) Append(protoTypes, ", ");
	}
      Append( protoTypes, ")\\n\"" );
    } while ((sibl = Getattr(sibl, "sym:nextSibling")));

    Append(f->code, "fail:\n");
    Printf(f->code, "Ruby_Format_OverloadedError( argc, %d, \"%s\", %s);\n", 
	   maxargs, methodName, protoTypes);
    Append(f->code, "\nreturn Qnil;\n");

    Delete(methodName);
    Delete(type);
    Delete(protoTypes);

    Printv(f->code, "}\n", NIL);
    Wrapper_print(f, f_wrappers);
    create_command(n, Char(symname));

    DelWrapper(f);
    Delete(dispatch);
    Delete(tmp);
    Delete(wname);
  }

  /* ---------------------------------------------------------------------
   * variableWrapper()
   * --------------------------------------------------------------------- */

  virtual int variableWrapper(Node *n) {
    String* docs = docstring(n, AUTODOC_GETTER);
    Printf(f_wrappers, "%s", docs);
    Delete(docs);


    char *name = GetChar(n, "name");
    char *iname = GetChar(n, "sym:name");
    SwigType *t = Getattr(n, "type");
    String *tm;
    String *getfname, *setfname;
    Wrapper *getf, *setf;

    getf = NewWrapper();
    setf = NewWrapper();

    /* create getter */
    int addfail = 0;
    String *getname = Swig_name_get(NSPACE_TODO, iname);
    getfname = Swig_name_wrapper(getname);
    Setattr(n, "wrap:name", getfname);
    Printv(getf->def, "SWIGINTERN VALUE\n", getfname, "(", NIL);
    Printf(getf->def, "VALUE self");
    Printf(getf->def, ") {");
    Wrapper_add_local(getf, "_val", "VALUE _val");

    tm = Swig_typemap_lookup("varout", n, name, 0);
    if (tm) {
      Replaceall(tm, "$result", "_val");
      Replaceall(tm, "$target", "_val");
      Replaceall(tm, "$source", name);
      /* Printv(getf->code,tm, NIL); */
      addfail = emit_action_code(n, getf->code, tm);
    } else {
      Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0));
    }
    Printv(getf->code, tab4, "return _val;\n", NIL);
    if (addfail) {
      Append(getf->code, "fail:\n");
      Append(getf->code, "  return Qnil;\n");
    }
    Append(getf->code, "}\n");

    Wrapper_print(getf, f_wrappers);

    if (!is_assignable(n)) {
      setfname = NewString("NULL");
    } else {
      /* create setter */
      String* docs = docstring(n, AUTODOC_SETTER);
      Printf(f_wrappers, "%s", docs);
      Delete(docs);

      String *setname = Swig_name_set(NSPACE_TODO, iname);
      setfname = Swig_name_wrapper(setname);
      Setattr(n, "wrap:name", setfname);
      Printv(setf->def, "SWIGINTERN VALUE\n", setfname, "(VALUE self, ", NIL);
      Printf(setf->def, "VALUE _val) {");
      tm = Swig_typemap_lookup("varin", n, name, 0);
      if (tm) {
	Replaceall(tm, "$input", "_val");
	Replaceall(tm, "$source", "_val");
	Replaceall(tm, "$target", name);
	/* Printv(setf->code,tm,"\n",NIL); */
	emit_action_code(n, setf->code, tm);
      } else {
	Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s\n", SwigType_str(t, 0));
      }
      Printv(setf->code, tab4, "return _val;\n", NIL);
      Printf(setf->code, "fail:\n");
      Printv(setf->code, tab4, "return Qnil;\n", NIL);
      Printf(setf->code, "}\n");
      Wrapper_print(setf, f_wrappers);
      Delete(setname);
    }

    /* define accessor method */
    if (CPlusPlus) {
      Insert(getfname, 0, "VALUEFUNC(");
      Append(getfname, ")");
      Insert(setfname, 0, "VALUEFUNC(");
      Append(setfname, ")");
    }

    String *s = NewString("");
    switch (current) {
    case STATIC_VAR:
      /* C++ class variable */
      Printv(s, tab4, "rb_define_singleton_method(", klass->vname, ", \"", klass->strip(iname), "\", ", getfname, ", 0);\n", NIL);
      if (!GetFlag(n, "feature:immutable")) {
	Printv(s, tab4, "rb_define_singleton_method(", klass->vname, ", \"", klass->strip(iname), "=\", ", setfname, ", 1);\n", NIL);
      }
      Printv(klass->init, s, NIL);
      break;
    default:
      /* C global variable */
      /* wrapped in Ruby module attribute */
      assert(current == NO_CPP);
      if (!useGlobalModule) {
	Printv(s, tab4, "rb_define_singleton_method(", modvar, ", \"", iname, "\", ", getfname, ", 0);\n", NIL);
	if (!GetFlag(n, "feature:immutable")) {
	  Printv(s, tab4, "rb_define_singleton_method(", modvar, ", \"", iname, "=\", ", setfname, ", 1);\n", NIL);
	}
      } else {
	Printv(s, tab4, "rb_define_global_method(\"", iname, "\", ", getfname, ", 0);\n", NIL);
	if (!GetFlag(n, "feature:immutable")) {
	  Printv(s, tab4, "rb_define_global_method(\"", iname, "=\", ", setfname, ", 1);\n", NIL);
	}
      }
      Printv(f_init, s, NIL);
      Delete(s);
      break;
    }
    Delete(getname);
    Delete(getfname);
    Delete(setfname);
    DelWrapper(setf);
    DelWrapper(getf);
    return SWIG_OK;
  }


  /* ---------------------------------------------------------------------
   * validate_const_name(char *name)
   *
   * Validate constant name.
   * --------------------------------------------------------------------- */

  char *validate_const_name(char *name, const char *reason) {
    if (!name || name[0] == '\0')
      return name;

    if (isupper(name[0]))
      return name;

    if (islower(name[0])) {
      name[0] = (char)toupper(name[0]);
      Swig_warning(WARN_RUBY_WRONG_NAME, input_file, line_number, "Wrong %s name (corrected to `%s')\n", reason, name);
      return name;
    }

    Swig_warning(WARN_RUBY_WRONG_NAME, input_file, line_number, "Wrong %s name %s\n", reason, name);

    return name;
  }

  /* ---------------------------------------------------------------------
   * constantWrapper()
   * --------------------------------------------------------------------- */

  virtual int constantWrapper(Node *n) {
    Swig_require("constantWrapper", n, "*sym:name", "type", "value", NIL);

    char *iname = GetChar(n, "sym:name");
    SwigType *type = Getattr(n, "type");
    String *rawval = Getattr(n, "rawval");
    String *value = rawval ? rawval : Getattr(n, "value");

    if (current == CLASS_CONST) {
      iname = klass->strip(iname);
    }
    validate_const_name(iname, "constant");
    SetChar(n, "sym:name", iname);

    /* Special hook for member pointer */
    if (SwigType_type(type) == T_MPOINTER) {
      String *wname = Swig_name_wrapper(iname);
      Printf(f_header, "static %s = %s;\n", SwigType_str(type, wname), value);
      value = Char(wname);
    }
    String *tm = Swig_typemap_lookup("constant", n, value, 0);
    if (!tm)
      tm = Swig_typemap_lookup("constcode", n, value, 0);
    if (tm) {
      Replaceall(tm, "$source", value);
      Replaceall(tm, "$target", iname);
      Replaceall(tm, "$symname", iname);
      Replaceall(tm, "$value", value);
      if (current == CLASS_CONST) {
	if (multipleInheritance) {
	  Replaceall(tm, "$module", klass->mImpl);
	  Printv(klass->init, tm, "\n", NIL);
	} else {
	  Replaceall(tm, "$module", klass->vname);
	  Printv(klass->init, tm, "\n", NIL);
	}
      } else {
	if (!useGlobalModule) {
	  Replaceall(tm, "$module", modvar);
	} else {
	  Replaceall(tm, "$module", "rb_cObject");
	}
	Printf(f_init, "%s\n", tm);
      }
    } else {
      Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value %s = %s\n", SwigType_str(type, 0), value);
    }
    Swig_restore(n);
    return SWIG_OK;
  }

  /* -----------------------------------------------------------------------------
   * classDeclaration() 
   *
   * Records information about classes---even classes that might be defined in
   * other modules referenced by %import.
   * ----------------------------------------------------------------------------- */

  virtual int classDeclaration(Node *n) {
    if (!Getattr(n, "feature:onlychildren")) {
      String *name = Getattr(n, "name");
      String *symname = Getattr(n, "sym:name");

      String *namestr = SwigType_namestr(name);
      klass = RCLASS(classes, Char(namestr));
      if (!klass) {
	klass = new RClass();
	String *valid_name = NewString(symname ? symname : namestr);
	validate_const_name(Char(valid_name), "class");
	klass->set_name(namestr, symname, valid_name);
	SET_RCLASS(classes, Char(namestr), klass);
	Delete(valid_name);
      }
      Delete(namestr);
    }
    return Language::classDeclaration(n);
  }

  /**
   * Process the comma-separated list of mixed-in module names (if any).
   */
  void includeRubyModules(Node *n) {
    String *mixin = Getattr(n, "feature:mixin");
    if (mixin) {
      List *modules = Split(mixin, ',', INT_MAX);
      if (modules && Len(modules) > 0) {
	Iterator mod = First(modules);
	while (mod.item) {
	  if (Len(mod.item) > 0) {
	    Printf(klass->init, "rb_include_module(%s, rb_eval_string(\"%s\"));\n", klass->vname, mod.item);
	  }
	  mod = Next(mod);
	}
      }
      Delete(modules);
    }
  }

  void handleBaseClasses(Node *n) {
    List *baselist = Getattr(n, "bases");
    if (baselist && Len(baselist)) {
      Iterator base = First(baselist);
      while (base.item && GetFlag(base.item, "feature:ignore")) {
	base = Next(base);
      }
      while (base.item) {
	String *basename = Getattr(base.item, "name");
	String *basenamestr = SwigType_namestr(basename);
	RClass *super = RCLASS(classes, Char(basenamestr));
	Delete(basenamestr);
	if (super) {
	  SwigType *btype = NewString(basename);
	  SwigType_add_pointer(btype);
	  SwigType_remember(btype);
	  if (multipleInheritance) {
	    String *bmangle = SwigType_manglestr(btype);
	    Insert(bmangle, 0, "((swig_class *) SWIGTYPE");
	    Append(bmangle, "->clientdata)->mImpl");
	    Printv(klass->init, "rb_include_module(", klass->mImpl, ", ", bmangle, ");\n", NIL);
	    Delete(bmangle);
	  } else {
	    String *bmangle = SwigType_manglestr(btype);
	    Insert(bmangle, 0, "((swig_class *) SWIGTYPE");
	    Append(bmangle, "->clientdata)->klass");
	    Replaceall(klass->init, "$super", bmangle);
	    Delete(bmangle);
	  }
	  Delete(btype);
	}
	base = Next(base);
	while (base.item && GetFlag(base.item, "feature:ignore")) {
	  base = Next(base);
	}
	if (!multipleInheritance) {
	  /* Warn about multiple inheritance for additional base class(es) */
	  while (base.item) {
	    if (GetFlag(base.item, "feature:ignore")) {
	      base = Next(base);
	      continue;
	    }
	    String *proxyclassname = SwigType_str(Getattr(n, "classtypeobj"), 0);
	    String *baseclassname = SwigType_str(Getattr(base.item, "name"), 0);
	    Swig_warning(WARN_RUBY_MULTIPLE_INHERITANCE, Getfile(n), Getline(n),
			 "Warning for %s proxy: Base %s ignored. Multiple inheritance is not supported in Ruby.\n", proxyclassname, baseclassname);
	    base = Next(base);
	  }
	}
      }
    }
  }

  /**
   * Check to see if a %markfunc was specified.
   */
  void handleMarkFuncDirective(Node *n) {
    String *markfunc = Getattr(n, "feature:markfunc");
    if (markfunc) {
      Printf(klass->init, "SwigClass%s.mark = (void (*)(void *)) %s;\n", klass->name, markfunc);
    } else {
      Printf(klass->init, "SwigClass%s.mark = 0;\n", klass->name);
    }
  }

  /**
   * Check to see if a %freefunc was specified.
   */
  void handleFreeFuncDirective(Node *n) {
    String *freefunc = Getattr(n, "feature:freefunc");
    if (freefunc) {
      Printf(klass->init, "SwigClass%s.destroy = (void (*)(void *)) %s;\n", klass->name, freefunc);
    } else {
      if (klass->destructor_defined) {
	Printf(klass->init, "SwigClass%s.destroy = (void (*)(void *)) free_%s;\n", klass->name, klass->mname);
      }
    }
  }

  /**
   * Check to see if tracking is enabled for this class.
   */
  void handleTrackDirective(Node *n) {
    int trackObjects = GetFlag(n, "feature:trackobjects");
    if (trackObjects) {
      Printf(klass->init, "SwigClass%s.trackObjects = 1;\n", klass->name);
    } else {
      Printf(klass->init, "SwigClass%s.trackObjects = 0;\n", klass->name);
    }
  }

  /* ----------------------------------------------------------------------
   * classHandler()
   * ---------------------------------------------------------------------- */

  virtual int classHandler(Node *n) {
    String* docs = docstring(n, AUTODOC_CLASS);
    Printf(f_wrappers, "%s", docs);
    Delete(docs);

    String *name = Getattr(n, "name");
    String *symname = Getattr(n, "sym:name");
    String *namestr = SwigType_namestr(name);	// does template expansion

    klass = RCLASS(classes, Char(namestr));
    assert(klass != 0);
    Delete(namestr);
    String *valid_name = NewString(symname);
    validate_const_name(Char(valid_name), "class");

    Clear(klass->type);
    Printv(klass->type, Getattr(n, "classtype"), NIL);
    Printv(f_wrappers, "static swig_class SwigClass", valid_name, ";\n\n", NIL);
    Printv(klass->init, "\n", tab4, NIL);

    if (!useGlobalModule) {
      Printv(klass->init, klass->vname, " = rb_define_class_under(", modvar, ", \"", klass->name, "\", $super);\n", NIL);
    } else {
      Printv(klass->init, klass->vname, " = rb_define_class(\"", klass->name, 
	     "\", $super);\n", NIL);
    }

    if (multipleInheritance) {
      Printv(klass->init, klass->mImpl, " = rb_define_module_under(", klass->vname, ", \"Impl\");\n", NIL);
    }

    SwigType *tt = NewString(name);
    SwigType_add_pointer(tt);
    SwigType_remember(tt);
    String *tm = SwigType_manglestr(tt);
    Printf(klass->init, "SWIG_TypeClientData(SWIGTYPE%s, (void *) &SwigClass%s);\n", tm, valid_name);
    Delete(tm);
    Delete(tt);
    Delete(valid_name);

    includeRubyModules(n);

    Printv(klass->init, "$allocator", NIL);
    Printv(klass->init, "$initializer", NIL);

    Language::classHandler(n);

    handleBaseClasses(n);
    handleMarkFuncDirective(n);
    handleFreeFuncDirective(n);
    handleTrackDirective(n);

    if (multipleInheritance) {
      Printv(klass->init, "rb_include_module(", klass->vname, ", ", klass->mImpl, ");\n", NIL);
    }

    String *s = NewString("");
    Printv(s, tab4, "rb_undef_alloc_func(", klass->vname, ");\n", NIL);
    Replaceall(klass->init, "$allocator", s);
    Replaceall(klass->init, "$initializer", "");

    if (GetFlag(n, "feature:exceptionclass")) {
      Replaceall(klass->init, "$super", "rb_eRuntimeError");
    } else {
      Replaceall(klass->init, "$super", "rb_cObject");
    }
    Delete(s);

    Printv(f_init, klass->init, NIL);
    klass = 0;
    return SWIG_OK;
  }

  /* ----------------------------------------------------------------------
   * memberfunctionHandler()
   *
   * Method for adding C++ member function
   *
   * By default, we're going to create a function of the form :
   *
   *         Foo_bar(this,args)
   *
   * Where Foo is the classname, bar is the member name and the this pointer
   * is explicitly attached to the beginning.
   *
   * The renaming only applies to the member function part, not the full
   * classname.
   *
   * --------------------------------------------------------------------- */

  virtual int memberfunctionHandler(Node *n) {
    current = MEMBER_FUNC;

    String* docs = docstring(n, AUTODOC_METHOD);
    Printf(f_wrappers, "%s", docs);
    Delete(docs);

    Language::memberfunctionHandler(n);
    current = NO_CPP;
    return SWIG_OK;
  }

  /* ---------------------------------------------------------------------
   * constructorHandler()
   *
   * Method for adding C++ member constructor
   * -------------------------------------------------------------------- */

  void set_director_ctor_code(Node *n) {
    /* director ctor code is specific for each class */
    Delete(director_prot_ctor_code);
    director_prot_ctor_code = NewString("");
    Node *pn = Swig_methodclass(n);
    String *symname = Getattr(pn, "sym:name");
    String *name = Copy(symname);
    char *cname = Char(name);
    if (cname)
      cname[0] = (char)toupper(cname[0]);
    Printv(director_prot_ctor_code,
	   "if ( $comparison ) { /* subclassed */\n",
	   "  $director_new \n",
	   "} else {\n", "  rb_raise(rb_eNameError,\"accessing abstract class or protected constructor\"); \n", "  return Qnil;\n", "}\n", NIL);
    Delete(director_ctor_code);
    director_ctor_code = NewString("");
    Printv(director_ctor_code, "if ( $comparison ) { /* subclassed */\n", "  $director_new \n", "} else {\n", "  $nondirector_new \n", "}\n", NIL);
    Delete(name);
  }

  virtual int constructorHandler(Node *n) {
    int use_director = Swig_directorclass(n);
    if (use_director) {
      set_director_ctor_code(n);
    }

    /* First wrap the allocate method */
    current = CONSTRUCTOR_ALLOCATE;
    Swig_name_register("construct", "%n%c_allocate");

    Language::constructorHandler(n);

    String* docs = docstring(n, AUTODOC_CTOR);
    Printf(f_wrappers, "%s", docs);
    Delete(docs);

    /* 
     * If we're wrapping the constructor of a C++ director class, prepend a new parameter
     * to receive the scripting language object (e.g. 'self')
     *
     */
    Swig_save("ruby:constructorHandler", n, "parms", NIL);
    if (use_director) {
      Parm *parms = Getattr(n, "parms");
      Parm *self;
      String *name = NewString("self");
      String *type = NewString("VALUE");
      self = NewParm(type, name, n);
      Delete(type);
      Delete(name);
      Setattr(self, "lname", "Qnil");
      if (parms)
	set_nextSibling(self, parms);
      Setattr(n, "parms", self);
      Setattr(n, "wrap:self", "1");
      Delete(self);
    }

    /* Now do the instance initialize method */
    current = CONSTRUCTOR_INITIALIZE;
    Swig_name_register("construct", "new_%n%c");
    Language::constructorHandler(n);

    /* Restore original parameter list */
    Delattr(n, "wrap:self");
    Swig_restore(n);

    /* Done */
    Swig_name_unregister("construct");
    current = NO_CPP;
    klass->constructor_defined = 1;
    return SWIG_OK;
  }

  virtual int copyconstructorHandler(Node *n) {
    int use_director = Swig_directorclass(n);
    if (use_director) {
      set_director_ctor_code(n);
    }

    /* First wrap the allocate method */
    current = CONSTRUCTOR_ALLOCATE;
    Swig_name_register("construct", "%n%c_allocate");

    return Language::copyconstructorHandler(n);
  }


  /* ---------------------------------------------------------------------
   * destructorHandler()
   * -------------------------------------------------------------------- */

  virtual int destructorHandler(Node *n) {

    /* Do no spit free function if user defined his own for this class */
    Node *pn = Swig_methodclass(n);
    String *freefunc = Getattr(pn, "feature:freefunc");
    if (freefunc) return SWIG_OK;

    current = DESTRUCTOR;
    Language::destructorHandler(n);

    freefunc = NewString("");
    String *freebody = NewString("");
    String *pname0 = Swig_cparm_name(0, 0);

    Printv(freefunc, "free_", klass->mname, NIL);
    Printv(freebody, "SWIGINTERN void\n", freefunc, "(", klass->type, " *", pname0, ") {\n", tab4, NIL);

    /* Check to see if object tracking is activated for the class
       that owns this destructor. */
    if (GetFlag(pn, "feature:trackobjects")) {
      Printf(freebody, "SWIG_RubyRemoveTracking(%s);\n", pname0);
      Printv(freebody, tab4, NIL);
    }

    if (Extend) {
      String *wrap = Getattr(n, "wrap:code");
      if (wrap) {
	Printv(f_wrappers, wrap, NIL);
      }
      /*    Printv(freebody, Swig_name_destroy(name), "(", pname0, ")", NIL); */
      Printv(freebody, Getattr(n, "wrap:action"), "\n", NIL);
    } else {
      String *action = Getattr(n, "wrap:action");
      if (action) {
	Printv(freebody, action, "\n", NIL);
      } else {
	/* In the case swig emits no destroy function. */
	if (CPlusPlus)
	  Printf(freebody, "delete %s;\n", pname0);
	else
	  Printf(freebody, "free((char*) %s);\n", pname0);
      }
    }

    Printv(freebody, "}\n\n", NIL);

    Printv(f_wrappers, freebody, NIL);

    klass->destructor_defined = 1;
    current = NO_CPP;
    Delete(freefunc);
    Delete(freebody);
    Delete(pname0);
    return SWIG_OK;
  }

  /* ---------------------------------------------------------------------
   * membervariableHandler()
   *
   * This creates a pair of functions to set/get the variable of a member.
   * -------------------------------------------------------------------- */

  virtual int membervariableHandler(Node *n) {
    String* docs = docstring(n, AUTODOC_GETTER);
    Printf(f_wrappers, "%s", docs);
    Delete(docs);

    if (is_assignable(n)) {
      String* docs = docstring(n, AUTODOC_SETTER);
      Printf(f_wrappers, "%s", docs);
      Delete(docs);
    }

    current = MEMBER_VAR;
    Language::membervariableHandler(n);
    current = NO_CPP;
    return SWIG_OK;
  }

  /* -----------------------------------------------------------------------
   * staticmemberfunctionHandler()
   *
   * Wrap a static C++ function
   * ---------------------------------------------------------------------- */

  virtual int staticmemberfunctionHandler(Node *n) {
    String* docs = docstring(n, AUTODOC_STATICFUNC);
    Printf(f_wrappers, "%s", docs);
    Delete(docs);

    current = STATIC_FUNC;
    Language::staticmemberfunctionHandler(n);
    current = NO_CPP;
    return SWIG_OK;
  }

  /* ----------------------------------------------------------------------
   * memberconstantHandler()
   *
   * Create a C++ constant
   * --------------------------------------------------------------------- */

  virtual int memberconstantHandler(Node *n) {
    String* docs = docstring(n, AUTODOC_STATICFUNC);
    Printf(f_wrappers, "%s", docs);
    Delete(docs);

    current = CLASS_CONST;
    Language::memberconstantHandler(n);
    current = NO_CPP;
    return SWIG_OK;
  }

  /* ---------------------------------------------------------------------
   * staticmembervariableHandler()
   * --------------------------------------------------------------------- */

  virtual int staticmembervariableHandler(Node *n) {
    String* docs = docstring(n, AUTODOC_GETTER);
    Printf(f_wrappers, "%s", docs);
    Delete(docs);

    if (is_assignable(n)) {
      String* docs = docstring(n, AUTODOC_SETTER);
      Printf(f_wrappers, "%s", docs);
      Delete(docs);
    }

    current = STATIC_VAR;
    Language::staticmembervariableHandler(n);
    current = NO_CPP;
    return SWIG_OK;
  }

  /* C++ director class generation */
  virtual int classDirector(Node *n) {
    return Language::classDirector(n);
  }

  virtual int classDirectorInit(Node *n) {
    String *declaration;
    declaration = Swig_director_declaration(n);
    Printf(f_directors_h, "\n");
    Printf(f_directors_h, "%s\n", declaration);
    Printf(f_directors_h, "public:\n");
    Delete(declaration);
    return Language::classDirectorInit(n);
  }

  virtual int classDirectorEnd(Node *n) {
    Printf(f_directors_h, "};\n\n");
    return Language::classDirectorEnd(n);
  }

  /* ------------------------------------------------------------
   * classDirectorConstructor()
   * ------------------------------------------------------------ */

  virtual int classDirectorConstructor(Node *n) {
    Node *parent = Getattr(n, "parentNode");
    String *sub = NewString("");
    String *decl = Getattr(n, "decl");
    String *supername = Swig_class_name(parent);
    String *classname = NewString("");
    Printf(classname, "SwigDirector_%s", supername);

    /* insert self parameter */
    Parm *p;
    ParmList *superparms = Getattr(n, "parms");
    ParmList *parms = CopyParmList(superparms);
    String *type = NewString("VALUE");
    p = NewParm(type, NewString("self"), n);
    set_nextSibling(p, parms);
    parms = p;

    if (!Getattr(n, "defaultargs")) {
      /* constructor */
      {
	Wrapper *w = NewWrapper();
	String *call;
	String *basetype = Getattr(parent, "classtype");
	String *target = Swig_method_decl(0, decl, classname, parms, 0, 0);
	call = Swig_csuperclass_call(0, basetype, superparms);
	Printf(w->def, "%s::%s: %s, Swig::Director(self) { }", classname, target, call);
	Delete(target);
	Wrapper_print(w, f_directors);
	Delete(call);
	DelWrapper(w);
      }

      /* constructor header */
      {
	String *target = Swig_method_decl(0, decl, classname, parms, 0, 1);
	Printf(f_directors_h, "    %s;\n", target);
	Delete(target);
      }
    }

    Delete(sub);
    Delete(classname);
    Delete(supername);
    Delete(parms);
    return Language::classDirectorConstructor(n);
  }

  /* ------------------------------------------------------------
   * classDirectorDefaultConstructor()
   * ------------------------------------------------------------ */

  virtual int classDirectorDefaultConstructor(Node *n) {
    String *classname;
    Wrapper *w;
    classname = Swig_class_name(n);
    w = NewWrapper();
    Printf(w->def, "SwigDirector_%s::SwigDirector_%s(VALUE self) : Swig::Director(self) { }", classname, classname);
    Wrapper_print(w, f_directors);
    DelWrapper(w);
    Printf(f_directors_h, "    SwigDirector_%s(VALUE self);\n", classname);
    Delete(classname);
    return Language::classDirectorDefaultConstructor(n);
  }

  /* ---------------------------------------------------------------
   * exceptionSafeMethodCall()
   *
   * Emit a virtual director method to pass a method call on to the 
   * underlying Ruby instance.
   *
   * --------------------------------------------------------------- */

  void exceptionSafeMethodCall(String *className, Node *n, Wrapper *w, int argc, String *args, bool initstack) {
    Wrapper *body = NewWrapper();
    Wrapper *rescue = NewWrapper();

    String *methodName = Getattr(n, "sym:name");

    String *bodyName = NewStringf("%s_%s_body", className, methodName);
    String *rescueName = NewStringf("%s_%s_rescue", className, methodName);
    String *depthCountName = NewStringf("%s_%s_call_depth", className, methodName);

    // Check for an exception typemap of some kind
    String *tm = Swig_typemap_lookup("director:except", n, Swig_cresult_name(), 0);
    if (!tm) {
      tm = Getattr(n, "feature:director:except");
    }

    if ((tm != 0) && (Len(tm) > 0) && (Strcmp(tm, "1") != 0)) {
      // Declare a global to hold the depth count
      if (!Getattr(n, "sym:nextSibling")) {
	Printf(body->def, "static int %s = 0;\n", depthCountName);

	// Function body
	Printf(body->def, "VALUE %s(VALUE data) {\n", bodyName);
	Wrapper_add_localv(body, "args", "Swig::body_args *", "args", "= reinterpret_cast<Swig::body_args *>(data)", NIL);
	Wrapper_add_localv(body, Swig_cresult_name(), "VALUE", Swig_cresult_name(), "= Qnil", NIL);
	Printf(body->code, "%s++;\n", depthCountName);
	Printv(body->code, Swig_cresult_name(), " = rb_funcall2(args->recv, args->id, args->argc, args->argv);\n", NIL);
	Printf(body->code, "%s--;\n", depthCountName);
	Printv(body->code, "return ", Swig_cresult_name(), ";\n", NIL);
	Printv(body->code, "}", NIL);

	// Exception handler
	Printf(rescue->def, "VALUE %s(VALUE args, VALUE error) {\n", rescueName);
	Replaceall(tm, "$error", "error");
	Printf(rescue->code, "%s--;\n", depthCountName);
	Printf(rescue->code, "if (%s == 0) ", depthCountName);
	Printv(rescue->code, Str(tm), "\n", NIL);
	Printv(rescue->code, "rb_exc_raise(error);\n", NIL);
	Printv(rescue->code, "}", NIL);
      }

      // Main code
      Wrapper_add_localv(w, "args", "Swig::body_args", "args", NIL);
      Wrapper_add_localv(w, "status", "int", "status", NIL);
      Printv(w->code, "args.recv = swig_get_self();\n", NIL);
      Printf(w->code, "args.id = rb_intern(\"%s\");\n", methodName);
      Printf(w->code, "args.argc = %d;\n", argc);
      if (argc > 0) {
	Printf(w->code, "args.argv = new VALUE[%d];\n", argc);
	for (int i = 0; i < argc; i++) {
	  Printf(w->code, "args.argv[%d] = obj%d;\n", i, i);
	}
      } else {
	Printv(w->code, "args.argv = 0;\n", NIL);
      }
      Printf(w->code, "%s = rb_protect(PROTECTFUNC(%s), reinterpret_cast<VALUE>(&args), &status);\n", Swig_cresult_name(), bodyName);
      if ( initstack ) Printf(w->code, "SWIG_RELEASE_STACK;\n");
      Printf(w->code, "if (status) {\n");
      Printf(w->code, "VALUE lastErr = rb_gv_get(\"$!\");\n");
      Printf(w->code, "%s(reinterpret_cast<VALUE>(&args), lastErr);\n", rescueName);
      Printf(w->code, "}\n");
      if (argc > 0) {
	Printv(w->code, "delete [] args.argv;\n", NIL);
      }
      // Dump wrapper code
      Wrapper_print(body, f_directors_helpers);
      Wrapper_print(rescue, f_directors_helpers);
    } else {
      if (argc > 0) {
	Printf(w->code, "%s = rb_funcall(swig_get_self(), rb_intern(\"%s\"), %d%s);\n", Swig_cresult_name(), methodName, argc, args);
      } else {
	Printf(w->code, "%s = rb_funcall(swig_get_self(), rb_intern(\"%s\"), 0, NULL);\n", Swig_cresult_name(), methodName);
      }
      if ( initstack ) Printf(w->code, "SWIG_RELEASE_STACK;\n");
    }

    // Clean up
    Delete(bodyName);
    Delete(rescueName);
    Delete(depthCountName);
    DelWrapper(body);
    DelWrapper(rescue);
  }

  virtual int classDirectorMethod(Node *n, Node *parent, String *super) {
    int is_void = 0;
    int is_pointer = 0;
    String *decl = Getattr(n, "decl");
    String *name = Getattr(n, "name");
    String *classname = Getattr(parent, "sym:name");
    String *c_classname = Getattr(parent, "name");
    String *symname = Getattr(n, "sym:name");
    String *declaration = NewString("");
    ParmList *l = Getattr(n, "parms");
    Wrapper *w = NewWrapper();
    String *tm;
    String *wrap_args = NewString("");
    String *returntype = Getattr(n, "type");
    Parm *p;
    String *value = Getattr(n, "value");
    String *storage = Getattr(n, "storage");
    bool pure_virtual = false;
    int status = SWIG_OK;
    int idx;
    bool ignored_method = GetFlag(n, "feature:ignore") ? true : false;
    bool asvoid = checkAttribute( n, "feature:numoutputs", "0") ? true : false;
    bool initstack = checkAttribute( n, "feature:initstack", "1") ? true : false;

    if (Cmp(storage, "virtual") == 0) {
      if (Cmp(value, "0") == 0) {
	pure_virtual = true;
      }
    }
    String *overnametmp = NewString(Getattr(n, "sym:name"));
    if (Getattr(n, "sym:overloaded")) {
      Printf(overnametmp, "::%s", Getattr(n, "sym:overname"));
    }

    /* determine if the method returns a pointer */
    is_pointer = SwigType_ispointer_return(decl);
    is_void = (!Cmp(returntype, "void") && !is_pointer);

    /* virtual method definition */
    String *target;
    String *pclassname = NewStringf("SwigDirector_%s", classname);
    String *qualified_name = NewStringf("%s::%s", pclassname, name);
    SwigType *rtype = Getattr(n, "conversion_operator") ? 0 : Getattr(n, "classDirectorMethods:type");
    target = Swig_method_decl(rtype, decl, qualified_name, l, 0, 0);
    Printf(w->def, "%s", target);
    Delete(qualified_name);
    Delete(target);
    /* header declaration */
    target = Swig_method_decl(rtype, decl, name, l, 0, 1);
    Printf(declaration, "    virtual %s", target);
    Delete(target);

    // Get any exception classes in the throws typemap
    ParmList *throw_parm_list = 0;

    if ((throw_parm_list = Getattr(n, "throws")) || Getattr(n, "throw")) {
      Parm *p;
      int gencomma = 0;

      Append(w->def, " throw(");
      Append(declaration, " throw(");

      if (throw_parm_list)
	Swig_typemap_attach_parms("throws", throw_parm_list, 0);
      for (p = throw_parm_list; p; p = nextSibling(p)) {
	if (Getattr(p, "tmap:throws")) {
	  if (gencomma++) {
	    Append(w->def, ", ");
	    Append(declaration, ", ");
	  }

	  Printf(w->def, "%s", SwigType_str(Getattr(p, "type"), 0));
	  Printf(declaration, "%s", SwigType_str(Getattr(p, "type"), 0));
	}
      }

      Append(w->def, ")");
      Append(declaration, ")");
    }

    Append(w->def, " {");
    Append(declaration, ";\n");

    if (initstack && !(ignored_method && !pure_virtual)) {
      Append(w->def, "\nSWIG_INIT_STACK;\n");
    }

    /* declare method return value 
     * if the return value is a reference or const reference, a specialized typemap must
     * handle it, including declaration of c_result ($result).
     */
    if (!is_void) {
      if (!(ignored_method && !pure_virtual)) {
	Wrapper_add_localv(w, "c_result", SwigType_lstr(returntype, "c_result"), NIL);
      }
    }

    if (ignored_method) {
      if (!pure_virtual) {
	if (!is_void)
	  Printf(w->code, "return ");
	String *super_call = Swig_method_call(super, l);
	Printf(w->code, "%s;\n", super_call);
	Delete(super_call);
      } else {
	Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"Attempted to invoke pure virtual method %s::%s\");\n", SwigType_namestr(c_classname),
	       SwigType_namestr(name));
      }
    } else {
      /* attach typemaps to arguments (C/C++ -> Ruby) */
      String *arglist = NewString("");

      Swig_director_parms_fixup(l);

      Swig_typemap_attach_parms("in", l, 0);
      Swig_typemap_attach_parms("directorin", l, 0);
      Swig_typemap_attach_parms("directorargout", l, w);

      char source[256];

      int outputs = 0;
      if (!is_void && !asvoid)
	outputs++;

      /* build argument list and type conversion string */
      idx = 0; p = l;
      while ( p ) {

	if (Getattr(p, "tmap:ignore")) {
	  p = Getattr(p, "tmap:ignore:next");
	  continue;
	}

	if (Getattr(p, "tmap:directorargout") != 0)
	  outputs++;

	if ( checkAttribute( p, "tmap:in:numinputs", "0") ) {
	  p = Getattr(p, "tmap:in:next");
	  continue;
	}

	String *parameterName = Getattr(p, "name");
	String *parameterType = Getattr(p, "type");

	Putc(',', arglist);
	if ((tm = Getattr(p, "tmap:directorin")) != 0) {
	  sprintf(source, "obj%d", idx++);
	  String *input = NewString(source);
	  Setattr(p, "emit:directorinput", input);
	  Replaceall(tm, "$input", input);
	  Replaceall(tm, "$owner", "0");
	  Delete(input);
	  Printv(wrap_args, tm, "\n", NIL);
	  Wrapper_add_localv(w, source, "VALUE", source, "= Qnil", NIL);
	  Printv(arglist, source, NIL);
	  p = Getattr(p, "tmap:directorin:next");
	  continue;
	} else if (Cmp(parameterType, "void")) {
	  /**
	   * Special handling for pointers to other C++ director classes.
	   * Ideally this would be left to a typemap, but there is currently no
	   * way to selectively apply the dynamic_cast<> to classes that have
	   * directors.  In other words, the type "SwigDirector_$1_lname" only exists
	   * for classes with directors.  We avoid the problem here by checking
	   * module.wrap::directormap, but it's not clear how to get a typemap to
	   * do something similar.  Perhaps a new default typemap (in addition
	   * to SWIGTYPE) called DIRECTORTYPE?
	   */
	  if (SwigType_ispointer(parameterType) || SwigType_isreference(parameterType)) {
	    Node *modname = Getattr(parent, "module");
	    Node *target = Swig_directormap(modname, parameterType);
	    sprintf(source, "obj%d", idx++);
	    String *nonconst = 0;
	    /* strip pointer/reference --- should move to Swig/stype.c */
	    String *nptype = NewString(Char(parameterType) + 2);
	    /* name as pointer */
	    String *ppname = Copy(parameterName);
	    if (SwigType_isreference(parameterType)) {
	      Insert(ppname, 0, "&");
	    }
	    /* if necessary, cast away const since Ruby doesn't support it! */
	    if (SwigType_isconst(nptype)) {
	      nonconst = NewStringf("nc_tmp_%s", parameterName);
	      String *nonconst_i = NewStringf("= const_cast< %s >(%s)", SwigType_lstr(parameterType, 0), ppname);
	      Wrapper_add_localv(w, nonconst, SwigType_lstr(parameterType, 0), nonconst, nonconst_i, NIL);
	      Delete(nonconst_i);
	      Swig_warning(WARN_LANG_DISCARD_CONST, input_file, line_number,
			   "Target language argument '%s' discards const in director method %s::%s.\n", SwigType_str(parameterType, parameterName),
			   SwigType_namestr(c_classname), SwigType_namestr(name));
	    } else {
	      nonconst = Copy(ppname);
	    }
	    Delete(nptype);
	    Delete(ppname);
	    String *mangle = SwigType_manglestr(parameterType);
	    if (target) {
	      String *director = NewStringf("director_%s", mangle);
	      Wrapper_add_localv(w, director, "Swig::Director *", director, "= 0", NIL);
	      Wrapper_add_localv(w, source, "VALUE", source, "= Qnil", NIL);
	      Printf(wrap_args, "%s = dynamic_cast<Swig::Director *>(%s);\n", director, nonconst);
	      Printf(wrap_args, "if (!%s) {\n", director);
	      Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle);
	      Printf(wrap_args, "} else {\n");
	      Printf(wrap_args, "%s = %s->swig_get_self();\n", source, director);
	      Printf(wrap_args, "}\n");
	      Delete(director);
	      Printv(arglist, source, NIL);
	    } else {
	      Wrapper_add_localv(w, source, "VALUE", source, "= Qnil", NIL);
	      Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle);
	      //Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE_p_%s, 0);\n", 
	      //       source, nonconst, base);
	      Printv(arglist, source, NIL);
	    }
	    Delete(mangle);
	    Delete(nonconst);
	  } else {
	    Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number,
			 "Unable to use type %s as a function argument in director method %s::%s (skipping method).\n", SwigType_str(parameterType, 0),
			 SwigType_namestr(c_classname), SwigType_namestr(name));
	    status = SWIG_NOWRAP;
	    break;
	  }
	}
	p = nextSibling(p);
      }

      /* declare Ruby return value */
      String *value_result = NewStringf("VALUE %s", Swig_cresult_name());
      Wrapper_add_local(w, Swig_cresult_name(), value_result);
      Delete(value_result);

      /* wrap complex arguments to VALUEs */
      Printv(w->code, wrap_args, NIL);

      /* pass the method call on to the Ruby object */
      exceptionSafeMethodCall(classname, n, w, idx, arglist, initstack);

      /*
       * Ruby method may return a simple object, or an Array of objects.
       * For in/out arguments, we have to extract the appropriate VALUEs from the Array,
       * then marshal everything back to C/C++ (return value and output arguments).
       */

      /* Marshal return value and other outputs (if any) from VALUE to C/C++ type */

      String *cleanup = NewString("");
      String *outarg = NewString("");

      if (outputs > 1) {
	Wrapper_add_local(w, "output", "VALUE output");
	Printf(w->code, "if (TYPE(%s) != T_ARRAY) {\n", Swig_cresult_name());
	Printf(w->code, "Ruby_DirectorTypeMismatchException(\"Ruby method failed to return an array.\");\n");
	Printf(w->code, "}\n");
      }

      idx = 0;

      /* Marshal return value */
      if (!is_void) {
	tm = Swig_typemap_lookup("directorout", n, Swig_cresult_name(), w);
	if (tm != 0) {
	  if (outputs > 1 && !asvoid ) {
	    Printf(w->code, "output = rb_ary_entry(%s, %d);\n", Swig_cresult_name(), idx++);
	    Replaceall(tm, "$input", "output");
	  } else {
	    Replaceall(tm, "$input", Swig_cresult_name());
	  }
	  /* TODO check this */
	  if (Getattr(n, "wrap:disown")) {
	    Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN");
	  } else {
	    Replaceall(tm, "$disown", "0");
	  }
	  Replaceall(tm, "$result", "c_result");
	  Printv(w->code, tm, "\n", NIL);
	} else {
	  Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number,
		       "Unable to use return type %s in director method %s::%s (skipping method).\n", SwigType_str(returntype, 0),
		       SwigType_namestr(c_classname), SwigType_namestr(name));
	  status = SWIG_ERROR;
	}
      }

      /* Marshal outputs */
      for (p = l; p;) {
	if ((tm = Getattr(p, "tmap:directorargout")) != 0) {
	  if (outputs > 1) {
	    Printf(w->code, "output = rb_ary_entry(%s, %d);\n", Swig_cresult_name(), idx++);
	    Replaceall(tm, "$result", "output");
	  } else {
	    Replaceall(tm, "$result", Swig_cresult_name());
	  }
	  Replaceall(tm, "$input", Getattr(p, "emit:directorinput"));
	  Printv(w->code, tm, "\n", NIL);
	  p = Getattr(p, "tmap:directorargout:next");
	} else {
	  p = nextSibling(p);
	}
      }

      Delete(arglist);
      Delete(cleanup);
      Delete(outarg);
    }

    /* any existing helper functions to handle this? */
    if (!is_void) {
      if (!(ignored_method && !pure_virtual)) {
	String *rettype = SwigType_str(returntype, 0);
	if (!SwigType_isreference(returntype)) {
	  Printf(w->code, "return (%s) c_result;\n", rettype);
	} else {
	  Printf(w->code, "return (%s) *c_result;\n", rettype);
	}
	Delete(rettype);
      }
    }

    Printf(w->code, "}\n");

    // We expose protected methods via an extra public inline method which makes a straight call to the wrapped class' method
    String *inline_extra_method = NewString("");
    if (dirprot_mode() && !is_public(n) && !pure_virtual) {
      Printv(inline_extra_method, declaration, NIL);
      String *extra_method_name = NewStringf("%sSwigPublic", name);
      Replaceall(inline_extra_method, name, extra_method_name);
      Replaceall(inline_extra_method, ";\n", " {\n      ");
      if (!is_void)
	Printf(inline_extra_method, "return ");
      String *methodcall = Swig_method_call(super, l);
      Printv(inline_extra_method, methodcall, ";\n    }\n", NIL);
      Delete(methodcall);
      Delete(extra_method_name);
    }

    /* emit the director method */
    if (status == SWIG_OK) {
      if (!Getattr(n, "defaultargs")) {
	Replaceall(w->code, "$symname", symname);
	Wrapper_print(w, f_directors);
	Printv(f_directors_h, declaration, NIL);
	Printv(f_directors_h, inline_extra_method, NIL);
      }
    }

    /* clean up */
    Delete(wrap_args);
    Delete(pclassname);
    DelWrapper(w);
    return status;
  }

  virtual int classDirectorConstructors(Node *n) {
    return Language::classDirectorConstructors(n);
  }

  virtual int classDirectorMethods(Node *n) {
    return Language::classDirectorMethods(n);
  }

  virtual int classDirectorDisown(Node *n) {
    return Language::classDirectorDisown(n);
  }

  String *runtimeCode() {
    String *s = NewString("");
    String *shead = Swig_include_sys("rubyhead.swg");
    if (!shead) {
      Printf(stderr, "*** Unable to open 'rubyhead.swg'\n");
    } else {
      Append(s, shead);
      Delete(shead);
    }
    String *serrors = Swig_include_sys("rubyerrors.swg");
    if (!serrors) {
      Printf(stderr, "*** Unable to open 'rubyerrors.swg'\n");
    } else {
      Append(s, serrors);
      Delete(serrors);
    }
    String *strack = Swig_include_sys("rubytracking.swg");
    if (!strack) {
      Printf(stderr, "*** Unable to open 'rubytracking.swg'\n");
    } else {
      Append(s, strack);
      Delete(strack);
    }
    String *sapi = Swig_include_sys("rubyapi.swg");
    if (!sapi) {
      Printf(stderr, "*** Unable to open 'rubyapi.swg'\n");
    } else {
      Append(s, sapi);
      Delete(sapi);
    }
    String *srun = Swig_include_sys("rubyrun.swg");
    if (!srun) {
      Printf(stderr, "*** Unable to open 'rubyrun.swg'\n");
    } else {
      Append(s, srun);
      Delete(srun);
    }
    return s;
  }

  String *defaultExternalRuntimeFilename() {
    return NewString("swigrubyrun.h");
  }
};				/* class RUBY */

/* -----------------------------------------------------------------------------
 * swig_ruby()    - Instantiate module
 * ----------------------------------------------------------------------------- */

static Language *new_swig_ruby() {
  return new RUBY();
}
extern "C" Language *swig_ruby(void) {
  return new_swig_ruby();
}


/*
 * Local Variables:
 * c-basic-offset: 2
 * End:
 */