summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_sequence.cpp
blob: 318cc8f4968eb0f28e3b1b8c32dc659e946320c5 (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    be_sequence.cpp
//
// = DESCRIPTION
//    Extension of class AST_Sequence that provides additional means for C++
//    mapping.
//
// = AUTHOR
//    Copyright 1994-1995 by Sun Microsystems, Inc.
//    and
//    Aniruddha Gokhale
//
// ============================================================================

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

/*
 * BE_Sequence
 */
be_sequence::be_sequence (void)
  : mt_ (be_sequence::MNG_UNKNOWN)
{
  this->size_type (be_decl::VARIABLE); // always the case
}

be_sequence::be_sequence (AST_Expression *v, AST_Type *t)
  : AST_Sequence (v, t),
    AST_Decl (AST_Decl::NT_sequence,
              NULL,
              NULL),
    mt_ (be_sequence::MNG_UNKNOWN)
{
  // check if we are bounded or unbounded. An expression value of 0 means
  // unbounded
  if (v->ev ()->u.ulval == 0)
    {
      this->unbounded_ = I_TRUE;
    }
  else
    {
      this->unbounded_ = I_FALSE;
    }

  this->size_type (be_decl::VARIABLE); // a sequence data type is always
                                       // VARIABLE
}

idl_bool
be_sequence::unbounded (void) const
{
  return this->unbounded_;
}

// helper to create_name
char *
be_sequence::gen_name (void)
{
  char namebuf [NAMEBUFSIZE];
  be_type *bt; // base type;

  ACE_OS::memset (namebuf, '\0', NAMEBUFSIZE);  // reset the buffer
  // retrieve the base type
  bt = be_type::narrow_from_decl (this->base_type ());
  if (!bt)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_sequence::"
                         "gen_name - "
                         "bad base type\n"),
                        0);
    }
  if (bt->node_type () == AST_Decl::NT_sequence)
    {
      // our base type is an anonymous sequence
      be_sequence *seq;
      seq = be_sequence::narrow_from_decl (bt);
      if (!seq)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_sequence::"
                             "gen_name - "
                             "error converting base type to sequence\n"),
                            0);
        }
      seq->set_defined_in (this); // set ourselves as its parent
      this->fe_add_sequence (seq); // add the child to our scope
      ACE_OS::sprintf (namebuf, "_tao_seq_%s", seq->gen_name ());
    }
  else
    {
      ACE_OS::sprintf (namebuf, "_tao_seq_%s", bt->local_name ()->get_string ());
    }
  // append the size (if any)
  if (!this->unbounded_)
    {
      ACE_OS::sprintf (namebuf, "%s_%d", namebuf, this->max_size ()->ev
                       ()->u.ulval);
    }
  return ACE_OS::strdup (namebuf);
}

// create a name for ourselves
int
be_sequence::create_name (be_typedef *node)
{
  static char namebuf [NAMEBUFSIZE];
  UTL_ScopedName *n = NULL;
  be_decl *scope; // scope in which we are defined

  // if there is a typedef node, we use its name as our name
  if (node)
    {
      n = (UTL_ScopedName *)node->name ()->copy ();
      this->set_name (n); // set our name
    }
  else
    {

      ACE_OS::memset (namebuf, '\0', NAMEBUFSIZE);  // reset the buffer
      ACE_OS::strcpy (namebuf, this->gen_name ()); // generate a local name

      // now see if we have a fully scoped name and if so, generate one
      scope = be_scope::narrow_from_scope (this->defined_in ())->decl ();
      if (scope)
        {
          // make a copy of the enclosing scope's  name
          n = (UTL_ScopedName *)scope->name ()->copy () ;

          // add our local name as the last component
          n->nconc (new UTL_ScopedName (new Identifier (ACE_OS::strdup
                                                        (namebuf), 1,
                                                        0, I_FALSE),
                                        NULL));
          // set the fully scoped name
          this->set_name (n);
        }
      else
        {
          // We better be not here because we must be inside some scope,
          // atleast the ROOT scope.
          return -1;
        }
    }
  return 0;
}

