summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_interface.cpp
blob: 0434c4ecd0f445f10d11f1ea19d3a57c61a77a91 (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
// $Id$

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

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

ACE_RCSID(be, be_interface, "$Id$")

/*
 * BE_Interface
 */

// default constructor
be_interface::be_interface (void)
  : full_skel_name_ (0),
    skel_count_ (0),
    full_coll_name_ (0),
    local_coll_name_ (0),
    in_mult_inheritance_ (-1)
{
  this->size_type (be_decl::VARIABLE); // always the case
}

// constructor used to build the AST
be_interface::be_interface (UTL_ScopedName *n, AST_Interface **ih, long nih,
                            UTL_StrList *p)
  : AST_Interface (n, ih, nih, p),
    AST_Decl (AST_Decl::NT_interface, n, p),
    UTL_Scope (AST_Decl::NT_interface),
    full_skel_name_ (0),
    skel_count_ (0),
    full_coll_name_ (0),
    local_coll_name_ (0),
    in_mult_inheritance_ (-1)
{
  this->size_type (be_decl::VARIABLE); // always the case
}

be_interface::~be_interface (void)
{
  if (this->full_skel_name_ != 0)
    {
      delete[] this->full_skel_name_;
      this->full_skel_name_ = 0;
    }
  if (this->full_coll_name_ != 0)
    {
      delete[] this->full_coll_name_;
      this->full_coll_name_ = 0;
    }
  if (this->local_coll_name_ != 0)
    {
      delete[] this->local_coll_name_;
      this->local_coll_name_ = 0;
    }
}

// compute stringified fully qualified collocated class name.
void
be_interface::compute_coll_name (void)
{
  if (this->full_coll_name_ != 0)
    return;

  const char collocated[] = "_tao_collocated_";
  const char poa[] = "POA_";
  // Reserve enough room for the "POA_" prefix, the "_tao_collocated_"
  // prefix and the local name and the (optional) "::"
  int namelen = sizeof (collocated) + sizeof (poa);

  UTL_IdListActiveIterator *i;
  ACE_NEW (i, UTL_IdListActiveIterator (this->name ()));
  while (!i->is_done ())
    {
      // reserve 2 characters for "::".
      namelen += ACE_OS::strlen (i->item ()->get_string ()) + 2;
      i->next ();
    }
  delete i;

  ACE_NEW (this->full_coll_name_,
           char[namelen+1]);
  this->full_coll_name_[0] = 0; // null terminate the string...

  // Iterate again....
  ACE_NEW (i, UTL_IdListActiveIterator (this->name ()));

  // Only the first component get the "POA_" preffix.
  int poa_added = 0;
  while (!i->is_done ())
    {
      const char* item = i->item ()->get_string ();

      // Increase right away, so we can test for the final component
      // in the loop.
      i->next ();

      // We add the POA_ preffix only if the first component is not
      // the global scope...
      if (ACE_OS::strcmp (item, "") != 0)
        {
          if (!i->is_done ())
            {
              // We only add the POA_ preffix if there are more than
              // two components in the name, in other words, if the
              // class is inside some scope.
              if (!poa_added)
                {
                  ACE_OS::strcat (this->full_coll_name_, poa);
                  poa_added = 1;
                }
              ACE_OS::strcat (this->full_coll_name_, item);
              ACE_OS::strcat (this->full_coll_name_, "::");
            }
          else
            {
              ACE_OS::strcat (this->full_coll_name_, collocated);
              ACE_OS::strcat (this->full_coll_name_, item);
            }
        }
    }
  delete i;

  // Compute the local name for the collocated class.
  int localen = sizeof (collocated);
  localen += ACE_OS::strlen (this->local_name ()->get_string ());
  ACE_NEW (this->local_coll_name_, char[localen]);
  ACE_OS::strcpy(this->local_coll_name_, collocated);
  ACE_OS::strcat(this->local_coll_name_,
                 this->local_name ()->get_string ());
}

const char *
be_interface::full_coll_name (void)
{
  if (this->full_coll_name_ == 0)
    this->compute_coll_name ();

  return this->full_coll_name_;
}

const char*
be_interface::local_coll_name (void) const
{
  if (this->local_coll_name_ == 0)
    ACE_const_cast (be_interface*, this)->compute_coll_name ();

  return this->local_coll_name_;
}

// compute stringified fully scoped skel name
void
be_interface::compute_fullskelname (void)
{
  if (full_skel_name_)
    return;
  else
    {
      long namelen;
      UTL_IdListActiveIterator *i;
      long first = I_TRUE;
      long second = I_FALSE;

      // in the first loop compute the total length
      namelen = 4;
      i = new UTL_IdListActiveIterator (this->name ());
      while (!(i->is_done ()))
        {
          if (!first)
            namelen += 2; // for "::"
          else if (second)
            first = second = I_FALSE;
          // print the identifier
          namelen += ACE_OS::strlen (i->item ()->get_string ()); //
          // additional 4 for the POA_ characters
          if (first)
            {
              if (ACE_OS::strcmp (i->item ()->get_string (), "") != 0)
                // does not start with a ""
                first = I_FALSE;
              else
                second = I_TRUE;
            }
          i->next ();
        }
      delete i;

      this->full_skel_name_ = new char [namelen+1];
      this->full_skel_name_[0] = '\0';
      first = I_TRUE;
      second = I_FALSE;
      ACE_OS::strcat (this->full_skel_name_, "POA_");
      i = new UTL_IdListActiveIterator (this->name ());
      while (!(i->is_done ()))
        {
          if (!first)
            ACE_OS::strcat (this->full_skel_name_, "::");
          else if (second)
            first = second = I_FALSE;
          // print the identifier
          ACE_OS::strcat (this->full_skel_name_, i->item ()->get_string ());
          if (first)
            {
              if (ACE_OS::strcmp (i->item ()->get_string (), "") != 0)
                // does not start with a ""
                first = I_FALSE;
              else
                second = I_TRUE;
            }
          i->next ();
        }
      delete i;
    }
  return;
}

// retrieve the fully scoped skeleton name
const char*
be_interface::full_skel_name (void)
{
  if (!this->full_skel_name_)
    compute_fullskelname ();

  return this->full_skel_name_;
}

// Am I in some kind of a multiple inheritance
int be_interface::in_mult_inheritance (void)
{
  if (this->in_mult_inheritance_ == -1)
    {
      // compute once for all
      // determine if we are in some form of a multiple inheritance
      if (this->traverse_inheritance_graph
          (be_interface::in_mult_inheritance_helper, 0) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "be_interface::in_mult_inheritance "
                             "error determining mult inheritance\n"),
                            -1);
        }
    }

  return this->in_mult_inheritance_;
}

