summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_visitor_union/union_ch.cpp
blob: 0fd71f3ad117ff1433ed01fe2487ada4dfa65566 (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
//
// $Id$
//

// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    union_ch.cpp
//
// = DESCRIPTION
//    Visitor generating code for Unions in the client header
//
// = AUTHOR
//    Aniruddha Gokhale
//
// ============================================================================

#include	"idl.h"
#include	"idl_extern.h"
#include	"be.h"

#include "be_visitor_union.h"

ACE_RCSID(be_visitor_union, union_ch, "$Id$")


// ******************************************************
// for client header
// ******************************************************

be_visitor_union_ch::be_visitor_union_ch (be_visitor_context *ctx)
  : be_visitor_union (ctx)
{
}

be_visitor_union_ch::~be_visitor_union_ch (void)
{
}

// visit the Union node and its scope
int be_visitor_union_ch::visit_union (be_union *node)
{
  TAO_OutStream *os; // output stream
  be_type *bt;       // type node
  // instantiate a visitor context with a copy of our context. This info
  // will be modified based on what type of node we are visiting
  be_visitor_context ctx (*this->ctx_);
  ctx.node (node); // set the node to be the node being visited. The scope is
                   // still the same


  if (!node->cli_hdr_gen () && !node->imported ()) // not already generated and
                                                   // not imported
    {
      os = this->ctx_->stream ();

      // generate the ifdefined macro for the union type
      os->gen_ifdef_macro (node->flatname ());
      os->indent (); // start with the current indentation level
      *os << "class " << idl_global->export_macro () << " "
          << node->local_name () << ": public TAO_Base_Union " << be_nl
          << "{" << be_nl
          << "public:" << be_idt_nl

        // generate default and copy constructors
          << node->local_name () << " (void); // default constructor" << be_nl
          << node->local_name () << " (const " << node->local_name ()
          << " &); // copy constructor" << be_nl
        // generate destructor
          << "~" << node->local_name () << " (void); // destructor" << be_nl
        // generate assignment operator
          << node->local_name () << " &operator= (const "
          << node->local_name () << " &); // copy constructor\n\n";

      // retrieve the disriminant type
      bt = be_type::narrow_from_decl (node->disc_type ());
      if (!bt)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_union_ch::"
                             "visit_union - "
                             "bad disciminant type\n"), -1);
        }

      // the discriminant type may have to be defined here if it was an enum
      // declaration inside of the union statement.

      ctx.state (TAO_CodeGen::TAO_UNION_DISCTYPEDEFN_CH); // set current code
                                                          // gen state
      be_visitor *visitor = tao_cg->make_visitor (&ctx);
      if (!visitor)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_union_ch::"
                             "visit_union - "
                             "bad visitor\n"), -1);
        }

      if (bt->accept (visitor) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_union_ch::"
                             " visit_union - "
                             "codegen for discriminant failed\n"), -1);
        }
      delete visitor;

      // now generate the public defn for the union branch members. For this,
      // set our state to reflect what we are aiming to do
      this->ctx_->state (TAO_CodeGen::TAO_UNION_PUBLIC_CH); // set current code
                                                            // gen state
      if (this->visit_scope (node) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_union_ch::"
                             "visit_union - "
                             "codegen for public defn of union members\n"),
                            -1);
        }

      // now generate the private data members of the union
      os->decr_indent ();
      *os << "private:\n";
      os->incr_indent ();
      *os << bt->nested_type_name (node) << " disc_;" << be_nl; // emit the
                                                 // ACE_NESTED_CLASS macro

      // the members are inside of a union
      *os << "union" << be_nl;
      *os << "{\n";
      os->incr_indent (0);
      this->ctx_->state (TAO_CodeGen::TAO_UNION_PRIVATE_CH); // set current
                                                             // code gen state
      if (this->visit_scope (node) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_union_ch::"
                             "visit_union - "
                             "codegen for private members of union\n"), -1);
        }

      os->decr_indent ();
      *os << "} u_; // end of union" << be_nl;

      // the reset method (TAO extension)
      *os << "// TAO extensions" << be_nl;
      *os << "void _reset (" << bt->nested_type_name (node)
          << ", CORBA::Boolean);" << be_nl;
      *os << "// Frees any allocated storage" << be_nl << be_nl;
      // the virtual overloaded _discriminant method
      *os << "virtual void *_discriminant (void);" << be_nl;
      *os << "// returns pointer to the discriminant" << be_nl << be_nl;
      // the overloaded virtual reset method
      *os << "virtual void _reset (void);" << be_nl;
      *os << "// calls the above reset with finalize=1" << be_nl << be_nl;
      // the _access method
      *os << "virtual void *_access (CORBA::Boolean flag);" << be_nl;
      *os << "// accesses the right data member. "
          << "Also will allocate on TRUE flag" << be_nl << be_uidt_nl;
      *os << "}; // " << node->name () << "\n\n";

      // by using a visitor to declare and define the TypeCode, we have the
      // added advantage to conditionally not generate any code. This will be
      // based on the command line options. This is still TO-DO
      ctx = *this->ctx_;
      ctx.state (TAO_CodeGen::TAO_TYPECODE_DECL);
      visitor = tao_cg->make_visitor (&ctx);
      if (!visitor || (node->accept (visitor) == -1))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_union_ch::"
                             "visit_union - "
                             "TypeCode declaration failed\n"
                             ), -1);
        }


      os->gen_endif ();

      // generate the ifdefined macro for the _var type
      os->gen_ifdef_macro (node->flatname (), "_var");
      // generate var defn
      if (node->gen_var_defn () == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_union_ch::"
                             "visit_union - "
                             "codegen for _var\n"), -1);
        }
      os->gen_endif ();

      // generate the ifdefined macro for the array type
      os->gen_ifdef_macro (node->flatname (), "_out");
      // a class is generated for an out defn only for a variable length struct
      if (node->size_type () == be_decl::VARIABLE)
        {
          if (node->gen_out_defn () == -1)
            {
              ACE_ERROR_RETURN ((LM_ERROR,
                                 "(%N:%l) be_visitor_union_ch::"
                                 "visit_union - "
                                 "codegen for _out\n"), -1);
            }
        }
      else
        {
          os->indent ();
          *os << "typedef " << node->local_name () << " &" << node->local_name
            () << "_out;\n\n";
        }
      os->gen_endif ();

      node->cli_hdr_gen (I_TRUE);
    }

  return 0;
}