summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/gcc/cp/pt.c
blob: 3ce0224890212c44ff64582baf548c12c917d6c3 (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
/* Handle parameterized types (templates) for GNU C++.
   Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
   Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.

This file is part of GNU CC.

GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* Known bugs or deficiencies include:
   * templates for class static data don't work (methods only)
   * duplicated method templates can crash the compiler
   * interface/impl data is taken from file defining the template
   * all methods must be provided in header files; can't use a source
     file that contains only the method templates and "just win"
   * method templates must be seen before the expansion of the
     class template is done
 */

#include "config.h"
#include <stdio.h>
#include "obstack.h"

#include "tree.h"
#include "flags.h"
#include "cp-tree.h"
#include "decl.h"
#include "parse.h"
#include "lex.h"
#include "output.h"
#include "defaults.h"

extern struct obstack permanent_obstack;

extern int lineno;
extern char *input_filename;
struct pending_inline *pending_template_expansions;

int processing_template_decl;
int processing_template_defn;

/* This is a kludge to handle instantiation of template methods that are
   used before their definition.  It should not be necessary after the
   template rewrite.  */
static tree template_classes;

#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free

static int unify ();
static void add_pending_template ();

void overload_template_name (), pop_template_decls ();

/* We've got a template header coming up; set obstacks up to save the
   nodes created permanently.  (There might be cases with nested templates
   where we don't have to do this, but they aren't implemented, and it
   probably wouldn't be worth the effort.)  */
void
begin_template_parm_list ()
{
  pushlevel (0);
  push_obstacks (&permanent_obstack, &permanent_obstack);
  pushlevel (0);
}

/* Process information from new template parameter NEXT and append it to the
   LIST being built.  The rules for use of a template parameter type name
   by later parameters are not well-defined for us just yet.  However, the
   only way to avoid having to parse expressions of unknown complexity (and
   with tokens of unknown types) is to disallow it completely.	So for now,
   that is what is assumed.  */
tree
process_template_parm (list, next)
     tree list, next;
{
  tree parm;
  tree decl = 0;
  tree defval;
  int is_type;
  parm = next;
  my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
  defval = TREE_PURPOSE (parm);
  parm = TREE_VALUE (parm);
  is_type = TREE_PURPOSE (parm) == class_type_node;
  if (!is_type)
    {
      tree tinfo = 0;
      my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
      /* is a const-param */
      parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
			     PARM, 0, NULL_TREE, NULL_TREE);
      /* A template parameter is not modifiable.  */
      TREE_READONLY (parm) = 1;
      if (IS_AGGR_TYPE (TREE_TYPE (parm)))
	{
	  sorry ("aggregate template parameter types");
	  TREE_TYPE (parm) = void_type_node;
	}
      tinfo = make_node (TEMPLATE_CONST_PARM);
      my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
      if (TREE_PERMANENT (parm) == 0)
        {
	  parm = copy_node (parm);
	  TREE_PERMANENT (parm) = 1;
        }
      TREE_TYPE (tinfo) = TREE_TYPE (parm);
      decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
      DECL_INITIAL (decl) = tinfo;
      DECL_INITIAL (parm) = tinfo;
    }
  else
    {
      tree t = make_node (TEMPLATE_TYPE_PARM);
      decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
      TYPE_MAIN_DECL (t) = decl;
      parm = decl;
      if (defval)
	{
	  if (IDENTIFIER_HAS_TYPE_VALUE (defval))
	    defval = IDENTIFIER_TYPE_VALUE (defval);
	  else
	    defval = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (defval));
	}
    }
  SET_DECL_ARTIFICIAL (decl);
  pushdecl (decl);
  parm = build_tree_list (defval, parm);
  return chainon (list, parm);
}

/* The end of a template parameter list has been reached.  Process the
   tree list into a parameter vector, converting each parameter into a more
   useful form.	 Type parameters are saved as IDENTIFIER_NODEs, and others
   as PARM_DECLs.  */

tree
end_template_parm_list (parms)
     tree parms;
{
  int nparms = 0;
  int saw_default = 0;
  tree saved_parmlist;
  tree parm;
  for (parm = parms; parm; parm = TREE_CHAIN (parm))
    nparms++;
  saved_parmlist = make_tree_vec (nparms);

  for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
    {
      tree p = TREE_VALUE (parm);
      if (TREE_PURPOSE (parm))
	saw_default = 1;
      else if (saw_default)
	{
	  error ("if a default argument is given for one template parameter");
	  error ("default arguments must be given for all subsequent");
	  error ("parameters as well");
	}

      if (TREE_CODE (p) == TYPE_DECL)
	{
	  tree t = TREE_TYPE (p);
	  TEMPLATE_TYPE_SET_INFO (t, saved_parmlist, nparms);
	}
      else
	{
	  tree tinfo = DECL_INITIAL (p);
	  DECL_INITIAL (p) = NULL_TREE;
	  TEMPLATE_CONST_SET_INFO (tinfo, saved_parmlist, nparms);
	}
      TREE_VEC_ELT (saved_parmlist, nparms) = parm;
    }
  set_current_level_tags_transparency (1);
  processing_template_decl++;
  return saved_parmlist;
}

/* end_template_decl is called after a template declaration is seen.
   D1 is template header; D2 is class_head_sans_basetype or a
   TEMPLATE_DECL with its DECL_RESULT field set.  */
void
end_template_decl (d1, d2, is_class, defn)
     tree d1, d2, is_class;
     int defn;
{
  tree decl;
  struct template_info *tmpl;

  tmpl = (struct template_info *) obstack_alloc (&permanent_obstack,
					    sizeof (struct template_info));
  tmpl->text = 0;
  tmpl->length = 0;
  tmpl->aggr = is_class;

  /* cloned from reinit_parse_for_template */
  tmpl->filename = input_filename;
  tmpl->lineno = lineno;
  tmpl->parm_vec = d1;          /* [eichin:19911015.2306EST] */

  if (d2 == NULL_TREE || d2 == error_mark_node)
    {
      decl = 0;
      goto lose;
    }

  if (is_class)
    {
      decl = build_lang_decl (TEMPLATE_DECL, d2, NULL_TREE);
      GNU_xref_decl (current_function_decl, decl);
    }
  else
    {
      if (TREE_CODE (d2) == TEMPLATE_DECL)
	decl = d2;
      else
	{
	  /* Class destructor templates and operator templates are
	     slipping past as non-template nodes.  Process them here, since
	     I haven't figured out where to catch them earlier.  I could
	     go do that, but it's a choice between getting that done and
	     staying only N months behind schedule.  Sorry....  */
	  enum tree_code code;
	  my_friendly_assert (TREE_CODE (d2) == CALL_EXPR, 263);
	  code = TREE_CODE (TREE_OPERAND (d2, 0));
	  my_friendly_assert (code == BIT_NOT_EXPR
		  || code == OP_IDENTIFIER
		  || code == SCOPE_REF, 264);
	  d2 = grokdeclarator (d2, NULL_TREE, MEMFUNCDEF, 0, NULL_TREE, NULL_TREE);
	  decl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (d2),
				  TREE_TYPE (d2));
	  DECL_TEMPLATE_RESULT (decl) = d2;
	  DECL_CONTEXT (decl) = DECL_CONTEXT (d2);
	  DECL_CLASS_CONTEXT (decl) = DECL_CLASS_CONTEXT (d2);
	  DECL_NAME (decl) = DECL_NAME (d2);
	  TREE_TYPE (decl) = TREE_TYPE (d2);
	  if (interface_unknown && flag_external_templates
	      && ! flag_alt_external_templates
	      && ! DECL_IN_SYSTEM_HEADER (decl))
	    warn_if_unknown_interface (decl);
	  TREE_PUBLIC (decl) = TREE_PUBLIC (d2) = flag_external_templates && !interface_unknown;
	  DECL_EXTERNAL (decl) = (DECL_EXTERNAL (d2)
				  && !(DECL_CLASS_CONTEXT (d2)
				       && !DECL_THIS_EXTERN (d2)));
	}

      /* All routines creating TEMPLATE_DECL nodes should now be using
	 build_lang_decl, which will have set this up already.	*/
      my_friendly_assert (DECL_LANG_SPECIFIC (decl) != 0, 265);

      /* @@ Somewhere, permanent allocation isn't being used.  */
      if (! DECL_TEMPLATE_IS_CLASS (decl)
	  && TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == FUNCTION_DECL)
	{
	  tree result = DECL_TEMPLATE_RESULT (decl);
	  /* Will do nothing if allocation was already permanent.  */
	  DECL_ARGUMENTS (result) = copy_to_permanent (DECL_ARGUMENTS (result));
	}

      /* If this is for a method, there's an extra binding level here.	*/
      if (DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
	{
	  /* @@ Find out where this should be getting set!  */
	  tree r = DECL_TEMPLATE_RESULT (decl);
	  if (DECL_LANG_SPECIFIC (r) && DECL_CLASS_CONTEXT (r) == NULL_TREE)
	    DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
	}
    }
  DECL_TEMPLATE_INFO (decl) = tmpl;
  DECL_TEMPLATE_PARMS (decl) = d1;

  /* So that duplicate_decls can do the right thing.  */
  if (defn)
    DECL_INITIAL (decl) = error_mark_node;
  
  /* If context of decl is non-null (i.e., method template), add it
     to the appropriate class template, and pop the binding levels.  */
  if (! is_class && DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
    {
      tree ctx = DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl));
      tree tmpl, t;
      my_friendly_assert (TREE_CODE (ctx) == UNINSTANTIATED_P_TYPE, 266);
      tmpl = UPT_TEMPLATE (ctx);
      for (t = DECL_TEMPLATE_MEMBERS (tmpl); t; t = TREE_CHAIN (t))
	if (TREE_PURPOSE (t) == DECL_NAME (decl)
	    && duplicate_decls (decl, TREE_VALUE (t)))
	  goto already_there;
      DECL_TEMPLATE_MEMBERS (tmpl) =
	perm_tree_cons (DECL_NAME (decl), decl, DECL_TEMPLATE_MEMBERS (tmpl));
    already_there:
      poplevel (0, 0, 0);
      poplevel (0, 0, 0);
    }
  /* Otherwise, go back to top level first, and push the template decl
     again there.  */
  else
    {
      poplevel (0, 0, 0);
      poplevel (0, 0, 0);
      pushdecl (decl);
    }
 lose:
