summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_type.cpp
blob: 79484c523ebba0937ddfb9b12f6967c85d88c24c (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
// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    be_type.cpp
//
// = DESCRIPTION
//    Extension of class AST_Type 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"

/*
 * BE_Type
 */

be_type::be_type (void)
  : tc_name_ (0),
    type_name_ (0),
    nested_type_name_ (0)
{
}

be_type::be_type (AST_Decl::NodeType nt, UTL_ScopedName *n, UTL_StrList *p)
  : AST_Decl (nt, n, p),
    tc_name_ (0),
    type_name_ (0),
    nested_type_name_ (0)
{
}

be_type::~be_type (void)
{
  if (this->nested_type_name_ != 0)
    {
      delete[] this->nested_type_name_;
      this->nested_type_name_ = 0;
    }
}

// compute the typecode name. The idea is to use the fully scoped name,
// however, prepend a _tc_ to the last component. A slightly different approach
// is required of the predefined types. Hence this method is overridden for
// predefined types.

void
be_type::compute_tc_name (void)
{
  static char namebuf [NAMEBUFSIZE];
  UTL_ScopedName *n;

  this->tc_name_ = NULL;
  ACE_OS::memset (namebuf, '\0', NAMEBUFSIZE);
  n = this->name ();
  while (n->tail () != NULL)
    {
      if (!this->tc_name_)
        {
          // does not exist
          this->tc_name_ = new UTL_ScopedName (n->head (), NULL);
        }
      else
        {
          this->tc_name_->nconc (new UTL_ScopedName (n->head (), NULL));
        }
      n = (UTL_ScopedName *)n->tail ();
    }
  ACE_OS::sprintf (namebuf, "_tc_%s", n->last_component ()->get_string ());
  if (!this->tc_name_)
    {
      // does not exist
      this->tc_name_ = new UTL_ScopedName (new Identifier (ACE_OS::strdup
                                                           (namebuf), 1, 0, I_FALSE), NULL);
    }
  else
    {
      this->tc_name_->nconc (new UTL_ScopedName (new Identifier (ACE_OS::strdup
                                                                 (namebuf), 1,
                                                                 0, I_FALSE), NULL));
    }
  return;
}

// retrieve typecode name
UTL_ScopedName *
be_type::tc_name (void)
{
  if (!this->tc_name_)
    compute_tc_name ();

  return this->tc_name_;
}

// XXXASG - This code works. However, whether we should generate the
// ACE_NESTED_CLASS macro or not should be based on an option to the
// compiler. In this version, we choose to generate a relative path.

// return the type name using the ACE_NESTED_CLASS macro
const char *
be_type::nested_type_name (be_decl *use_scope, const char *suffix)
{
  // 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);

  be_decl *def_scope = 0;  // our defining scope
  char // hold the fully scoped name
    def_name [NAMEBUFSIZE],
    use_name [NAMEBUFSIZE];
  char // these point to the curr and next component in the scope
    *def_curr = def_name,
    *def_next,
    *use_curr = use_name,
    *use_next;

  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

  def_scope = ((this->defined_in ())?
               (be_scope::narrow_from_scope (this->defined_in ())->decl ()):
               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->fullname ());
      ACE_OS::strcpy (use_name, use_scope->fullname ());

      // 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)
        *def_next = 0;

      if (use_next)
        *use_next = 0;

      if (!ACE_OS::strcmp (def_curr, use_curr))
        {
          // 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 (");
          //@@          ACE_OS::strcat (this->nested_type_name_, def_curr); // initialize the first argument

          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)
                *def_next = 0;

              if (use_next)
                *use_next = 0;

              if (!ACE_OS::strcmp (def_curr, use_curr))
                {
                  // they have same prefix, append to arg1
                  //@@    ACE_OS::strcat (this->nested_type_name_, "::");
                  //@@ ACE_OS::strcat (this->nested_type_name_, def_curr);
                  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 left)
          if (def_curr)
            ACE_OS::strcat (this->nested_type_name_, def_curr);

          // append our local name
          ACE_OS::strcat (this->nested_type_name_, this->local_name ()->get_string ());
          if (suffix)
            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 fullname
  ACE_OS::sprintf (this->nested_type_name_, this->fullname ());
  if (suffix)
    ACE_OS::strcat (this->nested_type_name_, suffix);

  return this->nested_type_name_;
}

// *****************************
// CODE GENERATION
// *****************************

// generate the _var definition for ourself
int
be_type::gen_var_defn (void)
{
  return 0;
}

// implementation of the _var class. All of these get generated in the inline
// file
int
be_type::gen_var_impl (void)
{
  return 0;
}

// generate the _out definition
int
be_type::gen_out_defn (void)
{
  return 0;
}

int
be_type::gen_out_impl (void)
{
  return 0;
}

AST_Decl::NodeType be_type::base_node_type (void) const
{
  return ACE_const_cast(be_type*, this)->node_type ();
}

int be_type::write_as_return (TAO_OutStream *, be_type *)
{
  ACE_ERROR_RETURN ((LM_ERROR,
		     "be_type::write_as_return - internal error,"
		     " method shouldn't be invoked\n"), -1);
}

int
be_type::accept (be_visitor *visitor)
{
  return visitor->visit_type (this);
}

// Narrowing
IMPL_NARROW_METHODS2 (be_type, AST_Type, be_decl)
IMPL_NARROW_FROM_DECL (be_type)