summaryrefslogtreecommitdiff
path: root/CIAO/tools/IDL3_to_IDL2/idl3_to_idl2_visitor.cpp
blob: 944e82fca9f5de1caf2fa5a2e2ed0f1f35285603 (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
/* -*- c++ -*- */
#include "idl3_to_idl2_visitor.h"
#include "idl3p_checking_visitor.h"
#include "identifier_helper.h"
#include "be_sunsoft.h"
#include "be_extern.h"

#include "ast_connector.h"
#include "ast_porttype.h"
#include "ast_mirror_port.h"
#include "ast_component_fwd.h"
#include "ast_provides.h"
#include "ast_uses.h"
#include "ast_publishes.h"
#include "ast_emits.h"
#include "ast_consumes.h"
#include "ast_eventtype.h"
#include "ast_eventtype_fwd.h"
#include "ast_home.h"
#include "ast_finder.h"
#include "ast_operation.h"
#include "ast_root.h"

#include "utl_exceptlist.h"
#include "utl_identifier.h"
#include "global_extern.h"
#include "nr_extern.h"

idl3_to_idl2_visitor::idl3_to_idl2_visitor (void)
  : home_ (0)
{
}

idl3_to_idl2_visitor::~idl3_to_idl2_visitor (void)
{
}

int
idl3_to_idl2_visitor::visit_module (AST_Module *node)
{
  if (node->imported ())
    {
      return 0;
    }

  idl3p_checking_visitor v;

  /// Inherited visit_* methods must return int, but this
  /// visitor never returns anything but 0.
  (void) v.visit_scope (node);

  if (!v.needs_codegen ())
    {
      /// We'd be generating an (illegal) empty module.
      return 0;
    }

  *os << be_nl << be_nl;

  ACE_CString name =
    IdentifierHelper::try_escape (node->original_local_name ());

  *os << "module " << name.c_str () << be_nl
      << "{" << be_idt;

  this->check_id_and_version (node);
  this->check_prefix (node);

  if (this->visit_scope (node) != 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                          "idl3_to_idl2_visitor::visit_module - "
                          "codegen for scope failed\n"),
                        -1);
    }

  *os << be_uidt_nl
      << "}; // end of module " << name.c_str ();

  return 0;
}

int
idl3_to_idl2_visitor::visit_interface (AST_Interface *node)
{
  if (node->imported ())
    {
      return 0;
    }

  *os << be_nl << be_nl;

  if (node->is_local ())
    {
      *os << "local ";
    }

  if (node->is_abstract ())
    {
      *os << "abstract ";
    }

  *os << "interface "
      << IdentifierHelper::try_escape (node->original_local_name ()).c_str ();

  AST_Type **parents = node->inherits ();

  for (long i = 0; i < node->n_inherits (); ++i)
    {
      if (i == 0)
        {
          *os << " : ";
        }
      else
        {
          *os << ", ";
        }

      *os << IdentifierHelper::orig_sn (parents[i]->name ()).c_str ();
    }

  *os << be_nl
      << "{" << be_idt;

  this->check_id_and_version (node);
  this->check_prefix (node);

  if (this->visit_scope (node) != 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                          "idl3_to_idl2_visitor::visit_interface - "
                          "codegen for scope failed\n"),
                        -1);
    }

  *os << be_uidt_nl
      << "};";

  return 0;
}

int
idl3_to_idl2_visitor::visit_component (AST_Component *node)
{
  if (node->imported ())
    {
      return 0;
    }

  *os << be_nl << be_nl
      << "interface "
      << IdentifierHelper::try_escape (node->original_local_name ()).c_str ();

  AST_Component *base = node->base_component ();

  *os << be_idt_nl
      << ": "
      << (base != 0
            ? IdentifierHelper::orig_sn (base->name ()).c_str ()
            : "Components::CCMObject")
      << be_idt;

  long nsupports = node->n_supports ();
  AST_Type **sups = node->supports ();

  for (long i = 0; i < nsupports; ++i)
    {
      *os << "," << be_nl
          << IdentifierHelper::orig_sn (sups[i]->name ()).c_str ();
    }

  *os << be_uidt << be_uidt_nl
      << "{" << be_idt;

  this->check_id_and_version (node);

  if (this->visit_scope (node) != 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                          "idl3_to_idl2_visitor::visit_component - "
                          "codegen for scope failed\n"),
                        -1);
    }

  *os << be_uidt_nl
      << "};";

  return 0;
}

