summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_visitor_array/serializer_op_cs.cpp
blob: 9fff70e75a43dc1d8d4baaea8183d8ad1bc9a0f0 (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
//
// $Id$
//

// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    serializer_op_cs.cpp
//
// = DESCRIPTION
//    Visitor for code generation of Arrays for the
//    TAO::DCPS::Serializer operators in the client stubs.
//
// = AUTHOR
//    Scott Harris <harris_s@ociweb.com> based on code by
//    Jeff Parsons <parsons@cs.wustl.edu>
//
// ============================================================================

ACE_RCSID (be_visitor_array,
           serializer_op_cs,
           "$Id$")

// ***************************************************************************
// Array visitor for generating Serializer operator declarations in the client
// stubs file
// ***************************************************************************

be_visitor_array_serializer_op_cs::be_visitor_array_serializer_op_cs (
    be_visitor_context *ctx
  )
  : be_visitor_decl (ctx)
{
}

be_visitor_array_serializer_op_cs::~be_visitor_array_serializer_op_cs (void)
{
}

int
be_visitor_array_serializer_op_cs::visit_array (be_array *node)
{
  if (this->ctx_->alias ())
    {
      // We are here because we are visiting base type
      // of the array node which is itself an
      // array, i.e., this is a case of array of array.
      return this->visit_node (node);
    }

  if (node->cli_stub_serializer_op_gen ()
      || node->imported ()
      || node->is_local ())
    {
      return 0;
    }

  // Retrieve the base type.
  be_type *bt = be_type::narrow_from_decl (node->base_type ());

  if (!bt)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_array - "
                         "Bad base type\n"),
                        -1);
    }
  
  AST_Decl::NodeType nt = bt->node_type ();

  // If we contain an anonymous sequence,
  // generate code for the sequence here.

  if (nt == AST_Decl::NT_sequence)
    {
      if (this->gen_anonymous_base_type (bt,
                                         TAO_CodeGen::TAO_ROOT_SERIALIZER_OP_CS)
          == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_array_serializer_op_cs::"
                             "visit_array - "
                             "gen_anonymous_base_type failed\n"),
                            -1);
        }
    }

  // If the array is an anonymous member and if its element type
  // is a declaration (not a reference), we must generate code for
  // the declaration.
  if (this->ctx_->alias () == 0 // Not a typedef.
      && bt->is_child (this->ctx_->scope ()))
    {
      int status = 0;
      be_visitor_context ctx (*this->ctx_);

      switch (nt)
      {
        case AST_Decl::NT_enum:
          {
            be_visitor_enum_serializer_op_cs ec_visitor (&ctx);
            status = bt->accept (&ec_visitor);
            break;
          }
        case AST_Decl::NT_struct:
          {
            be_visitor_structure_serializer_op_cs sc_visitor (&ctx);
            status = bt->accept (&sc_visitor);
            break;
          }
        case AST_Decl::NT_union:
          {
            be_visitor_union_serializer_op_cs uc_visitor (&ctx);
            status = bt->accept (&uc_visitor);
            break;
          }
        default:
          break;
      }

      if (status == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_array_ch::"
                             "visit_array - "
                             "array base type codegen failed\n"),
                            -1);
        }
    }

  // For anonymous arrays, the type name has a _ prepended. We compute the
  // full_name with or without the underscore and use it later on.
  char fname [NAMEBUFSIZE];  // to hold the full and

  // Save the node's local name and full name in a buffer for quick use later
  // on.
  ACE_OS::memset (fname,
                  '\0',
                  NAMEBUFSIZE);

  if (this->ctx_->tdef ())
    {
      ACE_OS::sprintf (fname, "%s", node->full_name ());
    }
  else
    {
      // For anonymous arrays ...
      // We have to generate a name for us that has an underscope prepended
      // to our local name. This needs to be inserted after the parents's
      // name.

      if (node->is_nested ())
        {
          be_decl *parent =
            be_scope::narrow_from_scope (node->defined_in ())->decl ();
          ACE_OS::sprintf (fname,
                           "%s::_%s",
                           parent->full_name (),
                           node->local_name ()->get_string ());
        }
      else
        {
          ACE_OS::sprintf (fname,
                           "_%s",
                           node->full_name ());
        }
    }

  // Generate the Serializer << and >> operator defns.

  TAO_OutStream *os = this->ctx_->stream ();
  this->ctx_->node (node);

  *os << be_nl << be_nl << "// TAO_IDL - Generated from" << be_nl
      << "// " << __FILE__ << ":" << __LINE__ << be_nl << be_nl;

  //  Set the sub state as generating code for the size method
  this->ctx_->sub_state (TAO_CodeGen::TAO_MAX_MARSHALED_SIZE);
  *os << "size_t _dcps_max_marshaled_size (" << be_idt << be_idt_nl
      << "const " << fname << "_forany &_tao_array" << be_uidt_nl
      <<")" << be_uidt_nl
      << "{" << be_idt_nl;

  if (bt->accept (this) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "be_visitor_array_serializer_op_cs::"
                         "visit_array - "
                         "Base type codegen failed\n"),
                        -1);
    }

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

  //  Set the sub state as generating code for the size method
  this->ctx_->sub_state (TAO_CodeGen::TAO_IS_BOUNDED_SIZE);
  *os << "::CORBA::Boolean _tao_is_bounded_size (" << be_idt << be_idt_nl
      << "const " << fname << "_forany &_tao_array" << be_uidt_nl
      <<")" << be_uidt_nl
      << "{" << be_idt_nl;

  if (bt->accept (this) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "be_visitor_array_serializer_op_cs::"
                         "visit_array - "
                         "Base type codegen failed\n"),
                        -1);
    }

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

  //  Set the sub state as generating code for the size method
  this->ctx_->sub_state (TAO_CodeGen::TAO_FIND_SIZE);
  *os << "size_t _dcps_find_size (" << be_idt << be_idt_nl
      << "const " << fname << "_forany &_tao_array" << be_uidt_nl
      <<")" << be_uidt_nl
      << "{" << be_idt_nl;

  if (bt->accept (this) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "be_visitor_array_serializer_op_cs::"
                         "visit_array - "
                         "Base type codegen failed\n"),
                        -1);
    }

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


  //  Set the sub state as generating code for the output operator.
  this->ctx_->sub_state (TAO_CodeGen::TAO_CDR_OUTPUT);
  *os << "::CORBA::Boolean operator<< (" << be_idt << be_idt_nl
      << "TAO::DCPS::Serializer &strm," << be_nl
      << "const " << fname << "_forany &_tao_array" << be_uidt_nl
      << ")" << be_uidt_nl
      << "{" << be_idt_nl;

  if (bt->accept (this) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "be_visitor_array_serializer_op_cs::"
                         "visit_array - "
                         "Base type codegen failed\n"),
                        -1);
    }

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

  this->ctx_->sub_state (TAO_CodeGen::TAO_CDR_INPUT);
  *os << "::CORBA::Boolean operator>> (" << be_idt << be_idt_nl
      << "TAO::DCPS::Serializer &strm," << be_nl
      << fname << "_forany &_tao_array" << be_uidt_nl
      << ")" << be_uidt_nl
      << "{" << be_idt_nl;

  if (bt->accept (this) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "be_visitor_array_serializer_op_cs::"
                         "visit_array - "
                         "Base type codegen failed\n"),
                        -1);
    }

  *os << "}";

  node->cli_stub_serializer_op_gen (1);
  return 0;
}

// Handle all the base types.

int
be_visitor_array_serializer_op_cs::visit_enum (be_enum *node)
{
  TAO_OutStream *os = this->ctx_->stream ();

  unsigned long i;

  // Grab the array node.
  be_array *array = this->ctx_->be_node_as_array ();

  if (!node)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_predefined_type - "
                         "bad array node\n"),
                        -1);
    }

  // Check what is the code generation substate. Are we generating
  // code for the in/out operators for our parent or for us?
  switch (this->ctx_->sub_state ())
    {
    case TAO_CodeGen::TAO_IS_BOUNDED_SIZE:
      *os << "ACE_UNUSED_ARG (_tao_array);" << be_nl
          << "return true; // array of enum" << be_uidt_nl;
      break;
    case TAO_CodeGen::TAO_FIND_SIZE:
    case TAO_CodeGen::TAO_MAX_MARSHALED_SIZE:
      {
        *os << "ACE_UNUSED_ARG (_tao_array);" << be_nl;
        *os << "// array of enum = sizeof ( ::CORBA::ULong) * array size" << be_nl
            << "return _dcps_max_marshaled_size_ulong () * ";

        unsigned long ndims = array->n_dims ();

        // Generate a product of all the dimensions. This will be the total length
        // of the "unfolded" single dimensional array.
        for (i = 0; i < ndims; ++i)
          {
            // Retrieve the ith dimension value.
            AST_Expression *expr = array->dims ()[i];

            if ((expr == 0) || ((expr != 0) && (expr->ev () == 0)))
              {
                ACE_ERROR_RETURN ((LM_ERROR,
                                  "(%N:%l) be_visitor_array_serializer_op_cs::"
                                  "visit_predefined_type - "
                                  "bad array dimension\n"),
                                  -1);
              }

            if (i != 0)
              {
                // Do not generate the multiplication operator the first time in.
                *os << "*";
              }

            if (expr->ev ()->et == AST_Expression::EV_ulong)
              {
                // Generate a loop for each dimension.
                *os << expr->ev ()->u.ulval;
              }
            else
              {
                ACE_ERROR_RETURN ((LM_ERROR,
                                  "(%N:%l) be_visitor_array_serializer_op_cs::"
                                  "visit_predefined_type - "
                                  "bad array dimension value\n"),
                                  -1);
              }
          }

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

    default:
      return this->visit_node (node);
    }
  return 0;
}

int
be_visitor_array_serializer_op_cs::visit_interface (be_interface *node)
{
  return this->visit_node (node);
}

int
be_visitor_array_serializer_op_cs::visit_interface_fwd (be_interface_fwd *node)
{
  return this->visit_node (node);
}

int
be_visitor_array_serializer_op_cs::visit_valuetype (be_valuetype *node)
{
  return this->visit_node (node);
}

int
be_visitor_array_serializer_op_cs::visit_valuetype_fwd (be_valuetype_fwd *node)
{
  return this->visit_node (node);
}

int
be_visitor_array_serializer_op_cs::visit_predefined_type (
    be_predefined_type *node
  )
{
  TAO_OutStream *os = this->ctx_->stream ();

  switch (node->pt ())
    {
    case AST_PredefinedType::PT_pseudo:
    case AST_PredefinedType::PT_object:
    case AST_PredefinedType::PT_value:
    case AST_PredefinedType::PT_abstract:
    case AST_PredefinedType::PT_any:
      // Let the helper handle this.
      return this->visit_node (node);
    case AST_PredefinedType::PT_void:
      // error
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_predefined_type - "
                         "Bad primitive type\n"),
                        -1);
    default:
      // All other primitive types. Handle them as shown below.
      break;
    };

  // We get here if the "type" of individual elements of the array is a
  // primitive type. In this case, we treat the array as a single dimensional
  // array (even though it was multi-dimensional), and pass the total length
  // of the array as a cross product of the dimensions.

  unsigned long i;

  // Grab the array node.
  be_array *array = this->ctx_->be_node_as_array ();

  if (!node)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_predefined_type - "
                         "bad array node\n"),
                        -1);
    }

  // Check what is the code generation substate. Are we generating
  // code for the in/out operators for our parent or for us?
  switch (this->ctx_->sub_state ())
    {
    case TAO_CodeGen::TAO_IS_BOUNDED_SIZE:
      // all predefined types are fixed size hence they are bounded.
      *os << "ACE_UNUSED_ARG(_tao_array);" << be_nl;
      *os << "return true;" << be_uidt_nl;
      return 0; // all done - no need to get the array size
      break;

    // all predefined types are fixed size so
    //      _dcps_find_size() = _dcps_max_marshaled_size
    case TAO_CodeGen::TAO_FIND_SIZE:
    case TAO_CodeGen::TAO_MAX_MARSHALED_SIZE:
      switch (node->pt ())
        {
        case AST_PredefinedType::PT_octet:
          *os << "ACE_UNUSED_ARG(_tao_array);" << be_nl;
          *os << "return (_dcps_max_marshaled_size_octet () *";
          break;
        case AST_PredefinedType::PT_char:
          *os << "ACE_UNUSED_ARG(_tao_array);" << be_nl;
          *os << "return (_dcps_max_marshaled_size_char () *";
          break;
        case AST_PredefinedType::PT_wchar:
          *os << "ACE_UNUSED_ARG(_tao_array);" << be_nl;
          *os << "return (_dcps_max_marshaled_size_wchar () *";
          break;
        case AST_PredefinedType::PT_long:
        case AST_PredefinedType::PT_ulong:
        case AST_PredefinedType::PT_short:
        case AST_PredefinedType::PT_ushort:
        case AST_PredefinedType::PT_float:
        case AST_PredefinedType::PT_double:
        case AST_PredefinedType::PT_longlong:
        case AST_PredefinedType::PT_ulonglong:
        case AST_PredefinedType::PT_longdouble:
          // array size = slice type size * length
          // note: *(_tao_array.in ()) is the array's slice; hence base type
          *os << "return (_dcps_max_marshaled_size (*(_tao_array.in ())) * ";
          break;
          break;
        case AST_PredefinedType::PT_boolean:
          *os << "ACE_UNUSED_ARG(_tao_array);" << be_nl;
          *os << "return (_dcps_max_marshaled_size_boolean () *";
          break;
        default:
          ACE_ERROR_RETURN ((LM_ERROR,
                            "(%N:%l) be_visitor_array_serializer_op_cs::"
                            "visit_predefined_type - "
                            "bad primitive type for _dcps_max_marshaled_size code gen\n"),
                            -1);
        }
      break;

        case TAO_CodeGen::TAO_CDR_INPUT:
        case TAO_CodeGen::TAO_CDR_OUTPUT:
          {
  // We generate optimized code based on an optimized interface available from
  // the Serializer class. These optimizations are applicable only to primitive
  // types.
  *os << " return strm.";

  // Based on our substate, we may be reading from a stream or writing into a
  // stream.
  switch (this->ctx_->sub_state ())
    {
    case TAO_CodeGen::TAO_CDR_INPUT:
      *os << "read_";
      break;
    case TAO_CodeGen::TAO_CDR_OUTPUT:
      *os << "write_";
      break;
    default:
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_predefined_type - "
                         "bad sub state\n"),
                        -1);
    }

  // Determine what kind of array are we reading/writing.
  switch (node->pt ())
    {
    case AST_PredefinedType::PT_long:
      *os << "long_array";
      break;
    case AST_PredefinedType::PT_ulong:
      *os << "ulong_array";
      break;
    case AST_PredefinedType::PT_short:
      *os << "short_array";
      break;
    case AST_PredefinedType::PT_ushort:
      *os << "ushort_array";
      break;
    case AST_PredefinedType::PT_octet:
      *os << "octet_array";
      break;
    case AST_PredefinedType::PT_char:
      *os << "char_array";
      break;
    case AST_PredefinedType::PT_wchar:
      *os << "wchar_array";
      break;
    case AST_PredefinedType::PT_float:
      *os << "float_array";
      break;
    case AST_PredefinedType::PT_double:
      *os << "double_array";
      break;
    case AST_PredefinedType::PT_longlong:
      *os << "longlong_array";
      break;
    case AST_PredefinedType::PT_ulonglong:
      *os << "ulonglong_array";
      break;
    case AST_PredefinedType::PT_longdouble:
      *os << "longdouble_array";
      break;
    case AST_PredefinedType::PT_boolean:
      *os << "boolean_array";
      break;
    default:
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_predefined_type - "
                         "bad primitive type for optimized code gen\n"),
                        -1);
    }

  // Handle special case to avoid compiler errors.
  switch (this->ctx_->sub_state ())
    {
    case TAO_CodeGen::TAO_CDR_INPUT:
      *os << " ((";
      break;
    case TAO_CodeGen::TAO_CDR_OUTPUT:
      *os << " ((const ";
      break;
    default:
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_predefined_type - "
                         "bad substate in context\n"),
                        -1);
    }

  switch (node->pt ())
    {
    case AST_PredefinedType::PT_long:
      *os << "ACE_CDR::Long *)";
      break;
    case AST_PredefinedType::PT_ulong:
      *os << "ACE_CDR::ULong *)";
      break;
    case AST_PredefinedType::PT_short:
      *os << "ACE_CDR::Short *)";
      break;
    case AST_PredefinedType::PT_ushort:
      *os << "ACE_CDR::UShort *)";
      break;
    case AST_PredefinedType::PT_octet:
      *os << "ACE_CDR::Octet *)";
      break;
    case AST_PredefinedType::PT_char:
      *os << "ACE_CDR::Char *)";
      break;
    case AST_PredefinedType::PT_wchar:
      *os << "ACE_CDR::WChar *)";
      break;
    case AST_PredefinedType::PT_float:
      *os << "ACE_CDR::Float *)";
      break;
    case AST_PredefinedType::PT_double:
      *os << "ACE_CDR::Double *)";
      break;
    case AST_PredefinedType::PT_longlong:
      *os << "ACE_CDR::LongLong *)";
      break;
    case AST_PredefinedType::PT_ulonglong:
      *os << "ACE_CDR::ULongLong *)";
      break;
    case AST_PredefinedType::PT_longdouble:
      *os << "ACE_CDR::LongDouble *)";
      break;
    case AST_PredefinedType::PT_boolean:
      *os << "ACE_CDR::Boolean *)";
      break;
    default:
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_predefined_type - "
                         "bad primitive type for optimized code gen\n"),
                        -1);
    }

  // Handle special case to avoid compiler errors.
  switch (this->ctx_->sub_state ())
    {
    case TAO_CodeGen::TAO_CDR_INPUT:
      *os << " _tao_array.out (), ";
      break;
    case TAO_CodeGen::TAO_CDR_OUTPUT:
      *os << "_tao_array.in (), ";
      break;
    default:
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_predefined_type - "
                         "bad substate in context\n"),
                        -1);
        }
    } // end TAO_CDR_INPUT/OUTPUT specific code.
    break;
    case TAO_CodeGen::TAO_CDR_SCOPE:
      // This is done in serializer_op_cs and hacked into *.i.
      break;
    default:
      // Error.
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_predefined_type - "
                         "bad sub state\n"),
                        -1);
    }

  unsigned long ndims = array->n_dims ();

  // Generate a product of all the dimensions. This will be the total length
  // of the "unfolded" single dimensional array.
  for (i = 0; i < ndims; ++i)
    {
      // Retrieve the ith dimension value.
      AST_Expression *expr = array->dims ()[i];

      if ((expr == 0) || ((expr != 0) && (expr->ev () == 0)))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_array_serializer_op_cs::"
                             "visit_predefined_type - "
                             "bad array dimension\n"),
                            -1);
        }

      if (i != 0)
        {
          // Do not generate the multiplication operator the first time in.
          *os << "*";
        }

      if (expr->ev ()->et == AST_Expression::EV_ulong)
        {
          // Generate a loop for each dimension.
          *os << expr->ev ()->u.ulval;
        }
      else
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_array_serializer_op_cs::"
                             "visit_predefined_type - "
                             "bad array dimension value\n"),
                            -1);
        }
    }

  *os << ");" << be_uidt_nl;

  return 0;
}

int
be_visitor_array_serializer_op_cs::visit_sequence (be_sequence *node)
{
  return this->visit_node (node);
}

int
be_visitor_array_serializer_op_cs::visit_string (be_string *node)
{
  return this->visit_node (node);
}

int
be_visitor_array_serializer_op_cs::visit_structure (be_structure *node)
{
  return this->visit_node (node);
}

int
be_visitor_array_serializer_op_cs::visit_union (be_union *node)
{
  return this->visit_node (node);
}

int
be_visitor_array_serializer_op_cs::visit_typedef (be_typedef *node)
{
  this->ctx_->alias (node);

  // The node to be visited in the base primitve type that gets typedefed.
  be_type *bt = node->primitive_base_type ();

  if (!bt || (bt->accept (this) == -1))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_typedef - "
                         "Bad primitive type\n"),
                        -1);
    }

  this->ctx_->alias (0);
  return 0;
}

// Helper.
int
be_visitor_array_serializer_op_cs::visit_node (be_type *bt)
{
  TAO_OutStream *os = this->ctx_->stream ();
  unsigned long i;
  be_array *node = this->ctx_->be_node_as_array ();
  AST_Decl::NodeType nt = bt->node_type ();

  if (!node)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_node - "
                         "bad array node\n"),
                        -1);
    }

  unsigned long ndims = node->n_dims ();

  AST_Decl::NodeType bnt = bt->node_type ();

  switch (this->ctx_->sub_state ())
      {
      case TAO_CodeGen::TAO_IS_BOUNDED_SIZE:
        switch (bnt)
          {
          case AST_Decl::NT_string:
          case AST_Decl::NT_wstring:
            {
            be_string *str =
              be_string::narrow_from_decl (bt);

            if (!str)
              {
                ACE_ERROR_RETURN ((LM_ERROR,
                                   "(%N:%l) be_visitor_array_serializer_op_cs::"
                                   "visit_node - "
                                   "bad string node\n"),
                                  -1);
              }

            *os << "ACE_UNUSED_ARG(_tao_array);" << be_nl;
            if (0 == str->max_size ()->ev ()->u.ulval)
              *os << "return false; // array of unbounded string";
            else
              *os << "return true; //  array of bounded string ";
            }
            break;
          case AST_Decl::NT_array:
              *os << "// use ULong instead of literal to avoid overload ambiguity"
                  << be_nl << "::CORBA::ULong first = 0;" << be_nl
                  << "// must use forany instead of a slice to avoid overload ambiguity"
                  << be_nl
                  << bt->name () << "_var tmp_var ("
                  << bt->name () << "_dup (_tao_array";

              for (i = 0; i < ndims; ++i)
                {
                  *os << "[first]";
                }

              *os << "));" << be_nl;
              *os << bt->name () << "_forany tmp (tmp_var.inout ());" << be_nl;
              *os << "return _tao_is_bounded_size (tmp);";
            break;
          default:
            *os << "return _tao_is_bounded_size (*(_tao_array.in ()));";
            break;
          }
          *os << be_uidt_nl;
          break;
      case TAO_CodeGen::TAO_MAX_MARSHALED_SIZE:
        {
        switch (bnt)
          {
          case AST_Decl::NT_string:
          case AST_Decl::NT_wstring:
            {
            be_string *str =
              be_string::narrow_from_decl (bt);

            if (!str)
              {
                ACE_ERROR_RETURN ((LM_ERROR,
                                   "(%N:%l) be_visitor_array_serializer_op_cs::"
                                   "visit_node - "
                                   "bad string node\n"),
                                  -1);
              }
            *os << "ACE_UNUSED_ARG(_tao_array);" << be_nl;
            char buff[15];
            ACE_OS::sprintf(buff, "%ld", str->max_size ()->ev ()->u.ulval);
            *os << "return _dcps_max_marshaled_size_ulong () + (" << buff;
            }
            break;
          case AST_Decl::NT_array:
              *os << "// use ULong instead of literal to avoid overload ambiguity"
                  << be_nl << "::CORBA::ULong first = 0;" << be_nl
                  << "// must use forany instead of a slice to avoid overload ambiguity"
                  << be_nl
                  << bt->name () << "_var tmp_var ("
                  << bt->name () << "_dup (_tao_array";

              for (i = 0; i < ndims; ++i)
                {
                  *os << "[first]";
                }

              *os << "));" << be_nl;
              *os << bt->name () << "_forany tmp (tmp_var.inout ());" << be_nl;
              *os << "return (_dcps_max_marshaled_size (tmp)";
            break;
          default:
            // array size = slice type size * length
            // note: *(_tao_array.in ()) is the array's slice; hence base type
            *os << "return (_dcps_max_marshaled_size (*(_tao_array.in ()))";
            break;
          }
        *os << " * ";

        //SHH - TBD - this array dim size code could be common
        // Generate a product of all the dimensions. This will be the total length
        // of the "unfolded" single dimensional array.
        for (i = 0; i < ndims; ++i)
          {
            // Retrieve the ith dimension value.
            AST_Expression *expr = node->dims ()[i];

            if ((expr == 0) || ((expr != 0) && (expr->ev () == 0)))
              {
                ACE_ERROR_RETURN ((LM_ERROR,
                                  "(%N:%l) be_visitor_array_serializer_op_cs::"
                                  "visit_node - "
                                  "bad array dimension\n"),
                                  -1);
              }

            if (i != 0)
              {
                // Do not generate the multiplication operator the first time in.
                *os << "*";
              }

            if (expr->ev ()->et == AST_Expression::EV_ulong)
              {
                // Generate a loop for each dimension.
                *os << expr->ev ()->u.ulval;
              }
            else
              {
                ACE_ERROR_RETURN ((LM_ERROR,
                                  "(%N:%l) be_visitor_array_serializer_op_cs::"
                                  "visit_node - "
                                  "bad array dimension value\n"),
                                  -1);
              }
          }

        *os << ");" << be_uidt_nl;

      }
        break;
      case TAO_CodeGen::TAO_FIND_SIZE:
      case TAO_CodeGen::TAO_CDR_INPUT:
      case TAO_CodeGen::TAO_CDR_OUTPUT:
        {
          if (TAO_CodeGen::TAO_FIND_SIZE == this->ctx_->sub_state ())
            {
            *os << "//iterate over the array to find the current size" << be_nl
                << "size_t the_length = 0;" << be_nl;
            }
          else
            {
              // Initialize a boolean variable.
              *os << "::CORBA::Boolean _tao_marshal_flag = true;" << be_nl;
            }

    unsigned long ndims = node->n_dims ();

    // We get here if the "type" of individual elements of the array is not a
    // primitive type. In this case, we are left with no other alternative but to
    // encode/decode element by element.

    // generate nested loops for as many dimensions as there are
    for (i = 0; i < ndims; ++i)
      {
        // Retrieve the ith dimension value.
        AST_Expression *expr = node->dims ()[i];

        if ((expr == 0) || ((expr != 0) && (expr->ev () == 0)))
          {
            ACE_ERROR_RETURN ((LM_ERROR,
                              "(%N:%l) be_visitor_array_serializer_op_cs::"
                              "visit_node - "
                              "bad array dimension\n"),
                              -1);
          }

        if (expr->ev ()->et == AST_Expression::EV_ulong)
          {
            // Generate a loop for each dimension.
            *os << be_nl << "for ( ::CORBA::ULong i" << i
                << " = 0; i" << i << " < "
                << expr->ev ()->u.ulval;

            if (TAO_CodeGen::TAO_FIND_SIZE != this->ctx_->sub_state ())
                *os << " && _tao_marshal_flag";

            *os << "; i" << i
                << "++)" << be_idt_nl
                << "{" << be_idt;
          }
        else
          {
            ACE_ERROR_RETURN ((LM_ERROR,
                              "(%N:%l) be_visitor_array_serializer_op_cs::"
                              "visit_node - "
                              "bad array dimension value\n"),
                              -1);
          }
      }

    // @@ (JP) Need to factor out some of this into method call(s).
    switch (this->ctx_->sub_state ())
      {
      case TAO_CodeGen::TAO_FIND_SIZE:
        {
          *os << be_nl;
          if (AST_Decl::NT_array == bnt)
            {
              *os << bt->name () << "_var tmp_var ("
                  << bt->name () << "_dup (_tao_array";

              for (i = 0; i < ndims; ++i)
                {
                  *os << "[i" << i << "]";
                }

              *os << "));" << be_nl;
              *os << bt->name () << "_forany tmp (tmp_var.inout ());" << be_nl;
              *os << "the_length += _dcps_find_size (tmp);";
            }
          else
            {
              if (AST_Decl::NT_string == bnt ||
                  AST_Decl::NT_wstring == bnt )
                *os << "the_length += _dcps_max_marshaled_size_ulong() + "
                    << "ACE_OS::strlen(_tao_array";
              else
                *os << "the_length += _dcps_find_size (_tao_array";

              for (i = 0; i < ndims; ++i)
                {
                  *os << "[i" << i << "]";
                }
              if (AST_Decl::NT_string == bnt ||
                  AST_Decl::NT_wstring == bnt )
                *os << ".in()";

              *os << ");";
            }
        }
        break;
      case TAO_CodeGen::TAO_CDR_INPUT:
        *os << be_nl;

        // Handle the array of array case, where we need to pass the
        // forany type.
        if (nt == AST_Decl::NT_array)
          {
            *os << bt->name () << "_forany tmp ("
                << bt->name () << "_alloc ());" << be_nl;
            *os << "_tao_marshal_flag = (strm >> tmp);" << be_nl;
            *os << bt->name () << "_copy (_tao_array";

            for (i = 0; i < ndims; ++i)
              {
                *os << "[i" << i << "]";
              }

            *os << ", tmp.in ());" << be_nl;
            *os << bt->name () << "_free (tmp.inout ());";
          }
        else
          {
            *os << "_tao_marshal_flag = (strm >> ";
            *os << "_tao_array ";

            for (i = 0; i < ndims; ++i)
              {
                *os << "[i" << i << "]";
              }

            switch (bt->node_type ())
              {
                // The following have a _var type and must be
                // handled in a special way.
                case AST_Decl::NT_string:
                case AST_Decl::NT_wstring:
                case AST_Decl::NT_valuetype:
                case AST_Decl::NT_eventtype:
                case AST_Decl::NT_eventtype_fwd:
                case AST_Decl::NT_valuetype_fwd:
                case AST_Decl::NT_valuebox:
                  *os << ".out ()";
                  break;
                case AST_Decl::NT_pre_defined:
                  {
                    // We need to separately handle this case of pseudo
                    // objects because they have a _var type.
                    be_predefined_type *pt =
                      be_predefined_type::narrow_from_decl (bt);

                    if (!pt)
                      {
                        ACE_ERROR_RETURN ((LM_ERROR,
                                          "(%N:%l) be_visitor_array_serializer_op_cs::"
                                          "visit_node - "
                                          "bad predefined type node\n"),
                                          -1);
                      }

                    AST_PredefinedType::PredefinedType pdt = pt->pt ();

                    if (pdt == AST_PredefinedType::PT_pseudo
                        || pdt == AST_PredefinedType::PT_object)
                      {
                        *os << ".out ()";
                      }
                  }
                default:
                  break;
              }

            *os << ");";
          }

        break;
      case TAO_CodeGen::TAO_CDR_OUTPUT:
        *os << be_nl;

        // Handle the array of array case, where we need to pass the
        // forany type.
        if (nt == AST_Decl::NT_array)
          {
            *os << bt->name () << "_var tmp_var ("
                << bt->name () << "_dup (_tao_array";

            for (i = 0; i < ndims; ++i)
              {
                *os << "[i" << i << "]";
              }

            *os << "));" << be_nl;
            *os << bt->name () << "_forany tmp (tmp_var.inout ());" << be_nl;
            *os << "_tao_marshal_flag = (strm << tmp);";
          }
        else if (nt == AST_Decl::NT_interface
                || nt == AST_Decl::NT_interface_fwd)
          {
            *os << "_tao_marshal_flag = " << be_idt_nl;

            if (bt->is_defined ())
              {
                *os << "_tao_array";

                for (i = 0; i < ndims; ++i)
                  {
                    *os << "[i" << i << "]";
                  }

                *os << ".in ()->marshal (strm);" << be_uidt;
              }
            else
              {

                AST_Decl *parent =
                  ScopeAsDecl (bt->defined_in ());

                if (parent != 0 && parent->node_type () != AST_Decl::NT_root)
                  {
                    *os << parent->name () << "::";
                  }

                *os << "TAO::Objref_Traits<" << bt->name () << ">::"
                    << "tao_marshal (_tao_array";

                for (i = 0; i < ndims; ++i)
                  {
                    *os << "[i" << i << "]";
                  }

                *os << ".in (), strm);" << be_uidt;
              }
          }
        else
          {
            *os << "_tao_marshal_flag = (strm << ";
            *os << "_tao_array ";

            for (i = 0; i < ndims; ++i)
              {
                *os << "[i" << i << "]";
              }

            switch (bt->node_type ())
              {
                // The following have a _var type and must be
                // handled in a special way.
                case AST_Decl::NT_string:
                case AST_Decl::NT_wstring:
                case AST_Decl::NT_valuetype:
                case AST_Decl::NT_eventtype:
                case AST_Decl::NT_eventtype_fwd:
                case AST_Decl::NT_valuetype_fwd:
                case AST_Decl::NT_valuebox:
                  *os << ".in ()";
                  break;
                case AST_Decl::NT_pre_defined:
                  {
                    // We need to separately handle this case of pseudo
                    // objects because they have a _var type.
                    be_predefined_type *pt =
                      be_predefined_type::narrow_from_decl (bt);

                    if (!pt)
                      {
                        ACE_ERROR_RETURN ((LM_ERROR,
                                          "(%N:%l) be_visitor_array_serializer_op_cs::"
                                          "visit_node - "
                                          "bad predefined type node\n"),
                                          -1);
                      }

                    AST_PredefinedType::PredefinedType pdt = pt->pt ();

                    if (pdt == AST_PredefinedType::PT_pseudo
                        || pdt == AST_PredefinedType::PT_object)
                      {
                        *os << ".in ()";
                      }
                  }
                default:
                  break;
              }

            *os << ");";
          }

        break;
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                          "(%N:%l) be_visitor_array_serializer_op_cs::"
                          "visit_node - "
                          "bad sub state\n"),
                          -1);
      }

    for (i = 0; i < ndims; ++i)
      {
        // Decrement indentation as many times as the number of dimensions.
        *os << be_uidt_nl << "}" << be_uidt;
      }

    if (TAO_CodeGen::TAO_FIND_SIZE == this->ctx_->sub_state ())
      {
      *os << be_nl << be_nl << "return the_length;" << be_uidt_nl;
      }
    else
      {
        *os << be_nl << be_nl << "return _tao_marshal_flag;" << be_uidt_nl;
      }

    } // end INPUT/OUTPUT_CDR case
    break;
    default:
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_array_serializer_op_cs::"
                         "visit_node outter- "
                         "bad sub state\n"),
                        -1);
  } // end outer switch

  return 0;
}