summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_decl.cpp
blob: e242c719b0a7cb8fe3b44ef0bb06f753db3bfb14 (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    be_decl.cpp
//
// = DESCRIPTION
//    Extension of class AST_Decl 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"

// Default Constructor
be_decl::be_decl (void)
  : cli_hdr_gen_ (I_FALSE),
    cli_stub_gen_ (I_FALSE),
    cli_inline_gen_ (I_FALSE),
    srv_hdr_gen_ (I_FALSE),
    srv_skel_gen_ (I_FALSE),
    srv_inline_gen_ (I_FALSE),
    fullname_ (0),
    flatname_ (0),
    repoID_ (0),
    size_type_ (be_decl::FIXED), // everybody is fixed size to start with
    encap_len_ (-1)
{
}

// Constructor
be_decl::be_decl (AST_Decl::NodeType type, UTL_ScopedName *n, UTL_StrList
                  *pragmas)
  : AST_Decl (type, n, pragmas),
    cli_hdr_gen_ (I_FALSE),
    cli_stub_gen_ (I_FALSE),
    cli_inline_gen_ (I_FALSE),
    srv_hdr_gen_ (I_FALSE),
    srv_skel_gen_ (I_FALSE),
    srv_inline_gen_ (I_FALSE),
    fullname_ (0),
    size_type_ (be_decl::FIXED), // everybody is fixed size to start with
    encap_len_ (-1)
{
}

//destructor
be_decl::~be_decl (void)
{
}

int
be_decl::gen_encapsulation (void)
{
  // do nothing
  return 0;
}

long
be_decl::tc_encap_len (void)
{
  return -1;
}

// return our size type
be_decl::SIZE_TYPE
be_decl::size_type (void) const
{
  return this->size_type_;
}

// set our size type and that of all our ancestors
void
be_decl::size_type (be_decl::SIZE_TYPE st)
{
  if (st == be_decl::FIXED) // does not affect ancestors
    return;

  if (this->size_type_ == st) // already of that type.
    return; // nothing to do

  // precondition
  ACE_ASSERT (st == be_decl::VARIABLE);

  this->size_type_ = st;

  // if we are just a typedef, nothing else to do
  if (this->node_type () == AST_Decl::NT_typedef)
    return;

  // update our parent if it is of a specific type
  if (!this->defined_in ()) // we are outermost
    return; // nothing to do
  else
    {
      // get the scope we are defined in
      be_decl *d = be_decl::narrow_from_decl (ScopeAsDecl (this->defined_in
                                                           ()));
      switch (d->node_type ())
        {
          // only these define valid scopes whose size needs to be set the same
          // as ours.
        case AST_Decl::NT_struct:
        case AST_Decl::NT_union:
          d->size_type (st); // call recursively to set the size type of
          // our ancestors
        default:
          return; // we are done
        }
    } // end else
}

// compute stringified fully scoped name
void
be_decl::compute_fullname (void)
{
  if (fullname_)
    return;
  else
    {
      long namelen;
      UTL_IdListActiveIterator	*i;
      long				first = I_TRUE;
      long				second = I_FALSE;

      // in the first loop compute the total length
      namelen = 0;
      i = new UTL_IdListActiveIterator (this->name ());
      while (!(i->is_done ()))
        {
          if (!first)
            namelen += 2; // for "::"
          else if (second)
            first = second = I_FALSE;
          // print the identifier
          namelen += ACE_OS::strlen (i->item ()->get_string ());
          if (first)
            {
              if (ACE_OS::strcmp (i->item ()->get_string (), "") != 0)
                // does not start with a ""
                first = I_FALSE;
              else
                second = I_TRUE;
            }
          i->next ();
        }
      delete i;

      this->fullname_ = new char [namelen+1];
      this->fullname_[0] = '\0';
      first = I_TRUE;
      second = I_FALSE;
      i = new UTL_IdListActiveIterator (this->name ());
      while (!(i->is_done ()))
        {
          if (!first)
            ACE_OS::strcat (this->fullname_, "::");
          else if (second)
            first = second = I_FALSE;
          // print the identifier
          ACE_OS::strcat (this->fullname_, i->item ()->get_string ());
          if (first)
            {
              if (ACE_OS::strcmp (i->item ()->get_string (), "") != 0)
                // does not start with a ""
                first = I_FALSE;
              else
                second = I_TRUE;
            }
          i->next ();
        }
      delete i;
    }
  return;
}

const char*
be_decl::fullname (void)
{
  if (!this->fullname_)
    compute_fullname ();

  return this->fullname_;
}

// compute stringified flattened fully scoped name
void
be_decl::compute_flatname (void)
{
  if (flatname_)
    return;
  else
    {
      long namelen;
      UTL_IdListActiveIterator	*i;
      long				first = I_TRUE;
      long				second = I_FALSE;

      // in the first loop compute the total length
      namelen = 0;
      i = new UTL_IdListActiveIterator (this->name ());
      while (!(i->is_done ()))
        {
          if (!first)
            namelen += 1; // for "_"
          else if (second)
            first = second = I_FALSE;
          // print the identifier
          namelen += ACE_OS::strlen (i->item ()->get_string ());
          if (first)
            {
              if (ACE_OS::strcmp (i->item ()->get_string (), "") != 0)
                // does not start with a ""
                first = I_FALSE;
              else
                second = I_TRUE;
            }
          i->next ();
        }
      delete i;

      this->flatname_ = new char [namelen+1];
      this->flatname_[0] = '\0';
      first = I_TRUE;
      second = I_FALSE;
      i = new UTL_IdListActiveIterator (this->name ());
      while (!(i->is_done ()))
        {
          if (!first)
            ACE_OS::strcat (this->flatname_, "_");
          else if (second)
            first = second = I_FALSE;
          // print the identifier
          ACE_OS::strcat (this->flatname_, i->item ()->get_string ());
          if (first)
            {
              if (ACE_OS::strcmp (i->item ()->get_string (), "") != 0)
                // does not start with a ""
                first = I_FALSE;
              else
                second = I_TRUE;
            }
          i->next ();
        }
      delete i;
    }
  return;
}

const char*
be_decl::flatname (void)
{
  if (!this->flatname_)
    compute_flatname ();

  return this->flatname_;
}

// compute stringified repository ID
void
be_decl::compute_repoID (void)
{
  if (repoID_)
    return;
  else
    {
      long namelen;
      UTL_IdListActiveIterator	*i;
      long				first = I_TRUE;
      long				second = I_FALSE;

      // in the first loop compute the total length
      namelen = 8; // for the prefix "IDL:" and suffix ":1.0"
      i = new UTL_IdListActiveIterator (this->name ());
      while (!(i->is_done ()))
        {
          if (!first)
            namelen += 1; // for "/"
          else if (second)
            first = second = I_FALSE;
          // print the identifier
          namelen += ACE_OS::strlen (i->item ()->get_string ());
          if (first)
            {
              if (ACE_OS::strcmp (i->item ()->get_string (), "") != 0)
                // does not start with a ""
                first = I_FALSE;
              else
                second = I_TRUE;
            }
          i->next ();
        }
      delete i;

      this->repoID_ = new char [namelen+1];
      this->repoID_[0] = '\0';
      ACE_OS::sprintf (this->repoID_, "%s", "IDL:");
      i = new UTL_IdListActiveIterator (this->name ());
      first = I_TRUE;
      second = I_FALSE;
      while (!(i->is_done ()))
        {
          if (!first)
            ACE_OS::strcat (this->repoID_, "/");
          else if (second)
            first = second = I_FALSE;
          // print the identifier
          ACE_OS::strcat (this->repoID_, i->item ()->get_string ());
          if (first)
            {
              if (ACE_OS::strcmp (i->item ()->get_string (), "") != 0)
                // does not start with a ""
                first = I_FALSE;
              else
                second = I_TRUE;
            }
          i->next ();
        }
      delete i;
      ACE_OS::strcat (this->repoID_, ":1.0");
    }
  return;
}

const char *
be_decl::repoID (void)
{
  if (!this->repoID_)
    compute_repoID ();

  return this->repoID_;
}

// converts a string name into an array of 4 byte longs
int
be_decl::tc_name2long (const char *name, long *&larr, long &arrlen)
{
  static char buf [NAMEBUFSIZE];
  long slen;
  unsigned int i;

  slen = ACE_OS::strlen (name) + 1; // 1 for NULL terminating

  // compute the number of bytes necessary to hold the name rounded to the next
  // multiple of 4 (i.e., size of long)
  arrlen = slen/4 + (slen%4 ? 1:0);

  ACE_OS::memset (buf, '\0', arrlen*4);
  larr = (long *)buf;

  for (i=0; i < ACE_OS::strlen (name); i++)
    {
      long shift; // num bytes to shift left

      shift = 3 - (i%4);
      // store the computed shifted quantity in the appropriate byte of the
      // array to be returned
      larr [i/4] |= ((name[i] & 0xff) << (shift*8));
    }
  return 0;
}

idl_bool
be_decl::is_nested (void)
{
  AST_Decl *d;

  d = ScopeAsDecl (this->defined_in ());
  // if we have an outermost scope and if that scope is not that of the Root,
  // then we are defined at some nesting level
  if (d && d->node_type () != AST_Decl::NT_root)
    return I_TRUE;

  return I_FALSE;
}

// return the length in bytes to hold the repoID inside a typecode. This
// comprises 4 bytes indicating the length of the string followed by the actual
// string represented as longs.
long
be_decl::repoID_encap_len (void)
{
  long slen;

  slen = ACE_OS::strlen (this->repoID ()) + 1; // + 1 for NULL terminating char
  // the number of bytes to hold the string must be a multiple of 4 since this
  // will be represented as an array of longs
  return 4 + 4 * (slen/4 + (slen%4 ? 1:0));
}

// return the length in bytes to hold the name inside a typecode. This
// comprises 4 bytes indicating the length of the string followed by the actual
// string represented as longs.
long
be_decl::name_encap_len (void)
{
  long slen;

  slen = ACE_OS::strlen (this->local_name ()->get_string ()) + 1; // + 1 for
                                                    // NULL terminating char
  // the number of bytes to hold the string must be a multiple of 4 since this
  // will be represented as an array of longs
  return 4 + 4 * (slen/4 + (slen%4 ? 1:0));
}

// narrowing methods
IMPL_NARROW_METHODS1 (be_decl, AST_Decl)
IMPL_NARROW_FROM_DECL (be_decl)