#if 0 /* It happens sometimes, with syntactic or semantic errors.

	 One specific case:
	 template <class A, int X, int Y> class Foo { ... };
	 template <class A, int X, int y> Foo<X,Y>::method (Foo& x) { ... }
	 Note the missing "A" in the class containing "method".  */
  my_friendly_assert (global_bindings_p (), 267);
#else
  while (! global_bindings_p ())
    poplevel (0, 0, 0);
#endif
  pop_obstacks ();
  processing_template_decl--;
  (void) get_pending_sizes ();
}

tree tsubst		PROTO ((tree, tree*, int, tree));

/* Convert all template arguments to their appropriate types, and return
   a vector containing the resulting values.  If any error occurs, return
   error_mark_node.  */
static tree
coerce_template_parms (parms, arglist, in_decl)
     tree parms, arglist;
     tree in_decl;
{
  int nparms, nargs, i, lost = 0;
  tree vec;

  if (arglist == NULL_TREE)
    nargs = 0;
  else if (TREE_CODE (arglist) == TREE_VEC)
    nargs = TREE_VEC_LENGTH (arglist);
  else
    nargs = list_length (arglist);

  nparms = TREE_VEC_LENGTH (parms);

  if (nargs > nparms
      || (nargs < nparms
	  && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
    {
      error ("incorrect number of parameters (%d, should be %d)",
	     nargs, nparms);
      if (in_decl)
	cp_error_at ("in template expansion for decl `%D'", in_decl);
      return error_mark_node;
    }

  if (arglist && TREE_CODE (arglist) == TREE_VEC)
    vec = copy_node (arglist);
  else
    {
      vec = make_tree_vec (nparms);
      for (i = 0; i < nparms; i++)
	{
	  tree arg;

	  if (arglist)
	    {
	      arg = arglist;
	      arglist = TREE_CHAIN (arglist);

	      if (arg == error_mark_node)
		lost++;
	      else
		arg = TREE_VALUE (arg);
	    }
	  else
	    arg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));

	  TREE_VEC_ELT (vec, i) = arg;
	}
    }
  for (i = 0; i < nparms; i++)
    {
      tree arg = TREE_VEC_ELT (vec, i);
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      tree val = 0;
      int is_type, requires_type;

      is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
      requires_type = TREE_CODE (parm) == TYPE_DECL;
      if (is_type != requires_type)
	{
	  if (in_decl)
	    cp_error ("type/value mismatch in template parameter list for `%D'",
		      in_decl);
	  lost++;
	  TREE_VEC_ELT (vec, i) = error_mark_node;
	  continue;
	}
      if (is_type)
	val = groktypename (arg);
      else
	{
	  tree t = tsubst (TREE_TYPE (parm), &TREE_VEC_ELT (vec, 0),
			   TREE_VEC_LENGTH (vec), in_decl);
	  val = digest_init (t, arg, (tree *) 0);

	  if (val == error_mark_node)
	    ;

	  /* 14.2: Other template-arguments must be constant-expressions,
	     addresses of objects or functions with external linkage, or of
	     static class members.  */
	  else if (!TREE_CONSTANT (val))
	    {
	      cp_error ("non-const `%E' cannot be used as template argument",
			arg);
	      val = error_mark_node;
	    }
	  else if (POINTER_TYPE_P (TREE_TYPE (val))
		   && ! integer_zerop (val)
		   && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != OFFSET_TYPE
		   && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != METHOD_TYPE)
	    {
	      t = val;
	      STRIP_NOPS (t);
	      if (TREE_CODE (t) == ADDR_EXPR)
		{
		  tree a = TREE_OPERAND (t, 0);
		  STRIP_NOPS (a);
		  if (TREE_CODE (a) == STRING_CST)
		    {
		      cp_error ("string literal %E is not a valid template argument", a);
		      error ("because it is the address of an object with static linkage");
		      val = error_mark_node;
		    }
		  else if (TREE_CODE (a) != VAR_DECL
			   && TREE_CODE (a) != FUNCTION_DECL)
		    goto bad;
		  else if (! DECL_PUBLIC (a))
		    {
		      cp_error ("address of non-extern `%E' cannot be used as template argument", a);
		      val = error_mark_node;
		    }
		}
	      else
		{
		bad:
		  cp_error ("`%E' is not a valid template argument", t);
		  error ("it must be %s%s with external linkage",
			 TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
			 ? "a pointer to " : "",
			 TREE_CODE (TREE_TYPE (TREE_TYPE (val))) == FUNCTION_TYPE
			 ? "a function" : "an object");
		  val = error_mark_node;
		}
	    }
	}

      if (val == error_mark_node)
	lost++;

      TREE_VEC_ELT (vec, i) = val;
    }
  if (lost)
    return error_mark_node;
  return vec;
}

/* Given class template name and parameter list, produce a user-friendly name
   for the instantiation.  */
static char *
mangle_class_name_for_template (name, parms, arglist)
     char *name;
     tree parms, arglist;
{
  static struct obstack scratch_obstack;
  static char *scratch_firstobj;
  int i, nparms;

  if (!scratch_firstobj)
    {
      gcc_obstack_init (&scratch_obstack);
      scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
    }
  else
    obstack_free (&scratch_obstack, scratch_firstobj);

#if 0
#define buflen	sizeof(buf)
#define check	if (bufp >= buf+buflen-1) goto too_long
#define ccat(c) *bufp++=(c); check
#define advance	bufp+=strlen(bufp); check
#define cat(s)	strncpy(bufp, s, buf+buflen-bufp-1); advance
#else
#define check
#define ccat(c)	obstack_1grow (&scratch_obstack, (c));
#define advance
#define cat(s)	obstack_grow (&scratch_obstack, (s), strlen (s))
#endif

  cat (name);
  ccat ('<');
  nparms = TREE_VEC_LENGTH (parms);
  my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
  for (i = 0; i < nparms; i++)
    {
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      tree arg = TREE_VEC_ELT (arglist, i);

      if (i)
	ccat (',');

      if (TREE_CODE (parm) == TYPE_DECL)
	{
	  cat (type_as_string (arg, 0));
	  continue;
	}
      else
	my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);

      if (TREE_CODE (arg) == TREE_LIST)
	{
	  /* New list cell was built because old chain link was in
	     use.  */
	  my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
	  arg = TREE_VALUE (arg);
	}
      /* No need to check arglist against parmlist here; we did that
	 in coerce_template_parms, called from lookup_template_class.  */
      cat (expr_as_string (arg, 0));
    }
  {
    char *bufp = obstack_next_free (&scratch_obstack);
    int offset = 0;
    while (bufp[offset - 1] == ' ')
      offset--;
    obstack_blank_fast (&scratch_obstack, offset);

    /* B<C<char> >, not B<C<char>> */
    if (bufp[offset - 1] == '>')
      ccat (' ');
  }
  ccat ('>');
  ccat ('\0');
  return (char *) obstack_base (&scratch_obstack);

#if 0
 too_long:
#endif
  fatal ("out of (preallocated) string space creating template instantiation name");
  /* NOTREACHED */
  return NULL;
}

/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
   parameters, find the desired type.

   D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
   Since ARGLIST is build on the decl_obstack, we must copy it here
   to keep it from being reclaimed when the decl storage is reclaimed.

   IN_DECL, if non-NULL, is the template declaration we are trying to
   instantiate.  */
tree
lookup_template_class (d1, arglist, in_decl)
     tree d1, arglist;
     tree in_decl;
{
  tree template, parmlist;
  char *mangled_name;
  tree id;

  my_friendly_assert (TREE_CODE (d1) == IDENTIFIER_NODE, 272);
  template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
  if (! template)
    template = IDENTIFIER_CLASS_VALUE (d1);
  /* With something like `template <class T> class X class X { ... };'
     we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
     We don't want to do that, but we have to deal with the situation, so
     let's give them some syntax errors to chew on instead of a crash.  */
  if (! template)
    return error_mark_node;
  if (TREE_CODE (template) != TEMPLATE_DECL)
    {
      cp_error ("non-template type `%T' used as a template", d1);
      if (in_decl)
	cp_error_at ("for template declaration `%D'", in_decl);
      return error_mark_node;
    }
  parmlist = DECL_TEMPLATE_PARMS (template);

  arglist = coerce_template_parms (parmlist, arglist, template);
  if (arglist == error_mark_node)
    return error_mark_node;
  if (uses_template_parms (arglist))
    {
      tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
      tree d;
      id = make_anon_name ();
      d = build_decl (TYPE_DECL, id, t);
      TYPE_NAME (t) = d;
      TYPE_VALUES (t) = build_tree_list (template, arglist);
      pushdecl_top_level (d);
    }
  else
    {
      mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
						     parmlist, arglist);
      id = get_identifier (mangled_name);
    }
  if (!IDENTIFIER_TEMPLATE (id))
    {
      arglist = copy_to_permanent (arglist);
      IDENTIFIER_TEMPLATE (id) = perm_tree_cons (template, arglist, NULL_TREE);
    }
  return id;
}

void
push_template_decls (parmlist, arglist, class_level)
     tree parmlist, arglist;
     int class_level;
{
  int i, nparms;

  /* Don't want to push values into global context.  */
  if (!class_level)
    {
      pushlevel (1);
      declare_pseudo_global_level ();
    }

  nparms = TREE_VEC_LENGTH (parmlist);

  for (i = 0; i < nparms; i++)
    {
      int requires_type, is_type;
      tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
      tree arg = TREE_VEC_ELT (arglist, i);
      tree decl = 0;

      requires_type = TREE_CODE (parm) == TYPE_DECL;
      is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
      if (is_type)
	{
	  /* add typename to namespace */
	  if (!requires_type)
	    {
	      error ("template use error: type provided where value needed");
	      continue;
	    }
	  decl = arg;
	  my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 't', 273);
	  decl = build_decl (TYPE_DECL, DECL_NAME (parm), decl);
	}
      else
	{
	  /* add const decl to namespace */
	  tree val;
	  tree parmtype;
	  if (requires_type)
	    {
	      error ("template use error: value provided where type needed");
	      continue;
	    }
	  parmtype = tsubst (TREE_TYPE (parm), &TREE_VEC_ELT (arglist, 0),
			     TREE_VEC_LENGTH (arglist), NULL_TREE);
	  val = digest_init (parmtype, arg, (tree *) 0);
	  if (val != error_mark_node)
	    {
	      decl = build_decl (CONST_DECL, DECL_NAME (parm),
				 parmtype);
	      DECL_INITIAL (decl) = val;
	      TREE_READONLY (decl) = 1;
	    }
	}
      if (decl != 0)
	{
	  SET_DECL_ARTIFICIAL (decl);
	  layout_decl (decl, 0);
	  if (class_level)
	    pushdecl_class_level (decl);
	  else
	    pushdecl (decl);
	}
    }
}