void be_interface::in_mult_inheritance (int mi)
{
  if (this->in_mult_inheritance_ == -1)
    this->in_mult_inheritance_ = mi;
}

// generate the var definition
int
be_interface::gen_var_defn (void)
{
  TAO_OutStream *ch; // output stream
  TAO_NL  nl;        // end line
  char namebuf [NAMEBUFSIZE];  // names

  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 ();

  // 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;
  *ch << namebuf << " (" << local_name () << "_ptr);" << 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= (" << local_name () << "_ptr);" << nl;

  // assignment from _var
  *ch << namebuf << " &operator= (const " << namebuf <<
    " &);" << nl;

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

  *ch << nl;

  // other extra types (cast operators, [] operator, and others)
  *ch << "operator const " << local_name () << "_ptr &() const;" << nl;
  *ch << "operator " << local_name () << "_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 << local_name () << "_ptr in (void) const;" << nl;
  *ch << local_name () << "_ptr &inout (void);" << nl;
  *ch << local_name () << "_ptr &out (void);" << nl;
  *ch << local_name () << "_ptr _retn (void);" << nl;

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

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

  // private
  *ch << "private:\n";
  ch->incr_indent ();
  *ch << local_name () << "_ptr ptr_;\n";

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

  return 0;
}

// implementation of the _var class. All of these get generated in the inline
// file
int
be_interface::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

  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", local_name ()->get_string ());

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

  ci = cg->client_inline ();

  // generate the var implementation in the inline file
  // Depending upon the data type, there are some differences which we account
  // for over here.

  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_ (" << this->name () << "::_nil ())" << nl;
  *ci << "{}\n\n";

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

  // the additional ptr () member function. This member function must be
  // defined before the remaining member functions including the copy
  // constructor because this inline function is used elsewhere. Hence to make
  // inlining of this function possible, we must define it before its use.
  ci->indent ();
  *ci << "ACE_INLINE " << name () << "_ptr " << nl;
  *ci << fname << "::ptr (void) const" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

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

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

  // assignment operator
  ci->indent ();
  *ci << "ACE_INLINE " << fname << " &" << nl;
  *ci << fname << "::operator= (" << name () <<
    "_ptr p)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "CORBA::release (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)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "if (this != &p)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "CORBA::release (this->ptr_);" << nl;
  *ci << "this->ptr_ = " << name () << "::_duplicate (p.ptr ());\n";
  ci->decr_indent ();
  *ci << "}" << 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 const " << name () <<
    "_ptr &() 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 () << "_ptr &() // cast " << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

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

  // in, inout, out, and _retn
  ci->indent ();
  *ci << "ACE_INLINE " << name () << "_ptr" << 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 () << "_ptr &" << 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 << "ACE_INLINE " << name () << "_ptr &" << nl;
  *ci << fname << "::out (void)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "CORBA::release (this->ptr_);" << nl;
  *ci << "this->ptr_ = " << this->name () << "::_nil ();" << nl;
  *ci << "return this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  ci->indent ();
  *ci << "ACE_INLINE " << name () << "_ptr " << nl;
  *ci << fname << "::_retn (void)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "// yield ownership of managed obj reference" << nl;
  *ci << this->name () << "_ptr val = this->ptr_;" << nl;
  *ci << "this->ptr_ = " << this->name () << "::_nil ();" << nl;
  *ci << "return val;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  return 0;
}