int
idl3_to_idl2_visitor::visit_component_fwd (AST_ComponentFwd *node)
{
  if (node->imported ())
    {
      return 0;
    }

  *os << be_nl << be_nl
      << "component "
      << IdentifierHelper::try_escape (node->original_local_name ()).c_str ()
      << ";";

  return 0;
}

int
idl3_to_idl2_visitor::visit_provides (AST_Provides *node)
{
  AST_Type *pt = node->provides_type ();

  /// These ports are always internal to the container, no
  /// equivalent IDL should be generated for them.
  if (pt->is_local ())
    {
      return 0;
    }

  Identifier *orig_id =
    IdentifierHelper::original_local_name (node->local_name ());

  UTL_ScopedName *n = pt->name ();
  ACE_CString impl_name =
    IdentifierHelper::orig_sn (n);

  *os << be_nl << be_nl
      << impl_name.c_str () << " provide_"
      << this->port_prefix_.c_str () << orig_id << " ();";

  orig_id->destroy ();
  delete orig_id;
  orig_id = 0;

  return 0;
}

int
idl3_to_idl2_visitor::visit_uses (AST_Uses *node)
{
  AST_Type *ut = node->uses_type ();

  /// These ports are always internal to the container, no
  /// equivalent IDL should be generated for them.
  if (ut->is_local ())
    {
      return 0;
    }

  *os << be_nl << be_nl;

  Identifier *orig_id =
    IdentifierHelper::original_local_name (node->local_name ());

  UTL_ScopedName *n = ut->name ();
  ACE_CString impl_str =
    IdentifierHelper::orig_sn (n);
  const char *impl_name = impl_str.c_str ();

  ACE_CString port_name (this->port_prefix_);
  port_name += orig_id->get_string ();
  const char *ext_port_name = port_name.c_str ();

  if (node->is_multiple ())
    {
      /// We generate these by hand instead of by traversal so
      /// they will be declared before the get_connections()
      /// operation below.
      *os << "struct " << ext_port_name
          << "Connection" << be_nl
          << "{" << be_idt_nl
          << impl_name << " objref;" << be_nl
          << "Components::Cookie ck;" << be_uidt_nl
          << "};" << be_nl << be_nl
          << "typedef sequence< " << ext_port_name
          << "Connection> " << ext_port_name << "Connections;"
          << be_nl << be_nl;

      *os << "Components::Cookie connect_" << ext_port_name
          << " (in " << impl_name << " connection)"
          << be_idt_nl
          << "raises (Components::ExceededConnectionLimit, "
          << "Components::InvalidConnection);"
          << be_uidt_nl << be_nl
          << impl_name << " disconnect_"
          << ext_port_name << " (in Components::Cookie ck)"
          << be_idt_nl
          << "raises (Components::InvalidConnection);"
          << be_uidt_nl << be_nl
          << ext_port_name << "Connections get_connections_"
          << ext_port_name << " ();";
    }
  else
    {
      *os << "void connect_" << ext_port_name << " (in "
          << impl_name << " conxn)" << be_idt_nl
          << "raises (Components::AlreadyConnected, "
          << "Components::InvalidConnection);"
          << be_uidt_nl << be_nl
          << impl_name << " disconnect_"
          << ext_port_name << " ()" << be_idt_nl
          << "raises (Components::NoConnection);"
          << be_uidt_nl << be_nl
          << impl_name << " get_connection_"
          << ext_port_name << " ();";
    }

  orig_id->destroy ();
  delete orig_id;
  orig_id = 0;

  return 0;
}

