summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/ast/ast_decl.cpp
blob: e2803ef69af260ae07982956cda66eaf4197dd70 (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
/*

COPYRIGHT

Copyright 1992, 1993, 1994 Sun Microsystems, Inc.  Printed in the United
States of America.  All Rights Reserved.

This product is protected by copyright and distributed under the following
license restricting its use.

The Interface Definition Language Compiler Front End (CFE) is made
available for your use provided that you include this license and copyright
notice on all media and documentation and the software program in which
this product is incorporated in whole or part. You may copy and extend
functionality (but may not remove functionality) of the Interface
Definition Language CFE without charge, but you are not authorized to
license or distribute it to anyone else except as part of a product or
program developed by you or with the express written consent of Sun
Microsystems, Inc. ("Sun").

The names of Sun Microsystems, Inc. and any of its subsidiaries or
affiliates may not be used in advertising or publicity pertaining to
distribution of Interface Definition Language CFE as permitted herein.

This license is effective until terminated by Sun for failure to comply
with this license.  Upon termination, you shall destroy or return all code
and documentation for the Interface Definition Language CFE.

INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED AS IS WITH NO WARRANTIES OF
ANY KIND INCLUDING THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS
FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR ARISING FROM A COURSE OF
DEALING, USAGE OR TRADE PRACTICE.

INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED WITH NO SUPPORT AND WITHOUT
ANY OBLIGATION ON THE PART OF Sun OR ANY OF ITS SUBSIDIARIES OR AFFILIATES
TO ASSIST IN ITS USE, CORRECTION, MODIFICATION OR ENHANCEMENT.

SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES SHALL HAVE NO LIABILITY WITH
RESPECT TO THE INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY
INTERFACE DEFINITION LANGUAGE CFE OR ANY PART THEREOF.

IN NO EVENT WILL SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES BE LIABLE FOR
ANY LOST REVENUE OR PROFITS OR OTHER SPECIAL, INDIRECT AND CONSEQUENTIAL
DAMAGES, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

Use, duplication, or disclosure by the government is subject to
restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
Technical Data and Computer Software clause at DFARS 252.227-7013 and FAR
52.227-19.

Sun, Sun Microsystems and the Sun logo are trademarks or registered
trademarks of Sun Microsystems, Inc.

SunSoft, Inc.
2550 Garcia Avenue
Mountain View, California  94043

NOTE:

SunOS, SunSoft, Sun, Solaris, Sun Microsystems or the Sun logo are
trademarks or registered trademarks of Sun Microsystems, Inc.

*/

#include <ast_interface.h>
#include <ast_module.h>
#include <ast_array.h>
#include <ast_field.h>
#include <ast_structure.h>
#include <ast_sequence.h>
#include <ast_string.h>
#include <ast_typedef.h>
#include <ast_visitor.h>
#include <global_extern.h>
#include <nr_extern.h>
#include <utl_identifier.h>
#include <utl_string.h>
#include <utl_scope.h>
#include <utl_err.h>
#include <ast_annotation_appl.h>
#include <utl_indenter.h>

// FUZZ: disable check_for_streams_include
#include <ace/streams.h>
#include <ace/OS_NS_stdio.h>

COMMON_Base::COMMON_Base (bool local,
                          bool abstract)
  : is_local_ (local),
    is_abstract_ (abstract)
{
}

COMMON_Base::~COMMON_Base ()
{
}

void
COMMON_Base::destroy ()
{
}

bool
COMMON_Base::is_local ()
{
  return this->is_local_;
}

void
COMMON_Base::is_local (bool val)
{
  this->is_local_ = val;
}

bool
COMMON_Base::is_abstract () const
{
  return this->is_abstract_;
}

void
COMMON_Base::is_abstract (bool val)
{
  this->is_abstract_ = val;
}

AST_Decl::AST_Decl (NodeType nt,
                    UTL_ScopedName *n,
                    bool anonymous)
  : COMMON_Base (),
    repoID_ (nullptr),
    flat_name_ (nullptr),
    contains_wstring_ (-1),
    annotation_appls_ (nullptr),
    builtin_ (idl_global->in_eval_),
    pd_imported (idl_global->imported ()),
    pd_in_main_file (idl_global->in_main_file ()),
    pd_defined_in (idl_global->scopes ().depth () > 0
                     ? idl_global->scopes ().top ()
                     : nullptr),
    pd_node_type (nt),
    pd_line (idl_global->lineno ()),
    pd_name (nullptr),
    pd_local_name (n ? n->last_component ()->copy () : nullptr),
    pd_original_local_name (nullptr),
    full_name_ (nullptr),
    prefix_ (nullptr),
    version_ (nullptr),
    anonymous_ (anonymous),
    typeid_set_ (false),
    last_referenced_as_ (nullptr),
    prefix_scope_ (nullptr),
    in_tmpl_mod_not_aliased_ (idl_global->in_tmpl_mod_no_alias ())
{
  // If this is the root node, the filename won't have been set yet.
  UTL_String *fn = idl_global->filename ();
  this->pd_file_name = (fn ? fn->get_string () : "");

  this->compute_full_name (n);

  char *prefix = nullptr;
  idl_global->pragma_prefixes ().top (prefix);
  this->prefix_ = ACE::strnew (prefix ? prefix : "");

  if (n)
    {
      // The function body creates its own copy.
      this->original_local_name (n->last_component ());
    }

  this->compute_repoID ();
}

AST_Decl::AST_Decl (
  UTL_ScopedName *name,
  AST_Decl *other)
  : COMMON_Base (),
    repoID_ (nullptr),
    flat_name_ (nullptr),
    contains_wstring_ (-1),
    annotation_appls_ (nullptr),
    builtin_ (idl_global->in_eval_),
    pd_imported (idl_global->imported ()),
    pd_in_main_file (idl_global->in_main_file ()),
    pd_defined_in (idl_global->scopes ().depth () > 0
                     ? idl_global->scopes ().top ()
                     : nullptr),
    pd_node_type (other->node_type ()),
    pd_line (idl_global->lineno ()),
    pd_name (nullptr),
    pd_local_name (name ? name->last_component ()->copy () : nullptr),
    pd_original_local_name (nullptr),
    full_name_ (nullptr),
    prefix_ (nullptr),
    version_ (nullptr),
    anonymous_ (other->anonymous ()),
    typeid_set_ (false),
    last_referenced_as_ (nullptr),
    prefix_scope_ (nullptr),
    in_tmpl_mod_not_aliased_ (idl_global->in_tmpl_mod_no_alias ())
{
  // If this is the root node, the filename won't have been set yet.
  UTL_String *fn = idl_global->filename ();
  this->pd_file_name = (fn ? fn->get_string () : "");

  this->compute_full_name (name);

  char *prefix = nullptr;
  idl_global->pragma_prefixes ().top (prefix);
  this->prefix_ = ACE::strnew (prefix ? prefix : "");

  if (name)
    {
      // The function body creates its own copy.
      this->original_local_name (name->last_component ());
    }

  this->compute_repoID ();
}

AST_Decl::~AST_Decl ()
{
}

void
AST_Decl::destroy ()
{
  // These are not set for the root node.
  if (this->pd_name)
    {
      this->pd_name->destroy ();
      delete this->pd_name;
      this->pd_name = nullptr;
    }

  if (this->pd_local_name)
    {
      this->pd_local_name->destroy ();
      delete this->pd_local_name;
      this->pd_local_name = nullptr;
    }

  if (this->pd_original_local_name)
    {
      this->pd_original_local_name->destroy ();
      delete this->pd_original_local_name;
      this->pd_original_local_name = nullptr;
    }

  if (this->last_referenced_as_)
    {
      this->last_referenced_as_->destroy ();
      delete this->last_referenced_as_;
      this->last_referenced_as_ = nullptr;
    }

  delete [] this->full_name_;
  this->full_name_ = nullptr;

  delete [] this->repoID_;
  this->repoID_ = nullptr;

  delete [] this->prefix_;
  this->prefix_ = nullptr;

  delete [] this->version_;
  this->version_ = nullptr;

  delete [] this->flat_name_;
  this->flat_name_ = nullptr;

  delete annotation_appls_;
  annotation_appls_ = nullptr;
}

AST_Decl *
AST_Decl::adjust_found (
  bool /*ignore_fwd*/,
  bool /*full_def_only*/)
{
  return this; // Defaults to no adjustment
}

bool
AST_Decl::is_fwd ()
{
  return false; // Not a fwd declared type (by default)
}

// Private operations.

// Compute our private UTL_ScopedName member.
void
AST_Decl::compute_full_name (UTL_ScopedName *n)
{
  // This should happen only when we are a non-void predefined type,
  // in which case our scoped name has already been created by the
  // AST_PredefinedType constructor.
  if (!n)
    {
      return;
    }

  // Global scope?
  if (!this->defined_in ())
    {
      this->pd_name = (UTL_IdList *) n->copy ();
      return;
    }

  // Initialize this name to 0.
  this->pd_name = nullptr;

  // OK, not global. So copy name of containing scope, then
  // smash last cdr of copy with new component
  UTL_ScopedName *cn = nullptr;
  AST_Decl *d = ScopeAsDecl (this->defined_in ());
  if (d)
    {
      cn = d->name ();
      if (cn)
        {
          this->pd_name = (UTL_IdList *) cn->copy ();
        }
    }

  if (this->pd_local_name)
    {
      if (this->pd_name)
        {
          UTL_ScopedName *conc_name = nullptr;
          ACE_NEW (conc_name,
                   UTL_ScopedName (this->pd_local_name->copy (), nullptr));
          this->pd_name->nconc (conc_name);
        }
      else
        {
          ACE_NEW (this->pd_name,
                   UTL_ScopedName (this->pd_local_name->copy (), nullptr));
        }
    }
}

void
AST_Decl::set_prefix_with_typeprefix_r (const char *value,
                                        UTL_Scope *appeared_in)
{
  if (this->typeid_set_)
    {
      return;
    }

  if (this->prefix_scope_)
    {
      AST_Decl *decl = ScopeAsDecl (this->prefix_scope_);
      bool const overridden =
        decl->has_ancestor (ScopeAsDecl (appeared_in));
      if (overridden)
        {
          return;
        }
    }

  delete [] this->repoID_;
  this->repoID_ = nullptr;
  this->prefix (value);
  this->prefix_scope_ = appeared_in;

  UTL_Scope *s = DeclAsScope (this);
  if (s)
    {
      for (UTL_ScopeActiveIterator i (s, UTL_Scope::IK_decls);
           !i.is_done ();
           i.next ())
        {
          AST_Decl *tmp = i.item ();
          UTL_Scope *s_tmp = DeclAsScope (tmp);
          if (s_tmp)
            {
              tmp->set_prefix_with_typeprefix_r (value, appeared_in);
            }
        }
    }

  // This will recursively catch all previous openings of a module.
  if (this->node_type () == AST_Decl::NT_module)
    {
      AST_Module *m = dynamic_cast<AST_Module*> (this);
      while (!!(m = m->previous_opening ()))
        {
          for (UTL_ScopeActiveIterator si (m, UTL_Scope::IK_decls);
               !si.is_done ();
               si.next ())
            {
              AST_Decl *d = si.item ();
              if (d->node_type () != AST_Decl::NT_pre_defined)
                {
                  d->set_prefix_with_typeprefix_r (value, appeared_in);
                }
            }
        }
    }

  this->compute_repoID ();
}

// Protected operations.

// Compute stringified fully scoped name.
void
AST_Decl::compute_full_name ()
{
  if (!this->full_name_)
    {
      size_t namelen = 0;
      long first = true;
      long second = false;
      char *name = nullptr;

      for (UTL_IdListActiveIterator i (this->name ());
           !i.is_done ();
           i.next ())
        {
          if (!first)
            {
              namelen += 2; // for "::"
            }
          else if (second)
            {
              first = second = false;
            }

          // Print the identifier.
          name = i.item ()->get_string ();
          namelen += ACE_OS::strlen (name);
          if (first)
            {
              if (ACE_OS::strcmp (name, ""))
                {
                  // Does not start with a "".
                  first = false;
                }
              else
                {
                  second = true;
                }
            }
        }

      ACE_NEW (this->full_name_, char [namelen + 1]);
      this->full_name_ [0] = '\0';
      first = true;
      second = false;

      for (UTL_IdListActiveIterator j (this->name ());
           !j.is_done ();
           j.next ())
        {
          if (!first)
            {
              ACE_OS::strcat (this->full_name_, "::");
            }
          else if (second)
            {
              first = second = false;
            }

          // Print the identifier.
          name = j.item ()->get_string ();
          ACE_OS::strcat (this->full_name_, name);
          if (first)
            {
              if (ACE_OS::strcmp (name, ""))
                {
                  // Does not start with a "".
                  first = false;
                }
              else
                {
                  second = true;
                }
            }
        }
    }
}

// Compute stringified repository ID.
void
AST_Decl::compute_repoID ()
{
  if (this->repoID_)
    {
      return;
    }

  size_t namelen = 4; // for the prefix "IDL:"
  long first = true;
  long second = false;
  char *name = nullptr;
  const char *prefix = (this->prefix_ ? this->prefix_ : "");
  UTL_Scope *scope = this->defined_in ();
  const char *parent_prefix = nullptr;

  // If our prefix is empty, we check to see if an ancestor has one.
  while (scope && !ACE_OS::strcmp (prefix, ""))
    {
      AST_Decl *parent = ScopeAsDecl (scope);
      if (parent->node_type () == AST_Decl::NT_root
          && parent->imported ())
        {
          break;
        }

      parent_prefix = parent->prefix ();
      prefix = (parent_prefix ? parent_prefix : "");
      scope = parent->defined_in ();
    }

  // in the first loop compute the total length
  namelen += ACE_OS::strlen (prefix) + 1;

  const char *version = this->version_;
  scope = this->defined_in ();

  // If our version is has not bee set, we use the parent's, if any.
  while (!version && scope)
    {
      AST_Decl *parent = ScopeAsDecl (scope);
      version = parent->version_;
      scope = parent->defined_in ();
    }

  if (version)
    {
      // Version member string + ':'
      namelen += ACE_OS::strlen (version) + 1;
    }
  else
    {
      // For ":1.0"
      namelen += 4;
    }

  for (UTL_IdListActiveIterator i (this->name ());
       !i.is_done ();
       i.next ())
    {
      if (!first)
        {
          namelen += 1; // for "/"
        }
      else if (second)
        {
          first = second = false;
        }

      // Print the identifier.
      name = i.item ()->get_string ();
      size_t item_len = ACE_OS::strlen (name);

      if (ACE_OS::strstr (name, "_cxx_") == name)
        {
          namelen += (item_len - ACE_OS::strlen ("_cxx_"));
        }
      else
        {
          namelen += item_len;
        }

      if (first)
        {
          if (ACE_OS::strcmp (name, ""))
            {
              // Does not start with a "".
              first = false;
            }
          else
            {
              second = true;
            }
        }
    }

  ACE_NEW (this->repoID_, char [namelen + 1]);
  ACE_OS::sprintf (this->repoID_, "IDL:");

  if (ACE_OS::strcmp (prefix, ""))
    {
      ACE_OS::strcat (this->repoID_, prefix);
      ACE_OS::strcat (this->repoID_, "/");
    }

  first = true;
  second = false;

  for (UTL_IdListActiveIterator j (this->name ());
       !j.is_done ();
       j.next ())
    {
      if (!first)
        {
          ACE_OS::strcat (this->repoID_, "/");
        }
      else if (second)
        {
          first = second = false;
        }

      // Print the identifier.
      name = j.item ()->get_string ();

      if (ACE_OS::strstr (name, "_cxx_") == name)
        {
          ACE_OS::strcat (this->repoID_,
                          name + ACE_OS::strlen ("_cxx_"));
        }
      else
        {
          ACE_OS::strcat (this->repoID_, name);
        }

      if (first)
        {
          if (ACE_OS::strcmp (name, ""))
            {
              // Does not start with a "".
              first = false;
            }
          else
            {
              second = true;
            }
        }
    }

  if (version)
    {
      ACE_OS::strcat (this->repoID_, ":");
      ACE_OS::strcat (this->repoID_, version);
    }
  else
    {
      ACE_OS::strcat (this->repoID_, ":1.0");
    }
}

// Public operations.

const char *
AST_Decl::flat_name ()
{
  if (!this->flat_name_)
    {
      this->compute_flat_name ();
    }

  return this->flat_name_;
}

// Compute stringified flattened fully scoped name.
void
AST_Decl::compute_flat_name ()
{
  if (this->flat_name_ == nullptr)
    {
      size_t namelen = 0;
      long first = true;
      long second = false;
      char *item_name = nullptr;

      // In the first loop, compute the total length.
      for (UTL_IdListActiveIterator i (this->name ());
           !i.is_done ();
           i.next ())
        {
          if (!first)
            {
              namelen += 1; // for "_"
            }
          else if (second)
            {
              first = second = false;
            }

          // Print the identifier, leaving out
          // _cxx_ prefix, if any.
          Identifier *tmp = i.item ()->copy ();
          FE_Utils::original_local_name (tmp);
          item_name = tmp->get_string ();
          namelen += ACE_OS::strlen (item_name);

          if (first)
            {
              if (ACE_OS::strcmp (item_name, "") != 0)
                {
                  // Does not start with a "".
                  first = false;
                }
              else
                {
                  second = true;
                }
            }

          tmp->destroy ();
          delete tmp;
          tmp = nullptr;
        }

      ACE_NEW (this->flat_name_, char [namelen + 1]);
      this->flat_name_ [0] = '\0';
      first = true;
      second = false;

      for (UTL_IdListActiveIterator j (this->name ());
           !j.is_done ();
           j.next ())
        {
          if (!first)
            {
              ACE_OS::strcat (this->flat_name_, "_");
            }
          else if (second)
            {
              first = second = false;
            }

          // Print the identifier, leaving out
          // _cxx_ prefix, if any.
          Identifier *tmp = j.item ()->copy ();
          FE_Utils::original_local_name (tmp);
          item_name = tmp->get_string ();
          ACE_OS::strcat (this->flat_name_, item_name);

          if (first)
            {
              if (ACE_OS::strcmp (item_name, ""))
                {
                  // Does not start with a "".
                  first = false;
                }
              else
                {
                  second = true;
                }
            }

          tmp->destroy ();
          delete tmp;
          tmp = nullptr;
        }
    }
}

const char *
AST_Decl::node_type_to_string (NodeType nt)
{
  switch (nt)
    {
    case NT_module:
      return "module";

    case NT_interface:
    case NT_interface_fwd:
      return "interface";

    case NT_valuetype:
    case NT_valuetype_fwd:
    case NT_valuebox:
      return "valuetype";

    case NT_const:
      return "const";

    case NT_except:
      return "exception";

    case NT_attr:
      return "attribute";

    case NT_union:
    case NT_union_fwd:
      return "union";

    case NT_struct:
    case NT_struct_fwd:
      return "struct";

    case NT_enum:
      return "enum";

    case NT_string:
      return "string";

    case NT_wstring:
      return "wstring";

    case NT_array:
      return "array";

    case NT_sequence:
      return "sequence";

    case NT_typedef:
      return "typedef";

    case NT_pre_defined:
      return "primitive";

    case NT_native:
      return "native";

    case NT_factory:
      return "factory";

    case NT_component:
    case NT_component_fwd:
      return "component";

    case NT_home:
      return "home";

    case NT_eventtype:
    case NT_eventtype_fwd:
      return "eventtype";

    case NT_type:
      return "typename";

    case NT_fixed:
      return "fixed";

    case NT_porttype:
      return "porttype";

    case NT_provides:
      return "provides";

    case NT_uses:
      return "uses";

    case NT_publishes:
      return "publishes";

    case NT_emits:
      return "emits";

    case NT_consumes:
      return "consumes";

    // No useful output for these.
    default:
      return "";
    }
}

// Return TRUE if one of my ancestor scopes is "s"
// and FALSE otherwise.
bool
AST_Decl::has_ancestor (AST_Decl *s)
{
  AST_Decl *work = this;
  do
    {
      if (work == s)
        {
          return true;
        }

      AST_Module *m = dynamic_cast<AST_Module*> (s);
      if (m)
        {
          while (!!(m = m->previous_opening ()))
            {
              if (static_cast<AST_Decl *> (m) == s)
                {
                  return true;
                }
            }
        }

      work = work->pd_defined_in ?
             ScopeAsDecl (work->pd_defined_in) :
             nullptr;
    } while (work);

  return false;
}

bool
AST_Decl::is_child (AST_Decl *s)
{
  if (this->defined_in ())
    {
      AST_Decl *d = ScopeAsDecl (this->defined_in ());
      if (!d)
        {
          return false;
        }

      if (!ACE_OS::strcmp (d->full_name (), s->full_name ()))
        {
          return true;
        }
    }

  return false; // Not a child.
}

bool
AST_Decl::is_nested ()
{
  AST_Decl *d = ScopeAsDecl (this->defined_in ());

  // If we have an outermost scope and if that scope is not that of the Root,
  // then we are defined at some nesting level.
  return (d && d->node_type () != AST_Decl::NT_root);
}

// Dump this AST_Decl to the ostream o.
void
AST_Decl::dump (ACE_OSTREAM_TYPE &o)
{
  this->pd_local_name->dump (o);
}

void
AST_Decl::dump_i (ACE_OSTREAM_TYPE &o, const char *s) const
{
  // Have to use ACE_CString here to avoid ambiguous overload error, see
  // SString.h for an the overloaded operator << () methods.
  o << ACE_CString(s);
}

int
AST_Decl::ast_accept (ast_visitor *visitor)
{
  return visitor->visit_decl (this);
}

// Data accessors.

const char *
AST_Decl::full_name ()
{
  if (!this->full_name_)
    {
      this->compute_full_name ();
    }

  return this->full_name_;
}

const char *
AST_Decl::repoID ()
{
  if (this->pd_node_type == NT_root)
    {
      delete [] this->repoID_;
      this->repoID_ = ACE::strnew ("");
    }

  if (!this->repoID_)
    {
      this->compute_repoID ();
    }

  return this->repoID_;
}

void
AST_Decl::repoID (char *value)
{
  delete [] this->repoID_;
  this->repoID_ = value;
}

const char *
AST_Decl::prefix ()
{
  return this->prefix_;
}

void
AST_Decl::prefix (const char *value)
{
  delete [] this->prefix_;
  this->prefix_ = ACE::strnew (value);
}

const char *
AST_Decl::version ()
{
  if (!this->version_)
    {
      // Calling the method will compute if necessary.
      const char *repo_id = this->repoID ();

      // All forms of repo id should contain two colons, the
      // version coming after the second one.
      const char *tail1 = nullptr;
      const char *tail2 = nullptr;

      if (repo_id)
        {
          tail1 = ACE_OS::strchr (repo_id, ':');
        }

      if (tail1)
        {
          tail2 = ACE_OS::strchr (tail1 + 1, ':');
        }

      if (!this->typeid_set_ && tail2)
        {
          this->version_ = ACE::strnew (tail2 + 1);
        }
      else
        {
          this->version_ = ACE::strnew ("1.0");
        }
    }

  return this->version_;
}

void
AST_Decl::version (char *value)
{
  // Previous #pragma version or #pragma id make this illegal.
  if ((!this->version_ || !ACE_OS::strcmp (this->version_, value))
      && !this->typeid_set_)
    {
      delete [] this->version_;
      this->version_ = value;

      // Repo id is now computed eagerly, so a version set must update
      // is as well.
      if (this->repoID_)
        {
          ACE_CString tmp (this->repoID_);
          ACE_CString::size_type const pos = tmp.rfind (':');
          if (pos != ACE_CString::npos)
            {
              tmp = tmp.substr (0, pos + 1) + value;
              delete [] this->repoID_;
              this->repoID_ = ACE::strnew (tmp.fast_rep ());
            }
        }
    }
  else
    {
      idl_global->err ()->version_reset_error ();
    }
}

bool
AST_Decl::anonymous () const
{
  return this->anonymous_;
}

void
AST_Decl::anonymous (bool val)
{
  this->anonymous_ = val;
}

bool
AST_Decl::typeid_set () const
{
  return this->typeid_set_;
}

void
AST_Decl::typeid_set (bool val)
{
  this->typeid_set_ = val;
}

void
AST_Decl::set_id_with_typeid (char *value)
{
  // Can't call 'typeid' twice, even with the same value.
  if (this->typeid_set ())
    {
      idl_global->err ()->error1 (UTL_Error::EIDL_TYPEID_RESET, this);
    }

  // Are we a legal type for 'typeid'?
  switch (this->pd_node_type)
    {
    case AST_Decl::NT_field:
      {
        AST_Decl::NodeType nt =
          ScopeAsDecl (this->defined_in ())->node_type ();

        if (   nt == AST_Decl::NT_valuetype
            || nt == AST_Decl::NT_eventtype)
          {
            break;
          }
        else
          {
            idl_global->err ()->error1 (UTL_Error::EIDL_INVALID_TYPEID, this);
            return;
          }
      }

    case AST_Decl::NT_module:
    case AST_Decl::NT_interface:
    case AST_Decl::NT_const:
    case AST_Decl::NT_typedef:
    case AST_Decl::NT_except:
    case AST_Decl::NT_attr:
    case AST_Decl::NT_op:
    case AST_Decl::NT_enum:
    case AST_Decl::NT_factory:
    case AST_Decl::NT_component:
    case AST_Decl::NT_home:
    case AST_Decl::NT_eventtype:
      break;

    default:
      idl_global->err ()->error1 (UTL_Error::EIDL_INVALID_TYPEID, this);
      return;
    }

  delete [] this->repoID_;
  this->repoID_ = nullptr;
  this->repoID (ACE::strnew (value));
  this->typeid_set_ = true;
}

void
AST_Decl::set_prefix_with_typeprefix (const char *value)
{
  // Are we a legal type for 'typeprefix'? This is checked only at
  // the top level.
  switch (this->pd_node_type)
    {
    case AST_Decl::NT_module:
    case AST_Decl::NT_interface:
    case AST_Decl::NT_valuetype:
    case AST_Decl::NT_eventtype:
    case AST_Decl::NT_struct:
    case AST_Decl::NT_union:
    case AST_Decl::NT_except:
      break;

    default:
      idl_global->err ()->error1 (UTL_Error::EIDL_INVALID_TYPEPREFIX, this);
      return;
    }

  this->set_prefix_with_typeprefix_r (value, DeclAsScope (this));
}

bool
AST_Decl::imported ()
{
  return this->pd_imported;
}

void
AST_Decl::set_imported (bool is_it)
{
  this->pd_imported = is_it;
}

bool
AST_Decl::in_main_file ()
{
  return this->pd_in_main_file;
}

void
AST_Decl::set_in_main_file (bool is_it)
{
  this->pd_in_main_file = is_it;
}

UTL_Scope *
AST_Decl::defined_in ()
{
  return this->pd_defined_in;
}

void
AST_Decl::set_defined_in (UTL_Scope *s)
{
  this->pd_defined_in = s;
}

AST_Decl::NodeType
AST_Decl::node_type ()
{
  return this->pd_node_type;
}

long
AST_Decl::line ()
{
  return this->pd_line;
}

void
AST_Decl::set_line (long l)
{
  this->pd_line = l;
}

ACE_CString &
AST_Decl::file_name ()
{
  return this->pd_file_name;
}

void
AST_Decl::set_file_name (ACE_CString s)
{
  this->pd_file_name = s;
}

UTL_ScopedName *
AST_Decl::name ()
{
  return this->pd_name;
}

// @@ Wherever compute_* are called, we should remember to delete them
//    after use.

// Variation of the <name>. Computes scoped name string, applying
// prefix and suffix to the local name component.
UTL_ScopedName *
AST_Decl::compute_name (const char *prefix,
                        const char *suffix)
{
  if (!prefix || !suffix)
    {
      return nullptr;
    }

  // Prepare prefix_<local_name>_suffix string.

  ACE_CString suffix_str (suffix);
  ACE_CString local_str (this->local_name ()->get_string ());

  ACE_CString result_local_str (prefix);
  result_local_str += local_str;
  result_local_str += suffix_str;

  // Identifier for the resulting local name.
  Identifier *result_local_id = nullptr;
  ACE_NEW_RETURN (result_local_id,
                  Identifier (result_local_str.c_str ()),
                  nullptr);

  // UTL_Scoped name for the resulting local name.
  UTL_ScopedName *result_local_name = nullptr;
  ACE_NEW_RETURN (result_local_name,
                  UTL_ScopedName (result_local_id, nullptr),
                  nullptr);

  // Global scope?
  UTL_ScopedName *result_name = nullptr;
  if (!this->defined_in ())
    {
      result_name = result_local_name;
    }
  else
    {
      // OK, not global. So copy name of containing scope, then
      // smash last cdr of copy with new component.

      AST_Decl *d = ScopeAsDecl (this->defined_in ());
      if (d)
        {
          UTL_ScopedName *cn = d->name ();
          if (cn)
            {
              result_name = (UTL_ScopedName *) cn->copy ();
              if (result_name)
                {
                  result_name->nconc (result_local_name);
                }
              else
                {
                  result_name = result_local_name;
                }
            }
        }
    }

  return result_name;
}

void
AST_Decl::set_name (UTL_ScopedName *n)
{
  if (this->pd_name == n)
    {
      return;
    }

  if (this->pd_name)
    {
      this->pd_name->destroy ();
      delete this->pd_name;
    }

  this->pd_name = n;

  if (n)
    {
      if (this->pd_local_name)
        {
          this->pd_local_name->destroy ();
          delete this->pd_local_name;
        }

      this->pd_local_name = n->last_component ()->copy ();

      // The name without _cxx_ prefix removed, if there was any.
      if (this->pd_original_local_name)
        {
          this->pd_original_local_name->destroy ();
          delete this->pd_original_local_name;
        }

      this->original_local_name (n->last_component ());

      // These will be recomputed on demand.
      delete [] this->flat_name_;
      this->flat_name_ = nullptr;

      delete [] this->full_name_;
      this->full_name_ = nullptr;

      delete [] this->repoID_;
      this->repoID_ = nullptr;
    }
}

Identifier *
AST_Decl::local_name ()
{
  return this->pd_local_name;
}

void
AST_Decl::local_name (Identifier *id)
{
  if (this->pd_local_name)
    {
      this->pd_local_name->destroy ();
    }

  delete this->pd_local_name;
  this->pd_local_name = id;
}

Identifier *
AST_Decl::compute_local_name (const char *prefix,
                              const char *suffix)
{
  if (!prefix || !suffix)
    {
      return nullptr;
    }

  // Init the result with prefix.
  ACE_CString result_str (prefix);

  // Put local.
  result_str += ACE_CString (this->local_name ()->get_string ());

  // Put suffix.
  result_str += ACE_CString (suffix);

  // Identifier for the resulting local name.
  Identifier *result_id = nullptr;
  ACE_NEW_RETURN (result_id,
                  Identifier (result_str.c_str ()),
                  nullptr);

  return result_id;
}

// If there is _cxx_ in the beginning, we will remove that and keep
// a copy of the original name. TAO IDL's front end adds _cxx_
// prefix to the all the reserved keywords. But when we invoke the
// operation remotely, we should be sending only the name with out
// "_cxx_" prefix.
void
AST_Decl::original_local_name (Identifier *local_name)
{
  // Remove _cxx_ if it is present.
  if (ACE_OS::strstr (local_name->get_string (), "_cxx_")
        == local_name->get_string ())
    {
      // AACE_CString class is good to do this stuff.
      ACE_CString name_str (local_name->get_string ());

      // Remove _cxx_.
      name_str = name_str.substr (ACE_OS::strlen ("_cxx_"));

      // Assign to the Identifier variable.
      ACE_NEW (this->pd_original_local_name,
               Identifier (name_str.c_str ()));
    }
  else
    {
      this->pd_original_local_name = local_name->copy ();
    }
}

Identifier *
AST_Decl::original_local_name ()
{
  return this->pd_original_local_name;
}

bool
AST_Decl::is_defined ()
{
  // AST_Interface, AST_Structure, and AST_Union will
  // override this, as will AST_InterfaceFwd, etc.
  return true;
}

UTL_ScopedName *
AST_Decl::last_referenced_as () const
{
  return this->last_referenced_as_;
}

void
AST_Decl::last_referenced_as (UTL_ScopedName *n)
{
  if (this->last_referenced_as_)
    {
      this->last_referenced_as_->destroy ();
    }

  delete this->last_referenced_as_;
  this->last_referenced_as_ = n;
}

UTL_Scope *
AST_Decl::prefix_scope ()
{
  return this->prefix_scope_;
}

void
AST_Decl::prefix_scope (UTL_Scope *s)
{
  this->prefix_scope_ = s;
}

// Container types will override this.
int
AST_Decl::contains_wstring ()
{
  if (this->contains_wstring_ == -1)
    {
      switch (this->node_type ())
        {
        case AST_Decl::NT_array:
          {
            AST_Array *a = dynamic_cast<AST_Array*> (this);
            this->contains_wstring_ = a->base_type ()->contains_wstring ();
            break;
          }

        case AST_Decl::NT_except:
        case AST_Decl::NT_struct:
        case AST_Decl::NT_union:
          {
            AST_Structure *s = dynamic_cast<AST_Structure*> (this);
            this->contains_wstring_ = s->contains_wstring ();
            break;
          }

        case AST_Decl::NT_sequence:
          {
            AST_Sequence *s = dynamic_cast<AST_Sequence*> (this);
            this->contains_wstring_ = s->base_type ()->contains_wstring ();
            break;
          }

        case AST_Decl::NT_attr:
        case AST_Decl::NT_field:
        case AST_Decl::NT_union_branch:
          {
            AST_Field *f = dynamic_cast<AST_Field*> (this);
            this->contains_wstring_ = f->field_type ()->contains_wstring ();
            break;
          }

        case AST_Decl::NT_typedef:
          {
            AST_Typedef *td = dynamic_cast<AST_Typedef*> (this);
            this->contains_wstring_ =
              td->primitive_base_type ()->contains_wstring ();
            break;
          }

        case AST_Decl::NT_wstring:
          this->contains_wstring_ = 1;
          break;

        default:
          this->contains_wstring_ = 0;
          break;
        }
    }

  return this->contains_wstring_;
}

// Non-virtual - no need to override this one.
void
AST_Decl::contains_wstring (int val)
{
  this->contains_wstring_ = val;
}

bool
AST_Decl::masking_checks (AST_Decl *mod)
{
  if (!this->pd_local_name->case_compare (mod->local_name ()))
    {
      return true;
    }

  AST_Module *me_mod = dynamic_cast<AST_Module*> (this);

  if (me_mod != nullptr)
    {
      AST_Module *po_mod = dynamic_cast<AST_Module*> (mod);
      if (po_mod)
        {
          while (!!(po_mod = po_mod->previous_opening ()))
            {
              if (po_mod == me_mod)
                {
                  return true;
                }
            }
        }
    }

  return false;
}

bool
AST_Decl::in_tmpl_mod_not_aliased () const
{
  return this->in_tmpl_mod_not_aliased_;
}

void
AST_Decl::in_tmpl_mod_not_aliased (bool val)
{
  this->in_tmpl_mod_not_aliased_ = val;
}

void
AST_Decl::annotation_appls (const AST_Annotation_Appls &annotations)
{
  if (annotatable ())
    {
      annotation_appls () = annotations;
    }
  else
    {
      ACE_ERROR ((LM_ERROR,
        ACE_TEXT ("WARNING: %C is annotated but its type can't be annotated!\n"),
        full_name ()
        ));
    }
}

AST_Annotation_Appls&
AST_Decl::annotation_appls ()
{
  if (!annotation_appls_)
    {
      annotation_appls_ = new AST_Annotation_Appls ();
    }
  return *annotation_appls_;
}

void
AST_Decl::dump_annotations (ACE_OSTREAM_TYPE &o, bool print_inline)
{
  AST_Annotation_Appls::iterator
    i = annotation_appls ().begin (),
    finished = annotation_appls ().end ();
  for (; i != finished; ++i)
    {
      AST_Annotation_Appl* a = i->get ();
      a->dump (o);
      if (print_inline)
        {
          dump_i (o, " ");
        }
      else
        {
          dump_i (o, "\n");
          idl_global->indent ()->skip_to (o);
        }
    }
}

void
AST_Decl::dump_with_annotations (ACE_OSTREAM_TYPE &o, bool inline_annotations)
{
  if (annotatable () && auto_dump_annotations())
    {
      dump_annotations (o, inline_annotations);
    }

  dump (o);
}

ACE_OSTREAM_TYPE &
operator<< (ACE_OSTREAM_TYPE &o, AST_Decl &d)
{
  d.dump_with_annotations (o, d.dump_annotations_inline ());

  return o;
}

bool
AST_Decl::annotatable () const
{
  return true;
}

bool
AST_Decl::dump_annotations_inline () const
{
  return false;
}

bool
AST_Decl::auto_dump_annotations () const
{
  return true;
}

bool
AST_Decl::builtin () const
{
  return builtin_;
}

bool
AST_Decl::should_be_dumped () const
{
  return !builtin () || idl_global->dump_builtins_;
}

AST_Annotation_Appls &
AST_Decl::annotations ()
{
  return annotation_appls ();
}

bool
AST_Decl::ami_visit ()
{
  return true;
}