summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_visitor_connector/connector_dds_exh.cpp
blob: 03394c4b548ab2a82f5ea238fac0061f020c87ca (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

//=============================================================================
/**
 *  @file    connector_dds_exh.cpp
 *
 *  Visitor generating code for DDS Connectors in the exec impl header.
 *
 *  @author Jeff Parsons
 */
//=============================================================================

#include "connector.h"

#include "global_extern.h"
#include "utl_strlist.h"
#include "utl_string.h"

#include "ast_structure.h"
#include "ast_union.h"
#include "ast_template_module_ref.h"
#include "ast_template_module.h"
#include "ast_typedef.h"
#include "ast_native.h"

be_visitor_connector_dds_exh::be_visitor_connector_dds_exh (
      be_visitor_context *ctx)
  : be_visitor_connector_dds_ex_base (ctx)
{
  // This is initialized in the base class to svnt_export_macro()
  // or skel_export_macro(), since there are many more visitor
  // classes generating servant code. So we can just override
  // all that here.
  export_macro_ = be_global->conn_export_macro ();
}

be_visitor_connector_dds_exh::~be_visitor_connector_dds_exh ()
{
}

int
be_visitor_connector_dds_exh::visit_connector (be_connector *node)
{
  if (node->imported ())
    {
      return 0;
    }

  if (!this->begin (node))
    {
      return -1;
    }

  // If we have a connector within a templated module
  if (! this->t_args_.is_empty ())
    {
      // Generate all needed dds_traits
      for (FE_Utils::T_ARGLIST::CONST_ITERATOR i (this->t_args_);
          !i.done ();
          i.advance ())
        {
          AST_Decl **item = nullptr;
          i.next (item);
          AST_Decl *d = *item;

          if (this->is_dds_type (node, d))
            {
              this->gen_dds_traits (d);
            }
        }

      // Generate connector traits
      this->gen_connector_traits ();

      os_ << be_nl_2
          << "class " << this->export_macro_.c_str () << " "
          << this->node_->local_name () << "_exec_i" << be_idt_nl
          << ": public " << this->base_tname_ << "_Connector_T";

      os_ << " <" << be_idt << be_idt_nl;

      os_ << "CCM_" << this->node_->flat_name ()
          << "_Traits," << be_nl;

      size_t slot = 1UL;

      for (FE_Utils::T_ARGLIST::CONST_ITERATOR i (this->t_args_);
          !i.done ();
          i.advance (), ++slot)
        {
          AST_Decl **item = nullptr;
          i.next (item);
          AST_Decl *d = *item;

          if (this->is_dds_type (node, d))
            {
              os_ << d->flat_name ()
                  << "_DDS_Traits";
            }
          else
            {
              os_ << d->name ();
            }

          bool needs_bool = false;
          bool is_fixed = false;
          FE_Utils::T_Param_Info *param = nullptr;

          if (this->t_params_->get (param, slot - 1) != 0)
            {
              ACE_ERROR_RETURN ((LM_ERROR,
                                 ACE_TEXT ("be_visitor_connector_dds_exh::")
                                 ACE_TEXT ("visit_connector - ")
                                 ACE_TEXT ("template param fetch failed\n ")),
                                -1);
            }

          if (d->node_type () == AST_Decl::NT_typedef)
            {
              /// Strip away all layers of typedef before narrowing.
              AST_Typedef *td = dynamic_cast<AST_Typedef*> (d);
              d = td->primitive_base_type ();
            }

          /// No need to check if this is 0, but must narrow
          /// to call virtual function size_type() below.
          AST_Type *t = dynamic_cast<AST_Type*> (d);

          switch (param->type_)
            {
              case AST_Decl::NT_type:
              case AST_Decl::NT_struct:
              case AST_Decl::NT_union:
                needs_bool = true;
                is_fixed = (t->size_type () == AST_Type::FIXED);
                break;
              default:
                break;
            }

          if (needs_bool)
            {
              os_ << "," << be_nl
                  << (is_fixed ? "true" : "false");
            }

          if (slot < this->t_args_.size ())
            {
              os_ << "," << be_nl;
            }
        }

      os_ << ">";

      os_ << be_uidt << be_uidt << be_uidt_nl
          << "{" << be_nl
          << "public:" << be_idt_nl
          << this->node_->local_name () << "_exec_i ();" << be_nl
          << "virtual ~" << this->node_->local_name ()
          << "_exec_i ();" << be_uidt_nl
          << "};";

      this->gen_exec_entrypoint_decl ();

    }

  os_ << be_uidt_nl
      << "}";

  /// Unset the flags in the port interfaces list. This is
  /// also done in visit_mirror_port(), but we must also do
  /// it here to catch a port interface that didn't come to
  /// us from an extended port or mirror port.
  for (ACE_Unbounded_Queue<be_interface *>::ITERATOR iter (
        this->port_ifaces_);
      !iter.done ();
      iter.advance ())
    {
      be_interface **item = nullptr;
      iter.next (item);

      (*item)->dds_connector_traits_done (false);
    }

  return 0;
}

int
be_visitor_connector_dds_exh::visit_mirror_port (
  be_mirror_port *node)
{
  os_ << be_nl
      << "struct " << node->local_name ()->get_string ()
      << "_traits" << be_nl
      << "{" << be_idt;

  /// Reuse the code in the base class to flip the
  /// facet/receptacle elements in the scope. We will end up
  /// in visit_provides() and visit_uses() below.
  int status =
    this->be_visitor_component_scope::visit_mirror_port (node);

  if (status != 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("be_visitor_connector_dds_exh::")
                         ACE_TEXT ("visit_mirror_port - ")
                         ACE_TEXT ("base class traversal failed\n ")),
                        -1);
    }

  os_ << be_uidt_nl
      << "};" << be_nl;

  /// Unset the flags in the port interfaces list so
  /// they can be used again in another port.
  for (ACE_Unbounded_Queue<be_interface *>::ITERATOR iter (
         this->port_ifaces_);
       !iter.done ();
       iter.advance ())
    {
      be_interface **item = nullptr;
      iter.next (item);

      (*item)->dds_connector_traits_done (false);
    }

  return 0;
}

