summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_sequence.cpp
blob: 6ffdfbe899721aab5618a5a267ffb81c18c0f3bf (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    be_sequence.cpp
//
// = DESCRIPTION
//    Extension of class AST_Sequence that provides additional means for C++
//    mapping.
//
// = AUTHOR
//    Copyright 1994-1995 by Sun Microsystems, Inc.
//    and
//    Aniruddha Gokhale
//
// ============================================================================

#include "idl.h"
#include "idl_extern.h"
#include "be.h"
#include "be_visitor_sequence.h"

/*
 * BE_Sequence
 */
be_sequence::be_sequence (void)
  : mt_ (be_sequence::MNG_UNKNOWN)
{
  this->size_type (be_decl::VARIABLE); // always the case
}

be_sequence::be_sequence (AST_Expression *v, AST_Type *t)
  : AST_Sequence (v, t),
    AST_Decl (AST_Decl::NT_sequence,
              NULL,
              NULL),
    mt_ (be_sequence::MNG_UNKNOWN)
{
  // check if we are bounded or unbounded. An expression value of 0 means
  // unbounded
  if (v->ev ()->u.ulval == 0)
    {
      this->unbounded_ = I_TRUE;
    }
  else
    {
      this->unbounded_ = I_FALSE;
    }

  this->size_type (be_decl::VARIABLE); // a sequence data type is always
                                       // VARIABLE
}

idl_bool
be_sequence::unbounded (void) const
{
  return this->unbounded_;
}

// helper to create_name
char *
be_sequence::gen_name (void)
{
  char namebuf [NAMEBUFSIZE];
  be_type *bt; // base type;

  ACE_OS::memset (namebuf, '\0', NAMEBUFSIZE);  // reset the buffer
  // retrieve the base type
  bt = be_type::narrow_from_decl (this->base_type ());
  if (!bt)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_sequence::"
                         "gen_name - "
                         "bad base type\n"),
                        0);
    }
  if (bt->node_type () == AST_Decl::NT_sequence)
    {
      // our base type is an anonymous sequence
      be_sequence *seq;
      seq = be_sequence::narrow_from_decl (bt);
      if (!seq)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_name - "
                             "error converting base type to sequence\n"),
                            0);
        }
      seq->set_defined_in (this); // set ourselves as its parent
      this->fe_add_sequence (seq); // add the child to our scope
      ACE_OS::sprintf (namebuf, "_tao_seq_%s", seq->gen_name ());
    }
  else
    {
      ACE_OS::sprintf (namebuf, "_tao_seq_%s", bt->local_name ()->get_string ());
    }
  // append the size (if any)
  if (!this->unbounded_)
    {
      ACE_OS::sprintf (namebuf, "%s_%d", namebuf, this->max_size ()->ev
                       ()->u.ulval);
    }
  return ACE_OS::strdup (namebuf);
}

// create a name for ourselves
int
be_sequence::create_name (be_typedef *node)
{
  static char namebuf [NAMEBUFSIZE];
  UTL_ScopedName *n = NULL;
  be_decl *scope; // scope in which we are defined

  // if there is a typedef node, we use its name as our name
  if (node)
    {
      n = (UTL_ScopedName *)node->name ()->copy ();
      this->set_name (n); // set our name
    }
  else
    {

      ACE_OS::memset (namebuf, '\0', NAMEBUFSIZE);  // reset the buffer
      ACE_OS::strcpy (namebuf, this->gen_name ()); // generate a local name

      // now see if we have a fully scoped name and if so, generate one
      scope = be_scope::narrow_from_scope (this->defined_in ())->decl ();
      if (scope)
        {
          // make a copy of the enclosing scope's  name
          n = (UTL_ScopedName *)scope->name ()->copy () ;

          // add our local name as the last component
          n->nconc (new UTL_ScopedName (new Identifier (ACE_OS::strdup
                                                        (namebuf), 1,
                                                        0, I_FALSE),
                                        NULL));
          // set the fully scoped name
          this->set_name (n);
        }
      else
        {
          // We better be not here because we must be inside some scope,
          // atleast the ROOT scope.
          return -1;
        }
    }
  return 0;
}

// Does this sequence have a managed type sequence element?
be_sequence::MANAGED_TYPE
be_sequence::managed_type (void)
{
  if (this->mt_ == be_sequence::MNG_UNKNOWN) // not calculated yet
    {
      be_type  *bt, *prim_type; // base types

      bt = be_type::narrow_from_decl (this->base_type ());

      if (bt->node_type () == AST_Decl::NT_typedef)
        {
          // get the primitive base type of this typedef node
          be_typedef *t = be_typedef::narrow_from_decl (bt);
          prim_type = t->primitive_base_type ();
        }
      else
        prim_type = bt;

      // determine if we need a managed type and which one
      switch (prim_type->node_type ())
        {
        case AST_Decl::NT_interface:
        case AST_Decl::NT_interface_fwd:
          this->mt_ = be_sequence::MNG_OBJREF;
          break;
        case AST_Decl::NT_string:
          this->mt_ = be_sequence::MNG_STRING;
          break;
        case AST_Decl::NT_pre_defined:
          {
            be_predefined_type *bpd = be_predefined_type::narrow_from_decl
              (prim_type);
            if (bpd->pt () == AST_PredefinedType::PT_pseudo)
              {
                this->mt_ = be_sequence::MNG_OBJREF;
              }
            else
              {
                this->mt_ = be_sequence::MNG_NONE;
              }
          }
          break;
        default:
          this->mt_ = be_sequence::MNG_NONE;
        } // end of switch
    }
  return this->mt_;
}

int
be_sequence::gen_client_header (void)
{
#if 0 /* visitor code */
  TAO_OutStream *ch; // output stream
  TAO_NL  nl;        // end line
  be_type *bt;       // type node
  be_state *s;       // state based code gen object
#endif /* visitor code */

  if (!this->cli_hdr_gen_)
    {
      // retrieve a singleton instance of the code generator
      TAO_CodeGen *cg = TAO_CODEGEN::instance ();

      // first create a name for ourselves. We defer name creation for
      // ourselves to this point since named sequences should get the name
      // of the typedef node, else some other technique of name generation
      // should be used.
      if (this->create_name () == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "be_sequence::gen_client_header - name creation failed\n"),
                            -1);
        }

      be_visitor *visitor_seq_ch = cg->make_visitor (0);
        //        (TAO_CodeGen::TAO_SEQUENCE_BODY_CH);

      if (this->accept (visitor_seq_ch) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence - "
                             "gen_client_header"
                             "\n"),
                            -1);
        }

