summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/ast/ast_type.cpp
blob: a9014341a49c622d521f0ee862b750785bf4a035 (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// $Id$

/*

COPYRIGHT

Copyright 1992, 1993, 1994 Sun Microsystems, Inc.  Printed in the United
States of America.  All Rights Reserved.

This product is protected by copyright and distributed under the following
license restricting its use.

The Interface Definition Language Compiler Front End (CFE) is made
available for your use provided that you include this license and copyright
notice on all media and documentation and the software program in which
this product is incorporated in whole or part. You may copy and extend
functionality (but may not remove functionality) of the Interface
Definition Language CFE without charge, but you are not authorized to
license or distribute it to anyone else except as part of a product or
program developed by you or with the express written consent of Sun
Microsystems, Inc. ("Sun").

The names of Sun Microsystems, Inc. and any of its subsidiaries or
affiliates may not be used in advertising or publicity pertaining to
distribution of Interface Definition Language CFE as permitted herein.

This license is effective until terminated by Sun for failure to comply
with this license.  Upon termination, you shall destroy or return all code
and documentation for the Interface Definition Language CFE.

INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED AS IS WITH NO WARRANTIES OF
ANY KIND INCLUDING THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS
FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR ARISING FROM A COURSE OF
DEALING, USAGE OR TRADE PRACTICE.

INTERFACE DEFINITION LANGUAGE CFE IS PROVIDED WITH NO SUPPORT AND WITHOUT
ANY OBLIGATION ON THE PART OF Sun OR ANY OF ITS SUBSIDIARIES OR AFFILIATES
TO ASSIST IN ITS USE, CORRECTION, MODIFICATION OR ENHANCEMENT.

SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES SHALL HAVE NO LIABILITY WITH
RESPECT TO THE INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY
INTERFACE DEFINITION LANGUAGE CFE OR ANY PART THEREOF.

IN NO EVENT WILL SUN OR ANY OF ITS SUBSIDIARIES OR AFFILIATES BE LIABLE FOR
ANY LOST REVENUE OR PROFITS OR OTHER SPECIAL, INDIRECT AND CONSEQUENTIAL
DAMAGES, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

Use, duplication, or disclosure by the government is subject to
restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
Technical Data and Computer Software clause at DFARS 252.227-7013 and FAR
52.227-19.

Sun, Sun Microsystems and the Sun logo are trademarks or registered
trademarks of Sun Microsystems, Inc.

SunSoft, Inc.
2550 Garcia Avenue
Mountain View, California  94043

NOTE:

SunOS, SunSoft, Sun, Solaris, Sun Microsystems or the Sun logo are
trademarks or registered trademarks of Sun Microsystems, Inc.

*/

// AST_Type is the base class for all AST classes which represent
// IDL type constructs.

#include "ast_type.h"
#include "ast_visitor.h"
#include "utl_identifier.h"
#include "idl_defines.h"
#include "nr_extern.h"
#include "ace/Log_Msg.h"

ACE_RCSID (ast, 
           ast_type, 
           "$Id$")

AST_Type::AST_Type (void)
  : ifr_added_ (0),
    ifr_fwd_added_ (0),
    size_type_ (AST_Type::SIZE_UNKNOWN),
    has_constructor_ (0),
    nested_type_name_ (0)
{
}

AST_Type::AST_Type (AST_Decl::NodeType nt,
                    UTL_ScopedName *n)
  : AST_Decl (nt,
              n),
    ifr_added_ (0),
    ifr_fwd_added_ (0),
    size_type_ (AST_Type::SIZE_UNKNOWN),
    has_constructor_ (0),
    nested_type_name_ (0)
{
}

AST_Type::~AST_Type (void)
{
}

// Public operations.

// Return our size type.
AST_Type::SIZE_TYPE
AST_Type::size_type (void)
{
  if (this->size_type_ == AST_Type::SIZE_UNKNOWN)
    {
      (void) this->compute_size_type ();
    }

  return this->size_type_;
}

// Set our size type and that of all our ancestors.
void
AST_Type::size_type (AST_Type::SIZE_TYPE st)
{
  // Precondition - you cannot set somebody's sizetype to unknown.
  ACE_ASSERT (st != AST_Type::SIZE_UNKNOWN);

  // Size type can be VARIABLE or FIXED.
  if (this->size_type_ == AST_Type::SIZE_UNKNOWN) // not set yet
    {
      this->size_type_ = st; // set it
    }
  else if ((this->size_type_ == AST_Type::FIXED)
           && (st == AST_Type::VARIABLE))
    {
      // Once we are VARIABLE, we cannot be FIXED. But if we were FIXED and then
      // get overwritten to VARIABLE, it is fine. Such a situation occurs only
      // when setting the sizes of structures and unions.
      this->size_type_ = st;
    }
}

// Compute the size type of the node in question
int
AST_Type::compute_size_type (void)
{
  return 0;
}

idl_bool
AST_Type::in_recursion (AST_Type *)
{
  // By default we are not involved in recursion.
  return 0;
}

idl_bool
AST_Type::is_defined (void)
{
  // AST_Interface, AST_Structure, and AST_Union will
  // override this, as will AST_InterfaceFwd, etc.
  return 1;
}

idl_bool
AST_Type::ifr_added (void)
{
  return this->ifr_added_;
}

void
AST_Type::ifr_added (idl_bool val)
{
  this->ifr_added_ = val;
}

idl_bool
AST_Type::ifr_fwd_added (void)
{
  return this->ifr_fwd_added_;
}

void
AST_Type::ifr_fwd_added (idl_bool val)
{
  this->ifr_fwd_added_ = val;
}

idl_bool
AST_Type::has_constructor (void)
{
  return this->has_constructor_;
}

void
AST_Type::has_constructor (idl_bool value)
{
  // Similarly to be_decl::size_type_, once this
  // gets set to I_TRUE, we don't want it to
  // change back.
  if (this->has_constructor_ == 0)
    {
      this->has_constructor_ = value;
    }
}

// This code works. However, whether we should generate the
// ACE_NESTED_CLASS macro or not should be based on an option to the
// compiler. The previous version generated a relative path.
// This version always generates ACE_NESTED_CLASS, (leave ace/ACE.h and friends
// do the porting)
//
// Caution: returns the same buffer pointer even if the contents may change
// in the next call.  (return std::string anyone?)
//
// Return the type name using the ACE_NESTED_CLASS macro

const char *
AST_Type::nested_type_name (AST_Decl *use_scope,
                            const char *suffix,
                            const char *prefix)
{
  return this->nested_name (this->local_name ()->get_string (),
                            this->full_name (),
                            use_scope,
                            suffix,
                            prefix);
}

// This is the real thing used by the method above.
const char *
AST_Type::nested_name (const char* local_name,
                       const char* full_name,
                       AST_Decl *use_scope,
                       const char *suffix,
                       const char *prefix)
{
  // Some compilers do not like generating a fully scoped name for a type that
  // was defined in the same enclosing scope in which it was defined. For such,
  // we emit a macro defined in the ACE library.
  //

  // The tricky part here is that it is not enough to check if the
  // typename we are using was defined in the current scope. But we
  // need to ensure that it was not defined in any of our ancestor
  // scopes as well. If that is the case, then we can generate a fully
  // scoped name for that type, else we use the ACE_NESTED_CLASS macro.

  // Thus we need some sort of relative name to be generated.

  if (this->nested_type_name_ == 0)
    {
      ACE_NEW_RETURN (this->nested_type_name_,
                      char[NAMEBUFSIZE],
                      0);
    }

  // Our defining scope.
  AST_Decl *def_scope = 0;

  // Hold the fully scoped name.
  char def_name [NAMEBUFSIZE];
  char use_name [NAMEBUFSIZE];

  // These point to the prev, curr and next component in the scope.
  char *def_curr = def_name;
  char *def_next = 0;
  char *use_curr = use_name;
  char *use_next = 0;

  // How many chars to compare.
  int len_to_match = 0;

  // Initialize the buffers.
  ACE_OS::memset (this->nested_type_name_,
                  '\0',
                  NAMEBUFSIZE);

  ACE_OS::memset (def_name,
                  '\0',
                  NAMEBUFSIZE);

  ACE_OS::memset (use_name,
                  '\0',
                  NAMEBUFSIZE);

  // Traverse every component of the def_scope and use_scope beginning at the
  // root and proceeding towards the leaf trying to see if the components
  // match. Continue until there is a match and keep accumulating the path
  // traversed. This forms the first argument to the ACE_NESTED_CLASS
  // macro. Whenever there is no match, the remaining components of the
  // def_scope form the second argument.

  UTL_Scope *s = this->defined_in ();

  def_scope = s ? ScopeAsDecl (s) : 0;

  if (def_scope
      && def_scope->node_type () != AST_Decl::NT_root
      && use_scope)
    // If both scopes exist and that we are not in the root scope.
    {
      ACE_OS::strcpy (def_name,
                      def_scope->full_name ());

      ACE_OS::strcpy (use_name,
                      use_scope->full_name ());

      // Find the first occurrence of a :: and advance
      // the next pointers accordingly.
      def_next = ACE_OS::strstr (def_curr, "::");
      use_next = ACE_OS::strstr (use_curr, "::");

      // If the scopes are identical, don't supply them.
      if  (ACE_OS::strcmp (def_name,
                         use_name)
             == 0)
        {
          if (prefix != 0)
            {
              ACE_OS::strcat (this->nested_type_name_,
                              prefix);
            }

          ACE_OS::strcat (this->nested_type_name_,
                          local_name);
          if (suffix != 0)
            {
              ACE_OS::strcat (this->nested_type_name_,
                              suffix);
            }

          return this->nested_type_name_;
        }

      if (def_next != 0)
        {
          len_to_match =
            ACE_OS::strlen (def_curr) - ACE_OS::strlen (def_next);
        }
      else
        {
          len_to_match = ACE_OS::strlen (def_curr);
        }

      if (use_next != 0)
        {
          int len =
            ACE_OS::strlen (use_curr) - ACE_OS::strlen (use_next);

          if (len > len_to_match)
            {
              len_to_match = len;
            }
        }
      else
        {
          int len = ACE_OS::strlen (use_curr);

          if (len > len_to_match)
            {
              len_to_match = len;
            }
        }

      if (ACE_OS::strncmp (def_curr,
                           use_curr,
                           len_to_match)
            == 0)
        {
          // Initial prefix matches i.e., they have a common root.
          // Start by initializing the macro.
          ACE_OS::sprintf (this->nested_type_name_,
                           "ACE_NESTED_CLASS (");

          // Initialize the first argument.
          ACE_OS::strncat (this->nested_type_name_,
                           def_curr,
                           len_to_match);

          // Shift the current scopes to the next level.
          def_curr = (def_next ? (def_next + 2) : 0); // Skip the ::
          use_curr = (use_next ? (use_next + 2) : 0); // Skip the ::

          while (def_curr && use_curr)
            {
              // Find the first occurrence of a :: and advance the
              // next pointers accordingly.
              def_next = ACE_OS::strstr (def_curr, "::");
              use_next = ACE_OS::strstr (use_curr, "::");

              if (def_next != 0)
                {
                  len_to_match =
                    ACE_OS::strlen (def_curr) - ACE_OS::strlen (def_next);
                }
              else
                {
                  len_to_match = ACE_OS::strlen (def_curr);
                }

              if (use_next != 0)
                {
                  int len  =
                    ACE_OS::strlen (use_curr) - ACE_OS::strlen (use_next);

                  if (len > len_to_match)
                    {
                      len_to_match = len;
                    }
                }
              else
                {
                  int len = ACE_OS::strlen (use_curr);

                  if (len > len_to_match)
                    {
                      len_to_match = len;
                    }
                }

              if (ACE_OS::strncmp (def_curr,
                                   use_curr,
                                   len_to_match)
                    == 0)
                {
                  // They have same prefix, append to arg1.
                  ACE_OS::strcat (this->nested_type_name_,
                                  "::");

                  ACE_OS::strncat (this->nested_type_name_,
                                   def_curr,
                                   len_to_match);

                  def_curr = (def_next ? (def_next + 2) : 0); // Skip the ::
                  use_curr = (use_next ? (use_next + 2) : 0); // Skip the ::
                }
              else
                {
                  // No match. This is the end of the first argument. Get out
                  // of the loop as no more comparisons are necessary.
                  break;
                }
            }

          // Start the 2nd argument of the macro.
          ACE_OS::strcat (this->nested_type_name_, ", ");

          // Copy the remaining def_name (if any are left).
          if (def_curr != 0)
            {
              ACE_OS::strcat (this->nested_type_name_,
                              def_curr);

              ACE_OS::strcat (this->nested_type_name_,
                              "::");
            }

          // Append our local name.
          if (prefix != 0)
            {
              ACE_OS::strcat (this->nested_type_name_, prefix);
            }

          ACE_OS::strcat (this->nested_type_name_,
                          local_name);

          if (suffix != 0)
            {
              ACE_OS::strcat (this->nested_type_name_,
                              suffix);
            }

          ACE_OS::strcat (this->nested_type_name_,
                          ")");

          return this->nested_type_name_;
        } // End of if the root prefixes match.
    }

  // Otherwise just emit our full_name.
  if (prefix != 0)
    {
      ACE_OS::strcat (this->nested_type_name_, prefix);
    }

  ACE_OS::strcat (this->nested_type_name_,
                  full_name);

  if (suffix != 0)
    {
      ACE_OS::strcat (this->nested_type_name_,
                      suffix);
    }

  return this->nested_type_name_;
}

int
AST_Type::ast_accept (ast_visitor *visitor)
{
  return visitor->visit_type (this);
}

void
AST_Type::destroy (void)
{
  this->AST_Decl::destroy ();
}

// Narrowing.
IMPL_NARROW_METHODS1(AST_Type, AST_Decl)
IMPL_NARROW_FROM_DECL(AST_Type)