int
be_visitor_connector_dds_exh::visit_provides (be_provides *node)
{
  be_interface *iface =
    dynamic_cast<be_interface*> (node->provides_type ());

  this->gen_interface_connector_trait (iface, node, true);

  return 0;
}

int
be_visitor_connector_dds_exh::visit_uses (be_uses *node)
{
  be_interface *iface =
    dynamic_cast<be_interface*> (node->uses_type ());

  this->gen_interface_connector_trait (iface, node, false);

  return 0;
}

int
be_visitor_connector_dds_exh::visit_attribute (
  be_attribute *node)
{
  AST_Decl::NodeType nt =
    ScopeAsDecl (node->defined_in ())->node_type ();

  /// We are interested in attributes in extended ports
  /// and connectors, skip all others.
  if (nt == AST_Decl::NT_component)
    {
      return 0;
    }

  os_ << be_nl
      << "typedef "
      << node->field_type ()->full_name () << " "
      << node->local_name ()->get_string () << "_type;";

  return 0;
}

void
be_visitor_connector_dds_exh::gen_dds_traits (AST_Decl *datatype)
{
  AST_Decl *comp_scope =
    ScopeAsDecl (this->node_->defined_in ());

  bool const global_comp =
    (comp_scope->node_type () == AST_Decl::NT_root);

  UTL_ScopedName *dt_name = datatype->name ();
  BE_GlobalData::DDS_IMPL the_dds_impl = be_global->dds_impl ();

  if (the_dds_impl != BE_GlobalData::DDS_NONE)
    {
      os_ << be_nl
          << "struct " << datatype->flat_name () << "_DDS_Traits" << be_nl
          << "{" << be_idt_nl
          << "static const char* get_type_name () { return \"" << dt_name << "\"; };" << be_nl
          << "typedef ::" << dt_name << " value_type;" << be_nl
          << "typedef ::" << dt_name;

      if (the_dds_impl == BE_GlobalData::NDDS)
        {
          os_ << "RTI";
        }

      os_ << "Seq dds_seq_type;" << be_nl;

      if (the_dds_impl == BE_GlobalData::NDDS)
        {
          os_ << "typedef ::" << dt_name
              << "TypeSupport type_support;" << be_nl;
        }
      else if (the_dds_impl == BE_GlobalData::COREDX)
        {
          os_ << "typedef ::" << dt_name
              << "TypeSupport type_support;" << be_nl;
        }
      else if (the_dds_impl == BE_GlobalData::OPENDDS)
        {
          os_ << "typedef ::" << dt_name
              << "TypeSupportImpl type_support;" << be_nl;
        }

      if (the_dds_impl == BE_GlobalData::NDDS)
        {
          os_ << "typedef ::DDS_SampleInfoSeq sampleinfo_seq_type;" << be_nl
              << "typedef ::DDS_SampleInfo sampleinfo_type;" << be_nl;
        }
      else if (the_dds_impl == BE_GlobalData::COREDX)
        {
          os_ << "typedef ::DDS_SampleInfoSeq sampleinfo_seq_type;" << be_nl
              << "typedef ::DDS_SampleInfo sampleinfo_type;" << be_nl;
        }
      else if (the_dds_impl == BE_GlobalData::OPENDDS)
        {
          os_ << "typedef ::DDS::SampleInfoSeq sampleinfo_seq_type;" << be_nl
              << "typedef ::DDS::SampleInfo sampleinfo_type;" << be_nl;
        }

      os_ << "typedef ::" << dt_name
          << "DataWriter datawriter_type;" << be_nl
          << "typedef ::" << dt_name
          << "DataReader datareader_type;" << be_nl;

      if (the_dds_impl == BE_GlobalData::NDDS)
        {
          os_ << "typedef " << (global_comp ? "" : "::") << comp_scope->full_name ()
              << "::DataWriter typed_writer_type;" << be_nl
              << "typedef " << (global_comp ? "" : "::") << comp_scope->full_name ()
              << "::DataReader typed_reader_type;" ;
        }
      else if (the_dds_impl == BE_GlobalData::OPENDDS)
        {
          os_ << "typedef ::" << dt_name
              << "DataWriter typed_writer_type;" << be_nl
              << "typedef ::" << dt_name
              << "DataReader typed_reader_type;";
        }

      os_ << be_uidt_nl << "};";
    }
}