#if 0 /* visitor code */
      ch = cg->client_header ();

      // generate the ifdefined macro for the sequence type
      ch->gen_ifdef_macro (this->flatname ());

      ch->indent (); // start with the current indentation level

      *ch << "// *************************************************************"
          << nl;
      *ch << "// class " << this->local_name () << nl;
      *ch << "// *************************************************************"
              << nl << nl;

      *ch << "class " << idl_global->export_macro ()
      << " " << this->local_name () << nl;
      *ch << "{" << nl;
      *ch << "public:\n";
      ch->incr_indent (0);

      // retrieve the base type since we may need to do some code
      // generation for the base type.
      bt = be_type::narrow_from_decl (this->base_type ());
      if (!bt)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence.cpp - "
                             "Bad base type\n"),
                            -1);
        }

      cg->push (TAO_CodeGen::TAO_SEQUENCE_BASE_CH); // set current code gen
      // state
      s = cg->make_state ();
      if (!s || (s->gen_code (bt, this) == -1))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence.cpp - "
                             "base type codegen failed\n"),
                            -1);
        }
      cg->pop ();

      // now generate the sequence body
      cg->push (TAO_CodeGen::TAO_SEQUENCE_BODY_CH);
      s = cg->make_state ();
      if (!s)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_header - "
                             "bad state\n"),
                            -1);
        }

      // generate constructors
      ch->indent ();
      *ch << this->local_name () << " (void); // default constructor" << nl;
      // check whether we are bounded or not. Depending on that the mapping is
      // slightly different as shown below
      if (this->unbounded_)
        {
          *ch << local_name () << " (CORBA::ULong max);" << nl;
          *ch << local_name () << " (CORBA::ULong max, CORBA::ULong length, " << nl;
        }
      else
        {
          // bounded seq does not take the "max" argument
          *ch << local_name () << " (CORBA::ULong length, " << nl;
        }

      *ch << "  ";
      // generate the type info for the element type
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_header - "
                             "state based codegen failed\n"),
                            -1);
        }

      *ch << " *value, CORBA::Boolean release=CORBA::B_FALSE);" << nl;
      *ch << local_name () << "(const " << local_name () <<
        " &); // copy constructor" << nl;
      *ch << "~" << this->local_name () << " (void);" << nl;
      *ch << this->local_name () << " &operator= (const " <<
        this->local_name () << " &);" << nl;
      *ch << "CORBA::ULong maximum (void) const;" << nl;
      *ch << "void length (CORBA::ULong);" << nl;
      *ch << "CORBA::ULong length (void) const;" << nl;

      // we need a new state here because the [] operator for strings and obj
      // references returns a managed type as opposed to the normal type
      cg->push (TAO_CodeGen::TAO_SEQELEM_RETTYPE_CH);
      s = cg->make_state ();
      if (!s || s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_header - "
                             "state based codegen failed\n"),
                            -1);
        }
      *ch << "operator[] (CORBA::ULong index);" << nl;
      *ch << "const ";
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_header - "
                             "state based codegen failed\n"),
                            -1);
        }
      *ch << "operator[] (CORBA::ULong index) const;" << nl;
      cg->pop (); // back to the previous state

      s = cg->make_state ();
      // generate the static allocbuf and freebuf methods
      *ch << "static ";
      if (!s || s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_header - "
                             "state based codegen failed\n"),
                            -1);
        }
      *ch << " *allocbuf (CORBA::ULong nelems);" << nl;
      *ch << "static void freebuf (";
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_header - "
                             "state based codegen failed\n"),
                            -1);
        }
      *ch << " *);\n" ;

      // the spec says that for managed sequence types, the freebuf method
      // should individually free up the elements before deleting the
      // buffer. However, freebuf does not have a parameter that indicates the
      // total number of elements that are in the buffer. Hence we provide a
      // helper method to freebuf
      switch (this->managed_type ())
        {
        case be_sequence::MNG_OBJREF:
        case be_sequence::MNG_STRING:
          ch->indent ();
          *ch << "static void freebuf (" << nl;
          if (s->gen_code (bt, this) == -1)
            {
              ACE_ERROR_RETURN ((LM_ERROR,
                                 "(%N:%l) be_sequence::"
                                 "gen_client_header - "
                                 "state based codegen failed\n"),
                                -1);
            }
          *ch << " *, CORBA::ULong);\n" ;
          break;
        default:
          break;
        }
      ch->decr_indent ();
      *ch << "private:\n";
      ch->incr_indent ();
      *ch << "CORBA::ULong maximum_;" << nl;
      *ch << "CORBA::ULong length_;" << nl;
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_header - "
                             "state based codegen failed\n"),
                            -1);
        }

      *ch << " *buffer_;" << nl;
      *ch << "CORBA::Boolean release_;\n";
      ch->decr_indent ();
      *ch << "};\n";
      ch->indent ();
      *ch << "typedef " << this->local_name () << "* "
          << this->local_name () << "_ptr;\n";
      // Generate the typecode decl
      if (this->is_nested ())
        {
          // we have a scoped name
          ch->indent ();
          *ch << "static CORBA::TypeCode_ptr " << this->tc_name
            ()->last_component () << ";\n\n";
        }
      else
        {
          // we are in the ROOT scope
          ch->indent ();
          *ch << "extern "
              << idl_global->export_macro ()
              << " CORBA::TypeCode_ptr "
              << this->tc_name ()->last_component () << ";\n\n";
        }

      ch->gen_endif (); // endif macro

      // generate the ifdefined macro for the var type
      ch->gen_ifdef_macro (this->flatname (), "_var");

      // generate the var and out types
      if (this->gen_var_defn () == -1)
      {
        ACE_ERROR_RETURN ((LM_ERROR,
                           "(%N:%l) be_sequence::"
                           "gen_client_header - "
                           "codegen for _var failed\n"),
                          -1);
      }

      ch->gen_endif ();

      // generate the ifdefined macro for the var type
      ch->gen_ifdef_macro (this->flatname (), "_out");

      if (this->gen_out_defn () == -1)
      {
        ACE_ERROR_RETURN ((LM_ERROR,
                           "(%N:%l) be_sequence::"
                           "gen_client_header - "
                           "codegen for _out failed\n"),
                          -1);
      }
      ch->gen_endif ();

      cg->pop ();
#endif /* visitor code */

      this->cli_hdr_gen_ = I_TRUE;
    } // if (cli_hdr_gen_)
  return 0;
}