// generate the _out definition
int
be_interface::gen_out_defn (void)
{
  TAO_OutStream *ch; // output stream
  TAO_NL  nl;        // end line
  char namebuf [NAMEBUFSIZE];  // to hold the _out name

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

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

  ch = cg->client_header ();

  // 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 << " (" << local_name () << "_ptr &);" << nl;
  // constructor from a _var &
  *ch << namebuf << " (" << 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
  // only interface allows assignment from var &
  *ch << namebuf << " &operator= (const " << local_name () << "_var &);" << nl;
  *ch << namebuf << " &operator= (" << local_name () << "_ptr);" << nl;
  // cast
  *ch << "operator " << local_name () << "_ptr &();" << nl;
  // ptr fn
  *ch << local_name () << "_ptr &ptr (void);" << nl;
  // operator ->
  *ch << local_name () << "_ptr operator-> (void);" << nl;

  *ch << "\n";
  ch->decr_indent ();
  *ch << "private:\n";
  ch->incr_indent ();
  *ch << local_name () << "_ptr &ptr_;\n";

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

int
be_interface::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

  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", local_name ()->get_string ());

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

  ci = cg->client_inline ();

  // generate the var implementation in the inline file
  // Depending upon the data type, there are some differences which we account
  // for over here.

  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 _ptr
  ci->indent ();
  *ci << "ACE_INLINE" << nl;
  *ci << fname << "::" << lname << " (" << name () << "_ptr &p)" << nl;
  *ci << "  : ptr_ (p)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "this->ptr_ = " << this->name () << "::_nil ();\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 << "CORBA::release (this->ptr_);" << nl;
  *ci << "this->ptr_ = " << this->name () << "::_nil ();\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 operator from _var
  ci->indent ();
  *ci << "ACE_INLINE " << fname << " &" << nl;
  *ci << fname << "::operator= (const " << this->name () <<
    "_var &p)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "this->ptr_ = " << this->name () << "::_duplicate (p.ptr ());" << nl;
  *ci << "return *this;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

      // assignment operator from _ptr
  ci->indent ();
  *ci << "ACE_INLINE " << fname << " &" << nl;
  *ci << fname << "::operator= (" << this->name () <<
    "_ptr 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 () <<
    "_ptr &() // 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 () << "_ptr &" << 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 () << "_ptr " << nl;
  *ci << fname << "::operator-> (void)" << nl;
  *ci << "{\n";
  ci->incr_indent ();
  *ci << "return this->ptr_;\n";
  ci->decr_indent ();
  *ci << "}\n\n";

  return 0;
}

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

int
be_interface::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_objref, // 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_interface::gen_encapsulation (void)
{
  TAO_OutStream *cs; // output stream
  TAO_NL  nl;        // end line
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();
  long i, arrlen;
  long *arr;  // an array holding string names converted to array of longs

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

  // XXXASG - byte order must be based on what m/c we are generating code -
  // TODO
  *cs << "TAO_ENCAP_BYTE_ORDER, // byte order" << nl;
  // generate repoID
  *cs << (ACE_OS::strlen (this->repoID ())+1) << ", ";
  (void)this->tc_name2long (this->repoID (), arr, arrlen);
  for (i=0; i < arrlen; i++)
    {
      cs->print ("ACE_NTOHL (0x%x), ", arr[i]);
    }
  *cs << " // repository ID = " << this->repoID () << nl;
  // generate name
  *cs << (ACE_OS::strlen (this->local_name ()->get_string ())+1) << ", ";
  (void)this->tc_name2long(this->local_name ()->get_string (), arr, arrlen);
  for (i=0; i < arrlen; i++)
    {
      cs->print ("ACE_NTOHL (0x%x), ", arr[i]);
    }
  *cs << " // name = " << this->local_name () << ",\n";

  return 0;
}

// compute size of typecode
long
be_interface::tc_size (void)
{
   return 4 + 4 + this->tc_encap_len ();
}

// compute the encapsulation length
long
be_interface::tc_encap_len (void)
{
  if (this->encap_len_ == -1) // not computed yet
    {
      long slen;

      // Macro to avoid "warning: unused parameter" type warning.
      ACE_UNUSED_ARG (slen);

      this->encap_len_ = 4;  // holds the byte order flag

      this->encap_len_ += this->repoID_encap_len (); // for repoID

      // do the same thing for the local name
      this->encap_len_ += this->name_encap_len ();

    }
  return this->encap_len_;
}