void
pop_template_decls (parmlist, arglist, class_level)
     tree parmlist, arglist;
     int class_level;
{
  if (!class_level)
    poplevel (0, 0, 0);
}

/* Should be defined in parse.h.  */
extern int yychar;

int
uses_template_parms (t)
     tree t;
{
  if (!t)
    return 0;
  switch (TREE_CODE (t))
    {
    case INDIRECT_REF:
    case COMPONENT_REF:
      /* We assume that the object must be instantiated in order to build
	 the COMPONENT_REF, so we test only whether the type of the
	 COMPONENT_REF uses template parms.  */
      return uses_template_parms (TREE_TYPE (t));

    case IDENTIFIER_NODE:
      if (!IDENTIFIER_TEMPLATE (t))
	return 0;
      return uses_template_parms (TREE_VALUE (IDENTIFIER_TEMPLATE (t)));

      /* aggregates of tree nodes */
    case TREE_VEC:
      {
	int i = TREE_VEC_LENGTH (t);
	while (i--)
	  if (uses_template_parms (TREE_VEC_ELT (t, i)))
	    return 1;
	return 0;
      }
    case TREE_LIST:
      if (uses_template_parms (TREE_PURPOSE (t))
	  || uses_template_parms (TREE_VALUE (t)))
	return 1;
      return uses_template_parms (TREE_CHAIN (t));

      /* constructed type nodes */
    case POINTER_TYPE:
    case REFERENCE_TYPE:
      return uses_template_parms (TREE_TYPE (t));
    case RECORD_TYPE:
      if (TYPE_PTRMEMFUNC_FLAG (t))
	return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
    case UNION_TYPE:
      if (!TYPE_NAME (t))
	return 0;
      if (!TYPE_IDENTIFIER (t))
	return 0;
      return uses_template_parms (TYPE_IDENTIFIER (t));
    case FUNCTION_TYPE:
      if (uses_template_parms (TYPE_ARG_TYPES (t)))
	return 1;
      return uses_template_parms (TREE_TYPE (t));
    case ARRAY_TYPE:
      if (uses_template_parms (TYPE_DOMAIN (t)))
	return 1;
      return uses_template_parms (TREE_TYPE (t));
    case OFFSET_TYPE:
      if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
	return 1;
      return uses_template_parms (TREE_TYPE (t));
    case METHOD_TYPE:
      if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
	return 1;
      if (uses_template_parms (TYPE_ARG_TYPES (t)))
	return 1;
      return uses_template_parms (TREE_TYPE (t));

      /* decl nodes */
    case TYPE_DECL:
      return uses_template_parms (DECL_NAME (t));
    case FUNCTION_DECL:
      if (uses_template_parms (TREE_TYPE (t)))
	return 1;
      /* fall through */
    case VAR_DECL:
    case PARM_DECL:
      /* ??? What about FIELD_DECLs?  */
      /* The type of a decl can't use template parms if the name of the
	 variable doesn't, because it's impossible to resolve them.  So
	 ignore the type field for now.	 */
      if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
	return 1;
      if (uses_template_parms (TREE_TYPE (t)))
	{
	  error ("template parms used where they can't be resolved");
	}
      return 0;

    case CALL_EXPR:
      return uses_template_parms (TREE_TYPE (t));
    case ADDR_EXPR:
      return uses_template_parms (TREE_OPERAND (t, 0));

      /* template parm nodes */
    case TEMPLATE_TYPE_PARM:
    case TEMPLATE_CONST_PARM:
      return 1;

      /* simple type nodes */
    case INTEGER_TYPE:
      if (uses_template_parms (TYPE_MIN_VALUE (t)))
	return 1;
      return uses_template_parms (TYPE_MAX_VALUE (t));

    case REAL_TYPE:
    case VOID_TYPE:
    case ENUMERAL_TYPE:
    case BOOLEAN_TYPE:
      return 0;

      /* constants */
    case INTEGER_CST:
    case REAL_CST:
    case STRING_CST:
      return 0;

    case ERROR_MARK:
      /* Non-error_mark_node ERROR_MARKs are bad things.  */
      my_friendly_assert (t == error_mark_node, 274);
      /* NOTREACHED */
      return 0;

    case UNINSTANTIATED_P_TYPE:
      return 1;

    case CONSTRUCTOR:
      if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
	return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
      /* else fall through */

    default:
      switch (TREE_CODE_CLASS (TREE_CODE (t)))
	{
	case '1':
	case '2':
	case '3':
	case '<':
	  {
	    int i;
	    for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
	      if (uses_template_parms (TREE_OPERAND (t, i)))
		return 1;
	    return 0;
	  }
	default:
	  break;
	}
      sorry ("testing %s for template parms",
	     tree_code_name [(int) TREE_CODE (t)]);
      my_friendly_abort (82);
      /* NOTREACHED */
      return 0;
    }
}

void
instantiate_member_templates (classname)
     tree classname;
{
  tree t;
  tree id = classname;
  tree members = DECL_TEMPLATE_MEMBERS (TREE_PURPOSE (IDENTIFIER_TEMPLATE (id)));

  for (t = members; t; t = TREE_CHAIN (t))
    {
      tree parmvec, type, classparms, tdecl, t2;
      int nparms, xxx = 0, i;

      my_friendly_assert (TREE_VALUE (t) != NULL_TREE, 275);
      my_friendly_assert (TREE_CODE (TREE_VALUE (t)) == TEMPLATE_DECL, 276);
      /* @@ Should verify that class parm list is a list of
	 distinct template parameters, and covers all the template
	 parameters.  */
      tdecl = TREE_VALUE (t);
      type = DECL_CONTEXT (DECL_TEMPLATE_RESULT (tdecl));
      classparms = UPT_PARMS (type);
      nparms = TREE_VEC_LENGTH (classparms);
      parmvec = make_tree_vec (nparms);
      for (i = 0; i < nparms; i++)
	TREE_VEC_ELT (parmvec, i) = NULL_TREE;
      switch (unify (DECL_TEMPLATE_PARMS (tdecl),
		     &TREE_VEC_ELT (parmvec, 0), nparms,
		     type, IDENTIFIER_TYPE_VALUE (classname),
		     &xxx))
	{
	case 0:
	  /* Success -- well, no inconsistency, at least.  */
	  for (i = 0; i < nparms; i++)
	    if (TREE_VEC_ELT (parmvec, i) == NULL_TREE)
	      goto failure;
	  t2 = instantiate_template (tdecl,
				     &TREE_VEC_ELT (parmvec, 0));
	  type = IDENTIFIER_TYPE_VALUE (id);
	  my_friendly_assert (type != 0, 277);
	  break;
	case 1:
	  /* Failure.  */
	failure:
	  cp_error_at ("type unification error instantiating `%D'", tdecl);
	  cp_error ("while instantiating members of `%T'", classname);

	  continue /* loop of members */;
	default:
	  /* Eek, a bug.  */
	  my_friendly_abort (83);
	}
    }
}

static struct tinst_level *current_tinst_level = 0;
static struct tinst_level *free_tinst_level = 0;
static int tinst_depth = 0;
int max_tinst_depth = 17;

int
push_tinst_level (name)
     tree name;
{
  struct tinst_level *new;
  tree global = IDENTIFIER_GLOBAL_VALUE (name);

  if (tinst_depth >= max_tinst_depth)
    {
      error ("template instantiation depth exceeds maximum of %d",
	     max_tinst_depth);
      cp_error ("  instantiating `%D'", name);
      return 0;
    }

  if (free_tinst_level)
    {
      new = free_tinst_level;
      free_tinst_level = new->next;
    }
  else
    new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));

  new->classname = name;
  if (global)
    {
      new->line = DECL_SOURCE_LINE (global);
      new->file = DECL_SOURCE_FILE (global);
    }
  else
    {
      new->line = lineno;
      new->file = input_filename;
    }
  new->next = current_tinst_level;
  current_tinst_level = new;
  ++tinst_depth;
  return 1;
}

void
pop_tinst_level ()
{
  struct tinst_level *old = current_tinst_level;

  current_tinst_level = old->next;
  old->next = free_tinst_level;
  free_tinst_level = old;
  --tinst_depth;
}

struct tinst_level *
tinst_for_decl ()
{
  struct tinst_level *p = current_tinst_level;

  if (p)
    for (; p->next ; p = p->next )
      ;
  return p;
}