int
be_sequence::gen_client_stubs (void)
{
  TAO_OutStream *cs; // output stream
  TAO_NL  nl;        // end line
#if 0 /* visitor code */
  be_type *bt; // base type
  be_state *s; //state object
#endif /* visitor code */

  if (!this->cli_stub_gen_)
    {
      // retrieve a singleton instance of the code generator
      TAO_CodeGen *cg = TAO_CODEGEN::instance ();

      cs = cg->client_stubs (); // retrieve the client stubs stream
      // generate the ifdefined macro for the sequence type
      cs->gen_ifdef_macro (this->flatname ());

#if 0 /* visitor code */
      // retrieve base type
      bt = be_type::narrow_from_decl (this->base_type ());
      if (!bt)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_stubs - "
                             "bad type\n"),
                            -1);
        }

      cg->push (TAO_CodeGen::TAO_SEQUENCE_BASE_CS);
      s = cg->make_state ();

      // generate stubs for our base type if it itself is a sequence  or we
      // need to generate code for our managed types
      if (!s || (s->gen_code (bt, this) == -1))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_stubs - "
                             "state based codegen failed\n"),
                            -1);
        }

      cg->pop ();

      // generate the methods of the sequence C++ mapping
      cg->push (TAO_CodeGen::TAO_SEQUENCE_BODY_CS);
      s = cg->make_state ();
      if (!s)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_stubs - "
                             "bad state\n"),
                            -1);
        }


      *cs << "// *************************************************************"
          << nl;
      *cs << "// class " << this->name () << nl;
      *cs << "// *************************************************************\n\n";

      // copy constructor
      cs->indent ();
      *cs << "// copy constructor" << nl;
      *cs << this->name () << "::" << this->local_name () <<
        " (const " << this->name () << " &seq)" << nl;
      *cs << "  : maximum_ (seq.maximum_)," << nl;
      *cs << "    length_ (seq.length_)," << nl;
      *cs << "    buffer_ (" << this->name () << "::allocbuf (seq.maximum_)),"
          << nl;
      *cs << "    release_ (1) // we always own it" << nl;
      *cs << "{\n";
      cs->incr_indent ();
      // copy each element
      switch (this->managed_type ())
        {
        case be_sequence::MNG_OBJREF:
          {
            *cs << "for (CORBA::ULong i=0; i < seq.length_; i++)" << nl;
            *cs << "{" << nl;
            *cs << "  this->buffer_[i] = " << bt->name () << "::_duplicate ("
                << "seq.buffer_[i]);" << nl;
            *cs << "}\n";
          }
          break;
        case be_sequence::MNG_STRING:
          {
            *cs << "for (CORBA::ULong i=0; i < seq.length_; i++)" << nl;
            *cs << "{" << nl;
            *cs << "  this->buffer_[i] = CORBA::string_dup (" <<
              "seq.buffer_[i]);" << nl;
            *cs << "}\n";
          }
          break;
        default: // all other types are self managed, just assign them.
          *cs << "for (CORBA::ULong i=0; i < seq.length_; i++)" << nl;
          *cs << "  this->buffer_[i] = seq.buffer_[i];\n";
        }
      cs->decr_indent ();
      *cs << "}\n\n";

      // constructor only for unbounded seq. This takes in "max length"
      if (this->unbounded_)
        {
          cs->indent ();
          *cs << "// constructor for unbounded seq" << nl;
          *cs << this->name () << "::" << this->local_name () <<
            "(CORBA::ULong max )" << nl;
          *cs << "  : maximum_ (max)," << nl;
          *cs << "    length_ (0)," << nl;
          *cs << "    buffer_ (" << this->name () << "::allocbuf (max))," << nl;
          *cs << "    release_ (1) // owns" << nl;
          *cs << "{\n";
          *cs << "}\n\n";
        }

      // constructor that takes in the data buffer
      cs->indent ();
      *cs << "// constructor from data buffer" << nl;
      *cs << this->name () << "::" << this->local_name ();
      // depending on whether we are bounded or not, the constructor has
      // different sets of parameters
      if (this->unbounded_)
        {
          *cs << " (CORBA::ULong max, CORBA::ULong length, " << nl;
        }
      else
        {
          // bounded seq does not take the "max" argument
          *cs << " (CORBA::ULong length, " << nl;
        }
      *cs << "  ";
      if (s->gen_code (bt, this) == -1)
        return -1;
      *cs << " *value, CORBA::Boolean release)" << nl;
      // for unbounded we have the additional max parameter
      if (this->unbounded_)
        {
          *cs << "  : maximum_ (max)," << nl;
        }
      else
        {
          *cs << "  : maximum_ (" << this->max_size () << ")," << nl;
        }
      *cs << "    length_ (length)," << nl;
      *cs << "    buffer_ (value)," << nl;
      *cs << "    release_ (release) // ownership depends on release" << nl;
      *cs << "{\n";
      *cs << "}\n\n";

      // destructor
      cs->indent ();
      *cs << "// destructor" << nl;
      *cs << this->name () << "::~" << this->local_name () << " (void)" << nl;
      *cs << "{\n";
      cs->incr_indent ();
      *cs << "if (this->release_) // we own the buffer" << nl;
      *cs << "{\n";
      cs->incr_indent ();

      // only for obj references and strings, we need to free each individual
      // element
      // call the appropriate freebuf on the buffer
      if (this->managed_type () != be_sequence::MNG_NONE)
        {
          *cs << this->name () << "::freebuf (this->buffer_, " <<
            "this->maximum_);\n";
        }
      else
        *cs << this->name () << "::freebuf (this->buffer_);\n";

      cs->decr_indent ();
      *cs << "}\n";
      cs->decr_indent ();
      *cs << "}\n\n";

      // assignment operator
      cs->indent ();
      *cs << "// assignment operator" << nl;
      *cs << this->name () << "& " << nl;
      *cs << this->name () << "::operator=" <<
        " (const " << this->name () << " &seq)" << nl;
      *cs << "{\n";
      cs->incr_indent ();
      // check for equality
      *cs << "if (this == &seq) return *this;" << nl;
      // otherwise, if release flag, free the buffer
      *cs << "if (this->release_)" << nl;
      *cs << "{\n";
      cs->incr_indent ();
      // only for obj references and strings, we need to free each individual
      // element
      // call the appropriate freebuf on the buffer
      if (this->managed_type () != be_sequence::MNG_NONE)
        {
          *cs << this->name () << "::freebuf (this->buffer_, " <<
            "this->maximum_);\n";
        }
      else
        *cs << this->name () << "::freebuf (this->buffer_);\n";

      cs->decr_indent ();
      *cs << "}" << nl;

      *cs << "this->length_ = seq.length_;" << nl;
      *cs << "this->maximum_ = seq.maximum_;" << nl;
      *cs << "this->buffer_ = " << this->name () << "::allocbuf (seq.maximum_),"
          << nl;
      *cs << "this->release_ = 1; // we always own it" << nl;
      // copy each element
      switch (this->managed_type ())
        {
        case be_sequence::MNG_OBJREF:
          {
            *cs << "for (CORBA::ULong i=0; i < seq.length_; i++)" << nl;
            *cs << "{" << nl;
            *cs << "  this->buffer_[i] = " << bt->name () << "::_duplicate ("
                << "seq.buffer_[i]);" << nl;
            *cs << "}" << nl;
          }
          break;
        case be_sequence::MNG_STRING:
          {
            *cs << "for (CORBA::ULong i=0; i < seq.length_; i++)" << nl;
            *cs << "{" << nl;
            *cs << "  this->buffer_[i] = CORBA::string_dup (" <<
              "seq.buffer_[i]);" << nl;
            *cs << "}" << nl;
          }
          break;
        default: // all other types are self managed, just assign them.
          *cs << "for (CORBA::ULong i=0; i < seq.length_; i++)" << nl;
          *cs << "  this->buffer_[i] = seq.buffer_[i];" << nl;
        }
      *cs << "return *this;\n";
      cs->decr_indent ();
      *cs << "}\n\n";

      // the length method
      cs->indent ();
      *cs << "void" << nl;
      *cs << this->name () << "::length (CORBA::ULong length)" << nl;
      *cs << "{\n";
      cs->incr_indent ();
      if (!this->unbounded_) // bounded sequence - we cannot increase length
                             // more than its max => no reallocation necessary
        {
          // The sequence has a maximum length, check that the new
          // length is valid before changing anything.
          *cs << "if (length > this->maximum_)" << nl;
          *cs << "{\n";
          cs->incr_indent ();
          *cs << "// @@ throw something?" << nl;
          *cs << "return;\n";
          cs->decr_indent ();
          *cs << "}" << nl;
          *cs << "this->length_ = length;\n";
        }
      else // unbounded buffer - may need reallocation
        {
          // Reallocate the buffer.
          *cs << "if (length > this->maximum_) // need realloc" << nl;
          *cs << "{\n";
          cs->incr_indent ();
          if (s->gen_code (bt, this) == -1)
            {
              ACE_ERROR_RETURN ((LM_ERROR,
                                 "(%N:%l) be_sequence::"
                                 "gen_client_stubs - "
                                 "bad state\n"),
                                -1);
            }

          *cs << " *tmp = " << this->name ()
              << "::allocbuf (length);" << nl;
          *cs << "if (!tmp)" << nl;
          *cs << "  return;" << nl;

          // copy each element. Allocate a new manager and initialize it.
          switch (this->managed_type ())
            {
            case be_sequence::MNG_OBJREF:
              {
                *cs << "CORBA::ULong i;" << nl;
                *cs << "// copy old buffer" << nl;
                *cs << "for (i=0; i < this->length_; i++)" << nl;
                *cs << "{" << nl;
                *cs << "  tmp[i] = " << bt->name () << "::_duplicate ("
                    << "this->buffer_[i]);" << nl;
                *cs << "}" << nl;
              }
              break;
            case be_sequence::MNG_STRING:
              {
                *cs << "CORBA::ULong i;" << nl;
                *cs << "// copy old buffer" << nl;
                *cs << "for (i=0; i < this->length_; i++)" << nl;
                *cs << "{" << nl;
                *cs << "  tmp[i] = CORBA::string_dup (" <<
                  "this->buffer_[i]);" << nl;
                *cs << "}" << nl;
              }
              break;
            default: // all other types are self managed, just assign them.
              {
                *cs << "CORBA::ULong i;" << nl;
                *cs << "// copy old buffer" << nl;
                *cs << "for (i=0; i < this->length_; i++)" << nl;
                *cs << "{" << nl;
                *cs << "  tmp[i] = this->buffer_[i];" << nl;
                *cs << "}" << nl;
              }
            }
          // if release is set, we must free the previous buffer
          *cs << "if (this->release_) // free old one if we own it" << nl;
          *cs << "{\n";
          cs->incr_indent ();
          // only for obj references and strings, we need to free each individual
          // element. Others are self managed.
          // call the appropriate freebuf on the buffer
          if (this->managed_type () != be_sequence::MNG_NONE)
            {
              *cs << this->name () << "::freebuf (this->buffer_, " <<
                "this->maximum_);\n";
            }
          else
            *cs << this->name () << "::freebuf (this->buffer_);\n";

          cs->decr_indent ();
          *cs << "}" << nl;

          *cs << "//assign the newly reallocated buffer" << nl;
          *cs << "this->buffer_ = tmp;" << nl;
          *cs << "this->release_ = 1; //after reallocation, we own it" << nl;
          *cs << "this->maximum_ = length;\n";
          cs->decr_indent ();
          *cs << "}" << nl;;
          *cs << "this->length_ = length;\n";
        }
      cs->decr_indent ();
      *cs << "}\n\n";

      // the allocbuf method
      cs->indent ();
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_stubs - "
                             "state based codegen failed\n"),
                            -1);
        }

      *cs << " *" << nl;
      *cs << this->name () << "::allocbuf (CORBA::ULong nelems)" << nl;
      *cs << "{\n";
      cs->incr_indent ();
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_stubs - "
                             "state based codegen failed\n"),
                            -1);
        }

      *cs << " *buf = new ";
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_stubs - "
                             "state based codegen failed\n"),
                            -1);
        }
      *cs << "[nelems]; // allocate from heap" << nl;
      // the managed types must be initilaized
      switch (this->managed_type ())
        {
        case be_sequence::MNG_OBJREF:
          {
            *cs << "for (CORBA::ULong i=0; i < nelems; i++)" << nl;
            *cs << "  buf[i] = " << bt->name () << "::_nil ();" << nl;
          }
          break;
        case be_sequence::MNG_STRING:
          {
            *cs << "for (CORBA::ULong i=0; i < nelems; i++)" << nl;
            *cs << "  buf[i] = (char *)0;" << nl;
          }
          break;
        }
      *cs << "return buf;\n";

      cs->decr_indent ();
      *cs << "}\n\n";

      // extra freebuf method for managed types
      if (this->managed_type () != MNG_NONE)
        {
          cs->indent ();
          *cs << "void" << nl;
          *cs << this->name () << "::freebuf (";
          if (s->gen_code (bt, this) == -1)
            {
              ACE_ERROR_RETURN ((LM_ERROR,
                                 "(%N:%l) be_sequence::"
                                 "gen_client_inline - "
                                 "state based codegen failed\n"),
                                -1);
            }

          *cs << " *seq, CORBA::ULong nelems)" << nl;
          *cs << "{\n";
          cs->incr_indent ();
          *cs << "if (!seq) return; // null sequence" << nl;
          // the managed types must be individually freed. The others will have
          // their destructors called.
          switch (this->managed_type ())
            {
            case be_sequence::MNG_OBJREF:
              {
                *cs << "for (CORBA::ULong i=0; i < nelems; i++)" << nl;
                *cs << "  CORBA::release (seq[i]);" << nl;
              }
              break;
            case be_sequence::MNG_STRING:
              {
                *cs << "for (CORBA::ULong i=0; i < nelems; i++)" << nl;
                *cs << "  CORBA::string_free (seq[i]);" << nl;
              }
              break;
            }
          *cs << this->name () << "::freebuf (seq);\n";
          cs->decr_indent ();
          *cs << "}\n\n";
        }
#endif /* visitor code */
      // generate the typecode information here
      cs->indent (); // start from current indentation level
      *cs << "static const CORBA::Long _oc_" << this->flatname () << "[] =" <<
        nl;
      *cs << "{\n";
      cs->incr_indent (0);
      if (this->gen_encapsulation () == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_stubs - "
                             "codegen for encapsulation failed\n"),
                            -1);
        }

      cs->decr_indent ();
      *cs << "};" << nl;

      *cs << "static CORBA::TypeCode _tc__tc_" << this->flatname () <<
        " (CORBA::tk_sequence, sizeof (_oc_" <<  this->flatname () <<
        "), (char *) &_oc_" << this->flatname () <<
        ", CORBA::B_FALSE);" << nl;
      *cs << "CORBA::TypeCode_ptr " << this->tc_name () << " = &_tc__tc_" <<
        this->flatname () << ";\n\n";

#if 0
      cg->pop ();
#endif
      this->cli_stub_gen_ = I_TRUE;

      cs->gen_endif ();
    }
  return 0;
}

// Generates the client-side inline information
int
be_sequence::gen_client_inline (void)
{
  TAO_OutStream *ci; // output stream
#if 0 /* visitor code */
  TAO_NL  nl;        // end line
  be_state *s;       // code gen state
  be_type *bt;  // base type
#endif /* visitor code */

  if (!this->cli_inline_gen_)
    {
      // retrieve a singleton instance of the code generator
      TAO_CodeGen *cg = TAO_CODEGEN::instance ();

      ci = cg->client_inline ();
#if 0
      // retrieve base type
      bt = be_type::narrow_from_decl (this->base_type ());
      if (!bt)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_inline - "
                             "bad type\n"),
                            -1);
        }

      cg->push (TAO_CodeGen::TAO_SEQUENCE_BASE_CI);
      s = cg->make_state ();

      // generate inline methods for our base type if it itself is a sequence
      if (!s || (s->gen_code (bt, this) == -1))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_inline - "
                             "state based codegen failed\n"),
                            -1);
        }
      cg->pop ();

      // generate the methods of the sequence C++ mapping
      cg->push (TAO_CodeGen::TAO_SEQUENCE_BODY_CI);
      s = cg->make_state ();
      if (!s)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_inline - "
                             "bad state\n"),
                            -1);
        }

      // generate the ifdefined macro for type
      ci->gen_ifdef_macro (this->flatname ());

      *ci << "// *************************************************************"
          << nl;
      *ci << "// class " << this->name () << nl;
      *ci << "// *************************************************************\n\n";

      // freebuf method
      ci->indent ();
      *ci << "ACE_INLINE void" << nl;
      *ci << this->name () << "::freebuf (";
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_inline - "
                             "state based codegen failed\n"),
                            -1);
        }

      *ci << " *seq)" << nl;
      *ci << "{\n";
      ci->incr_indent ();
      *ci << "delete []seq;\n";
      ci->decr_indent ();
      *ci << "}\n\n";

      // default constructor
      ci->indent ();
      *ci << "//default constructor" << nl;
      *ci << "ACE_INLINE " << nl;
      *ci << this->name () << "::" << this->local_name () << " (void)" << nl;
      // for bounded and unbounded, initialize the data members differently
      if (this->unbounded_)
        {
          *ci << "  : maximum_ (0)," << nl;
          *ci << "    length_ (0)," << nl;
          *ci << "    buffer_ (0)," << nl;
          *ci << "    release_ (0) // does not own" << nl;
        }
      else
        {
          *ci << "  : maximum_ (" << this->max_size () << ")," << nl;
          *ci << "    length_ (0)," << nl;
          *ci << "    buffer_ (" << this->name () << "::allocbuf (" <<
            this->max_size () << "))," << nl;
          *ci << "    release_ (1) // owns" << nl;
        }
      *ci << "{}\n\n";

      // the maximum method
      ci->indent ();
      *ci << "ACE_INLINE CORBA::ULong" << nl;
      *ci << this->name () << "::maximum (void) const" << nl;
      *ci << "{\n";
      ci->incr_indent ();
      *ci << "return this->maximum_;\n";
      ci->decr_indent ();
      *ci << "}\n\n";

      // the length method
      ci->indent ();
      *ci << "ACE_INLINE CORBA::ULong" << nl;
      *ci << this->name () << "::length  (void) const" << nl;
      *ci << "{\n";
      ci->incr_indent ();
      *ci << "return this->length_;\n";
      ci->decr_indent ();
      *ci << "}\n\n";

      cg->pop ();

      // subscript operators (1) read-only, (2) read/write
      cg->push (TAO_CodeGen::TAO_SEQELEM_RETTYPE_CI);
      s = cg->make_state ();
      if (!s)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_inline - "
                             "bad state\n"),
                            -1);
        }

      ci->indent ();
      *ci << "ACE_INLINE ";
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_inline - "
                             "state based codegen failed\n"),
                            -1);
        }

      *ci << nl;
      *ci << this->name () << "::operator[] (CORBA::ULong index) // read/write"
         << nl;
      *ci << "{\n";
      ci->incr_indent ();
      switch (this->managed_type ())
        {
        case be_sequence::MNG_OBJREF:
          *ci << "return " << this->name () << "::TAO_ObjRefMngType (" <<
            "&this->buffer_[index], this->release_);\n";
          break;
        case be_sequence::MNG_STRING:
          *ci << "return " << this->name () << "::TAO_StrMngType (" <<
            "&this->buffer_[index], this->release_);\n";
          break;
        default:
          *ci << "return this->buffer_[index];\n";
        }
      ci->decr_indent ();
      *ci << "}\n\n";

      ci->indent ();
      *ci << "ACE_INLINE const ";
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_inline - "
                             "state based codegen failed\n"),
                            -1);
        }

      *ci << nl;
      *ci << this->name () << "::operator[] (CORBA::ULong index) const // read"
         << nl;
      *ci << "{\n";
      ci->incr_indent ();
      switch (this->managed_type ())
        {
        case be_sequence::MNG_OBJREF:
          *ci << "return " << this->name () << "::TAO_ObjRefMngType (" <<
            "&this->buffer_[index], this->release_);\n";
          break;
        case be_sequence::MNG_STRING:
          *ci << "return " << this->name () << "::TAO_StrMngType (" <<
            "&this->buffer_[index], this->release_);\n";
          break;
        default:
          *ci << "return this->buffer_[index];\n";
        }
      ci->decr_indent ();
      *ci << "}\n\n";
      cg->pop ();
      ci->gen_endif (); // endif macro
#endif
      // generate the ifdefined macro for the var type
      ci->gen_ifdef_macro (this->flatname (), "_var");

      // generate the implementations for the _var and _impl classes
      if (this->gen_var_impl () == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_inline - "
                             "var implementation failed\n"),
                            -1);
        }
      ci->gen_endif ();

      // generate the ifdefined macro for the var type
      ci->gen_ifdef_macro (this->flatname (), "_out");

      if (this->gen_out_impl () == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_client_inline - "
                             "out impl failed\n"),
                            -1);
        }
      ci->gen_endif (); // endif macro

      this->cli_inline_gen_ = I_TRUE;
    }
  return 0;
}

int
be_sequence::gen_server_header (void)
{
  // nothing to be done
  return 0;
}

int
be_sequence::gen_server_skeletons (void)
{
  // nothing to be done
  return 0;
}

// Generates the server-side inline
int
be_sequence::gen_server_inline (void)
{
  // nothing to be done
  return 0;
}

// generate the _var definition for ourself
int
be_sequence::gen_var_defn (void)
{
  TAO_OutStream *ch; // output stream
  TAO_NL  nl;        // end line
  char namebuf [NAMEBUFSIZE];  // names
  be_state *s;       // code gen state
  be_type *bt;  // base type
  be_decl *scope; // our enclosing scope


  ACE_OS::memset (namebuf, '\0', NAMEBUFSIZE);
  ACE_OS::sprintf (namebuf, "%s_var", this->local_name ()->get_string ());

  // retrieve a singleton instance of the code generator
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  ch = cg->client_header ();

  *ch << "// *************************************************************"
      << nl;
  *ch << "// class " << this->name () << "_var" << nl;
  *ch << "// *************************************************************\n\n";

  cg->push (TAO_CodeGen::TAO_SEQUENCE_BODY_CH);
  s = cg->make_state ();

  if (!s)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_sequence::"
                         "gen_var_defn - "
                         "state based codegen failed\n"),
                        -1);
    }

  // retrieve base type
  bt = be_type::narrow_from_decl (this->base_type ());

  if (this->defined_in ())
    scope = be_scope::narrow_from_scope (this->defined_in ())->decl ();
  else
    scope = 0;

  // generate the var definition (always in the client header).
  // Depending upon the data type, there are some differences which we account
  // for over here.

  ch->indent (); // start with whatever was our current indent level
  *ch << "class " << idl_global->export_macro ()
      << " " << namebuf << nl;
  *ch << "{" << nl;
  *ch << "public:\n";
  ch->incr_indent ();
  // default constr
  *ch << namebuf << " (void); // default constructor" << nl;
  // constr
  *ch << namebuf << " (" << this->local_name () << " *);" << nl;
  // copy constructor
  *ch << namebuf << " (const " << namebuf <<
    " &); // copy constructor" << nl;
  // destructor
  *ch << "~" << namebuf << " (void); // destructor" << nl;
  *ch << nl;
  // assignment operator from a pointer
  *ch << namebuf << " &operator= (" << this->local_name () << " *);" << nl;
  // assignment from _var
  *ch << namebuf << " &operator= (const " << namebuf <<
    " &);" << nl;

  // arrow operator
  *ch << this->local_name () << " *operator-> (void);" << nl;
  *ch << "const " << this->local_name () << " *operator-> (void) const;" << nl;
  *ch << nl;

  // other extra types (cast operators, [] operator, and others)

  // cast operator
  *ch << "operator const " << this->local_name () << " &() const;" << nl;
  *ch << "operator " << this->local_name () << " &();" << nl;
  *ch << "operator " << this->local_name () << " &() const;" << nl;

  // overloaded [] operator. The const version is not required for sequences

#if 0
  switch (this->managed_type ())
    {
    case be_sequence::MNG_STRING:
      if (scope)
        {
          *ch << "ACE_NESTED_CLASS (" << scope->name () << "," <<
            this->local_name () << "::TAO_StrMngType) ";
        }
      else
        {
          *ch << this->local_name () << "::TAO_StrMngType ";
        }
      break;
    case be_sequence::MNG_OBJREF:
      if (scope)
        {
          *ch << "ACE_NESTED_CLASS (" << scope->name () << "," <<
            this->local_name () << "::TAO_ObjRefMngType) ";
        }
      else
        {
          *ch << this->local_name () << "::TAO_ObjRefMngType ";
        }
      break;
    default:
      // gen code for base return type
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_var_defn - "
                             "state based codegen failed\n"),
                            -1);
        }
      *ch << " &";
    }

  be_visitor_sequence_elemtype elemtype (ch, this, bt);
  if (bt->accept (&elemtype) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_sequence::"
                         "gen_var_defn - "
                         "[] ret type gen failed\n"),
                        -1);
    }