// helper.
int
be_interface::gen_operation_table (void)
{
  TAO_OutStream *ss; // output stream.
  TAO_NL  nl;        // end line.

  // Retrieve the singleton instance of the CodeGen.
  TAO_CodeGen *cg = 0;
  cg = TAO_CODEGEN::instance ();

  // Check out the op_lookup_strategy.
  switch (cg->lookup_strategy ())
    {
    case TAO_CodeGen::TAO_DYNAMIC_HASH:
      // Init the outstream appropriately.
      ss = cg->server_skeletons ();

      // start from current indentation level.
      ss->indent ();

      // Start the table generation.
      *ss << "static const TAO_operation_db_entry " << this->flatname () <<
        "_operations [] = {\n";
      ss->incr_indent (0);

      // Traverse the graph.
      if (this->traverse_inheritance_graph (be_interface::gen_optable_helper, ss) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_interface::gen_operation_table - "
                             "inheritance graph traversal failed\n"), -1);
        }

      // generate the skeleton for the is_a method.
      ss->indent ();
      *ss << "{\"_is_a\", &" << this->full_skel_name () << "::_is_a_skel},\n";
      this->skel_count_++;

      ss->indent ();
      *ss << "{\"_non_existent\", &" << this->full_skel_name () << "::_non_existent_skel}\n";
      this->skel_count_++;

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

      // XXXASG - this code should be based on using different strategies for
      // demux - for next release
      *ss << "static const CORBA::Long _tao_" << this->flatname ()
          << "_optable_size = sizeof (ACE_Hash_Map_Entry<const char *,"
          << " TAO_Skeleton>) * (" << (3*this->skel_count_)
          << ");" << be_nl;
      *ss << "static char _tao_" << this->flatname () << "_optable_pool "
          << "[_tao_" << this->flatname () << "_optable_size];" << be_nl;
      *ss << "static ACE_Static_Allocator_Base _tao_" << this->flatname ()
          << "_allocator (_tao_" << this->flatname () << "_optable_pool, "
          << "_tao_" << this->flatname () << "_optable_size);" << be_nl;
      *ss << "TAO_Dynamic_Hash_OpTable tao_" << this->flatname () << "_optable "
          << "(" << this->flatname () << "_operations, " << this->skel_count_
          << ", " << 2*this->skel_count_ << ", &_tao_" << this->flatname ()
          << "_allocator);" << be_nl;

      break;

    case TAO_CodeGen::TAO_PERFECT_HASH:
      // For each interface in the IDL, have a new temp file to
      // collect the input for the gperf program.
      {
        // Temp file name.
        char *temp_file = 0;
        ACE_NEW_RETURN (temp_file,
                        char [ACE_OS::strlen (this->flatname ()) +
                             ACE_OS::strlen (".gperf")],
                        -1);
        ACE_OS::sprintf (temp_file, "%s.gperf", this->flatname ());

        // Save this file name with the codegen singleton.
        cg->gperf_input_filename (temp_file);

        // Make a new outstream to hold the gperf_temp_file for this
        // interface.

        // Retrieve the singleton instance to the outstream factory.
        TAO_OutStream_Factory *factory =
          TAO_OUTSTREAM_FACTORY::instance ();

        // Get a new instance for the temp file.
        ss = factory->make_outstream ();
        if (ss == 0)
          ACE_ERROR_RETURN ((LM_ERROR,
                             "be_visitor_interface_ss",
                             "::",
                             "visit_interface-",
                             "make_outstream failed"),
                            -1);

        // Store the outstream with the codegen singleton.
        cg->gperf_input_stream (ss);

        // Open the temp file.
        if (ss->open (temp_file,
                      TAO_OutStream::TAO_GPERF_INPUT) == -1)
          ACE_ERROR_RETURN ((LM_ERROR,
                             "be_visitor_interface_ss",
                             "::",
                             "visit_interface-",
                             "gperf_input.tmp file open failed"),
                            -1);

        // Add the gperf input header.
        gen_gperf_input_header (ss);

        // Traverse the graph.
        if (this->traverse_inheritance_graph (be_interface::gen_optable_helper, ss) == -1)
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_interface::gen_operation_table - "
                             "inheritance graph traversal failed\n"),
                            -1);

        // Generate the skeleton for the is_a method.
        ss->indent ();
        *ss << "_is_a" << ",\t&" << this->full_skel_name () << "::_is_a_skel\n";
        this->skel_count_++;

        // Input to the gperf is ready. Run gperf and get things done.
        gen_perfect_hash_optable ();

        // Cleanup the temp file. Delete the stream, remove the file
        // and delete the filename ptr.
        cleanup_gperf_temp_file ();
      }
      break;

    default:
      ACE_ERROR_RETURN ((LM_ERROR,
                         "be_interface",
                         "::",
                         "gen_operation_table",
                         "unknown op_lookup_strategy"),
                        -1);
    }
  return 0;
}

// Output the header (type declaration and %%) to the gperf's input
// file.
void
be_interface::gen_gperf_input_header (TAO_OutStream *ss)
{
  *ss << "class TAO_operation_db_entry {\n"
      << "public:\n"
      << "\tchar *opname_;" << "\n"
      << "\tTAO_Skeleton skel_ptr_;" << "\n"
      << "};" << "\n"
      << "%%"
      << "\n";
}

// we separate the generation of operation table entries from the
// "gen_operation_table" method. This enables us to invoke generation of
// entries for interfaces from which we inherit without any additional
// code. The parameter "derived" is the one for which the entire operation
// table is being built.
int
be_interface::gen_optable_entries (be_interface *derived)
{
  UTL_ScopeActiveIterator *si;
  AST_Decl *d;
  TAO_OutStream *ss; // output stream

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

  switch (cg->lookup_strategy ())
    {
    case TAO_CodeGen::TAO_DYNAMIC_HASH:
      // Init the outstream.
      ss = cg->server_skeletons ();

      // The major stuff.
      if (this->nmembers () > 0)
        {
          // if there are elements in this scope i.e., any operations and
          // attributes defined by "this" which happens to be the same as "derived"
          // or one of its ancestors.

          si = new UTL_ScopeActiveIterator (this, UTL_Scope::IK_decls);
          // instantiate a scope iterator.

          while (!(si->is_done ()))
            {
              // get the next AST decl node
              d = si->item ();
              if (d->node_type () == AST_Decl::NT_op)
                {
                  ss->indent (); // start from current indentation level
                  // we are an operation node
                  *ss << "{\"" << d->local_name () << "\", &"
                      << derived->full_skel_name () << "::"
                      << d->local_name () << "_skel},\n";
                  derived->skel_count_++;
                }
              else if (d->node_type () == AST_Decl::NT_attr)
                {
                  AST_Attribute *attr;

                  ss->indent (); // start from current indentation level
                  // generate only the "get" entry if we are readonly
                  *ss << "{\"_get_" << d->local_name () << "\", &" <<
                    derived->full_skel_name () << "::_get_" << d->local_name () <<
                    "_skel},\n";
                  derived->skel_count_++;

                  attr = AST_Attribute::narrow_from_decl (d);
                  if (!attr)
                    return -1;

                  if (!attr->readonly ())
                    {
                      // the set method
                      ss->indent (); // start from current indentation level
                      *ss << "{\"_set_" << d->local_name () << "\", &" <<
                        derived->full_skel_name () << "::_set_" << d->local_name
                        () << "_skel},\n";
                      derived->skel_count_++;
                    }
                }
              si->next ();
            } // end of while
          delete si; // free the iterator object
        }
      break;

    case TAO_CodeGen::TAO_PERFECT_HASH:
      // Init the outstream.
      ss = cg->gperf_input_stream ();

      if (this->nmembers () > 0)
        {
          // if there are elements in this scope i.e., any operations and
          // attributes defined by "this" which happens to be the same as "derived"
          // or one of its ancestors.

          si = new UTL_ScopeActiveIterator (this, UTL_Scope::IK_decls);
          // instantiate a scope iterator.

          while (!(si->is_done ()))
            {
              // Get the next AST decl node.
              d = si->item ();
              if (d->node_type () == AST_Decl::NT_op)
                {
                  ss->indent (); // start from current indentation level
                  // we are an operation node
                  *ss << d->local_name () << ",\t&"
                      << derived->full_skel_name () << "::"
                      << d->local_name () << "_skel" << "\n";
                  derived->skel_count_++;
                }
              else if (d->node_type () == AST_Decl::NT_attr)
                {
                  AST_Attribute *attr;

                  ss->indent (); // start from current indentation level
                  // generate only the "get" entry if we are readonly
                  *ss << "_get_" << d->local_name () << ",\t&"
                      << derived->full_skel_name () << "::_get_"
                      << d->local_name () << "_skel\n";
                  derived->skel_count_++;

                  attr = AST_Attribute::narrow_from_decl (d);
                  if (!attr)
                    return -1;

                  if (!attr->readonly ())
                    {
                      // the set method
                      ss->indent (); // start from current indentation level
                      *ss << "_set_" << d->local_name () << ",\t&"
                          << derived->full_skel_name () << "::_set_"
                          << d->local_name () << "_skel\n";
                      derived->skel_count_++;
                    }
                }
              si->next ();
            } // end of while
          delete si; // free the iterator object
        }
      break;

    default:
      ACE_ERROR_RETURN ((LM_ERROR,
                         "be_interface",
                         "::",
                         "gen_optable_entries",
                         "unknown op_lookup_strategy"),
                        -1);
    }
  return 0;
}