tree
instantiate_class_template (classname, setup_parse)
     tree classname;
     int setup_parse;
{
  struct template_info *template_info;
  tree template, t1;

  if (classname == error_mark_node)
    return error_mark_node;

  my_friendly_assert (TREE_CODE (classname) == IDENTIFIER_NODE, 278);
  template = IDENTIFIER_TEMPLATE (classname);

  if (IDENTIFIER_HAS_TYPE_VALUE (classname))
    {
      tree type = IDENTIFIER_TYPE_VALUE (classname);
      if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
	return type;
      if (TYPE_BEING_DEFINED (type)
	  || TYPE_SIZE (type)
	  || CLASSTYPE_USE_TEMPLATE (type) != 0)
	return type;
    }

  /* If IDENTIFIER_LOCAL_VALUE is already set on this template classname
     (it's something like `foo<int>'), that means we're already working on
     the instantiation for it.  Normally, a classname comes in with nothing
     but its IDENTIFIER_TEMPLATE slot set.  If we were to try to instantiate
     this again, we'd get a redeclaration error.  Since we're already working
     on it, we'll pass back this classname's TYPE_DECL (it's the value of
     the classname's IDENTIFIER_LOCAL_VALUE).  Only do this if we're setting
     things up for the parser, though---if we're just trying to instantiate
     it (e.g., via tsubst) we can trip up cuz it may not have an
     IDENTIFIER_TYPE_VALUE when it will need one.  */
  if (setup_parse && IDENTIFIER_LOCAL_VALUE (classname))
    return IDENTIFIER_LOCAL_VALUE (classname);

  if (uses_template_parms (classname))
    {
      if (!TREE_TYPE (classname))
	{
	  tree t = make_lang_type (RECORD_TYPE);
	  tree d = build_decl (TYPE_DECL, classname, t);
	  DECL_NAME (d) = classname;
	  TYPE_NAME (t) = d;
	  pushdecl (d);
	}
      return NULL_TREE;
    }

  t1 = TREE_PURPOSE (template);
  my_friendly_assert (TREE_CODE (t1) == TEMPLATE_DECL, 279);

  /* If a template is declared but not defined, accept it; don't crash.
     Later uses requiring the definition will be flagged as errors by
     other code.  Thanks to niklas@appli.se for this bug fix.  */
  if (DECL_TEMPLATE_INFO (t1)->text == 0)
    setup_parse = 0;

  push_to_top_level ();
  template_info = DECL_TEMPLATE_INFO (t1);
  if (setup_parse && push_tinst_level (classname))
    {
      push_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (template)),
			   TREE_VALUE (template), 0);
      set_current_level_tags_transparency (1);
      feed_input (template_info->text, template_info->length, (struct obstack *)0);
      lineno = template_info->lineno;
      input_filename = template_info->filename;
      /* Get interface/implementation back in sync.  */
      extract_interface_info ();
      overload_template_name (classname, 0);
      /* Kludge so that we don't get screwed by our own base classes.  */
      TYPE_BEING_DEFINED (TREE_TYPE (classname)) = 1;
      yychar = PRE_PARSED_CLASS_DECL;
      yylval.ttype = classname;
      processing_template_defn++;
      if (!flag_external_templates)
	interface_unknown++;
      template_classes
	= perm_tree_cons (classname, NULL_TREE, template_classes);
    }
  else
    {
      tree t, decl, id, tmpl;

      id = classname;
      tmpl = TREE_PURPOSE (IDENTIFIER_TEMPLATE (id));
      t = xref_tag (DECL_TEMPLATE_INFO (tmpl)->aggr, id, NULL_TREE, 0);
      my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
			  || TREE_CODE (t) == UNION_TYPE, 280);

      /* Now, put a copy of the decl in global scope, to avoid
       * recursive expansion.  */
      decl = IDENTIFIER_LOCAL_VALUE (id);
      if (!decl)
	decl = IDENTIFIER_CLASS_VALUE (id);
      if (decl)
	{
	  my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 281);
	  /* We'd better make sure we're on the permanent obstack or else
	   * we'll get a "friendly" abort 124 in pushdecl.  Perhaps a
	   * copy_to_permanent would be sufficient here, but then a
	   * sharing problem might occur.  I don't know -- niklas@appli.se */
	  push_obstacks (&permanent_obstack, &permanent_obstack);
	  pushdecl_top_level (copy_node (decl));
	  pop_obstacks ();
	}
      pop_from_top_level ();
    }

  return NULL_TREE;
}

static int
list_eq (t1, t2)
     tree t1, t2;
{
  if (t1 == NULL_TREE)
    return t2 == NULL_TREE;
  if (t2 == NULL_TREE)
    return 0;
  /* Don't care if one declares its arg const and the other doesn't -- the
     main variant of the arg type is all that matters.  */
  if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
      != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
    return 0;
  return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
}

static tree 
lookup_nested_type_by_name (ctype, name)
        tree ctype, name;
{
  tree t;

  for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
    {
      if (name == TREE_PURPOSE (t))
	return TREE_VALUE (t);
    }
  return NULL_TREE;
}

static tree
search_nested_type_in_tmpl (tmpl, type)
        tree tmpl, type;
{
  tree t;

  if (tmpl == NULL || TYPE_CONTEXT(type) == NULL)
    return tmpl;
  t = search_nested_type_in_tmpl (tmpl, TYPE_CONTEXT(type));
  if (t == NULL) return t;
  t = lookup_nested_type_by_name(t, DECL_NAME(TYPE_NAME(type)));
  return t;
}

