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

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


TAO::be_visitor_union_typecode::be_visitor_union_typecode (
  be_visitor_context * ctx)
  : be_visitor_typecode_defn (ctx)
  , is_recursive_ (false)
  , is_nested_ (false)
{
}

int
TAO::be_visitor_union_typecode::visit_union (be_union * node)
{
  if (!node->is_defined ())
    {
      return this->gen_forward_declared_typecode (node);
    }

  // Check if we are repeated.
  be_visitor_typecode_defn::QNode const * const qnode =
    this->queue_lookup (this->tc_queue_, node);

  ACE_Unbounded_Queue<AST_Type *> recursion_queue;

  if (qnode
      && node->in_recursion (recursion_queue))
    {
      this->is_recursive_ = true;

      return 0;
    }
  else if (this->queue_insert (this->tc_queue_, node, 0) == 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_union_typecode::"
                         "visit_type - "
                         "queue insert failed\n"),
                        -1);
    }

  if (this->recursion_detect_ || this->is_nested_)
    {
      return 0;
    }

  this->is_nested_ = true;

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

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

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

  ACE_ASSERT (discriminant_type != 0);

  if (this->gen_case_typecodes (node) != 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "TAO::be_visitor_union_typecode::visit_union - "
                         "Unable to generate union field "
                         "TypeCodes.\n"),
                        -1);
    }

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

  static char const StringType[]      = "char const *";
  static char const TypeCodeType[]    = "::CORBA::TypeCode_ptr const *";
  static char const MemberArrayType[] =
    "TAO::TypeCode::Case<char const *, "
    "::CORBA::TypeCode_ptr const *> const * const *";

  // Generate the TypeCode instantiation.
  os << "static ";

  if (this->is_recursive_)
    {
      os << "TAO::TypeCode::Recursive_Type<" << be_idt_nl;
    }

  // -- TypeCodeBase --
  os << "TAO::TypeCode::Union<" << StringType << "," << be_nl
     << "                            " << TypeCodeType << "," << be_nl
     << "                            " << MemberArrayType << "," << be_nl
     << "                            TAO::Null_RefCount_Policy>";

  if (this->is_recursive_)
    {
      os << "," << be_nl
         << TypeCodeType << "," << be_nl
         << MemberArrayType << " >" << be_uidt_nl;
    }

  os << 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_uidt_nl
     << be_uidt_nl;

  if (this->gen_typecode_ptr (be_type::narrow_from_decl (node)) != 0)
    {
      return -1;
    }

  return 0;
}

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

  size_t const count = node->nfields ();

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

      be_type * const member_type =
        be_type::narrow_from_decl ((*member_ptr)->field_type ());

      if (this->is_typecode_generation_required (member_type)
          && member_type->accept (this) != 0)
        {
          return -1;
        }

      be_visitor_typecode_defn::QNode const * const qnode =
        this->queue_lookup (this->tc_queue_, node);

      ACE_Unbounded_Queue<AST_Type *> recursion_queue;
      if (qnode
        && member_type->in_recursion (recursion_queue))
        {
          this->is_recursive_ = true;
        }
    }

  return 0;
}

int
TAO::be_visitor_union_typecode::visit_cases (be_union * node)
{
  // Generate array containing union case/branch characterstics.

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

  ACE_CString tao_cases ("_tao_cases_");
  ACE_CString const fields_name (tao_cases
                                 + node->flat_name ());
  const char *fields_name_str = fields_name.c_str ();

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

  // Start with static instances of each array element-to-be.
  ACE_CDR::ULong const count = node->nfields ();

  for (ACE_CDR::ULong i = 0; i < count; ++i)
    {
      AST_Field ** member_ptr = 0;

      node->field (member_ptr, i);

      be_type * const type =
        be_type::narrow_from_decl ((*member_ptr)->field_type ());

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

      ACE_ASSERT (branch != 0);

      os << "static TAO::TypeCode::Case_T<"
         << discriminant_type->full_name () << ", "
         << "char const *, ::CORBA::TypeCode_ptr const *> const "
         << fields_name_str << "__" << i <<" (";

      if (branch->label ()->label_kind () == AST_UnionLabel::UL_label)
        {
          // Non-default case.

          // 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);
        }
      else
        {
          // Default case.

          ACE_ASSERT (branch->label ()->label_kind ()
                      == AST_UnionLabel::UL_default);

          branch->gen_default_label_value (&os, node);
        }

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

  // Now generate the TAO::TypeCode::Case array.
  os << be_nl
     << "static TAO::TypeCode::Case<char const *, "
     << "::CORBA::TypeCode_ptr const *> const * const "
     << fields_name_str
     << "[] =" << be_idt_nl
     << "{" << be_idt_nl;

  for (ACE_CDR::ULong n = 0; n < count; ++n)
    {
      os << "&" << fields_name_str << "__" << n;

      if (n < count - 1)
        {
          os << ",";
        }

      os << be_nl;
    }

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

  return 0;
}