// template method that traverses the inheritance graph in a breadth-first
// style. The actual work on each element in the inheritance graph is carried
// out by the function passed as argument
int
be_interface::traverse_inheritance_graph (be_interface::tao_code_emitter gen,
                                              TAO_OutStream *os)
{
  long i;            // loop index
  ACE_Unbounded_Queue <be_interface*> queue; // Queue data structure needed for
                                            // breadth-first traversal of
                                            // inheritance tree

  // For a special case of a deeply nested inheritance graph and one specific
  // way of inheritance in which a node that was already visited, but is not present in
  // the queue, gets inserted at the tail. This situation arises when a node
  // multiply inherits from two or more interfaces in which the first parent is
  // higher up in the tree than the second parent. In addition, if the second
  // parent turns out to be a child of the first .

  ACE_Unbounded_Queue <be_interface*> del_queue; // queue of dequeued nodes to
                                                 // be searched for the above case

  // insert ourselves in the Queue
  if (queue.enqueue_tail (this) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR, "(%N:%l) be_interface::gen_operation_table - "
                         "error generating entries\n"), -1);
    }

  // do until queue is empty
  while (!queue.is_empty ())
    {
      be_interface *bi;  // element inside the queue

      // use breadth-first strategy i.e., first generate entries for ourselves,
      // followed by nodes that we immediately inherit from, and so on. In the
      // process make sure that we do not generate code for the same node more
      // than once. Such a case may arise due to multiple inheritance forming a
      // diamond like inheritance graph.

      // dequeue the element at the head of the queue
      if (queue.dequeue_head (bi))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_interface::traverse_graph - "
                             "dequeue_head failed\n"), -1);
        }

      // insert the dequeued element in the del_queue
      if (del_queue.enqueue_tail (bi) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_interface::traverse_graph - "
                             "enqueue_head failed\n"), -1);
        }

      // use the helper method to generate code for ourself using the
      // properties of the element dequeued. For the first iteration, the
      // element dequeued and "this" will be the same i.e., ourselves
      if (gen (this, bi, os) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_interface::traverse_graph - "
                             "helper code gen failed\n"), -1);
        }

      // now check if the dequeued element has any ancestors. If yes, insert
      // them inside the queue making sure that there are no duplicates
      for (i=0; i < bi->n_inherits (); i++)
        {
          be_interface *parent;  // parent of the dequeued element

          // initialize an iterator to search the queue for duplicates
          ACE_Unbounded_Queue_Iterator<be_interface*> q_iter (queue);

          // retrieve the next parent from which the dequeued element inherits
          parent = be_interface::narrow_from_decl (bi->inherits ()[i]);
          if (!parent)
            {
              ACE_ERROR_RETURN ((LM_ERROR,
                                 "(%N:%l) be_interface::gen_server_skeletons - "
                                 "bad inherited interface\n"), -1);
            }

          // now insert this node at the tail of the queue, but make sure that
          // it doesn't already exist in the queue
          int found = 0;
          while (!q_iter.done ())
            {
              be_interface **temp;  // queue element

              (void) q_iter.next (temp);
              if (!ACE_OS::strcmp (parent->fullname (), (*temp)->fullname ()))
                {
                  // we exist in this queue and cannot be inserted
                  found = 1;
                }
              if (found)
                break;
              (void) q_iter.advance ();
            } // end of while

          // initialize an iterator to search the del_queue for duplicates
          ACE_Unbounded_Queue_Iterator<be_interface*> del_q_iter (del_queue);

          while (!found && !del_q_iter.done ())
            {
              be_interface **temp;  // queue element

              (void) del_q_iter.next (temp);
              if (!ACE_OS::strcmp (parent->fullname (), (*temp)->fullname ()))
                {
                  // we exist in this del_queue and cannot be inserted
                  found = 1;
                }
              if (found)
                break;
              (void) del_q_iter.advance ();
            } // end of while

          if (!found)
            {
              // insert the parent in the queue
              if (queue.enqueue_tail (parent) == -1)
                {
                  ACE_ERROR_RETURN ((LM_ERROR,
                                 "(%N:%l) be_interface::gen_server_skeletons - "
                                 "enqueue op failed\n"), -1);
                }
            }
        } // end of for loop
    } // end of while queue not empty
  return 0;
}

