summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_union.cpp
blob: f13367ddab055014471b54022b5faa086f980854 (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

//=============================================================================
/**
 *  @file    be_union.cpp
 *
 *  Extension of class AST_Union that provides additional means for C++
 *  mapping.
 *
 *  @author Copyright 1994-1995 by Sun Microsystems
 *  @author Inc. and Aniruddha Gokhale
 */
//=============================================================================

#include "be_union.h"
#include "be_union_branch.h"
#include "be_union_label.h"
#include "be_visitor.h"
#include "be_codegen.h"
#include "be_helper.h"
#include "be_extern.h"

#include "ast_union_branch.h"
#include "ast_enum.h"
#include "utl_identifier.h"
#include "idl_defines.h"
#include "global_extern.h"

be_union::be_union (AST_ConcreteType *dt,
                    UTL_ScopedName *n,
                    bool local,
                    bool abstract)
  : COMMON_Base (local,
                 abstract),
    AST_Decl (AST_Decl::NT_union,
              n),
    AST_Type (AST_Decl::NT_union,
              n),
    AST_ConcreteType (AST_Decl::NT_union,
                      n),
    UTL_Scope (AST_Decl::NT_union),
    AST_Structure (AST_Decl::NT_union,
                   n,
                   local,
                   abstract),
    AST_Union (dt,
               n,
               local,
               abstract),
    be_scope (AST_Decl::NT_union),
    be_decl (AST_Decl::NT_union,
             n),
    be_type (AST_Decl::NT_union,
             n)
{
  // Always the case.
  this->has_constructor (true);

  if (!this->imported ())
    {
      idl_global->aggregate_seen_ = true;

      idl_global->union_seen_ = true;
    }
}

void
be_union::redefine (AST_Structure *from)
{
  be_union *bu = dynamic_cast<be_union*> (from);
  this->common_varout_gen_ = bu->common_varout_gen_;
  AST_Union::redefine (from);
}

bool
be_union::has_duplicate_case_labels ()
{
  for (UTL_ScopeActiveIterator si (this, UTL_Scope::IK_decls);
       !si.is_done ();
       si.next ())
    {
      AST_Decl *d = si.item ();
      AST_UnionBranch *ub =
        dynamic_cast<AST_UnionBranch*> (d);

      if (ub->label_list_length () > 1)
        {
          return true;
        }
    }

  return false;
}

void
be_union::gen_ostream_operator (TAO_OutStream *os,
                                bool /*use_underscore*/)
{
  *os << be_nl
      << "std::ostream& operator<< (" << be_idt << be_idt_nl
      << "std::ostream &strm," << be_nl
      << "const " << this->name () << " &_tao_union" << be_uidt_nl
      << ")" << be_uidt_nl
      << "{" << be_idt_nl
      << "strm << \"" << this->name () << "(\";" << be_nl_2
      << "switch (_tao_union._d ())" << be_nl
      << "{" << be_idt;

  for (long i = 0; i < this->pd_decls_used; ++i)
    {
      be_union_branch *ub =
        dynamic_cast<be_union_branch*> (this->pd_decls[i]);

      // We don't want any decls, just members.
      if (ub == nullptr)
        {
          continue;
        }

      *os << be_nl;

      unsigned long ll_len = ub->label_list_length ();

      for (unsigned long j = 0; j < ll_len; ++j)
        {
          // Check if we are printing the default case.
          if (ub->label (j)->label_kind () == AST_UnionLabel::UL_default)
            {
              *os << "default:";
            }
          else
            {
              *os << "case ";

              ub->gen_label_value (os, j);

              *os << ":";
            }

          if (j == ll_len - 1)
            {
              *os << be_idt_nl;
            }
          else
            {
              *os << be_nl;
            }
        }

      ACE_CString instance_name ("_tao_union.");
      instance_name += ub->local_name ()->get_string ();

      *os << "strm << ";

      be_type *ub_ft = dynamic_cast<be_type*> (ub->field_type ());
      AST_Decl::NodeType ub_nt = ub_ft->node_type ();
      // catch anonymous Array member types
      bool ub_use_underscore = ub_nt == AST_Decl::NT_array;

      ub->gen_member_ostream_operator (os,
                                       instance_name.c_str (),
                                       ub_use_underscore,
                                       true);

      *os << ";" << be_nl
          << "break;" << be_uidt;
    }

  // Some compilers complain unless this is present, but only
  // if not all values are covered in case statements.
  if (this->gen_empty_default_label ())
    {
      *os << be_nl
          << "default:" << be_idt_nl
          << "break;" << be_uidt;
    }

  *os << be_uidt_nl
      << "}" << be_nl_2
      << "return strm << \")\";" << be_uidt_nl
      << "}" << be_nl;
}

void
be_union::destroy ()
{
  // Call the destroy methods of our base classes.
  this->be_scope::destroy ();
  this->be_type::destroy ();
  this->AST_Union::destroy ();

}

// Visitor method.
int
be_union::accept (be_visitor *visitor)
{
  return visitor->visit_union (this);
}

bool
be_union::gen_empty_default_label ()
{
  // A non-empty explicit default label will be generated.
  if (this->default_index () != -1)
    {
      return false;
    }

  AST_ConcreteType *disc = this->disc_type ();
  if (disc == nullptr)
    {
      return true; // In reality this is an error.
    }

  AST_Decl::NodeType nt = disc->node_type ();
  ACE_UINT64 n_labels = this->nlabels ();

  if (nt == AST_Decl::NT_enum)
    {
      // Enums in CORBA are always 32bits in size, so unless
      // there are that many enum labels in the set, it is
      // incomplete (reguardless as to the actual member_count).
      return (n_labels <= ACE_UINT32_MAX);
    }

  AST_PredefinedType *pdt = dynamic_cast<AST_PredefinedType*> (disc);
  if (pdt == nullptr)
    {
      return true; // In reality this is an error.
    }

  switch (pdt->pt ())
    {
    case AST_PredefinedType::PT_boolean:
      return (n_labels < 2);

    case AST_PredefinedType::PT_char:
      return (n_labels <= ACE_OCTET_MAX);

    case AST_PredefinedType::PT_short:
    case AST_PredefinedType::PT_ushort:
      return (n_labels <= ACE_UINT16_MAX);

    case AST_PredefinedType::PT_long:
    case AST_PredefinedType::PT_ulong:
      return (n_labels <= ACE_UINT32_MAX);

    case AST_PredefinedType::PT_longlong:
    case AST_PredefinedType::PT_ulonglong:
      // We would wrap to 0 here - we are using a 64 bit count
      // this case is so marginal as to always be incomplete.
      return true;

    // Keep fussy compilers happy.
    default:
      break;
    }

  return true;
}

AST_UnionBranch *
be_union::be_add_union_branch (AST_UnionBranch *b)
{
  return this->fe_add_union_branch (b);
}

ACE_UINT64
be_union::nlabels ()
{
  ACE_UINT64 retval = 0;

  for (UTL_ScopeActiveIterator si (this, UTL_Scope::IK_decls);
       !si.is_done ();
       si.next ())
    {
      AST_Decl *d = si.item ();
      AST_UnionBranch *ub =
        dynamic_cast<AST_UnionBranch*> (d);

      if (ub != nullptr)
        {
          retval += ub->label_list_length ();
        }
    }

  return retval;
}