summaryrefslogtreecommitdiff
path: root/modules/TAO/TAO_IDL/fe/fe_obv_header.cpp
blob: 2ecb2c6a2f5408f929714ad877bcd7cd98f4e3ac (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
// $Id$

#include "fe_obv_header.h"

#include "ast_valuetype.h"
#include "ast_module.h"
#include "ast_param_holder.h"

#include "utl_err.h"
#include "utl_namelist.h"

#include "fe_extern.h"
#include "nr_extern.h"
#include "global_extern.h"

// @@@ (JP) Here are the rules for interface inheritance and
// value type inheritance and supports, straight from Jonathan
// Biggar <jon@floorboard.com> as of 3/28/02. The following was
// resolved by the OMG, but is not yet part of an official spec.

/*
An interface can inherit from any number of other interfaces, abstract
or not.

An abstract interface can only inherit from other abstract interfaces.

An abstract valuetype can inherit from any number of abstract
valuetypes.  It may support one interface, and in addition, any number
of abstract interfaces.

A concrete valuetype can inherit from only one concrete valuetype.  It
may inherit from any number of abstract valuetypes.  It may support one
interface, and any number of abstract interfaces.

The single concrete inherited valuetype must be the first one in the
inheritance list.

The single supported interface (for valuetypes) must also be the first
in the "supports" list.

And one more important clarification, if a base valuetype supports an
interface, a derived valuetype may also be declared to support an
interface, as long as it is derived from all interfaces that are
supported by any base valuetypes.  Here is an example:

interface I1 { };
interface I2 { };
interface I3: I1, I2 { };

abstract valuetype V1 supports I1 { };
abstract valuetype V2 supports I2 { };
valuetype V3: V1, V2 supports I3 { }; // legal
valuetype V4: V1 supports I2 { }; // illegal

This last rule was made to guarantee that any given valuetype supported
at most one most-derived interface.  We didn't want valuetypes to extend
the OMG model through the backdoor by providing multiple non-related
interfaces.
*/

FE_OBVHeader::FE_OBVHeader (UTL_ScopedName *n,
                            UTL_NameList *inherits,
                            UTL_NameList *supports,
                            bool truncatable,
                            bool is_eventtype)
  : FE_InterfaceHeader (n,
                        inherits,
                        false,
                        false,
                        false),
    supports_ (0),
    n_supports_ (0),
    inherits_concrete_ (0),
    supports_concrete_ (0),
    truncatable_ (truncatable)
{
  this->compile_inheritance (inherits,
                             is_eventtype);

  if (idl_global->err_count () == 0)
    {
      this->compile_supports (supports);
    }
}

FE_OBVHeader::~FE_OBVHeader (void)
{
}

AST_Type **
FE_OBVHeader::supports (void) const
{
  return this->supports_;
}

long
FE_OBVHeader::n_supports (void) const
{
  return this->n_supports_;
}

AST_Type *
FE_OBVHeader::inherits_concrete (void) const
{
  return this->inherits_concrete_;
}

AST_Type *
FE_OBVHeader::supports_concrete (void) const
{
  return this->supports_concrete_;
}

bool
FE_OBVHeader::truncatable (void) const
{
  return this->truncatable_;
}

void
FE_OBVHeader::destroy (void)
{
  this->FE_InterfaceHeader::destroy ();
}

void
FE_OBVHeader::compile_inheritance (UTL_NameList *vtypes,
                                   bool is_eventtype)
{
  this->FE_InterfaceHeader::compile_inheritance (vtypes,
                                                 true);

  if (this->n_inherits_ > 0)
    {
      AST_Type *t = this->inherits_[0];
      AST_ValueType *vt = AST_ValueType::narrow_from_decl (t);

      if (vt != 0
          && vt->is_abstract () == false)
        {
          this->inherits_concrete_ = vt;
        }

      if (! is_eventtype
          && this->inherits_[0]->node_type () == AST_Decl::NT_eventtype)
        {
          idl_global->err ()->valuetype_expected (this->inherits_[0]);
        }

      for (long i = 1; i < this->n_inherits_; ++i)
        {
          t = this->inherits_[i];

          if (!t->is_abstract ())
            {
              idl_global->err ()->abstract_expected (t);
            }

          if (! is_eventtype
              && t->node_type () == AST_Decl::NT_eventtype)
            {
              idl_global->err ()->valuetype_expected (t);
            }
        }
    }
}

void
FE_OBVHeader::compile_supports (UTL_NameList *supports)
{
  if (supports == 0)
    {
      this->supports_ = 0;
      this->n_supports_ = 0;
      return;
    }

  long length = supports->length ();
  this->n_supports_ = length;
  
  ACE_NEW (this->supports_,
           AST_Type *[length]);

  AST_Decl *d = 0;
  UTL_ScopedName *item = 0;;
  AST_Interface *iface = 0;
  AST_Type *t = 0;
  int i = 0;

  for (UTL_NamelistActiveIterator l (supports); !l.is_done (); l.next ())
    {
      item = l.item ();

      // Check that scope stack is valid.
      if (idl_global->scopes ().top () == 0)
        {
          idl_global->err ()->lookup_error (item);

          // This is probably the result of bad IDL.
          // We will crash if we continue from here.
          throw Bailout ();
        }

      // Look it up.
      UTL_Scope *s = idl_global->scopes ().top ();

      d = s->lookup_by_name  (item, true);

      if (d == 0)
        {
          AST_Decl *sad = ScopeAsDecl (s);

          if (sad->node_type () == AST_Decl::NT_module)
            {
              AST_Module *m = AST_Module::narrow_from_decl (sad);

              d = m->look_in_previous (item->last_component ());
            }
        }

      // Not found?
      if (d == 0)
        {
          idl_global->err ()->lookup_error (item);

          // This is probably the result of bad IDL.
          // We will crash if we continue from here.
          throw Bailout ();
        }

      // Remove typedefs, if any.
      if (d->node_type () == AST_Decl::NT_typedef)
        {
          d = AST_Typedef::narrow_from_decl (d)->primitive_base_type ();
        }
        
      AST_Decl::NodeType nt = d->node_type ();
      t = AST_Type::narrow_from_decl (d);

      if (nt == AST_Decl::NT_interface)
        {
          iface = AST_Interface::narrow_from_decl (d);
        }
      else if (nt == AST_Decl::NT_param_holder)
        {
          AST_Param_Holder *ph =
            AST_Param_Holder::narrow_from_decl (d);
            
          nt = ph->info ()->type_;
          
          if (nt != AST_Decl::NT_type
              && nt != AST_Decl::NT_interface)
            {
              idl_global->err ()->mismatched_template_param (
                ph->info ()->name_.c_str ());
                
              continue;
            }
        }
      else
        {
          idl_global->err ()->supports_error (this->interface_name_,
                                              d);
          continue;
        }

      // Forward declared interface?
      if (iface != 0 && !iface->is_defined ())
        {
          idl_global->err ()->supports_fwd_error (this->interface_name_,
                                                  iface);
          continue;
        }

      if (iface != 0 && !iface->is_abstract ())
        {
          if (i == 0)
            {
              this->supports_concrete_ = iface;

              if (!this->check_concrete_supported_inheritance (iface))
                {
                  idl_global->err ()->concrete_supported_inheritance_error (
                                          this->name (),
                                          iface->name ()
                                        );
                }
            }
          else
            {
              idl_global->err ()->abstract_expected (iface);
              continue;
            }
        }

      this->supports_[i++] = t;
    }
}

bool
FE_OBVHeader::check_concrete_supported_inheritance (AST_Interface *d)
{
  if (this->n_inherits_ == 0)
    {
      return true;
    }

  AST_ValueType *vt = 0;
  AST_Interface *concrete = 0;
  AST_Interface *ancestor = 0;

  for (long i = 0; i < this->n_inherits_; ++i)
    {
      vt = AST_ValueType::narrow_from_decl (this->inherits_[i]);
      concrete = vt->supports_concrete ();

      if (0 == concrete)
        {
          return true;
        }

      if (d == concrete)
        {
          return true;
        }

      for (long j = 0; j < d->n_inherits_flat (); ++j)
        {
          ancestor = d->inherits_flat ()[j];

          if (ancestor == concrete)
            {
              return true;
            }
        }
    }

  return false;
}