// helpers passed to the template method

int
be_interface::gen_optable_helper (be_interface *derived,
                                  be_interface *ancestor,
                                  TAO_OutStream * /*os*/)
{
  // generate entries for the derived class using the properties of its
  // ancestors
  if (ancestor->gen_optable_entries (derived) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_interface::gen_operation_table - "
                         "error generating entries for inherited"
                         "interfaces\n"), -1);
    }
  return 0;
}

// The main optable generator for the perfect hashing strategy.
int
be_interface::gen_perfect_hash_optable (void)
{
  // Output a class definition deriving from
  // TAO_Perfect_Hash_OpTable.
  gen_perfect_hash_class_definition ();

  // Call GPERF and get the methods defined.
  if (gen_perfect_hash_methods () == -1)
    return -1;

  // Create an instance of this perfect hash table.
  gen_perfect_hash_instance ();

  return 0;
}


// Outputs the class definition for the perfect hashing. This class
// will inherit from the TAO_Perfect_Hash_OpTable.
void
be_interface::gen_perfect_hash_class_definition (void)
{
  // Codegen singleton.
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  // Outstream.
  TAO_OutStream *ss = cg->server_skeletons ();

  *ss << "class " << "TAO_" << this->flatname () << "_Perfect_Hash_OpTable"
      << " : public TAO_Perfect_Hash_OpTable"
      << be_nl
      << "{"
      << be_nl
      << "private:"
      << be_nl
      << "  unsigned int hash (const char *str, int len);"
      << be_nl
      << "public:"
      << be_nl
      << " const TAO_operation_db_entry * lookup (const char *str, int len);"
      << be_nl
      << "};"
      << "\n";
}

// We have collected the input (Operations and the corresponding
// skeleton pointers) for the gperf program. Now let us execute gperf
// and get things done.
// GPERF reads from our temp file and write to the Server Skeleton
// file.
int
be_interface::gen_perfect_hash_methods (void)
{
  // Using ACE_Process.
  ACE_Process process_manager;
  ACE_Process_Options process_options;

  // Codegen's singleton.
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  // Adjust the offset of the underlying file pointer.
  ACE_OS::rewind (cg->gperf_input_stream ()->file ());

  // Set the stdin and stdout appropriately for the gperf program.

  // Stdin is our temp file. Close the temp file and open using
  // ACE_OS::open so that we will get ACE_HANDLE.

  if (ACE_OS::fclose (cg->gperf_input_stream ()->file ()) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p:File close failed on temp gperf's input file\n"),
                      -1);

  ACE_HANDLE input =  ACE_OS::open (cg->gperf_input_filename (),
                                    O_RDONLY);
  if (input == ACE_INVALID_HANDLE)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p:File open failed on gperf's temp input file\n"),
                      -1);

  // Stdout is server skeleton. Do *not* close the file, just open
  // again with ACE_OS::open with WRITE + APPEND option.. After this,
  // remember to update the file offset to the correct location.

  ACE_HANDLE output =  ACE_OS::open (idl_global->be_get_server_skeleton_fname (),
                                     O_WRONLY | O_APPEND);
  if (output == ACE_INVALID_HANDLE)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p:File open failed on server skeleton file\n"),
                      -1);

  // Set the handles now in the process options.
  process_options.set_handles (input, output);

  // Set the command line for the gperf program.

  // Form the absolute pathname.
  char *ace_root = ACE_OS::getenv ("ACE_ROOT");
  if (ace_root == NULL)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error:%p:Env variable 'ACE_ROOT' not found. Can't locate GPERF Program\n"),
                      -1);

  process_options.command_line ("%s/bin/gperf"
                                " "
                                "-m -M -J -c -C"
                                " "
                                "-D -E -T -f 0"
                                " "
                                "-a -o -t -p -K"
                                " "
                                "opname_ -L C++"
                                " "
                                "-Z TAO_%s_Perfect_Hash_OpTable"
                                " "
                                "-N lookup",
                                ace_root,
                                this->flatname ());

  // Spawn a process for gperf.
  if (process_manager.spawn (process_options) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error:%p:Couldnt spawn a process for gperf program\n"),
                      -1);

  // Wait for gperf to complete.
  if (process_manager.wait () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error:%p:Error on wait'ing for completion of gperf program.\n"),
                      -1);

  // Adjust the file offset to the EOF for the server skeleton file.
  ACE_OS::fseek (cg->server_skeletons ()->file (), 0, SEEK_END);

  return 0;
}

// Create an instance of this perfect hash table.
void
be_interface::gen_perfect_hash_instance ()
{
  // Codegen singleton.
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  // Outstream.
  TAO_OutStream *ss = cg->server_skeletons ();

  *ss << "TAO_" << this->flatname () << "_Perfect_Hash_OpTable"
      << " "
      << "tao_" << this->flatname () << "_optable"
      << ";"
      << be_nl;
}

// Delete the stream and filename for this temp file and also remove
// the temperary gperf's input file.
void
be_interface::cleanup_gperf_temp_file (void)
{
  // Codegen singleton.
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  // Delete the stream ptr.
  TAO_OutStream *ss = cg->gperf_input_stream ();
  if (ss != 0)
    delete ss;

  // Delete the temp file.
  ACE_OS::unlink (cg->gperf_input_filename ());

  // Delete the filename ptr.
  char *fname = cg->gperf_input_filename ();
  if (fname != 0)
    delete fname;
}