// Does this sequence have a managed type sequence element?
be_sequence::MANAGED_TYPE
be_sequence::managed_type (void)
{
  if (this->mt_ == be_sequence::MNG_UNKNOWN) // not calculated yet
    {
      be_type  *bt, *prim_type; // base types

      bt = be_type::narrow_from_decl (this->base_type ());

      if (bt->node_type () == AST_Decl::NT_typedef)
        {
          // get the primitive base type of this typedef node
          be_typedef *t = be_typedef::narrow_from_decl (bt);
          prim_type = t->primitive_base_type ();
        }
      else
        prim_type = bt;

      // determine if we need a managed type and which one
      switch (prim_type->node_type ())
        {
        case AST_Decl::NT_interface:
        case AST_Decl::NT_interface_fwd:
          this->mt_ = be_sequence::MNG_OBJREF;
          break;
        case AST_Decl::NT_string:
          this->mt_ = be_sequence::MNG_STRING;
          break;
        case AST_Decl::NT_pre_defined:
          {
            be_predefined_type *bpd = be_predefined_type::narrow_from_decl
              (prim_type);
            if (bpd->pt () == AST_PredefinedType::PT_pseudo)
              {
                this->mt_ = be_sequence::MNG_OBJREF;
              }
            else
              {
                this->mt_ = be_sequence::MNG_NONE;
              }
          }
          break;
        default:
          this->mt_ = be_sequence::MNG_NONE;
        } // end of switch
    }
  return this->mt_;
}

// generate typecode.
// Typecode for sequences comprises the enumerated value followed by the
// encapsulation of the parameters

int
be_sequence::gen_typecode (void)
{
  TAO_OutStream *cs; // output stream
  TAO_NL  nl;        // end line
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();

  cs = cg->client_stubs ();
  cs->indent (); // start from whatever indentation level we were at

  *cs << "CORBA::tk_sequence, // typecode kind" << nl;
  *cs << this->tc_encap_len () << ", // encapsulation length\n";
  // now emit the encapsulation
  return this->gen_encapsulation ();
}

// generate encapsulation
// An encapsulation for ourselves will be necessary when we are part of some
// other IDL type and a typecode for that other type is being generated. This
// will comprise our typecode kind. IDL types with parameters will additionally
// have the encapsulation length and the entire typecode description

int
be_sequence::gen_encapsulation (void)
{
  TAO_OutStream *os; // output stream
  TAO_CodeGen *cg = TAO_CODEGEN::instance ();
  be_type *bt; // base type

  os = cg->client_stubs ();
  os->incr_indent ();

  *os << "TAO_ENCAP_BYTE_ORDER, // byte order\n";

  // emit typecode of element type
  bt = be_type::narrow_from_decl (this->base_type ());
  if (!bt || (bt->gen_typecode () == -1))
    {
      ACE_ERROR ((LM_ERROR, "be_sequence::gen_typecode - bad base type\n"));
      return -1;
    }

  //  emit the length
  os->decr_indent ();
  *os << this->max_size () << ",\n";
  return 0;
}

// compute typecode size
long
be_sequence::tc_size (void)
{
  // 4 bytes for enumeration, 4 bytes for storing encap length val, followed by the
  // actual encapsulation length
  return 4 + 4 + this->tc_encap_len ();
}

long
be_sequence::tc_encap_len (void)
{
  if (this->encap_len_ == -1) // not computed yet
    {
      be_type *bt; // base type

      this->encap_len_ = 4;  // holds the byte order flag
      // add the encapsulation length of our base type
      bt = be_type::narrow_from_decl (this->base_type ());
      if (!bt)
        {
          ACE_ERROR ((LM_ERROR,
                      "be_sequence::tc_encap_len - bad base type\n"));
          return 0;
        }
      this->encap_len_ += bt->tc_size ();
      this->encap_len_ += 4; // to hold the max size

    }
  return this->encap_len_;
}

/*
 * Add this be_sequence to the locally defined types in this scope
 */
be_sequence *
be_sequence::fe_add_sequence (be_sequence *t)
{
  if (t == NULL)
    return NULL;

  this->add_to_local_types(t);
  return t;
}

// overridden method
be_decl *
be_sequence::decl (void)
{
  return this;
}

int be_sequence::write_as_return (TAO_OutStream *stream,
                                  be_type *type)
{
  *stream << type->name () << " *";
  return 0;
}

int
be_sequence::accept (be_visitor *visitor)
{
  return visitor->visit_sequence (this);
}

// Narrowing
IMPL_NARROW_METHODS3 (be_sequence, AST_Sequence, be_scope, be_type)
IMPL_NARROW_FROM_DECL (be_sequence)