tree
tsubst (t, args, nargs, in_decl)
     tree t, *args;
     int nargs;
     tree in_decl;
{
  tree type;

  if (t == NULL_TREE || t == error_mark_node)
    return t;

  type = TREE_TYPE (t);
  if (type
      /* Minor optimization.
	 ?? Are these really the most frequent cases?  Is the savings
	 significant?  */
      && type != integer_type_node
      && type != void_type_node
      && type != char_type_node)
    type = tsubst (type, args, nargs, in_decl);

  switch (TREE_CODE (t))
    {
    case RECORD_TYPE:
      if (TYPE_PTRMEMFUNC_P (t))
	return build_ptrmemfunc_type
	  (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
	  
      /* else fall through */

    case ERROR_MARK:
    case IDENTIFIER_NODE:
    case OP_IDENTIFIER:
    case VOID_TYPE:
    case REAL_TYPE:
    case ENUMERAL_TYPE:
    case BOOLEAN_TYPE:
    case INTEGER_CST:
    case REAL_CST:
    case STRING_CST:
    case UNION_TYPE:
      return t;

    case INTEGER_TYPE:
      if (t == integer_type_node)
	return t;

      if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
	  && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
	return t;
      return build_index_2_type
	(tsubst (TYPE_MIN_VALUE (t), args, nargs, in_decl),
	 tsubst (TYPE_MAX_VALUE (t), args, nargs, in_decl));

    case TEMPLATE_TYPE_PARM:
      {
	tree arg = args[TEMPLATE_TYPE_IDX (t)];
	return cp_build_type_variant
	  (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
	   TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
      }

    case TEMPLATE_CONST_PARM:
      return args[TEMPLATE_CONST_IDX (t)];

    case FUNCTION_DECL:
      {
	tree r;
	tree fnargs, result;
	
	if (type == TREE_TYPE (t)
	    && (DECL_CONTEXT (t) == NULL_TREE
		|| TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't'))
	  return t;
	fnargs = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
	result = tsubst (DECL_RESULT (t), args, nargs, t);
	if (DECL_CONTEXT (t) != NULL_TREE
	    && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
	  {
	    /* Look it up in that class, and return the decl node there,
	       instead of creating a new one.  */
	    tree ctx, methods, name, method;
	    int n_methods;
	    int i, found = 0;

	    name = DECL_NAME (t);
	    ctx = tsubst (DECL_CONTEXT (t), args, nargs, t);
	    methods = CLASSTYPE_METHOD_VEC (ctx);
	    if (methods == NULL_TREE)
	      /* No methods at all -- no way this one can match.  */
	      goto no_match;
	    n_methods = TREE_VEC_LENGTH (methods);

	    r = NULL_TREE;

	    if (!strncmp (OPERATOR_TYPENAME_FORMAT,
			  IDENTIFIER_POINTER (name),
			  sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
	      {
		/* Type-conversion operator.  Reconstruct the name, in
		   case it's the name of one of the template's parameters.  */
		name = build_typename_overload (TREE_TYPE (type));
	      }

	    if (DECL_CONTEXT (t) != NULL_TREE
		&& TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't'
		&& constructor_name (DECL_CONTEXT (t)) == DECL_NAME (t))
	      name = constructor_name (ctx);

	    if (DECL_CONSTRUCTOR_P (t) && TYPE_USES_VIRTUAL_BASECLASSES (ctx))
	      {
		/* Since we didn't know that this class had virtual bases until after
		   we instantiated it, we have to recreate the arguments to this
		   constructor, as otherwise it would miss the __in_chrg parameter.  */
		tree newtype, parm;
		tree parms = TREE_CHAIN (TYPE_ARG_TYPES (type));
		parms = hash_tree_chain (integer_type_node, parms);
		newtype = build_cplus_method_type (ctx,
						   TREE_TYPE (type),
						   parms);
		newtype = build_type_variant (newtype,
					      TYPE_READONLY (type),
					      TYPE_VOLATILE (type));
		type = newtype;

		fnargs = copy_node (DECL_ARGUMENTS (t));
		TREE_CHAIN (fnargs) = TREE_CHAIN (DECL_ARGUMENTS (t));

		/* In this case we need "in-charge" flag saying whether
		   this constructor is responsible for initialization
		   of virtual baseclasses or not.  */
		parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
		/* Mark the artificial `__in_chrg' parameter as "artificial".  */
		SET_DECL_ARTIFICIAL (parm);
		DECL_ARG_TYPE (parm) = integer_type_node;
		DECL_REGISTER (parm) = 1;
		TREE_CHAIN (parm) = TREE_CHAIN (fnargs);
		TREE_CHAIN (fnargs) = parm;

		fnargs = tsubst (fnargs, args, nargs, t);
	      }
#if 0
	    fprintf (stderr, "\nfor function %s in class %s:\n",
		     IDENTIFIER_POINTER (name),
		     IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
#endif
	    for (i = 0; i < n_methods; i++)
	      {
		int pass;

		method = TREE_VEC_ELT (methods, i);
		if (method == NULL_TREE || DECL_NAME (method) != name)
		  continue;

		pass = 0;
	      maybe_error:
		for (; method; method = DECL_CHAIN (method))
		  {
		    my_friendly_assert (TREE_CODE (method) == FUNCTION_DECL,
					282);
		    if (! comptypes (type, TREE_TYPE (method), 1))
		      {
			tree mtype = TREE_TYPE (method);
			tree t1, t2;

			/* Keep looking for a method that matches
			   perfectly.  This takes care of the problem
			   where destructors (which have implicit int args)
			   look like constructors which have an int arg.  */
			if (pass == 0)
			  continue;

			t1 = TYPE_ARG_TYPES (mtype);
			t2 = TYPE_ARG_TYPES (type);
			if (TREE_CODE (mtype) == FUNCTION_TYPE)
			  t2 = TREE_CHAIN (t2);

			if (list_eq (t1, t2))
			  {
			    if (TREE_CODE (mtype) == FUNCTION_TYPE)
			      {
				tree newtype;
				newtype = build_function_type (TREE_TYPE (type),
							       TYPE_ARG_TYPES (type));
				newtype = build_type_variant (newtype,
							      TYPE_READONLY (type),
							      TYPE_VOLATILE (type));
				type = newtype;
				if (TREE_TYPE (type) != TREE_TYPE (mtype))
				  goto maybe_bad_return_type;
			      }
			    else if (TYPE_METHOD_BASETYPE (mtype)
				     == TYPE_METHOD_BASETYPE (type))
			      {
				/* Types didn't match, but arg types and
				   `this' do match, so the return type is
				   all that should be messing it up.  */
			      maybe_bad_return_type:
				if (TREE_TYPE (type) != TREE_TYPE (mtype))
				  error ("inconsistent return types for method `%s' in class `%s'",
					 IDENTIFIER_POINTER (name),
					 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
			      }
			    r = method;
			    break;
			  }
			found = 1;
			continue;
		      }
#if 0
		    fprintf (stderr, "\tfound %s\n\n",
			     IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method)));
#endif
		    if (DECL_ARTIFICIAL (method))
		      {
			cp_error ("template for method `%D' which has default implementation in class `%T'", name, ctx);
			if (in_decl)
			  cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl);
			return error_mark_node;
		      }

		    if (DECL_ARGUMENTS (method)
			&& ! TREE_PERMANENT (DECL_ARGUMENTS (method)))
		      /* @@ Is this early enough?  Might we want to do
			 this instead while processing the expansion?	 */
		      DECL_ARGUMENTS (method)
			= tsubst (DECL_ARGUMENTS (t), args, nargs, t);
		    r = method;
		    break;
		  }
		if (r == NULL_TREE && pass == 0)
		  {
		    pass = 1;
		    method = TREE_VEC_ELT (methods, i);
		    goto maybe_error;
		  }
	      }
	    if (r == NULL_TREE)
	      {
	      no_match:
		cp_error
		  (found
		   ? "template for method `%D' doesn't match any in class `%T'"
		   : "method `%D' not found in class `%T'", name, ctx);
		if (in_decl)
		  cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl);
		return error_mark_node;
	      }
	  }
	else
	  {
	    r = DECL_NAME (t);
	    {
	      tree decls;
	      int got_it = 0;

	      decls = lookup_name_nonclass (r);
	      if (decls == NULL_TREE)
		/* no match */;
	      else if (TREE_CODE (decls) == TREE_LIST)
		for (decls = TREE_VALUE (decls); decls ;
		     decls = DECL_CHAIN (decls))
		  {
		    if (TREE_CODE (decls) == FUNCTION_DECL
			&& TREE_TYPE (decls) == type)
		      {
			got_it = 1;
			r = decls;
			break;
		      }
		  }
	      else
		{
		  tree val = decls;
		  decls = NULL_TREE;
		  if (TREE_CODE (val) == FUNCTION_DECL
		      && TREE_TYPE (val) == type)
		    {
		      got_it = 1;
		      r = val;
		    }
		}

	      if (!got_it)
		{
		  tree a = build_decl_overload (r, TYPE_VALUES (type),
						DECL_CONTEXT (t) != NULL_TREE);
		  r = build_lang_decl (FUNCTION_DECL, r, type);
		  DECL_ASSEMBLER_NAME (r) = a;
		}
	      else if (TREE_STATIC (r))
		{
		  /* This overrides the template version, use it. */
		  return r;
		}
	    }
	  }
	TREE_PUBLIC (r) = 1;
	DECL_EXTERNAL (r) = 1;
	TREE_STATIC (r) = 0;
	DECL_INTERFACE_KNOWN (r) = 0;
	DECL_INLINE (r) = DECL_INLINE (t);
	DECL_THIS_INLINE (r) = DECL_THIS_INLINE (t);
	TREE_READONLY (r) = TREE_READONLY (t);
	TREE_THIS_VOLATILE (r) = TREE_THIS_VOLATILE (t);
	{
#if 0				/* Maybe later.  -jason  */
	  struct tinst_level *til = tinst_for_decl();

	  /* should always be true under new approach */
	  if (til)
	    {
	      DECL_SOURCE_FILE (r) = til->file;
	      DECL_SOURCE_LINE (r) = til->line;
	    }
	  else
#endif
	    {
	      DECL_SOURCE_FILE (r) = DECL_SOURCE_FILE (t);
	      DECL_SOURCE_LINE (r) = DECL_SOURCE_LINE (t);
	    }
	}
	DECL_CLASS_CONTEXT (r) = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
	make_decl_rtl (r, NULL_PTR, 1);
	DECL_ARGUMENTS (r) = fnargs;
	DECL_RESULT (r) = result;
#if 0
	if (DECL_CONTEXT (t) == NULL_TREE
	    || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't')
	  push_overloaded_decl_top_level (r, 0);
#endif
	return r;
      }

    case PARM_DECL:
      {
	tree r;
	r = build_decl (PARM_DECL, DECL_NAME (t), type);
	DECL_INITIAL (r) = TREE_TYPE (r);
	DECL_ARTIFICIAL (r) = DECL_ARTIFICIAL (t);
#ifdef PROMOTE_PROTOTYPES
	if ((TREE_CODE (type) == INTEGER_TYPE
	     || TREE_CODE (type) == ENUMERAL_TYPE)
	    && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
	  DECL_ARG_TYPE (r) = integer_type_node;
#endif
	if (TREE_CHAIN (t))
	  TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
	return r;
      }

    case TREE_LIST:
      {
	tree purpose, value, chain, result;
	int via_public, via_virtual, via_protected;

	if (t == void_list_node)
	  return t;

	via_public = TREE_VIA_PUBLIC (t);
	via_protected = TREE_VIA_PROTECTED (t);
	via_virtual = TREE_VIA_VIRTUAL (t);

	purpose = TREE_PURPOSE (t);
	if (purpose)
	  purpose = tsubst (purpose, args, nargs, in_decl);
	value = TREE_VALUE (t);
	if (value)
	  value = tsubst (value, args, nargs, in_decl);
	chain = TREE_CHAIN (t);
	if (chain && chain != void_type_node)
	  chain = tsubst (chain, args, nargs, in_decl);
	if (purpose == TREE_PURPOSE (t)
	    && value == TREE_VALUE (t)
	    && chain == TREE_CHAIN (t))
	  return t;
	result = hash_tree_cons (via_public, via_virtual, via_protected,
				 purpose, value, chain);
	TREE_PARMLIST (result) = TREE_PARMLIST (t);
	return result;
      }
    case TREE_VEC:
      {
	int len = TREE_VEC_LENGTH (t), need_new = 0, i;
	tree *elts = (tree *) alloca (len * sizeof (tree));
	bzero ((char *) elts, len * sizeof (tree));

	for (i = 0; i < len; i++)
	  {
	    elts[i] = tsubst (TREE_VEC_ELT (t, i), args, nargs, in_decl);
	    if (elts[i] != TREE_VEC_ELT (t, i))
	      need_new = 1;
	  }

	if (!need_new)
	  return t;

	t = make_tree_vec (len);
	for (i = 0; i < len; i++)
	  TREE_VEC_ELT (t, i) = elts[i];
	return t;
      }
    case POINTER_TYPE:
    case REFERENCE_TYPE:
      {
	tree r;
	enum tree_code code;
	if (type == TREE_TYPE (t))
	  return t;

	code = TREE_CODE (t);
	if (code == POINTER_TYPE)
	  r = build_pointer_type (type);
	else
	  r = build_reference_type (type);
	r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
	/* Will this ever be needed for TYPE_..._TO values?  */
	layout_type (r);
	return r;
      }
    case OFFSET_TYPE:
      return build_offset_type
	(tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
    case FUNCTION_TYPE:
    case METHOD_TYPE:
      {
	tree values = TYPE_ARG_TYPES (t);
	tree context = TYPE_CONTEXT (t);
	tree new_value;

	/* Don't bother recursing if we know it won't change anything.	*/
	if (values != void_list_node)
	  values = tsubst (values, args, nargs, in_decl);
	if (context)
	  context = tsubst (context, args, nargs, in_decl);
	/* Could also optimize cases where return value and
	   values have common elements (e.g., T min(const &T, const T&).  */

	/* If the above parameters haven't changed, just return the type.  */
	if (type == TREE_TYPE (t)
	    && values == TYPE_VALUES (t)
	    && context == TYPE_CONTEXT (t))
	  return t;

	/* Construct a new type node and return it.  */
	if (TREE_CODE (t) == FUNCTION_TYPE
	    && context == NULL_TREE)
	  {
	    new_value = build_function_type (type, values);
	  }
	else if (context == NULL_TREE)
	  {
	    tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
				args, nargs, in_decl);
	    new_value = build_cplus_method_type (base, type,
						 TREE_CHAIN (values));
	  }
	else
	  {
	    new_value = make_node (TREE_CODE (t));
	    TREE_TYPE (new_value) = type;
	    TYPE_CONTEXT (new_value) = context;
	    TYPE_VALUES (new_value) = values;
	    TYPE_SIZE (new_value) = TYPE_SIZE (t);
	    TYPE_ALIGN (new_value) = TYPE_ALIGN (t);
	    TYPE_MODE (new_value) = TYPE_MODE (t);
	    if (TYPE_METHOD_BASETYPE (t))
	      TYPE_METHOD_BASETYPE (new_value) = tsubst (TYPE_METHOD_BASETYPE (t),
							 args, nargs, in_decl);
	    /* Need to generate hash value.  */
	    my_friendly_abort (84);
	  }
	new_value = build_type_variant (new_value,
					TYPE_READONLY (t),
					TYPE_VOLATILE (t));
	return new_value;
      }
    case ARRAY_TYPE:
      {
	tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
	tree r;
	if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
	  return t;
	r = build_cplus_array_type (type, domain);
	return r;
      }

    case UNINSTANTIATED_P_TYPE:
      {
	int nparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (UPT_TEMPLATE (t)));
	tree argvec = make_tree_vec (nparms);
	tree parmvec = UPT_PARMS (t);
	int i;
	tree id, rt;
	for (i = 0; i < nparms; i++)
	  TREE_VEC_ELT (argvec, i) = tsubst (TREE_VEC_ELT (parmvec, i),
					     args, nargs, in_decl);
	id = lookup_template_class (DECL_NAME (UPT_TEMPLATE (t)), argvec, NULL_TREE);
	if (! IDENTIFIER_HAS_TYPE_VALUE (id)) {
	  instantiate_class_template(id, 0);
	  /* set up pending_classes */
	  add_pending_template (id);

	  TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (id)) =
	    IDENTIFIER_TYPE_VALUE (id);
	}
        rt = IDENTIFIER_TYPE_VALUE (id);

	/* kung: this part handles nested type in template definition */
        
	if ( !ANON_AGGRNAME_P (DECL_NAME(TYPE_NAME(t))))
          {
	    rt = search_nested_type_in_tmpl (rt, t);
          }

	return build_type_variant (rt, TYPE_READONLY (t), TYPE_VOLATILE (t));
      }

    case MINUS_EXPR:
    case PLUS_EXPR:
      return fold (build (TREE_CODE (t), TREE_TYPE (t),
			  tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
			  tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));

    case NEGATE_EXPR:
    case NOP_EXPR:
      return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
			   tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));

    default:
      sorry ("use of `%s' in function template",
	     tree_code_name [(int) TREE_CODE (t)]);
      return error_mark_node;
    }
}