int
be_interface::is_a_helper (be_interface * /*derived*/,
                           be_interface *bi,
                           TAO_OutStream *os)
{
  // emit the comparison code
  os->indent ();
  *os << "(!ACE_OS::strcmp ((char *)value, \"" << bi->repoID () <<
    "\")) ||\n";

  return 0;
}

int
be_interface::downcast_helper (be_interface * /* derived */,
                               be_interface *base,
                               TAO_OutStream *os)
{
  *os << "if (ACE_OS::strcmp (logical_type_id, \""
      << base->repoID () << "\") == 0)" << be_idt_nl
      << "return ACE_static_cast ("
      << base->full_skel_name () << "_ptr, this);" << be_uidt_nl;
  return 0;
}

int
be_interface::gen_skel_helper (be_interface *derived,
                               be_interface *ancestor,
                               TAO_OutStream *os)
{
  UTL_ScopeActiveIterator *si;
  AST_Decl *d;
  TAO_NL  nl;        // end line

  // if derived and ancestor are same, skip it
  if (derived == ancestor)
    return 0;

  // else generate code that does the cast to the appropriate type

  if (ancestor->nmembers () > 0)
    {
      // if there are elements in ancestor scope i.e., any operations and
      // attributes defined by "ancestor", become methods on the derived class
      // which call the corresponding method of the base class by doing the
      // proper casting

      si = new UTL_ScopeActiveIterator (ancestor, UTL_Scope::IK_decls);
      // instantiate a scope iterator.

      while (!(si->is_done ()))
        {
          // get the next AST decl node
          d = si->item ();
          if (d->node_type () == AST_Decl::NT_op)
            {
              os->indent (); // start from current indentation level
              if (os->stream_type () == TAO_OutStream::TAO_SVR_HDR)
                {
                  // generate the static method corresponding to this method
                  *os << "static void " << d->local_name () <<
                    "_skel (CORBA::ServerRequest &req, void *obj,"
                      << " void *context, CORBA::Environment &env);\n\n";
                }
              else
                { // generate code in the inline file
                  // generate the static method corresponding to this method
                  *os << "ACE_INLINE void " << derived->full_skel_name () <<
                    "::" << d->local_name () <<
                    "_skel (CORBA::ServerRequest &req, " <<
                    "void *obj, void *context, CORBA::Environment &env)" << nl;
                  *os << "{\n";
                  os->incr_indent ();
                  *os << ancestor->full_skel_name () << "_ptr impl = (" <<
                    derived->full_skel_name () << "_ptr) obj;" << nl;
                  *os << ancestor->full_skel_name () << "::" << d->local_name
                    () << "_skel (req, (" << ancestor->full_skel_name () <<
                    "_ptr) impl, context, env);\n";
                  os->decr_indent ();
                  *os << "}\n";
                }
            }
          else if (d->node_type () == AST_Decl::NT_attr)
            {
              AST_Attribute *attr;

              attr = AST_Attribute::narrow_from_decl (d);
              if (!attr)
                return -1;

              os->indent (); // start from current indentation level
              if (os->stream_type () == TAO_OutStream::TAO_SVR_HDR)
                {
                  // generate the static method corresponding to this method
                  *os << "static void _get_" << d->local_name () <<
                    "_skel (CORBA::ServerRequest &req, void *obj,"
                      << " void *context, CORBA::Environment &env);\n\n";
                }
              else
                { // generate code in the inline file
                  // generate the static method corresponding to this method
                  *os << "ACE_INLINE void " << derived->full_skel_name () <<
                    "::_get_" << d->local_name () <<
                    "_skel (CORBA::ServerRequest &req, " <<
                    "void *obj, void *context, CORBA::Environment &env)" << nl;
                  *os << "{\n";
                  os->incr_indent ();
                  *os << ancestor->full_skel_name () << "_ptr impl = (" <<
                    derived->full_skel_name () << "_ptr) obj;" << nl;
                  *os << ancestor->full_skel_name () << "::_get_" << d->local_name
                    () << "_skel (req, (" << ancestor->full_skel_name () <<
                    "_ptr) impl, context, env);\n";
                  os->decr_indent ();
                  *os << "}\n";
                }

              if (!attr->readonly ())
                {
                  // the set method
                  os->indent (); // start from current indentation level
                  if (os->stream_type () == TAO_OutStream::TAO_SVR_HDR)
                    {
                      // generate the static method corresponding to this method
                      *os << "static void _set_" << d->local_name () <<
                        "_skel (CORBA::ServerRequest &req, void *obj,"
                          << " void *context, CORBA::Environment &env);\n\n";
                    }
                  else
                    { // generate code in the inline file
                      // generate the static method corresponding to this method
                      *os << "ACE_INLINE void " << derived->full_skel_name ()
                          << "::_set_" << d->local_name () <<
                        "_skel (CORBA::ServerRequest &req, " <<
                        "void *obj, void *context, CORBA::Environment &env)" <<
                        nl;
                      *os << "{\n";
                      os->incr_indent ();
                      *os << ancestor->full_skel_name () << "_ptr impl = (" <<
                        derived->full_skel_name () << "_ptr) obj;" << nl;
                      *os << ancestor->full_skel_name () << "::_get_" <<
                        d->local_name () << "_skel (req, (" <<
                        ancestor->full_skel_name () <<
                        "_ptr) impl, context, env);\n";
                      os->decr_indent ();
                      *os << "}\n";
                    }

                }
            }
          si->next ();
        } // end of while
      delete si; // free the iterator object
    }
  return 0;
}