int
idl3_to_idl2_visitor::visit_publishes (AST_Publishes *node)
{
  Identifier *orig_id =
    IdentifierHelper::original_local_name (node->local_name ());

  UTL_ScopedName *n = node->publishes_type ()->name ();
  ACE_CString impl_name =
    IdentifierHelper::orig_sn (n, true);

  *os << be_nl << be_nl
      << "Components::Cookie subscribe_" << orig_id
      << " (in "
      << impl_name.c_str () << "Consumer consumer)"
      << be_idt_nl
      << "raises (Components::ExceededConnectionLimit);"
      << be_uidt_nl << be_nl
      << impl_name.c_str () << "Consumer unsubscribe_" << orig_id
      << " (in Components::Cookie ck)" << be_idt_nl
      << "raises (Components::InvalidConnection);" << be_uidt;

  orig_id->destroy ();
  delete orig_id;
  orig_id = 0;

  return 0;
}

int
idl3_to_idl2_visitor::visit_emits (AST_Emits *node)
{
  Identifier *orig_id =
    IdentifierHelper::original_local_name (node->local_name ());

  UTL_ScopedName *n = node->emits_type ()->name ();
  ACE_CString impl_name =
    IdentifierHelper::orig_sn (n, true);

  *os << be_nl << be_nl
      << "void connect_" << orig_id
      << " (in "
      << impl_name.c_str () << "Consumer consumer)" << be_idt_nl
      << "raises (Components::AlreadyConnected);"
      << be_uidt_nl << be_nl
      << impl_name.c_str () << "Consumer disconnect_" << orig_id
      << " ()" << be_idt_nl
      << "raises (Components::NoConnection);" << be_uidt;

  orig_id->destroy ();
  delete orig_id;
  orig_id = 0;

  return 0;
}

int
idl3_to_idl2_visitor::visit_consumes (AST_Consumes *node)
{
  Identifier *orig_id =
    IdentifierHelper::original_local_name (node->local_name ());

  UTL_ScopedName *n = node->consumes_type ()->name ();
  ACE_CString impl_name =
    IdentifierHelper::orig_sn (n, true);

  *os << be_nl << be_nl
      << impl_name.c_str () << "Consumer get_consumer_" << orig_id
      << " ();";

  orig_id->destroy ();
  delete orig_id;
  orig_id = 0;

 return 0;
}

int
idl3_to_idl2_visitor::visit_porttype (AST_PortType *)
{
  /// We want to visit these nodes only by navigating from an
  /// extended port or a mirror port.
  return 0;
}

int
idl3_to_idl2_visitor::visit_extended_port (AST_Extended_Port *node)
{
  AST_Decl::NodeType nt =
    ScopeAsDecl (node->defined_in ())->node_type ();

  /// Skip if we are defined inside a porttype.
  /// Depends on nested ports not being allowed.
  if (nt == AST_Decl::NT_component || nt == AST_Decl::NT_connector)
    {
      this->port_prefix_ = node->local_name ()->get_string ();
      this->port_prefix_ += '_';
    }

  if (this->visit_porttype_scope (node->port_type ()) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("idl3_to_idl2_visitor")
                         ACE_TEXT ("::visit_extended_port - ")
                         ACE_TEXT ("visit porttype scope failed\n")),
                        -1);
    }

  /// Reset port prefix string.
  this->port_prefix_ = "";
  return 0;
}

int
idl3_to_idl2_visitor::visit_mirror_port (AST_Mirror_Port *node)
{
  AST_Decl::NodeType nt =
    ScopeAsDecl (node->defined_in ())->node_type ();

  /// Skip if we are defined inside a porttype.
  /// Depends on nested ports not being allowed.
  if (nt == AST_Decl::NT_component || nt == AST_Decl::NT_connector)
    {
      this->port_prefix_ = node->local_name ()->get_string ();
      this->port_prefix_ += '_';
    }

  int status =
    this->visit_porttype_scope_mirror (node->port_type ());

  if (status == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("idl3_to_idl2_visitor")
                         ACE_TEXT ("::visit_mirror_port - ")
                         ACE_TEXT ("visit_porttype_scope")
                         ACE_TEXT ("_mirror failed\n")),
                        -1);
    }

  /// Reset port prefix string.
  this->port_prefix_ = "";
  return 0;
}

int
idl3_to_idl2_visitor::visit_connector (AST_Connector *node)
{
  return this->visit_component (node);
}

int
idl3_to_idl2_visitor::visit_param_holder (AST_Param_Holder *)
{
  return 0;
}

int
idl3_to_idl2_visitor::visit_eventtype (AST_EventType *node)
{
  if (node->imported ())
    {
      return 0;
    }

  if (this->visit_valuetype (node) != 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                          "idl3_to_idl2_visitor::visit_eventtype - "
                          "codegen for valuetype failed\n"),
                        -1);
    }

  *os << be_nl << be_nl
      << "interface " << node->original_local_name () << "Consumer : ";

  AST_Type *parent = 0;
  AST_Decl::NodeType nt = AST_Decl::NT_native;

  if (node->n_inherits () > 0)
    {
      parent = node->inherits ()[0];
      AST_Type *ut = parent->unaliased_type ();
      nt = ut->node_type ();
    }

  if (node->n_inherits () == 0 || nt == AST_Decl::NT_valuetype)
    {
      *os << "Components::EventConsumerBase";
    }
  else
    {
      *os << IdentifierHelper::orig_sn (node->inherits ()[0]->name (), true).c_str ()
          << "Consumer";
    }

  *os << be_nl
      << "{" << be_idt_nl
      << "void push_" << node->original_local_name () << " (in "
      << IdentifierHelper::try_escape (node->original_local_name ()).c_str ()
      << " the_"
      << node->original_local_name () << ");" << be_uidt_nl
      << "};";

  return 0;
}

int
idl3_to_idl2_visitor::visit_eventtype_fwd (AST_EventTypeFwd *node)
{
  if (node->imported ())
    {
      return 0;
    }

  if (this->visit_valuetype_fwd (node) != 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                          "idl3_to_idl2_visitor::visit_eventtype_fwd - "
                          "codegen for valuetype_fwd failed\n"),
                        -1);
    }

  *os << be_nl
      << "interface " << node->original_local_name () << "Consumer;";

  return 0;
}

int
idl3_to_idl2_visitor::visit_home (AST_Home *node)
{
  if (node->imported ())
    {
      return 0;
    }

  this->home_ = node;

  ACE_CString explicit_name = node->original_local_name ()->get_string ();
  explicit_name += "Explicit";

  *os << be_nl << be_nl
      << "interface " << explicit_name.c_str () << be_idt_nl
      << ": ";

  AST_Home *base = node->base_home ();

  if (base == 0)
    {
      *os << "Components::CCMHome";
    }
  else
    {
      *os << IdentifierHelper::orig_sn (base->name (), true).c_str ()
          << "Explicit";
    }

  *os << be_idt;

  long nsupports = node->n_supports ();
  AST_Type **sups = node->supports ();

  for (long i = 0; i < nsupports; ++i)
    {
      *os << "," << be_nl
          << IdentifierHelper::orig_sn (sups[i]->name ()).c_str ();
    }

  *os << be_uidt << be_uidt_nl
      << "{" << be_idt;

  this->check_id_and_version (node);

  // Create a temporary interface node corresponding to the one we
  // just generated above for the home explicit interface.
  UTL_ScopedName *sn =
    this->create_scoped_name (0,
                              explicit_name.c_str (),
                              0,
                              ScopeAsDecl (node->defined_in ()));

  AST_Interface xplicit (sn,
                         0,
                         0,
                         0,
                         0,
                         false,
                         false);
  xplicit.set_defined_in (node->defined_in ());

  // Reset the home's decls to be defined in the explicit home interface.
  this->tranfer_scope_elements (node, xplicit);

  *os << be_uidt_nl
      << "};" << be_nl << be_nl;

  xplicit.set_defined_in (0);
  xplicit.destroy ();
  sn->destroy ();
  delete sn;
  sn = 0;

  AST_Type *key = node->primary_key ();
  ACE_CString mng_name =
    IdentifierHelper::orig_sn (node->managed_component ()->name ());
  ACE_CString key_name;

  // Generate the implicit home interface and its operations.
  *os << "interface " << node->original_local_name () << "Implicit"
      << (key == 0 ? " : Components::KeylessCCMHome" : "") << be_nl
      << "{" << be_idt_nl
      << mng_name.c_str () << " create (";

  if (key != 0)
    {
      key_name =
        IdentifierHelper::orig_sn (key->name ());

      *os << "in " << key_name.c_str () << " key";
    }

  *os << ")" << be_idt_nl
      << "raises (Components::CreateFailure";

  if (key != 0)
    {
      *os << ", Components::InvalidKey, Components::DuplicateKeyValue";
    }

  *os << ");" << be_uidt;

  if (key != 0)
    {
      *os << be_nl << be_nl
          << mng_name.c_str ()
          << " find_by_primary_key (in " << key_name.c_str ()
          << " key)" << be_idt_nl
          << "raises (Components::InvalidKey, Components::UnknownKeyValue, "
          << "Components::FinderFailure);" << be_uidt;

      *os << be_nl << be_nl
          << "void remove (in " << key_name.c_str () << " key)" << be_idt_nl
          << "raises (Components::InvalidKey, Components::UnknownKeyValue, "
          << "Components::RemoveFailure);" << be_uidt;

      *os << be_nl << be_nl
          << key_name.c_str () << " get_primary_key (in "
          << mng_name.c_str () << " comp);";
    }

  *os << be_uidt_nl
      << "};";

  // Create equivalent interface.
  *os << be_nl << be_nl
      << "interface "
      << IdentifierHelper::try_escape (node->original_local_name ()).c_str ()
      << " : "
      << node->original_local_name () << "Explicit, "
      << node->original_local_name () << "Implicit" << be_nl
      << "{" << be_nl
      << "};";

  return 0;
}