tree
instantiate_template (tmpl, targ_ptr)
     tree tmpl, *targ_ptr;
{
  tree targs, fndecl;
  int i, len;
  struct pending_inline *p;
  struct template_info *t;
  struct obstack *old_fmp_obstack;
  extern struct obstack *function_maybepermanent_obstack;

  push_obstacks (&permanent_obstack, &permanent_obstack);
  old_fmp_obstack = function_maybepermanent_obstack;
  function_maybepermanent_obstack = &permanent_obstack;

  my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
  len = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));

  i = len;
  while (i--)
    targ_ptr[i] = copy_to_permanent (targ_ptr[i]);

  for (fndecl = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
       fndecl; fndecl = TREE_CHAIN (fndecl))
    {
      tree *t1 = &TREE_VEC_ELT (TREE_PURPOSE (fndecl), 0);
      for (i = len - 1; i >= 0; i--)
	if (simple_cst_equal (t1[i], targ_ptr[i]) <= 0)
	  goto no_match;

      /* Here, we have a match.  */
      fndecl = TREE_VALUE (fndecl);
      goto exit;

    no_match:
      ;
    }

  targs = make_tree_vec (len);
  i = len;
  while (i--)
    TREE_VEC_ELT (targs, i) = targ_ptr[i];

  /* substitute template parameters */
  fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr,
		   TREE_VEC_LENGTH (targs), tmpl);

  if (fndecl == error_mark_node)
    goto exit;

  assemble_external (fndecl);

  /* If it's a static member fn in the template, we need to change it
     into a FUNCTION_TYPE and chop off its this pointer.  */
  if (TREE_CODE (TREE_TYPE (DECL_RESULT (tmpl))) == METHOD_TYPE
      && DECL_STATIC_FUNCTION_P (fndecl))
    {
      revert_static_member_fn (&DECL_RESULT (tmpl), NULL, NULL);
      /* Chop off the this pointer that grokclassfn so kindly added
	 for us (it didn't know yet if the fn was static or not).  */
      DECL_ARGUMENTS (fndecl) = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
    }
     
  t = DECL_TEMPLATE_INFO (tmpl);

  /* If we have a preexisting version of this function, don't expand
     the template version, use the other instead.  */
  if (TREE_STATIC (fndecl) || DECL_TEMPLATE_SPECIALIZATION (fndecl))
    {
      SET_DECL_TEMPLATE_SPECIALIZATION (fndecl);
      p = (struct pending_inline *)0;
    }
  else if (t->text)
    {
      SET_DECL_IMPLICIT_INSTANTIATION (fndecl);
      repo_template_used (fndecl);
      p = (struct pending_inline *) permalloc (sizeof (struct pending_inline));
      p->parm_vec = t->parm_vec;
      p->bindings = targs;
      p->can_free = 0;
      p->deja_vu = 0;
      p->buf = t->text;
      p->len = t->length;
      p->fndecl = fndecl;
      {
	int l = lineno;
	char * f = input_filename;

	lineno = p->lineno = t->lineno;
	input_filename = p->filename = t->filename;

	extract_interface_info ();

	if (interface_unknown && flag_external_templates)
	  {
	    if (DECL_CLASS_CONTEXT (fndecl)
		&& CLASSTYPE_INTERFACE_KNOWN (DECL_CLASS_CONTEXT (fndecl)))
	      {
		interface_unknown = 0;
		interface_only
		  = CLASSTYPE_INTERFACE_ONLY (DECL_CLASS_CONTEXT (fndecl));
	      }
	    else if (! DECL_IN_SYSTEM_HEADER (tmpl))
	      warn_if_unknown_interface (tmpl);
	  }

	if (interface_unknown || ! flag_external_templates)
	  p->interface = 1;		/* unknown */
	else
	  p->interface = interface_only ? 0 : 2;

	lineno = l;
	input_filename = f;

	extract_interface_info ();
      }
    }
  else
    p = (struct pending_inline *)0;

  DECL_TEMPLATE_INSTANTIATIONS (tmpl) =
    tree_cons (targs, fndecl, DECL_TEMPLATE_INSTANTIATIONS (tmpl));

  if (p == (struct pending_inline *)0)
    {
      /* do nothing */
    }
  else if (DECL_INLINE (fndecl))
    {
      DECL_PENDING_INLINE_INFO (fndecl) = p;
      p->next = pending_inlines;
      pending_inlines = p;
    }
  else
    {
      p->next = pending_template_expansions;
      pending_template_expansions = p;
    }
 exit:
  function_maybepermanent_obstack = old_fmp_obstack;
  pop_obstacks ();

  return fndecl;
}

/* classlevel should now never be true.  jason 4/12/94 */
void
undo_template_name_overload (id, classlevel)
     tree id;
     int classlevel;
{
  tree template;

  template = IDENTIFIER_TEMPLATE (id);
  if (!template)
    return;

#if 0 /* not yet, should get fixed properly later */
  poplevel (0, 0, 0);
#endif
#if 1 /* XXX */
  /* This was a botch... See `overload_template_name' just below.  */
  if (!classlevel)
    poplevel (0, 0, 0);
#endif
}

/* classlevel should now never be true.  jason 4/12/94 */
void
overload_template_name (id, classlevel)
     tree id;
     int classlevel;
{
  tree template, t, decl;
  struct template_info *tinfo;

  my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 284);
  template = IDENTIFIER_TEMPLATE (id);
  if (!template)
    return;

  template = TREE_PURPOSE (template);
  tinfo = DECL_TEMPLATE_INFO (template);
  template = DECL_NAME (template);
  my_friendly_assert (template != NULL_TREE, 285);

#if 1 /* XXX */
  /* This was a botch... names of templates do not get their own private
     scopes.  Rather, they should go into the binding level already created
     by push_template_decls.  Except that there isn't one of those for
     specializations.  */
  if (!classlevel)
    {
      pushlevel (1);
      declare_pseudo_global_level ();
    }
#endif

  t = xref_tag (tinfo->aggr, id, NULL_TREE, 1);
  my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
		      || TREE_CODE (t) == UNION_TYPE
		      || TREE_CODE (t) == UNINSTANTIATED_P_TYPE, 286);

  decl = build_decl (TYPE_DECL, template, t);
  SET_DECL_ARTIFICIAL (decl);

#if 0 /* fix this later */
  /* We don't want to call here if the work has already been done.  */
  t = (classlevel
       ? IDENTIFIER_CLASS_VALUE (template)
       : IDENTIFIER_LOCAL_VALUE (template));
  if (t
      && TREE_CODE (t) == TYPE_DECL
      && TREE_TYPE (t) == t)
    my_friendly_abort (85);
#endif

  if (classlevel)
    pushdecl_class_level (decl);
  else
    pushdecl (decl);

#if 0 /* This seems bogus to me; if it isn't, explain why.  (jason) */
  /* Fake this for now, just to make dwarfout.c happy.  It will have to
     be done in a proper way later on.  */
  DECL_CONTEXT (decl) = t;
#endif
}

extern struct pending_input *to_be_restored;

/* NAME is the IDENTIFIER value of a PRE_PARSED_CLASS_DECL. */
void
end_template_instantiation (name)
     tree name;
{
  tree t, decl;

  processing_template_defn--;
  if (!flag_external_templates)
    interface_unknown--;

  /* Restore the old parser input state.  */
  if (yychar == YYEMPTY)
    yychar = yylex ();
  if (yychar != END_OF_SAVED_INPUT)
    error ("parse error at end of class template");
  else
    {
      restore_pending_input (to_be_restored);
      to_be_restored = 0;
    }

  /* Our declarations didn't get stored in the global slot, since
     there was a (supposedly tags-transparent) scope in between.  */
  t = IDENTIFIER_TYPE_VALUE (name);
  my_friendly_assert (t != NULL_TREE
		      && TREE_CODE_CLASS (TREE_CODE (t)) == 't',
		      287);
  SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
  /* Make methods of template classes static, unless
     -fexternal-templates is given.  */
  if (!flag_external_templates)
    SET_CLASSTYPE_INTERFACE_UNKNOWN (t);
  decl = IDENTIFIER_GLOBAL_VALUE (name);
  my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 288);

  undo_template_name_overload (name, 0);
  t = IDENTIFIER_TEMPLATE (name);
  pop_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (t)), TREE_VALUE (t),
		      0);
  /* This will fix up the type-value field.  */
  pushdecl (decl);
  pop_from_top_level ();

#ifdef DWARF_DEBUGGING_INFO
  if (write_symbols == DWARF_DEBUG && TREE_CODE (decl) == TYPE_DECL)
    {
      /* We just completed the definition of a new file-scope type,
	 so we can go ahead and output debug-info for it now.  */
      TYPE_STUB_DECL (TREE_TYPE (decl)) = decl;
      rest_of_type_compilation (TREE_TYPE (decl), 1);
    }
#endif /* DWARF_DEBUGGING_INFO */

  /* Restore interface/implementation settings.	 */
  extract_interface_info ();
}

/* Store away the text of an template.  */

void
reinit_parse_for_template (yychar, d1, d2)
     int yychar;
     tree d1, d2;
{
  struct template_info *template_info;
  extern struct obstack inline_text_obstack; /* see comment in lex.c */

  if (d2 == NULL_TREE || d2 == error_mark_node)
    {
    lose:
      /* @@ Should use temp obstack, and discard results.  */
      reinit_parse_for_block (yychar, &inline_text_obstack, 1);
      return;
    }

  if (TREE_CODE (d2) == IDENTIFIER_NODE)
    d2 = IDENTIFIER_GLOBAL_VALUE (d2);
  if (!d2)
    goto lose;
  template_info = DECL_TEMPLATE_INFO (d2);
  if (!template_info)
    {
      template_info = (struct template_info *) permalloc (sizeof (struct template_info));
      bzero ((char *) template_info, sizeof (struct template_info));
      DECL_TEMPLATE_INFO (d2) = template_info;
    }
  template_info->filename = input_filename;
  template_info->lineno = lineno;
  reinit_parse_for_block (yychar, &inline_text_obstack, 1);
  template_info->text = obstack_base (&inline_text_obstack);
  template_info->length = obstack_object_size (&inline_text_obstack);
  obstack_finish (&inline_text_obstack);
  template_info->parm_vec = d1;
}

/* Type unification.

   We have a function template signature with one or more references to
   template parameters, and a parameter list we wish to fit to this
   template.  If possible, produce a list of parameters for the template
   which will cause it to fit the supplied parameter list.

   Return zero for success, 2 for an incomplete match that doesn't resolve
   all the types, and 1 for complete failure.  An error message will be
   printed only for an incomplete match.

   TPARMS[NTPARMS] is an array of template parameter types;
   TARGS[NTPARMS] is the array of template parameter values.  PARMS is
   the function template's signature (using TEMPLATE_PARM_IDX nodes),
   and ARGS is the argument list we're trying to match against it.

   If SUBR is 1, we're being called recursively (to unify the arguments of
   a function or method parameter of a function template), so don't zero
   out targs and don't fail on an incomplete match. */

int
type_unification (tparms, targs, parms, args, nsubsts, subr)
     tree tparms, *targs, parms, args;
     int *nsubsts, subr;
{
  tree parm, arg;
  int i;
  int ntparms = TREE_VEC_LENGTH (tparms);

  my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
  my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
  /* ARGS could be NULL (via a call from parse.y to
     build_x_function_call).  */
  if (args)
    my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
  my_friendly_assert (ntparms > 0, 292);

  if (!subr)
    bzero ((char *) targs, sizeof (tree) * ntparms);

  while (parms
	 && parms != void_list_node
	 && args
	 && args != void_list_node)
    {
      parm = TREE_VALUE (parms);
      parms = TREE_CHAIN (parms);
      arg = TREE_VALUE (args);
      args = TREE_CHAIN (args);

      if (arg == error_mark_node)
	return 1;
      if (arg == unknown_type_node)
	return 1;

      if (! uses_template_parms (parm)
	  && TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
	{
	  if (can_convert_arg (parm, TREE_TYPE (arg), arg))
	    continue;
	  return 1;
	}
	
#if 0
      if (TREE_CODE (arg) == VAR_DECL)
	arg = TREE_TYPE (arg);
      else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
	arg = TREE_TYPE (arg);
#else
      if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
	{
	  my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
	  if (TREE_CODE (arg) == TREE_LIST
	      && TREE_TYPE (arg) == unknown_type_node
	      && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
	    {
	      int nsubsts, ntparms;
	      tree *targs;

	      /* Have to back unify here */
	      arg = TREE_VALUE (arg);
	      nsubsts = 0;
	      ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg));
	      targs = (tree *) alloca (sizeof (tree) * ntparms);
	      parm = tree_cons (NULL_TREE, parm, NULL_TREE);
	      return type_unification (DECL_TEMPLATE_PARMS (arg), targs,
				       TYPE_ARG_TYPES (TREE_TYPE (arg)),
				       parm, &nsubsts, 0);
	    }
	  arg = TREE_TYPE (arg);
	}
#endif
      if (TREE_CODE (arg) == REFERENCE_TYPE)
	arg = TREE_TYPE (arg);

      if (TREE_CODE (parm) != REFERENCE_TYPE)
	{
	  if (TREE_CODE (arg) == FUNCTION_TYPE
	      || TREE_CODE (arg) == METHOD_TYPE)
	    arg = build_pointer_type (arg);
	  else if (TREE_CODE (arg) == ARRAY_TYPE)
	    arg = build_pointer_type (TREE_TYPE (arg));
	  else
	    arg = TYPE_MAIN_VARIANT (arg);
	}

      switch (unify (tparms, targs, ntparms, parm, arg, nsubsts))
	{
	case 0:
	  break;
	case 1:
	  return 1;
	}
    }
  /* Fail if we've reached the end of the parm list, and more args
     are present, and the parm list isn't variadic.  */
  if (args && args != void_list_node && parms == void_list_node)
    return 1;
  /* Fail if parms are left and they don't have default values.	 */
  if (parms
      && parms != void_list_node
      && TREE_PURPOSE (parms) == NULL_TREE)
    return 1;
  if (!subr)
    for (i = 0; i < ntparms; i++)
      if (!targs[i])
	{
	  error ("incomplete type unification");
	  return 2;
	}
  return 0;
}

/* Tail recursion is your friend.  */
static int
unify (tparms, targs, ntparms, parm, arg, nsubsts)
     tree tparms, *targs, parm, arg;
     int *nsubsts, ntparms;
{
  int idx;

  /* I don't think this will do the right thing with respect to types.
     But the only case I've seen it in so far has been array bounds, where
     signedness is the only information lost, and I think that will be
     okay.  */
  while (TREE_CODE (parm) == NOP_EXPR)
    parm = TREE_OPERAND (parm, 0);

  if (arg == error_mark_node)
    return 1;
  if (arg == unknown_type_node)
    return 1;
  if (arg == parm)
    return 0;

  switch (TREE_CODE (parm))
    {
    case TEMPLATE_TYPE_PARM:
      (*nsubsts)++;
      if (TEMPLATE_TYPE_TPARMLIST (parm) != tparms)
	{
	  error ("mixed template headers?!");
	  my_friendly_abort (86);
	  return 1;
	}
      idx = TEMPLATE_TYPE_IDX (parm);
#if 0
      /* Template type parameters cannot contain cv-quals; i.e.
         template <class T> void f (T& a, T& b) will not generate
	 void f (const int& a, const int& b).  */
      if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
	  || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
	return 1;
      arg = TYPE_MAIN_VARIANT (arg);
#else
      {
	int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
	int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
	arg = cp_build_type_variant (arg, constp, volatilep);
      }
#endif
      /* Simple cases: Value already set, does match or doesn't.  */
      if (targs[idx] == arg)
	return 0;
      else if (targs[idx])
	return 1;
      /* Check for mixed types and values.  */
      if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
	return 1;
      targs[idx] = arg;
      return 0;
    case TEMPLATE_CONST_PARM:
      (*nsubsts)++;
      idx = TEMPLATE_CONST_IDX (parm);
      if (targs[idx] == arg)
	return 0;
      else if (targs[idx])
	{
	  tree t = targs[idx];
	  if (TREE_CODE (t) == TREE_CODE (arg))
	    switch (TREE_CODE (arg))
	      {
	      case INTEGER_CST:
		if (tree_int_cst_equal (t, arg))
		  return 0;
		break;
	      case REAL_CST:
		if (REAL_VALUES_EQUAL (TREE_REAL_CST (t), TREE_REAL_CST (arg)))
		  return 0;
		break;
	      /* STRING_CST values are not valid template const parms.  */
	      default:
		;
	      }
	  my_friendly_abort (87);
	  return 1;
	}
/*	else if (typeof arg != tparms[idx])
	return 1;*/

      targs[idx] = copy_to_permanent (arg);
      return 0;

    case POINTER_TYPE:
      if (TREE_CODE (arg) != POINTER_TYPE)
	return 1;
      return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
		    nsubsts);

    case REFERENCE_TYPE:
      if (TREE_CODE (arg) == REFERENCE_TYPE)
	arg = TREE_TYPE (arg);
      return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg, nsubsts);

    case ARRAY_TYPE:
      if (TREE_CODE (arg) != ARRAY_TYPE)
	return 1;
      if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
		 nsubsts) != 0)
	return 1;
      return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
		    nsubsts);

    case REAL_TYPE:
    case INTEGER_TYPE:
      if (TREE_CODE (arg) != TREE_CODE (parm))
	return 1;

      if (TREE_CODE (parm) == INTEGER_TYPE)
	{
	  if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
	      && unify (tparms, targs, ntparms,
			TYPE_MIN_VALUE (parm), TYPE_MIN_VALUE (arg), nsubsts))
	    return 1;
	  if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
	      && unify (tparms, targs, ntparms,
			TYPE_MAX_VALUE (parm), TYPE_MAX_VALUE (arg), nsubsts))
	    return 1;
	}
      /* As far as unification is concerned, this wins.	 Later checks
	 will invalidate it if necessary.  */
      return 0;

      /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
    case INTEGER_CST:
      if (TREE_CODE (arg) != INTEGER_CST)
	return 1;
      return !tree_int_cst_equal (parm, arg);

    case MINUS_EXPR:
      {
	tree t1, t2;
	t1 = TREE_OPERAND (parm, 0);
	t2 = TREE_OPERAND (parm, 1);
	return unify (tparms, targs, ntparms, t1,
		      fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
		      nsubsts);
      }

    case TREE_VEC:
      {
	int i;
	if (TREE_CODE (arg) != TREE_VEC)
	  return 1;
	if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
	  return 1;
	for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
	  if (unify (tparms, targs, ntparms,
		     TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
		     nsubsts))
	    return 1;
	return 0;
      }

    case UNINSTANTIATED_P_TYPE:
      {
	tree a;
	/* Unification of something that is not a class fails.  */
	if (! IS_AGGR_TYPE (arg))
	  return 1;
	a = IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (arg));
	if (a && UPT_TEMPLATE (parm) == TREE_PURPOSE (a))
	  return unify (tparms, targs, ntparms, UPT_PARMS (parm),
			TREE_VALUE (a), nsubsts);
	/* FIXME: Should check base conversions here.  */
	return 1;
      }

    case RECORD_TYPE:
      if (TYPE_PTRMEMFUNC_FLAG (parm))
	return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
		      arg, nsubsts);

      /* Allow trivial conversions.  */
      if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg)
	  || TYPE_READONLY (parm) < TYPE_READONLY (arg)
	  || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
	return 1;
      return 0;

    case METHOD_TYPE:
      if (TREE_CODE (arg) != METHOD_TYPE)
	return 1;
      goto check_args;

    case FUNCTION_TYPE:
      if (TREE_CODE (arg) != FUNCTION_TYPE)
	return 1;
     check_args:
      if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
		 TREE_TYPE (arg), nsubsts))
	return 1;
      return type_unification (tparms, targs, TYPE_ARG_TYPES (parm),
			       TYPE_ARG_TYPES (arg), nsubsts, 1);

    case OFFSET_TYPE:
      if (TREE_CODE (arg) != OFFSET_TYPE)
	return 1;
      if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
		 TYPE_OFFSET_BASETYPE (arg), nsubsts))
	return 1;
      return unify (tparms, targs, ntparms, TREE_TYPE (parm),
		    TREE_TYPE (arg), nsubsts);

    default:
      sorry ("use of `%s' in template type unification",
	     tree_code_name [(int) TREE_CODE (parm)]);
      return 1;
    }
}