int
be_interface::collocated_ctor_helper (be_interface *derived,
                                      be_interface *base,
                                      TAO_OutStream *os)
{
  if (derived == base)
    // we are the same. Don't do anything, otherwise we will end up calling
    // ourself
    return 0;

  if (base->is_nested ())
    {
      be_decl *scope;
      scope = be_scope::narrow_from_scope (base->defined_in ())->decl ();
      *os << "  ACE_NESTED_CLASS (POA_" << scope->name () << ","
          << base->local_coll_name () << ") (servant, stub)," << be_nl;
    }
  else
    {
      *os << "  " << base->full_coll_name () << " (servant, stub)," << be_nl;
    }

  return 0;
}

int
be_interface::copy_ctor_helper (be_interface *derived,
				be_interface *base,
				TAO_OutStream *os)
{
  if (derived == base)
    // we are the same. Don't do anything, otherwise we will end up calling
    // ourself
    return 0;

  if (base->is_nested ())
    {
      be_decl *scope;
      scope = be_scope::narrow_from_scope (base->defined_in ())->decl ();
      *os << "  ACE_NESTED_CLASS (POA_" << scope->name () << ","
          << base->local_name () << ") (rhs)," << be_nl;
    }
  else
    {
      *os << "  " << base->full_skel_name () << " (rhs)," << be_nl;
    }

  return 0;
}

int
be_interface::in_mult_inheritance_helper (be_interface *derived,
                                          be_interface *base,
                                          TAO_OutStream *)
{
  switch (derived->n_inherits ())
    {
    case 0:
      // no parent
      derived->in_mult_inheritance (0);
      break;
    case 1:
      if (derived == base)
        // prevent indefinite recursion
        derived->in_mult_inheritance (-1);
      else
        // one parent. We have the same characteristics as our base
        derived->in_mult_inheritance (base->in_mult_inheritance ());
      break;
    default:
      // direct multiple inheritance
      derived->in_mult_inheritance (1);
    }
  return 0;
}

const char*
be_interface::relative_coll_name (const char *collname)
{
  return be_interface::relative_name (this->full_coll_name (),
                                      collname);
}

// return the relative skeleton name (needed due to NT compiler insanity)
const char *
be_interface::relative_skel_name (const char *skelname)
{
  return be_interface::relative_name (this->full_skel_name (),
                                      skelname);
}

const char*
be_interface::relative_name (const char *localname,
                             const char *othername)
{
  // some compilers do not like generating a fully scoped name for a
  // type that was defined in the same enclosing scope in which it was
  // defined.  We have to emit just the partial name, relative to our
  // "localname"

  // The tricky part here is that it is not enough to check if the
  // typename we are using was defined in the current scope. But we
  // need to ensure that it was not defined in any of our ancestor
  // scopes as well. If that is the case, then we can generate a fully
  // scoped name for that type, else we use the ACE_NESTED_CLASS macro

  // thus we need some sort of relative name to be generated

  static char macro [NAMEBUFSIZE];
  // UNUSED: be_decl *def_scope = 0;  // our defining scope
  char // hold the fully scoped name
    def_name [NAMEBUFSIZE],
    use_name [NAMEBUFSIZE];
  char // these point to the curr and next component in the scope
    *def_curr = def_name,
    *def_next,
    *use_curr = use_name,
    *use_next;

  ACE_OS::memset (macro, '\0', NAMEBUFSIZE);
  ACE_OS::memset (def_name, '\0', NAMEBUFSIZE);
  ACE_OS::memset (use_name, '\0', NAMEBUFSIZE);

  // traverse every component of the def_scope and use_scope beginning at the
  // root and proceeding towards the leaf trying to see if the components
  // match. Continue until there is a match and keep accumulating the path
  // traversed. This forms the first argument to the ACE_NESTED_CLASS
  // macro. Whenever there is no match, the remaining components of the
  // def_scope form the second argument

  ACE_OS::strcpy (def_name, localname);
  ACE_OS::strcpy (use_name, othername);

  while (def_curr && use_curr)
    {
      // find the first occurrence of a :: and advance the next pointers accordingly
      def_next = ACE_OS::strstr (def_curr, "::");
      use_next = ACE_OS::strstr (use_curr, "::");

      if (def_next)
        *def_next = 0;

      if (use_next)
        *use_next = 0;

      if (!ACE_OS::strcmp (def_curr, use_curr))
        {
          // they have same prefix, append to arg1
          def_curr = (def_next ? (def_next+2) : 0); // skip the ::
          use_curr = (use_next ? (use_next+2) : 0); // skip the ::
        }
      else
        {
          // we had overwritten a ':' by a '\0' for string comparison. We
          // revert back because we want the rest of the relative name to be
          // used
          if (def_next)
            *def_next = ':';

          if (use_next)
            *use_next = ':';

          // no match. This is the end of the first argument. Get out
          // of the loop as no more comparisons are necessary
          break;
        }
    }

  // start the 2nd argument of the macro

  // copy the remaining def_name (if any left)
  if (def_curr)
    ACE_OS::strcat (macro, def_curr);

  return macro;
}

int
be_interface::accept (be_visitor *visitor)
{
  return visitor->visit_interface (this);
}

// Narrowing
IMPL_NARROW_METHODS3 (be_interface, AST_Interface, be_scope, be_type)
IMPL_NARROW_FROM_DECL (be_interface)
IMPL_NARROW_FROM_SCOPE (be_interface)

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Node <be_interface*>;
template class ACE_Unbounded_Queue <be_interface*>;
template class ACE_Unbounded_Queue_Iterator <be_interface*>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Node<be_interface*>
#pragma instantiate ACE_Unbounded_Queue<be_interface*>
#pragma instantiate ACE_Unbounded_Queue_Iterator<be_interface*>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */