summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_visitor_typecode/union_typecode.cpp
blob: 1b075993cc2d007d94336c088f17f1039ac974c9 (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
// -*- C++ -*-

//=============================================================================
/**
 *  @file  union_typecode.cpp
 *
 *  $Id$
 *
 *  Union TypeCode generation visitor.
 *
 *  @author  Ossama Othman <ossama@dre.vanderbilt.edu>
 */
//=============================================================================


#include <string>
#include <iostream>


TAO::be_visitor_union_typecode::be_visitor_union_typecode (
  be_visitor_context * ctx)
  : be_visitor_typecode_defn (ctx)
{
}

int
TAO::be_visitor_union_typecode::visit_union (be_union * node)
{
  TAO_OutStream & os = *this->ctx_->stream ();

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


  std::string const fields_name (std::string ("_tao_cases_")
                                 + node->flat_name ());


  be_type * const discriminant_type =
    be_type::narrow_from_decl (node->disc_type ());

  ACE_ASSERT (discriminant_type != 0);

  std::string const case_array_type (
    std::string ("TAO::TypeCode::Non_Default_Case<")
    + std::string (discriminant_type->full_name ())
    + std::string (", char const *>"));

  // Generate array containing union non-default case
  // characteristics.
  os << "static " << case_array_type.c_str () << " const "
     << fields_name.c_str ()
     << "[] =" << be_idt_nl
     << "{" << be_idt_nl;

  if (this->visit_cases (node) != 0)
    return -1;

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

  // Generate the TypeCode instantiation.
  os
    << "static TAO::TypeCode::Union<char const *," << be_nl
    << "                            " << case_array_type.c_str () << " const *," << be_nl
    << "                            TAO::Null_RefCount_Policy> const"
    << be_idt_nl
    << "_tao_tc_" << node->flat_name () << " (" << be_idt_nl
    << "\"" << node->repoID () << "\"," << be_nl
    << "\"" << node->original_local_name () << "\"," << be_nl
    << discriminant_type->tc_name () << "," << be_nl
    << "_tao_cases_" << node->flat_name () << "," << be_nl
    << node->nfields () << ","
    << node->default_index () << "," << be_nl
//     << default_member_name,
//     << default_member_type
    << ");" << be_uidt_nl
    << be_uidt_nl;

  return
    this->gen_typecode_ptr (be_type::narrow_from_decl (node));
}

int
TAO::be_visitor_union_typecode::visit_cases (be_union * node)
{
  AST_Field ** member_ptr = 0;

  size_t const count = node->nfields ();

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

  for (size_t i = 0; i < count; ++i)
    {
      node->field (member_ptr, i);

      be_union_branch * const branch =
        be_union_branch::narrow_from_decl (*member_ptr);

      ACE_ASSERT (branch != 0);

      if (branch->label ()->label_kind () == AST_UnionLabel::UL_label)
        {
          // Only deal with non-default cases.  The default case has
          // special handling in the union TypeCode implementation.

          os << "{ ";

          // Generate the label value.  Only the first label value is
          // used in the case where multiple labels are used for the
          // same union branch/case.
          branch->gen_label_value (&os, 0);

          os << ", ";
          
          be_type * const type =
            be_type::narrow_from_decl ((*member_ptr)->field_type ());

          os << "\"" << branch->original_local_name () << "\", "
             << "&"  << type->tc_name ()
             << " }";

          if (i < count - 1)
            os << ",";

          os << be_nl;
        }
    }

  return 0;
}