#endif

  *ch << "operator[] (CORBA::ULong index);" << nl;

  *ch << "// in, inout, out, _retn " << nl;
  // the return types of in, out, inout, and _retn are based on the parameter
  // passing rules and the base type
  *ch << "const " << this->local_name () << " &in (void) const;" << nl;
  *ch << this->local_name () << " &inout (void);" << nl;
  *ch << this->local_name () << " *&out (void);" << nl;
  *ch << this->local_name () << " *_retn (void);" << nl;

  // generate an additional member function that returns the underlying pointer
  *ch << this->local_name () << " *ptr (void) const;\n";

  *ch << "\n";
  ch->decr_indent ();

  // generate the private section
  *ch << "private:\n";
  ch->incr_indent ();
  *ch << this->local_name () << " *ptr_;\n";

  ch->decr_indent ();
  *ch << "};\n\n";
  cg->pop ();

  return 0;
}

// implementation of the _var class. All of these get generated in the inline
// file
int
be_sequence::gen_var_impl (void)
{
  TAO_OutStream *ci; // output stream
  TAO_NL  nl;        // end line
  char fname [NAMEBUFSIZE];  // to hold the full and
  char lname [NAMEBUFSIZE];  // local _var names
  be_state *s;       // code gen state
  be_type *bt;  // base type


  ACE_OS::memset (fname, '\0', NAMEBUFSIZE);
  ACE_OS::sprintf (fname, "%s_var", this->fullname ());

  ACE_OS::memset (lname, '\0', NAMEBUFSIZE);
  ACE_OS::sprintf (lname, "%s_var", this->local_name ()->get_string ());

  // retrieve a singleton instance of the code generator
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  ci = cg->client_inline ();

  cg->push (TAO_CodeGen::TAO_SEQUENCE_BODY_CI);
  s = cg->make_state ();

  if (!s)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
          "be_sequence::gen_var_impl - invalid state obj\n"), -1);
    }

  // retrieve base type
  bt = be_type::narrow_from_decl (this->base_type ());

  // generate the var implementation in the inline file
  ci->indent (); // start with whatever was our current indent level

  *ci << "// *************************************************************"
      << nl;
  *ci << "// Inline operations for class " << fname << nl;
  *ci << "// *************************************************************\n\n";

  // default constr
  *ci << "ACE_INLINE" << nl;
  *ci << fname << "::" << lname <<
    " (void) // default constructor" << nl;
  *ci << "  " << ": ptr_ (0)" << nl;
  *ci << "{}\n\n";

  // constr from a _ptr
  ci->indent ();
  *ci << "ACE_INLINE" << nl;
  *ci << fname << "::" << lname << " (" << name () << " *p)" << nl;
  *ci << "  : ptr_ (p)" << nl;
  *ci << "{}\n\n";

  // copy constructor
  ci->indent ();
  *ci << "ACE_INLINE" << nl;
  *ci << fname << "::" << lname << " (const " << fname <<
    " &p) // copy constructor" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "if (p.ptr_)" << nl;
  *ci << "  this->ptr_ = new " << this->name () << "(*p.ptr_);" << nl;
  *ci << "else" << nl;
  *ci << "  this->ptr_ = 0;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // destructor
  ci->indent ();
  *ci << "ACE_INLINE" << nl;
  *ci << fname << "::~" << lname << " (void) // destructor" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "delete this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // assignment operator from a pointer
  ci->indent ();
  *ci << "ACE_INLINE " << fname << " &" << nl;
  *ci << fname << "::operator= (" << name () <<
    " *p)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "delete this->ptr_;" << nl;
  *ci << "this->ptr_ = p;" << nl;
  *ci << "return *this;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // assignment operator from _var
  ci->indent ();
  *ci << "ACE_INLINE " << fname << " &" << nl;
  *ci << fname << "::operator= (const " << fname <<
    " &p) // deep copy" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "if (this != &p)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "delete this->ptr_;" << nl;
  *ci << "this->ptr_ = new " << this->name () << " (*p.ptr_);\n";
  ci->decr_indent ();
  *ci << "}" << nl;
  *ci << "return *this;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // two arrow operators
  ci->indent ();
  *ci << "ACE_INLINE const " << this->name () << " *" << nl;
  *ci << fname << "::operator-> (void) const" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  ci->indent ();
  *ci << "ACE_INLINE " << this->name () << " *" << nl;
  *ci << fname << "::operator-> (void)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // other extra methods - 3 cast operator ()
  ci->indent ();
  *ci << "ACE_INLINE " << nl;
  *ci << fname << "::operator const " << name () <<
    " &() const // cast" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return *this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  ci->indent ();
  *ci << "ACE_INLINE " << nl;
  *ci << fname << "::operator " << name () << " &() // cast " << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return *this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  ci->indent ();
  *ci << "ACE_INLINE " << nl;
  *ci << fname << "::operator " << name () << " &() const// cast " << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return *this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // operator []
  ci->indent ();
  *ci << "ACE_INLINE ";

#if 0
  switch (this->managed_type ())
    {
    case be_sequence::MNG_STRING:
      *ci << this->name () << "::TAO_StrMngType ";
      break;
    case be_sequence::MNG_OBJREF:
      *ci << this->name () << "::TAO_ObjRefMngType ";
      break;
    default:
      // gen code for base return type
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_var_impl - "
                             "state based codegen failed\n"),
                            -1);
        }
      *ci << " &";
    }

  be_visitor_sequence_elemtype elemtype (ci, 0, bt);
  if (bt->accept (&elemtype) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_sequence::"
                         "gen_var_defn - "
                         "[] ret type gen failed\n"),
                        -1);
    }
#endif


  *ci << nl;
  *ci << fname << "::operator[] (CORBA::ULong index)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return this->ptr_->operator[] (index);\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // in, inout, out, and _retn
  ci->indent ();
  *ci << "ACE_INLINE const " << name () << " &" << nl;
  *ci << fname << "::in (void) const" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return *this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  ci->indent ();
  *ci << "ACE_INLINE " << name () << " &" << nl;
  *ci << fname << "::inout (void)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return *this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  ci->indent ();
  *ci << "// mapping for variable size " << nl;
  *ci << "ACE_INLINE " << name () << " *&" << nl;
  *ci << fname << "::out (void)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "delete this->ptr_;" << nl;
  *ci << "this->ptr_ = 0;" << nl;
  *ci << "return this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  ci->indent ();
  *ci << "ACE_INLINE " << name () << " *" << nl;
  *ci << fname << "::_retn (void)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << this->name () << " *tmp = this->ptr_;" << nl;
  *ci << "this->ptr_ = 0;" << nl;
  *ci << "return tmp;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // the additional ptr () member function
  ci->indent ();
  *ci << "ACE_INLINE " << name () << " *" << nl;
  *ci << fname << "::ptr (void) const" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  cg->pop ();

  return 0;
}

// generate the _out definition
int
be_sequence::gen_out_defn (void)
{
  TAO_OutStream *ch; // output stream
  TAO_NL  nl;        // end line
  char namebuf [NAMEBUFSIZE];  // to hold the _out name
  be_state *s;       // code gen state
  be_type *bt;  // base type
  be_decl *scope; // our enclosing scope

  ACE_OS::memset (namebuf, '\0', NAMEBUFSIZE);
  ACE_OS::sprintf (namebuf, "%s_out", this->local_name ()->get_string ());

  // retrieve a singleton instance of the code generator
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  ch = cg->client_header ();
  cg->push (TAO_CodeGen::TAO_SEQUENCE_BODY_CH);
  s = cg->make_state ();

  if (!s)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
          "be_sequence::gen_out_defn - invalid state obj\n"), -1);
    }

  // retrieve base type
  bt = be_type::narrow_from_decl (this->base_type ());

  if (this->defined_in ())
    scope = be_scope::narrow_from_scope (this->defined_in ())->decl ();
  else
    scope = 0;

  // generate the out definition (always in the client header)
  ch->indent (); // start with whatever was our current indent level

  *ch << "class " << idl_global->export_macro ()
      << " " << namebuf << nl;
  *ch << "{" << nl;
  *ch << "public:\n";
  ch->incr_indent ();

  // No default constructor

  // constructor from a pointer
  *ch << namebuf << " (" << this->local_name () << " *&);" << nl;
  // constructor from a _var &
  *ch << namebuf << " (" << this->local_name () << "_var &);" << nl;
  // constructor from a _out &
  *ch << namebuf << " (" << namebuf << " &);" << nl;
  // assignment operator from a _out &
  *ch << namebuf << " &operator= (" << namebuf << " &);" << nl;
  // assignment operator from a pointer &, cast operator, ptr fn, operator
  // -> and any other extra operators
  // assignment
  *ch << namebuf << " &operator= (" << this->local_name () << " *);" << nl;
  // operator ()
  *ch << "operator " << this->local_name () << " *&();" << nl;
  // ptr fn
  *ch << this->local_name () << " *&ptr (void);" << nl;
  // operator ->
  *ch << this->local_name () << " *operator-> (void);" << nl;

  // overloaded [] operator only for sequence. The const version is not
  // required

