summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_sequence.cpp
blob: 5d88dabd8acf453df469444fb47e46ff11ee62d6 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// $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"

ACE_RCSID(be, be_sequence, "$Id$")


/*
 * 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)
              {
                // if this pseudo is a CORBA::Object, then the managed type is
                // an objref
                if (!ACE_OS::strcmp (bpd->local_name ()->get_string (),
                                     "Object"))
                  this->mt_ = be_sequence::MNG_OBJREF;
                else
                  this->mt_ = be_sequence::MNG_PSEUDO;
              }
            else
              {
                this->mt_ = be_sequence::MNG_NONE;
              }
          }
          break;
        default:
          this->mt_ = be_sequence::MNG_NONE;
        } // end of switch
    }
  return this->mt_;
}

/*
 * Add this be_sequence to the locally defined types in this scope
 */
AST_Sequence *
be_sequence::fe_add_sequence (AST_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::accept (be_visitor *visitor)
{
  return visitor->visit_sequence (this);
}


const char *
be_sequence::instance_name ()
{
  static char namebuf[NAMEBUFSIZE];
  ACE_OS::memset (namebuf, '\0', NAMEBUFSIZE);

  be_type *bt;
  bt = be_type::narrow_from_decl (this->base_type ());
  if (!bt)
    {
      ACE_DEBUG ((LM_ERROR,
                  "(%N:%l) be_visitor_sequence_ch::"
                  "gen_instantiate_name - "
                  "Bad element type\n"));
      return namebuf;
    }

  // generate the class name

  // the base type after removing all the aliases
  be_type  *prim_type = bt;
  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 ();
    }

  // generate the appropriate sequence type
  switch (this->managed_type ())
    {
    case be_sequence::MNG_PSEUDO:
    case be_sequence::MNG_OBJREF:
      if (this->unbounded ())
        ACE_OS::sprintf (namebuf,
                         "_TAO_Unbounded_Object_Sequence_%s",
                         this->flatname());
      else
        ACE_OS::sprintf (namebuf,
                         "_TAO_Bounded_Object_Sequence_%s_%d",
                         this->flatname(),
                         this->max_size ()->ev()->u.ulval);
      break;
    case be_sequence::MNG_STRING: // sequence of strings
      if (this->unbounded ())
        ACE_OS::sprintf (namebuf,
                         "TAO_Unbounded_String_Sequence");
      else
        ACE_OS::sprintf (namebuf,
                         "_TAO_Bounded_String_Sequence_%s",
                         this->flatname());
      break;
    default: // not a managed type
      if (this->unbounded ())
	{
	  // TAO provides extensions for octet sequences, first find out
	  // if the base type is an octet (or an alias for octet)
	  be_predefined_type *predef =
	    be_predefined_type::narrow_from_decl (prim_type);
	  if (predef != 0 &&
	      predef->pt() == AST_PredefinedType::PT_octet)
	    ACE_OS::sprintf (namebuf,
			     "TAO_Unbounded_Sequence<CORBA::Octet>");
	  else
            ACE_OS::sprintf (namebuf,
                             "_TAO_Unbounded_Sequence_%s",
                             this->flatname());
                             // or prim_type->flatname ());
	  // ACE_DEBUG ((LM_DEBUG, "testing.... %d, %d = <%s>\n",
	  // predef, predef->pt (), namebuf));
	}
      else
        ACE_OS::sprintf (namebuf,
                         "_TAO_Bounded_Sequence_%s_%d",
                          this->flatname(),
                          //prim_type->flatname (),
                          this->max_size()->ev()->u.ulval);
      break;
    }

  return namebuf;
}

idl_bool
be_sequence::in_recursion (be_type *node)
{
  if (!node)
    {
      // there has to be a parameter
      ACE_ERROR_RETURN ((LM_ERROR,
                         ASYS_TEXT ("(%N:%l) be_sequence::")
                         ASYS_TEXT ("in_recursion - ")
                         ASYS_TEXT ("bad parameter node\n")),
                        0);
    }

  be_type *type = be_type::narrow_from_decl (this->base_type ());
  if (!type)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ASYS_TEXT ("(%N:%l) be_sequence::")
                         ASYS_TEXT ("in_recursion - ")
                         ASYS_TEXT ("bad base type\n")),
                        0);
    }

  if (!ACE_OS::strcmp (node->fullname (), type->fullname ()))
    // they match
    return 1;
  else
    // not in recursion
    return 0;
}

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