int
idl3_to_idl2_visitor::visit_factory (AST_Factory *node)
{
  Identifier *id = node->original_local_name ();

  *os << be_nl << be_nl;

  if (this->home_ == 0)
    {
      *os << "factory ";
    }
  else
    {
      AST_Component *c = this->home_->managed_component ();

      *os << IdentifierHelper::orig_sn (c->name ()).c_str ()
          << " ";
    }

  *os << IdentifierHelper::try_escape (id).c_str ()
      << " (";

  this->gen_params (node, node->argument_count ());

  *os << ")";

  this->gen_exception_list (node->exceptions ());

  *os << ";";

  return 0;
}

int
idl3_to_idl2_visitor::visit_finder (AST_Finder *node)
{
  return this->visit_factory (node);
}

int
idl3_to_idl2_visitor::visit_root (AST_Root *node)
{
  int status = be_global->outfile_init (this->os,
                                        "",
                                        "_IDL2.idl",
                                        "_TAO_IDL_",
                                        "_IDL_");

  if (status == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("idl3_to_idl2_visitor::visit_root - ")
                         ACE_TEXT ("failed to initialize output file\n")),
                        -1);
    }

  ACE_CString filename;

  for (size_t i = 0; i < idl_global->n_included_idl_files (); ++i)
    {
      if (i == 0)
        {
          *os << be_nl;
        }

      ACE_CString raw_filename = idl_global->included_idl_files ()[i];
      bool excluded_file_found =
        this->match_excluded_file (raw_filename.c_str ());

      if (raw_filename.find (".pidl") != ACE_CString::npos
          || raw_filename == "orb.idl"
          || raw_filename == "Components.idl"
          || excluded_file_found)
        {
          filename = raw_filename;
        }
      else
        {
          filename =
            raw_filename.substr (0, raw_filename.rfind ('.')) + "_IDL2.idl";
        }

      *os << be_nl
          << "#include \"" << filename.c_str () << "\"";
    }

  const char *pfix = node->prefix ();

  if (ACE_OS::strcmp (pfix, "") != 0)
    {
      *os << be_nl << be_nl
          << "#pragma prefix \"" << pfix << "\"";
    }

  if (this->visit_scope (node) != 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("idl3_to_idl2_visitor::visit_root - ")
                         ACE_TEXT ("codegen for scope failed\n")),
                        -1);
    }

  *os << be_nl << be_nl
      << "#endif /* ifndef */" << be_nl << be_nl;

  return 0;
}