#if 0
  switch (this->managed_type ())
    {
    case be_sequence::MNG_STRING:
      if (scope)
        {
          *ch << "ACE_NESTED_CLASS (" << scope->name () << "," <<
            this->local_name () << "::TAO_StrMngType) ";
        }
      else
        {
          *ch << this->local_name () << "::TAO_StrMngType ";
        }
      break;
    case be_sequence::MNG_OBJREF:
      if (scope)
        {
          *ch << "ACE_NESTED_CLASS (" << scope->name () << "," <<
            this->local_name () << "::TAO_ObjRefMngType) ";
        }
      else
        {
          *ch << this->local_name () << "::TAO_ObjRefMngType ";
        }
      break;
    default:
      // gen code for base return type
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_out_defn - "
                             "state based codegen failed\n"),
                            -1);
        }
      *ch << " &";
    }

  be_visitor_sequence_elemtype elemtype(ch, this, bt);
  if (bt->accept (&elemtype) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_sequence::"
                         "gen_var_defn - "
                         "[] ret type gen failed\n"),
                        -1);
    }
#endif

  *ch << "operator[] (CORBA::ULong index);" << nl;
  *ch << "\n";
  ch->decr_indent ();
  *ch << "private:\n";
  ch->incr_indent ();

  *ch << this->local_name () << " *&ptr_;" << nl;
  *ch << "// assignment from T_var not allowed" << nl;
  *ch << "void operator= (const " << this->local_name () << "_var &);\n";

  ch->decr_indent ();
  *ch << "};\n\n";

  cg->pop ();
  return 0;
}

int
be_sequence::gen_out_impl (void)
{
  TAO_OutStream *ci; // output stream
  TAO_NL  nl;        // end line
  char fname [NAMEBUFSIZE];  // to hold the full and
  char lname [NAMEBUFSIZE];  // local _out names
  be_state *s;       // code gen state
  be_type *bt;  // base type


  ACE_OS::memset (fname, '\0', NAMEBUFSIZE);
  ACE_OS::sprintf (fname, "%s_out", this->fullname ());

  ACE_OS::memset (lname, '\0', NAMEBUFSIZE);
  ACE_OS::sprintf (lname, "%s_out", this->local_name ()->get_string ());

  // retrieve a singleton instance of the code generator
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  ci = cg->client_inline ();

  cg->push (TAO_CodeGen::TAO_SEQUENCE_BODY_CI);
  s = cg->make_state ();

  if (!s)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
          "be_sequence::gen_out_impl - invalid state obj\n"), -1);
    }

  // retrieve base type
  bt = be_type::narrow_from_decl (this->base_type ());

  // generate the out implementation in the inline file

  ci->indent (); // start with whatever was our current indent level

  *ci << "// *************************************************************"
      << nl;
  *ci << "// Inline operations for class " << fname << nl;
  *ci << "// *************************************************************\n\n";

  // constr from a pointer
  ci->indent ();
  *ci << "ACE_INLINE" << nl;
  *ci << fname << "::" << lname << " (" << name () << " *&p)" << nl;
  *ci << "  : ptr_ (p)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "this->ptr_ = 0;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // constructor from _var &
  ci->indent ();
  *ci << "ACE_INLINE" << nl;
  *ci << fname << "::" << lname << " (" << this->name () <<
    "_var &p) // constructor from _var" << nl;
  *ci << "  : ptr_ (p.out ())" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "delete this->ptr_;" << nl;
  *ci << "this->ptr_ = 0;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // copy constructor
  ci->indent ();
  *ci << "ACE_INLINE" << nl;
  *ci << fname << "::" << lname << " (" << fname <<
    " &p) // copy constructor" << nl;
  *ci << "  : ptr_ (p.ptr_)" << nl;
  *ci << "{}\n\n";

  // assignment operator from _out &
  ci->indent ();
  *ci << "ACE_INLINE " << fname << " &" << nl;
  *ci << fname << "::operator= (" << fname <<
    " &p)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "this->ptr_ = p.ptr_;" << nl;
  *ci << "return *this;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // assignment from _var is not allowed by a private declaration

  // assignment operator from pointer
  ci->indent ();
  *ci << "ACE_INLINE " << fname << " &" << nl;
  *ci << fname << "::operator= (" << this->name () <<
    " *p)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "this->ptr_ = p;" << nl;
  *ci << "return *this;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // other extra methods - cast operator ()
  ci->indent ();
  *ci << "ACE_INLINE " << nl;
  *ci << fname << "::operator " << this->name () <<
    " *&() // cast" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // ptr function
  ci->indent ();
  *ci << "ACE_INLINE " << this->name () << " *&" << nl;
  *ci << fname << "::ptr (void) // ptr" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // operator ->
  ci->indent ();
  *ci << "ACE_INLINE " << this->name () << " *" << nl;
  *ci << fname << "::operator-> (void)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  // sequence has an additional method
  ci->indent ();
  *ci << "ACE_INLINE ";
#if 0
  switch (this->managed_type ())
    {
    case be_sequence::MNG_STRING:
      *ci << this->name () << "::TAO_StrMngType ";
      break;
    case be_sequence::MNG_OBJREF:
      *ci << this->name () << "::TAO_ObjRefMngType ";
      break;
    default:
      // gen code for base return type
      if (s->gen_code (bt, this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_out_impl - "
                             "state based codegen failed\n"),
                            -1);
        }
      *ci << " &";
    }

  be_visitor_sequence_elemtype elemtype (ci, 0, bt);
  if (bt->accept (&elemtype) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_sequence::"
                         "gen_var_defn - "
                         "[] ret type gen failed\n"),
                        -1);
    }
#endif


  *ci << nl;
  *ci << fname << "::operator[] (CORBA::ULong index)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return this->ptr_->operator[] (index);\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  cg->pop ();
  return 0;
}

// generate the managed type
int
be_sequence::gen_managed_type_ch (void)
{
  TAO_OutStream *ch; // output stream
  TAO_NL  nl;        // end line
  char namebuf [NAMEBUFSIZE];  // names
  char typebuf [NAMEBUFSIZE];  // generated type
  be_type *bt;  // base type

  // retrieve a singleton instance of the code generator
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  ch = cg->client_header ();

  // retrieve base type
  bt = be_type::narrow_from_decl (this->base_type ());
  if (!bt)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_sequence::"
                         "gen_gen_managed_type_ch - "
                         "state based codegen failed\n"),
                        -1);
    }

  // set the name of the generated class
  // the name depends on whether we are interface  type or a string type
  ACE_OS::memset (namebuf, '\0', NAMEBUFSIZE);
  ACE_OS::memset (typebuf, '\0', NAMEBUFSIZE);
  switch (this->managed_type ())
    {
    case be_sequence::MNG_OBJREF:
      {
        ACE_OS::sprintf (namebuf, "%s", "TAO_ObjRefMngType");
        ACE_OS::sprintf (typebuf, "%s", bt->nested_type_name (this, "_ptr"));
      }
      break;
    case be_sequence::MNG_STRING:
      {
        ACE_OS::sprintf (namebuf, "%s", "TAO_StrMngType");
        ACE_OS::sprintf (typebuf, "%s", "char *");
      }
      break;
    default:
      // cannot be here
      {
        ACE_ERROR_RETURN ((LM_ERROR,
                           "(%N:%l) be_sequence::"
                           "gen_gen_managed_type_ch - "
                           "bad managed type\n"),
                          -1);
      }
    }

  ch->indent (); // start with whatever was our current indent level
  *ch << "class " << idl_global->export_macro ()
      << " " << namebuf << nl;
  *ch << "{" << nl;
  *ch << "public:\n";
  ch->incr_indent ();
  // generate the friend instruction
  *ch << "friend " << this->local_name () << ";" << nl;
  // default constr is protected as this managed type is not available
  // outside. Only this sequence can play with it.
  //*ch << namebuf << " (void); // default constructor" << nl;
  // copy constructor
  *ch << namebuf << "(const " << namebuf << " &); // copy ctor " << nl;
  // the constructor that will be used
  if (this->managed_type () == be_sequence::MNG_STRING)
    {
      *ch << namebuf << "(char **buffer, CORBA::Boolean release);" << nl;
    }
  else
    {
      *ch << namebuf << "(" << bt->nested_type_name (this, "_ptr*") <<
        ", CORBA::Boolean release);" << nl;
    }
  // destructor
  *ch << "~" << namebuf << " (void); // destructor" << nl;
  *ch << nl;
  // assignment operator from another managed type
  *ch << namebuf << " &operator= (const " << namebuf << "&);" << nl;
  // assignment operator from a pointer
  *ch << namebuf << " &operator= (" << typebuf << ");" << nl;

  // others
  if (this->managed_type () == be_sequence::MNG_STRING)
    {
      // assignment operator from a pointer to const
      *ch << namebuf << " &operator= (const " << typebuf << ");" << nl;
      // cast op
      *ch << "operator const char*() const;" << nl;
      *ch << "operator char *();" << nl;
      // overloaded [] operator - only for strings
      *ch << "char &operator[] (CORBA::ULong index);" << nl;
      *ch << "const char &operator[] (CORBA::ULong index) const;" << nl;
      *ch << "// in, inout, out, _retn " << nl;
      // the return types of in, out, inout, and _retn are based on the parameter
      // passing rules and the base type
      *ch << "const char *in (void) const;" << nl;
      *ch << "char *&inout (void);" << nl;
      *ch << "char *&out (void);" << nl;
      *ch << "char *_retn (void);" << nl;
    }
  else
    {
      *ch << "operator const " << bt->nested_type_name (this, "_ptr") <<
        " &() const;" << nl;
      *ch << "operator " << bt->nested_type_name (this, "_ptr") <<
        " &();" << nl;
      *ch << "// in, inout, out, _retn " << nl;
      // the return types of in, out, inout, and _retn are based on the parameter
      // passing rules and the base type
      *ch << "const " << bt->nested_type_name (this, "_ptr") <<
        " in (void) const;" << nl;
      *ch << bt->nested_type_name (this, "_ptr") << " &inout (void);" << nl;
      *ch << bt->nested_type_name (this, "_ptr") << " &out (void);" << nl;
      *ch << bt->nested_type_name (this, "_ptr") << " _retn (void);" << nl;
    }

  *ch << "\n";
  ch->decr_indent ();

  // generate the private section
  *ch << "private:\n";
  ch->incr_indent ();
  //  *ch << namebuf << "(const " << namebuf << " &); // copy ctor " << nl;
  *ch << typebuf << " *ptr_;" << nl;
  *ch << "CORBA::Boolean release_;\n";
  ch->decr_indent ();
  *ch << "};\n\n";

  return 0;
}

// implementation of the managed type class. All of these get generated in the
// inline file
int
be_sequence::gen_managed_type_ci (void)
{
  TAO_OutStream *ci; // output stream
  TAO_NL  nl;        // end line
  be_type *bt;  // base type
  char fnamebuf [NAMEBUFSIZE];  // full name
  char lnamebuf [NAMEBUFSIZE];  // local name
  char typebuf [NAMEBUFSIZE];  // type name
  char macro [NAMEBUFSIZE]; // for ifdef macro

  // retrieve a singleton instance of the code generator
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  // get the inline stream
  ci = cg->client_inline ();

  // retrieve base type
  bt = be_type::narrow_from_decl (this->base_type ());
  if (!bt)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_sequence.cpp::"
                         "gen_managed_types_ci - "
                         "Bad base type\n"),
                        -1);
    }

  // set the names (this is for easier code generation)
  ACE_OS::memset (fnamebuf, '\0', NAMEBUFSIZE);
  ACE_OS::memset (lnamebuf, '\0', NAMEBUFSIZE);
  ACE_OS::memset (typebuf, '\0', NAMEBUFSIZE);
  ACE_OS::memset (macro, '\0', NAMEBUFSIZE);
  switch (this->managed_type ())
    {
    case be_sequence::MNG_OBJREF:
      {
        ACE_OS::sprintf (fnamebuf, "%s::%s", this->fullname (), "TAO_ObjRefMngType");
        ACE_OS::sprintf (lnamebuf, "%s", "TAO_ObjRefMngType");
        ACE_OS::sprintf (typebuf, "%s_ptr", bt->fullname ());
        ACE_OS::sprintf (macro, "%s_%s", this->flatname (), "TAO_ObjRefMngType");
      }
      break;
    case be_sequence::MNG_STRING:
      {
        ACE_OS::sprintf (fnamebuf, "%s::%s", this->fullname (), "TAO_StrMngType");
        ACE_OS::sprintf (lnamebuf, "%s", "TAO_StrMngType");
        ACE_OS::sprintf (typebuf, "%s", "char *");
        ACE_OS::sprintf (macro, "%s_%s", this->flatname (), "TAO_StrMngType");
      }
      break;
    default:
      // cannot be here
      {
        ACE_ERROR_RETURN ((LM_ERROR,
                           "(%N:%l) be_sequence::"
                           "gen_managed_types_ci - "
                           "Bad managed type\n"),
                          -1);
      }
    }

  // generate the ifdefined macro for the managed
  ci->gen_ifdef_macro (macro);

  // generate the managed type implementation in the inline file
  ci->indent (); // start with whatever was our current indent level

  *ci << "// *************************************************************"
      << nl;
  *ci << "// Inline operations for class " << fnamebuf << nl;
  *ci << "// *************************************************************\n\n";

  // destructor
  ci->indent ();
  *ci << "ACE_INLINE " << nl;
  *ci << fnamebuf << "::~" << lnamebuf << " (void) // destructor" << nl;
  *ci << "{}\n\n";

  // copy constructor not allowed

  // operators
  if (this->managed_type () == be_sequence::MNG_STRING)
    {
      // constructor that will be used
      ci->indent ();
      *ci << "ACE_INLINE " << nl;
      *ci << fnamebuf << "::" << lnamebuf <<
        "(char **buffer, CORBA::Boolean release)" << nl;
      *ci << "  : ptr_ (buffer)," << nl;
      *ci << "    release_ (release)" << nl;
      *ci << "{}\n\n";

      // other extra methods - cast operator ()
      ci->indent ();
      *ci << "ACE_INLINE " << nl;
      *ci << fnamebuf << "::operator const " << typebuf <<
        " () const // cast" << nl;
      *ci << "{\n";
      ci->incr_indent ();
      *ci << "return *this->ptr_;\n";
      ci->decr_indent ();
      *ci << "}\n\n";

      ci->indent ();
      *ci << "ACE_INLINE " << nl;
      *ci << fnamebuf << "::operator " << typebuf << " () // cast " << nl;
      *ci << "{\n";
      ci->incr_indent ();
      *ci << "return *this->ptr_;\n";
      ci->decr_indent ();
      *ci << "}\n\n";

      // operator []
      ci->indent ();
      *ci << "ACE_INLINE char&" << nl;
      *ci << fnamebuf << "::operator[] (CORBA::ULong index)" << nl;
      *ci << "{\n";
      ci->incr_indent ();
      *ci << "return *this->ptr_[index];\n";
      ci->decr_indent ();
      *ci << "}\n\n";

      // operator []
      ci->indent ();
      *ci << "ACE_INLINE const char&" << nl;
      *ci << fnamebuf << "::operator[] (CORBA::ULong index) const" << nl;
      *ci << "{\n";
      ci->incr_indent ();
      *ci << "return *this->ptr_[index];\n";
      ci->decr_indent ();
      *ci << "}\n\n";

    }
  else if (this->managed_type () == be_sequence::MNG_OBJREF)
    {
      // constructor that will be used
      ci->indent ();
      *ci << "ACE_INLINE " << nl;
      *ci << fnamebuf << "::" << lnamebuf << "(" << bt->name () <<
        "_ptr* buffer, CORBA::Boolean release)" << nl;
      *ci << "  : ptr_ (buffer)," << nl;
      *ci << "    release_ (release)" << nl;
      *ci << "{}\n\n";

      // other extra methods - cast operator ()
      ci->indent ();
      *ci << "ACE_INLINE " << nl;
      *ci << fnamebuf << "::operator const " << typebuf <<
        " &() const // cast" << nl;
      *ci << "{\n";
      ci->incr_indent ();
      *ci << "return *this->ptr_;\n";
      ci->decr_indent ();
      *ci << "}\n\n";

      ci->indent ();
      *ci << "ACE_INLINE " << nl;
      *ci << fnamebuf << "::operator " << typebuf << " &() // cast " << nl;
      *ci << "{\n";
      ci->incr_indent ();
      *ci << "return *this->ptr_;\n";
      ci->decr_indent ();
      *ci << "}\n\n";

    }
  else
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_sequence::"
                         "gen_managed_types_ci - "
                         "Bad managed type\n"),
                        -1);
    }

  // in, inout
  ci->indent ();
  *ci << "ACE_INLINE const " << typebuf << nl;
  *ci << fnamebuf << "::in (void) const" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return *this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  ci->indent ();
  *ci << "ACE_INLINE " << typebuf << "&" << nl;
  *ci << fnamebuf << "::inout (void)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return *this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  ci->gen_endif ();
  return 0;
}