void
be_visitor_connector_dds_exh::gen_connector_traits ()
{
  AST_Decl *comp_scope =
    ScopeAsDecl (this->node_->defined_in ());

  bool global_comp =
    (comp_scope->node_type () == AST_Decl::NT_root);

  os_ << be_nl_2
      << "struct CCM_" << this->node_->flat_name ()
      << "_Traits" << be_nl
      << "{" << be_idt_nl
      << "typedef ::CIAO_" << this->node_->flat_name () << "_Impl::"
      << this->node_->local_name () << "_Exec base_type;" << be_nl
      << "typedef " << (global_comp ? "" : "::")
      << comp_scope->name () << "::CCM_"
      << this->node_->local_name () << "_Context context_type;"
      << be_nl;

  if (this->visit_component_scope (this->node_) == -1)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("be_visitor_connector_dds_exh::")
                  ACE_TEXT ("gen_connector_traits - ")
                  ACE_TEXT ("visit_scope_failed\n ")));

      return;
    }
  os_ << be_uidt_nl
      << "};";
}

void
be_visitor_connector_dds_exh::gen_interface_connector_trait (
  be_interface *iface,
  be_field *port_elem,
  bool for_facet)
{
  if (!iface->dds_connector_traits_done ())
    {
      AST_Decl *scope = ScopeAsDecl (iface->defined_in ());
      bool global = (scope->node_type () == AST_Decl::NT_root);
      const char *smart_scope = (global ? "" : "::");
      const char *lname = iface->local_name ();

      os_ << be_nl
          << "typedef ::" << scope->name () << smart_scope
          << (!for_facet ? "" : "CCM_") << lname
          << " " << port_elem->local_name ()->get_string ()
          << "_type;";

      iface->dds_connector_traits_done (true);
      this->port_ifaces_.enqueue_tail (iface);
    }
}