UTL_ScopedName *
idl3_to_idl2_visitor::create_scoped_name (const char *prefix,
                                          const char *local_name,
                                          const char *suffix,
                                          AST_Decl *parent)
{
  ACE_CString local_string (prefix,
                            0,
                            false);
  local_string += local_name;
  local_string += suffix;
  Identifier *local_id = 0;
  ACE_NEW_RETURN (local_id,
                  Identifier (local_string.fast_rep ()),
                  0);
  UTL_ScopedName *last_segment = 0;
  ACE_NEW_RETURN (last_segment,
                  UTL_ScopedName (local_id,
                                  0),
                  0);
  UTL_ScopedName *full_name =
    static_cast<UTL_ScopedName *> (parent->name ()->copy ());
  full_name->nconc (last_segment);
  return full_name;
}

void
idl3_to_idl2_visitor::tranfer_scope_elements (AST_Home *src,
                                              AST_Interface &dst)
{
  // Transfer the elements of the home's scope to the temporary
  // explicit home interface.
  src->transfer_scope_elements (&dst);

  // Visit the transferred scope elements normally to generate the IDL.
  // This way referenced items will have the interface's name in the
  // scoped name instead of the home's name.
  for (UTL_ScopeActiveIterator dst_iter (&dst, UTL_Scope::IK_decls);
       ! dst_iter.is_done ();
       dst_iter.next ())
    {
      if (dst_iter.item ()->ast_accept (this) != 0)
        {
          ACE_ERROR ((LM_ERROR,
                      "idl3_to_idl2_visitor::tranfer_scope_elements - "
                      "codegen for destination scope failed\n"));
        }
    }
}

int
idl3_to_idl2_visitor::visit_porttype_scope (AST_PortType *node)
{
  return this->visit_scope (node);
}

int
idl3_to_idl2_visitor::visit_porttype_scope_mirror (
  AST_PortType *node)
{
  for (UTL_ScopeActiveIterator si (node, UTL_Scope::IK_decls);
       !si.is_done ();
       si.next ())
    {
      AST_Decl *d = si.item ();

      switch (d->node_type ())
        {
          case AST_Decl::NT_provides:
            {
              AST_Provides *p =
                AST_Provides::narrow_from_decl (d);

              AST_Uses mirror_node (p->name (),
                                    p->provides_type (),
                                    false);

              if (this->visit_uses (&mirror_node) == -1)
                {
                  ACE_ERROR_RETURN ((LM_ERROR,
                                     ACE_TEXT ("idl3_to_idl2_visitor")
                                     ACE_TEXT ("::visit_porttype_mirror - ")
                                     ACE_TEXT ("visit_uses() failed\n")),
                                    -1);
                }

              mirror_node.destroy ();
              break;
            }
          case AST_Decl::NT_uses:
            {
              AST_Uses *u =
                AST_Uses::narrow_from_decl (d);

              AST_Provides mirror_node (u->name (),
                                        u->uses_type ());

              if (this->visit_provides (&mirror_node) == -1)
                {
                  ACE_ERROR_RETURN ((LM_ERROR,
                                     ACE_TEXT ("idl3_to_idl2_visitor")
                                     ACE_TEXT ("::visit_porttype_mirror - ")
                                     ACE_TEXT ("visit_provides() failed\n")),
                                    -1);
                }

              mirror_node.destroy ();
              break;
            }
          default:
            {
              if (d->ast_accept (this) == -1)
                {
                  ACE_ERROR_RETURN ((LM_ERROR,
                                     ACE_TEXT ("idl3_to_idl2_visitor")
                                     ACE_TEXT ("::visit_porttype_mirror - ")
                                     ACE_TEXT ("codegen for mirror port failed\n")),
                                    -1);
                }

              break;
            }
        }
    }

  return 0;
}