// implementation of the managed types
int
be_sequence::gen_managed_type_cs (void)
{
  TAO_OutStream *cs; // output stream
  TAO_NL  nl;        // end line
  be_type *bt;  // base type
  char fnamebuf [NAMEBUFSIZE];  // full name
  char lnamebuf [NAMEBUFSIZE];  // local name
  char typebuf [NAMEBUFSIZE];  // type name
  char macro [NAMEBUFSIZE]; // for ifdef macro

  // retrieve a singleton instance of the code generator
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  // get the stubs stream
  cs = cg->client_stubs ();

  // retrieve base type
  bt = be_type::narrow_from_decl (this->base_type ());
  if (!bt)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_sequence.cpp::"
                         "gen_managed_types_cs - "
                         "Bad base type\n"),
                        -1);
    }

  // set the names (this is for easier code generation)
  ACE_OS::memset (fnamebuf, '\0', NAMEBUFSIZE);
  ACE_OS::memset (lnamebuf, '\0', NAMEBUFSIZE);
  ACE_OS::memset (typebuf, '\0', NAMEBUFSIZE);
  ACE_OS::memset (macro, '\0', NAMEBUFSIZE);
  switch (this->managed_type ())
    {
    case be_sequence::MNG_OBJREF:
      {
        ACE_OS::sprintf (fnamebuf, "%s::%s", this->fullname (), "TAO_ObjRefMngType");
        ACE_OS::sprintf (lnamebuf, "%s", "TAO_ObjRefMngType");
        ACE_OS::sprintf (typebuf, "%s_ptr", bt->fullname ());
        ACE_OS::sprintf (macro, "%s_%s", this->flatname (), "TAO_ObjRefMngType");
      }
      break;
    case be_sequence::MNG_STRING:
      {
        ACE_OS::sprintf (fnamebuf, "%s::%s", this->fullname (), "TAO_StrMngType");
        ACE_OS::sprintf (lnamebuf, "%s", "TAO_StrMngType");
        ACE_OS::sprintf (typebuf, "%s", "char *");
        ACE_OS::sprintf (macro, "%s_%s", this->flatname (), "TAO_StrMngType");
      }
      break;
    default:
      // cannot be here
      {
        ACE_ERROR_RETURN ((LM_ERROR,
                           "(%N:%l) be_sequence.cpp::"
                           "gen_managed_types_cs - "
                           "Bad managed type\n"),
                          -1);
      }
    }

  // generate the ifdefined macro for the sequence type
  cs->gen_ifdef_macro (macro);

  switch (this->managed_type ())
    {
    case be_sequence::MNG_OBJREF:
      {
        // copy ctro
        cs->indent ();
        *cs << fnamebuf << "::" << lnamebuf << "(const " << fnamebuf <<
          " &_tao_mng_type)" << nl;
        *cs << "{\n";
        cs->incr_indent ();
        *cs << "*this->ptr_ = " << bt->name () <<
          "::_duplicate (*_tao_mng_type.ptr_);" << nl;
        *cs << "this->release_ = _tao_mng_type.release_;\n";
        cs->decr_indent ();
        *cs << "}\n\n";

        // assignment operator
        cs->indent ();
        *cs << fnamebuf << "&" << nl;
        *cs << fnamebuf << "::operator= (const " << fnamebuf <<
          " &_tao_mng_type)" << nl;
        *cs << "{\n";
        cs->incr_indent ();
        *cs << "if (this == &_tao_mng_type) return *this;" << nl;
        *cs << "if (this->release_) // need to free old one" << nl;
        *cs << "  CORBA::release (*this->ptr_);" << nl;
        *cs << "*this->ptr_ = " << bt->name () <<
          "::_duplicate (*_tao_mng_type.ptr_);" << nl;
        *cs << "return *this;\n";
        cs->decr_indent ();
        *cs << "}\n\n";

        // assignment operator from a pointer
        cs->indent ();
        *cs << fnamebuf << " &" << nl;
        *cs << fnamebuf << "::operator= (" << typebuf << " p)" << nl;
        *cs << "{\n";
        cs->incr_indent ();
        *cs << "if (this->release_) // need to free old one" << nl;
        *cs << "  CORBA::release (*this->ptr_);" << nl;
        *cs << "*this->ptr_ = p; // no copy" << nl;
        *cs << "return *this;\n";
        cs->decr_indent ();
        *cs << "}\n\n";

        // out method and _retn
        cs->indent ();
        *cs << "// mapping for variable size " << nl;
        *cs << bt->name () << "_ptr &" << nl;
        *cs << fnamebuf << "::out (void)" << nl;
        *cs << "{\n";
        cs->incr_indent ();
        *cs << "if (this->release_)" << nl;
        *cs << "  CORBA::release (*this->ptr_);" << nl;
        *cs << "*this->ptr_ = " << bt->name () << "::_nil ();" << nl;
        *cs << "return *this->ptr_;\n";
        cs->decr_indent ();
        *cs << "}\n\n";

        cs->indent ();
        *cs << bt->name () << "_ptr" << nl;
        *cs << fnamebuf << "::_retn (void)" << nl;
        *cs << "{\n";
        cs->incr_indent ();
        *cs << bt->name () << "_ptr tmp = *this->ptr_;" << nl;
        *cs << "*this->ptr_ = " << bt->name () << "::_nil ();" << nl;
        *cs << "return tmp;\n";
        cs->decr_indent ();
        *cs << "}\n\n";
      }
      break;
    case be_sequence::MNG_STRING:
      {
        // copy constructor
        cs->indent ();
        *cs << fnamebuf << "::" << lnamebuf << "(const " << fnamebuf <<
          " &_tao_mng_type)" << nl;
        *cs << "{\n";
        cs->incr_indent ();
        *cs << "*this->ptr_ = CORBA::string_dup (*_tao_mng_type.ptr_);" << nl;
        *cs << "this->release_ = _tao_mng_type.release_;\n";
        cs->decr_indent ();
        *cs << "}\n\n";

        // assignment operator (does not change the release flag)
        cs->indent ();
        *cs << fnamebuf << "&" << nl;
        *cs << fnamebuf << "::operator= (const " << fnamebuf <<
          " &_tao_mng_type)" << nl;
        *cs << "{\n";
        cs->incr_indent ();
        *cs << "if (this == &_tao_mng_type) return *this;" << nl;
        *cs << "if (this->release_) // need to free old one" << nl;
        *cs << "  CORBA::string_free (*this->ptr_);" << nl;
        *cs << "*this->ptr_ = CORBA::string_dup (*_tao_mng_type.ptr_);" << nl;
        *cs << "return *this;\n";
        cs->decr_indent ();
        *cs << "}\n\n";

        // assignment operator from a pointer
        cs->indent ();
        *cs << fnamebuf << " &" << nl;
        *cs << fnamebuf << "::operator= (" << typebuf << " p)" << nl;
        *cs << "{\n";
        cs->incr_indent ();
        *cs << "if (this->release_) // need to free old one" << nl;
        *cs << "  CORBA::string_free (*this->ptr_);" << nl;
        *cs << "*this->ptr_ = p; // no copy" << nl;
        *cs << "return *this;\n";
        cs->decr_indent ();
        *cs << "}\n\n";

        // assignment operator from a pointer to const
        cs->indent ();
        *cs << fnamebuf << " &" << nl;
        *cs << fnamebuf << "::operator= (const " << typebuf << " p)" << nl;
        *cs << "{\n";
        cs->incr_indent ();
        *cs << "if (this->release_) // need to free old one" << nl;
        *cs << "{" << nl;
        *cs << "  CORBA::string_free (*this->ptr_);" << nl;
        *cs << "  *this->ptr_ = CORBA::string_dup (p);// make copy" << nl;
        *cs << "}" << nl;
        *cs << "else // make no copy as we don't own it" << nl;
        *cs << "  *this->ptr_ = (char *)p; // no copy" << nl;
        *cs << "return *this;\n";
        cs->decr_indent ();
        *cs << "}\n\n";

        // out method and _retn
        cs->indent ();
        *cs << "// mapping for variable size " << nl;
        *cs << "char *&" << nl;
        *cs << fnamebuf << "::out (void)" << nl;
        *cs << "{\n";
        cs->incr_indent ();
        *cs << "if (this->release_)" << nl;
        *cs << "  CORBA::string_free (*this->ptr_);" << nl;
        *cs << "*this->ptr_ = 0;" << nl;
        *cs << "return *this->ptr_;\n";
        cs->decr_indent ();
        *cs << "}\n\n";

        cs->indent ();
        *cs << "char *" << nl;
        *cs << fnamebuf << "::_retn (void)" << nl;
        *cs << "{\n";
        cs->incr_indent ();
        *cs << "char *tmp = *this->ptr_;" << nl;
        *cs << "*this->ptr_ = 0;" << nl;
        *cs << "return tmp;\n";
        cs->decr_indent ();
        *cs << "}\n\n";
      }
      break;
    }

  cs->gen_endif ();
  return 0;
}

// generate typecode.
// Typecode for sequences comprises the enumerated value followed by the
// encapsulation of the parameters

int
be_sequence::gen_typecode (void)
{
  TAO_OutStream *cs; // output stream
  TAO_NL  nl;        // end line
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  cs = cg->client_stubs ();
  cs->indent (); // start from whatever indentation level we were at

  *cs << "CORBA::tk_sequence, // typecode kind" << nl;
  *cs << this->tc_encap_len () << ", // encapsulation length\n";
  // now emit the encapsulation
  return this->gen_encapsulation ();
}

// generate encapsulation
// An encapsulation for ourselves will be necessary when we are part of some
// other IDL type and a typecode for that other type is being generated. This
// will comprise our typecode kind. IDL types with parameters will additionally
// have the encapsulation length and the entire typecode description

int
be_sequence::gen_encapsulation (void)
{
  TAO_OutStream *os; // output stream
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();
  be_type *bt; // base type

  os = cg->client_stubs ();
  os->incr_indent ();

  *os << "TAO_ENCAP_BYTE_ORDER, // byte order\n";

  // emit typecode of element type
  bt = be_type::narrow_from_decl (this->base_type ());
  if (!bt || (bt->gen_typecode () == -1))
    {
      ACE_ERROR ((LM_ERROR, "be_sequence::gen_typecode - bad base type\n"));
      return -1;
    }

  //  emit the length
  os->decr_indent ();
  *os << this->max_size () << ",\n";
  return 0;
}

// compute typecode size
long
be_sequence::tc_size (void)
{
  // 4 bytes for enumeration, 4 bytes for storing encap length val, followed by the
  // actual encapsulation length
  return 4 + 4 + this->tc_encap_len ();
}

long
be_sequence::tc_encap_len (void)
{
  if (this->encap_len_ == -1) // not computed yet
    {
      be_type *bt; // base type

      this->encap_len_ = 4;  // holds the byte order flag
      // add the encapsulation length of our base type
      bt = be_type::narrow_from_decl (this->base_type ());
      if (!bt)
        {
          ACE_ERROR ((LM_ERROR,
                      "be_sequence::tc_encap_len - bad base type\n"));
          return 0;
        }
      this->encap_len_ += bt->tc_size ();
      this->encap_len_ += 4; // to hold the max size

    }
  return this->encap_len_;
}

/*
 * Add this be_sequence to the locally defined types in this scope
 */
be_sequence *
be_sequence::fe_add_sequence (be_sequence *t)
{
  if (t == NULL)
    return NULL;

  this->add_to_local_types(t);
  return t;
}

// overridden method
be_decl *
be_sequence::decl (void)
{
  return this;
}

int be_sequence::write_as_return (TAO_OutStream *stream,
                                  be_type *type)
{
  *stream << type->name () << " *";
  return 0;
}

int
be_sequence::accept (be_visitor *visitor)
{
  return visitor->visit_sequence (this);
}

// Narrowing
IMPL_NARROW_METHODS3 (be_sequence, AST_Sequence, be_scope, be_type)
IMPL_NARROW_FROM_DECL (be_sequence)