#undef DEBUG

int
do_pending_expansions ()
{
  struct pending_inline *i, *new_list = 0;

  {
    tree t;
    for (t = template_classes; t; t = TREE_CHAIN (t))
      instantiate_member_templates (TREE_PURPOSE (t));
  }
  
  if (!pending_template_expansions)
    return 0;

#ifdef DEBUG
  fprintf (stderr, "\n\n\t\t IN DO_PENDING_EXPANSIONS\n\n");
#endif

  i = pending_template_expansions;
  while (i)
    {
      tree context;

      struct pending_inline *next = i->next;
      tree t = i->fndecl;

      int decision = 0;
#define DECIDE(N) do {decision=(N); goto decided;} while(0)

      my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
			  || TREE_CODE (t) == VAR_DECL, 294);
      if (TREE_ASM_WRITTEN (t))
	DECIDE (0);

      if (DECL_EXPLICIT_INSTANTIATION (t))
	DECIDE (DECL_NOT_REALLY_EXTERN (t));
      else if (! flag_implicit_templates)
	DECIDE (0);

      if (i->interface == 1)
	/* OK, it was an implicit instantiation.  */
	{
	  if (SUPPORTS_WEAK)
	    DECL_WEAK (t) = 1;
	  else
	    TREE_PUBLIC (t) = 0;
	}

      /* If it's a method, let the class type decide it.
	 @@ What if the method template is in a separate file?
	 Maybe both file contexts should be taken into account?
	 Maybe only do this if i->interface == 1 (unknown)?  */
      context = DECL_CONTEXT (t);
      if (context != NULL_TREE
	  && TREE_CODE_CLASS (TREE_CODE (context)) == 't')
	{
	  /* I'm interested in the context of this version of the function,
	     not the original virtual declaration.  */
	  context = DECL_CLASS_CONTEXT (t);

	  /* If `unknown', we might want a static copy.
	     If `implementation', we want a global one.
	     If `interface', ext ref.  */
	  if (CLASSTYPE_INTERFACE_KNOWN (context))
	    DECIDE (!CLASSTYPE_INTERFACE_ONLY (context));
#if 1 /* This doesn't get us stuff needed only by the file initializer.  */
	  DECIDE (TREE_USED (t));
#else /* This compiles too much stuff, but that's probably better in
	 most cases than never compiling the stuff we need.  */
	  DECIDE (1);
#endif
	}

      if (i->interface == 1)
	DECIDE (TREE_USED (t));
      else
	DECIDE (i->interface);

    decided:
#ifdef DEBUG
      print_node_brief (stderr, decision ? "yes: " : "no: ", t, 0);
      fprintf (stderr, "\t%s\n",
	       (DECL_ASSEMBLER_NAME (t)
		? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t))
		: ""));
#endif
      if (decision)
	{
	  i->next = pending_inlines;
	  pending_inlines = i;
	}
      else
	{
	  i->next = new_list;
	  new_list = i;
	}
      i = next;
    }
  pending_template_expansions = new_list;
  if (!pending_inlines)
    return 0;
  do_pending_inlines ();
  return 1;
}


struct pending_template {
  struct pending_template *next;
  tree id;
};

static struct pending_template* pending_templates;

void
do_pending_templates ()
{
  struct pending_template* t;
  
  for ( t = pending_templates; t; t = t->next)
    {
      instantiate_class_template (t->id, 1);
    }

  for ( t = pending_templates; t; t = pending_templates)
    {
      pending_templates = t->next;
      free(t);
    }
}

static void
add_pending_template (pt)
     tree pt;
{
  struct pending_template *p;
  
  p = (struct pending_template *) malloc (sizeof (struct pending_template));
  p->next = pending_templates;
  pending_templates = p;
  p->id = pt;
}

void
mark_function_instantiated (result, extern_p)
     tree result;
     int extern_p;
{
  if (DECL_TEMPLATE_INSTANTIATION (result))
    SET_DECL_EXPLICIT_INSTANTIATION (result);
  TREE_PUBLIC (result) = 1;

  if (! extern_p)
    {
      DECL_INTERFACE_KNOWN (result) = 1;
      DECL_NOT_REALLY_EXTERN (result) = 1;
    }
}

/* called from the parser.  */
void
do_function_instantiation (declspecs, declarator, storage)
     tree declspecs, declarator, storage;
{
  tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0,
			      NULL_TREE, NULL_TREE);
  tree name;
  tree fn;
  tree result = NULL_TREE;
  int extern_p = 0;

  /* If we've already seen this template instance, use it.  */
  if (name = DECL_ASSEMBLER_NAME (decl),
      fn = IDENTIFIER_GLOBAL_VALUE (name),
      fn && DECL_TEMPLATE_INSTANTIATION (fn))
    result = fn;
  else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
    {
      for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
	if (decls_match (fn, decl)
	    && DECL_DEFER_OUTPUT (fn))
	  {
	    result = fn;
	    break;
	  }
	else if (TREE_CODE (fn) == TEMPLATE_DECL)
	  {
	    int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn));
	    tree *targs = (tree *) malloc (sizeof (tree) * ntparms);
	    int i, dummy = 0;
	    i = type_unification (DECL_TEMPLATE_PARMS (fn), targs,
				  TYPE_ARG_TYPES (TREE_TYPE (fn)),
				  TYPE_ARG_TYPES (TREE_TYPE (decl)),
				  &dummy, 0);
	    if (i == 0)
	      {
		if (result)
		  cp_error ("ambiguous template instantiation for `%D' requested", decl);
		else
		  result = instantiate_template (fn, targs);
	      }
	    free (targs);
	  }
    }
  if (! result)
    {
      cp_error ("no matching template for `%D' found", decl);
      return;
    }

  if (flag_external_templates)
    return;

  if (storage == NULL_TREE)
    ;
  else if (storage == ridpointers[(int) RID_EXTERN])
    extern_p = 1;
  else
    cp_error ("storage class `%D' applied to template instantiation",
	      storage);
  mark_function_instantiated (result, extern_p);
  repo_template_instantiated (result, extern_p);
}

void
mark_class_instantiated (t, extern_p)
     tree t;
     int extern_p;
{
  SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
  SET_CLASSTYPE_INTERFACE_KNOWN (t);
  CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
  CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
  TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
  if (! extern_p)
    {
      CLASSTYPE_DEBUG_REQUESTED (t) = 1;
      rest_of_type_compilation (t, 1);
    }
}     

void
do_type_instantiation (name, storage)
     tree name, storage;
{
  tree t = TREE_TYPE (name);
  int extern_p = 0;
  int nomem_p = 0;

  /* With -fexternal-templates, explicit instantiations are treated the same
     as implicit ones.  */
  if (flag_external_templates)
    return;

  if (TYPE_SIZE (t) == NULL_TREE)
    {
      cp_error ("explicit instantiation of `%#T' before definition of template",
		t);
      return;
    }

  if (storage == NULL_TREE)
    /* OK */;
  else if (storage == ridpointers[(int) RID_INLINE])
    nomem_p = 1;
  else if (storage == ridpointers[(int) RID_EXTERN])
    extern_p = 1;
  else
    {
      cp_error ("storage class `%D' applied to template instantiation",
		storage);
      extern_p = 0;
    }

  /* We've already instantiated this.  */
  if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
      && extern_p)
    return;

  if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
    {
      mark_class_instantiated (t, extern_p);
      repo_template_instantiated (t, extern_p);
    }

  if (nomem_p)
    return;

  {
    tree tmp;
    /* Classes nested in template classes currently don't have an
       IDENTIFIER_TEMPLATE--their out-of-line members are handled
       by the enclosing template class.  Note that there are name
       conflict bugs with this approach. */
    tmp = TYPE_IDENTIFIER (t);
    if (IDENTIFIER_TEMPLATE (tmp))
      instantiate_member_templates (tmp);

    /* this should really be done by instantiate_member_templates */
    tmp = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
    for (; tmp; tmp = TREE_CHAIN (tmp))
      if (DECL_TEMPLATE_INSTANTIATION (tmp))
	{
	  mark_function_instantiated (tmp, extern_p);
	  repo_template_instantiated (tmp, extern_p);
	}

#if 0
    for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
      {
	if (TREE_CODE (tmp) == VAR_DECL)
	  /* eventually do something */;
      }
#endif

    for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
      if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
	do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
  }
}

tree
create_nested_upt (scope, name)
     tree scope, name;
{
  tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
  tree d = build_decl (TYPE_DECL, name, t);

  TYPE_NAME (t) = d;
  TYPE_VALUES (t) = TYPE_VALUES (scope);
  TYPE_CONTEXT (t) = scope;

  pushdecl (d);
  return d;
}