summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_visitor_interface/interface_ss.cpp
blob: 01989fba163435c9e1fdaa5a6a7b9fbd4dacf7fb (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
//
// $Id$
//

// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    interface_ss.cpp
//
// = DESCRIPTION
//    Visitor generating code for Interfaces in the server skeletons file.
//
// = AUTHOR
//    Aniruddha Gokhale
//
// ============================================================================

#include        "idl.h"
#include        "idl_extern.h"
#include        "be.h"

#include "be_visitor_interface.h"

ACE_RCSID(be_visitor_interface, interface_ss, "$Id$")


// ************************************************************
// Interface visitor for server skeletons
// ************************************************************

be_visitor_interface_ss::be_visitor_interface_ss (be_visitor_context *ctx)
  : be_visitor_interface (ctx)
{
}

be_visitor_interface_ss::~be_visitor_interface_ss (void)
{
}

int
be_visitor_interface_ss::visit_interface (be_interface *node)
{
  TAO_OutStream *os; // output stream

  if (node->srv_skel_gen () || node->imported ())
    return 0;

  os = this->ctx_->stream ();

  // generate the skeleton class name

  os->indent (); // start with whatever indentation level we are at

  // Skip the generation of collocation factory function pointer
  // initializer if the interface is locality constraint.
  if (!idl_global->gen_locality_constraint ())
    {
      if (node->gen_operation_table () == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "be_visitor_interface_ss::"
                             "visit_interface - "
                             "codegen for operation table failed\n"),
                            -1);
        }

      // Collocation factory function pointer initializer.
      *os << node->full_name () << "_ptr _TAO_collocation_POA_"
          << node->flat_name () << "_Stub_Factory (" << be_idt << be_idt_nl
          << "CORBA::Object_ptr obj" << be_uidt_nl
          << ")" << be_uidt_nl;

      *os << "{" << be_idt_nl
          << "TAO_Stub *stub = obj->_stubobj ();" << be_nl << be_nl
          << "switch (stub->servant_orb_var ()->orb_core"
          << " ()->get_collocation_strategy ())" << be_idt_nl
          << "{" << be_nl << "case TAO_ORB_Core::THRU_POA:" << be_idt_nl;

      if (idl_global->gen_thru_poa_collocation ())
        *os << "return new " << node->full_coll_name (be_interface::THRU_POA)
            << " (stub);" << be_uidt_nl;
      else
        *os << "break;" << be_uidt_nl;

      *os << "case TAO_ORB_Core::DIRECT:" << be_idt_nl;

      if (idl_global->gen_direct_collocation ())
        *os << "if (obj->_servant () != 0)" << be_idt_nl
            << "{" << be_idt_nl
            << node->full_skel_name () << "* servant = ACE_reinterpret_cast ("
            << node->full_skel_name () << "*, obj->_servant ()->_downcast (\""
            << node->repoID () << "\"));" << be_nl
            << "if (servant != 0)" << be_idt_nl
            << "return new " << node->full_coll_name (be_interface::DIRECT)
            << " (servant, stub);" << be_uidt << be_uidt_nl
            << "}" << be_uidt_nl;

      *os << "break;" << be_uidt_nl
          << "default:" << be_idt_nl
          << "break;" << be_uidt_nl
          << "}" << be_uidt_nl
          << "return 0;" << be_uidt_nl
          << "}\n\n";

      *os << "int _TAO_collocation_POA_" << node->flat_name ()
          << "_Stub_Factory_Initializer"
          << " (long dummy)" << be_nl
          << "{" << be_idt_nl
          << "ACE_UNUSED_ARG (dummy);" << be_nl << be_nl
          << "_TAO_collocation_" << node->flat_name ()
          << "_Stub_Factory_function_pointer = " << be_idt_nl
          << "_TAO_collocation_POA_" << node->flat_name ()
          << "_Stub_Factory;" << be_uidt_nl << be_nl
          << "return 0;" << be_uidt_nl << "}" << be_nl << be_nl;

      *os << "static int _TAO_collocation_POA_" << node->flat_name ()
          << "_Stub_Factory_Initializer_Scarecrow = " << be_idt_nl
          << "_TAO_collocation_POA_" << node->flat_name ()
          << "_Stub_Factory_Initializer ("
          << "ACE_reinterpret_cast (long, _TAO_collocation_POA_"
          << node->flat_name () << "_Stub_Factory_Initializer));"
          << be_uidt_nl << be_nl;
    }

  // constructor
  *os << "// skeleton constructor" << be_nl;
  // find if we are at the top scope or inside some module
  if (!node->is_nested ())
    {
      // we are outermost. So the POA_ prefix is prepended to our name
      *os << node->full_skel_name () << "::POA_" << node->local_name () <<
        " (void)" << be_nl;
    }
  else
    {
      // the POA_ prefix is prepended to our outermost module name
      *os << node->full_skel_name () << "::" << node->local_name () <<
        " (void)" << be_nl;
    }

  *os << "{" << be_idt_nl;
  // No need to initialize optable for locality constraint interface.
  if (!idl_global->gen_locality_constraint ())
    *os << "this->optable_ = &tao_" << node->flat_name ()
        << "_optable;" << be_uidt_nl;
  else
    *os << be_uidt_nl;
  *os << "}" << be_nl << be_nl;

  // Skip copy constructor if interface is locality constraint.
  if (!idl_global->gen_locality_constraint ())
    {
      *os << "// copy ctor" << be_nl;
      // find if we are at the top scope or inside some module
      if (!node->is_nested ())
        {
          // we are outermost. So the POA_ prefix is prepended to our name
          *os << node->full_skel_name () << "::POA_"
              << node->local_name () << " ("
              << "const POA_" << node->local_name () << "& rhs)";
        }
      else
        {
          // the POA_ prefix is prepended to our outermost module name
          *os << node->full_skel_name () << "::"
              << node->local_name () << " (const "
              << node->local_name () << "& rhs)";
        }
      *os << be_idt_nl
          << ": ";
      if (node->traverse_inheritance_graph
          (be_interface::copy_ctor_helper, os) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "be_visitor_interface_ss::visit_interface - "
                           " copy ctor generation failed\n"), -1);
      *os << "  TAO_ServantBase (rhs)" << be_uidt_nl
          << "{}" << be_nl << be_nl;
    }

  *os << "// skeleton destructor" << be_nl;

  if (!node->is_nested ())
    {
      // we are outermost. So the POA_ prefix is prepended to our name
      *os << node->full_skel_name () << "::~POA_" << node->local_name () <<
        " (void)" << be_nl;
    }
  else
    {
      // the POA_ prefix is prepended to our outermost module name
      *os << node->full_skel_name () << "::~" << node->local_name () <<
        " (void)" << be_nl;
    }
  *os << "{" << be_nl;
  *os << "}\n\n";


  // generate code for elements in the scope (e.g., operations)
  if (this->visit_scope (node) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "be_visitor_interface_ss::"
                         "visit_interface - "
                         "codegen for scope failed\n"),
                        -1);
    }

  // Skip the generation of default static skeletons if the interface
  // is locality constraint.
  if (!idl_global->gen_locality_constraint ())
    {
      // generate code for the _is_a skeleton
      os->indent ();
      *os << "void " << node->full_skel_name ()
          << "::_is_a_skel (" << be_idt << be_idt_nl
          << "CORBA::ServerRequest &_tao_server_request, " << be_nl
          << "void * _tao_object_reference," << be_nl
          << "void * /* context */," << be_nl
          << "CORBA::Environment &ACE_TRY_ENV" << be_uidt_nl
          << ")" << be_uidt_nl;
      *os << "{" << be_idt_nl;
      *os << "TAO_InputCDR &_tao_in = _tao_server_request.incoming ();" << be_nl;
      *os << node->full_skel_name () << " *_tao_impl = ("
          << node->full_skel_name () << " *) _tao_object_reference;" << be_nl;
      *os << "CORBA::Boolean _tao_retval = 0;" << be_nl;
      *os << "CORBA::String_var value;" << be_nl;
      *os << "if (!((_tao_in >> value.out ())))" << be_idt_nl;
      *os << "ACE_THROW (CORBA::MARSHAL ());" << be_uidt_nl << be_nl;
      *os << "_tao_retval = _tao_impl->_is_a (value.in (), ACE_TRY_ENV);" << be_nl;
      *os << "ACE_CHECK;" << be_nl << be_nl;
      *os << "_tao_server_request.init_reply (ACE_TRY_ENV);" << be_nl;
      *os << "ACE_CHECK;" << be_nl;
      *os << "TAO_OutputCDR &_tao_out = _tao_server_request.outgoing ();" << be_nl;
      *os << "if (!((_tao_out << CORBA::Any::from_boolean (_tao_retval))))" << be_idt_nl;
      *os << "ACE_THROW (CORBA::MARSHAL ());" << be_uidt << be_uidt_nl;
      *os << "}" << be_nl << be_nl;


      // generate code for the _non_existent skeleton
      *os << "void " << node->full_skel_name ()
          << "::_non_existent_skel (" << be_idt << be_idt_nl
          << "CORBA::ServerRequest &_tao_server_request, " << be_nl
          << "void * _tao_object_reference," << be_nl
          << "void * /* context */," << be_nl
          << "CORBA::Environment &ACE_TRY_ENV" << be_uidt_nl
          << ")" << be_uidt_nl;
      *os << "{" << be_idt_nl;
      *os << node->full_skel_name () << " *_tao_impl = ("
          << node->full_skel_name () << " *) _tao_object_reference;" << be_nl;
      *os << "CORBA::Boolean _tao_retval = _tao_impl->_non_existent (ACE_TRY_ENV);" << be_nl;
      *os << "ACE_CHECK;" << be_nl << be_nl;
      *os << "_tao_server_request.init_reply (ACE_TRY_ENV);" << be_nl;
      *os << "ACE_CHECK;" << be_nl;
      *os << "TAO_OutputCDR &_tao_out = _tao_server_request.outgoing ();" << be_nl;
      *os << "if (!((_tao_out << CORBA::Any::from_boolean (_tao_retval))))" << be_idt_nl;
      *os << "ACE_THROW (CORBA::MARSHAL ());" << be_uidt << be_uidt_nl;
      *os << "}\n\n";
    }

  os->indent ();
  *os << "CORBA::Boolean " << node->full_skel_name ()
      << "::_is_a (" << be_idt << be_idt_nl
      << "const char* value," << be_nl
      << "CORBA::Environment &ACE_TRY_ENV" << be_uidt_nl
      << ")" << be_uidt_nl
      << "{" << be_idt_nl
      << "if (\n" << be_idt;
  if (node->traverse_inheritance_graph (be_interface::is_a_helper, os) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "be_visitor_interface_ss::"
                         "visit_interface - "
                         "traversal of inhertance graph failed\n"),
                        -1);
    }

  os->indent ();
  *os << "(!ACE_OS::strcmp ((char *)value, "
      << "CORBA::_tc_Object->id (ACE_TRY_ENV))))"
      << be_idt_nl << "return 1;" << be_uidt_nl
      << "else" << be_idt_nl
      << "return 0;" << be_uidt << be_uidt << be_uidt_nl
      << "}" << be_nl << be_nl;

  // the downcast method.
  *os << "void* " << node->full_skel_name ()
      << "::_downcast (" << be_idt << be_idt_nl
      << "const char* logical_type_id" << be_uidt_nl
      << ")" << be_uidt_nl
      << "{\n" << be_idt;

  if (node->traverse_inheritance_graph (be_interface::downcast_helper, os) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "be_visitor_interface_ss::"
                         "visit_interface - "
                         "traversal of inhertance graph failed\n"),
                        -1);
    }

  os->indent ();
  *os << "if (ACE_OS::strcmp (logical_type_id, "
      << "\"IDL:omg.org/CORBA/Object:1.0\") == 0)" << be_idt_nl
      << "return ACE_static_cast(PortableServer::Servant, this);"
      << be_uidt_nl;

  *os << "return 0;" << be_uidt_nl
      << "}" << be_nl << be_nl;

  // Also skip the generation of the _dispatch method for locality
  // constraint interface.
  if (!idl_global->gen_locality_constraint ())
    {
      // now the dispatch method
      *os << "void " << node->full_skel_name () <<
        "::_dispatch (CORBA::ServerRequest &req, " <<
        "void *context, CORBA::Environment &ACE_TRY_ENV)" << be_nl;
      *os << "{" << be_idt_nl;
      *os << "TAO_Skeleton skel; // pointer to skeleton for operation" << be_nl;
      *os << "const char *opname = req.operation (); // retrieve operation name"
          << be_nl;
      *os << "// find the skeleton corresponding to this opname" << be_nl;
      *os << "if (this->_find (opname, skel, req.operation_length ()) == -1)" << be_nl;
      *os << "{" << be_idt_nl;
      *os << "ACE_ERROR ((LM_ERROR, \"Bad operation <%s>\\n\", opname));" << be_nl;
      *os << "ACE_THROW (CORBA_BAD_OPERATION ());"
        //<< "ACE_TRY_ENV);" << be_uidt_nl;
          << be_uidt_nl;
      //  *os << "env.exception (new CORBA_BAD_OPERATION ());" << be_nl;
      *os << "}" << be_nl;
      *os << "else" << be_idt_nl;
      *os << "skel (req, this, context, ACE_TRY_ENV);" << be_uidt << be_uidt_nl;
      *os << "}" << be_nl << be_nl;
    }

  *os << "const char* " << node->full_skel_name ()
      << "::_interface_repository_id (void) const"
      << be_nl;
  *os << "{" << be_idt_nl;
  *os << "return \"" << node->repoID () << "\";" << be_uidt_nl;
  *os << "}" << be_nl << be_nl;

  // the _this () operation
  *os << node->full_name () << "*" << be_nl
      << node->full_skel_name ()
      << "::_this (CORBA_Environment &ACE_TRY_ENV)" << be_nl
      << "{" << be_idt_nl
      << "TAO_Stub *stub = this->_create_stub (ACE_TRY_ENV);" << be_nl
      << "ACE_CHECK_RETURN (0);" << be_nl;

  // The _this operation is much more simpler for locality constraint
  // interface.
  if (idl_global->gen_locality_constraint ())
    *os << "return new "
        << node->full_coll_name (be_interface::DIRECT) << " (this, stub);"
        << be_uidt_nl << "}\n\n";
  else
    {
      *os << "if (stub->servant_orb_var ()->orb_core ()->optimize_collocation_objects ())" << be_idt_nl
          << "switch (stub->servant_orb_var ()->orb_core ()->get_collocation_strategy ())" << be_idt_nl
          << "{" << be_nl
          << "case TAO_ORB_Core::THRU_POA:" << be_idt_nl;

      // Thru POA stub
      if (idl_global->gen_thru_poa_collocation ())
        *os << "return new "
            << node->full_coll_name (be_interface::THRU_POA) << " (stub);" << be_uidt_nl;
      else
        *os << "ACE_THROW_RETURN (CORBA::BAD_PARAM (), 0);" << be_uidt_nl;

      // Direct stub
      *os << "case TAO_ORB_Core::DIRECT:" << be_idt_nl;
      if (idl_global->gen_direct_collocation ())
        *os << "return new "
            << node->full_coll_name (be_interface::DIRECT) << " (this, stub);" << be_uidt_nl;
      else
        *os << "ACE_THROW_RETURN (CORBA::BAD_PARAM (), 0);" << be_uidt_nl;

      *os << "default:" << be_idt_nl
          << "ACE_THROW_RETURN (CORBA::BAD_PARAM (), 0);" << be_uidt_nl
          << "}" << be_uidt << be_uidt_nl
          << "else" << be_idt_nl
          << "{" << be_idt_nl
          << "// stub->_incr_refcnt ();" << be_nl
          << "CORBA::Object_var obj = new CORBA::Object (stub);" << be_nl
          << "return " << node->name () << "::_unchecked_narrow (obj.in ());" << be_uidt_nl
          << "}" << be_uidt << be_uidt_nl
          << "}\n\n";
    }

  // the _create_collocated_objref method.  If the idl compiler does
  // not generate the type of collocated stub but the orb is asking
  // for it, simply return null so a remote stub will be used.
  // generate the collocated class impl
  if (idl_global->gen_thru_poa_collocation ())
    {
      be_visitor_context ctx (*this->ctx_);
      if (this->ctx_->state () == TAO_CodeGen::TAO_AMI_HANDLER_SERVANT_CS)
        ctx.state (TAO_CodeGen::TAO_AMI_HANDLER_INTERFACE_THRU_POA_COLLOCATED_CS);
      else
        ctx.state (TAO_CodeGen::TAO_INTERFACE_THRU_POA_COLLOCATED_SS);
      be_visitor *visitor = tao_cg->make_visitor (&ctx);
      if (!visitor)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "be_visitor_interface_ss::"
                             "visit_interface - "
                             "Bad visitor for thru_poa collocated class\n"),
                            -1);
        }

      if (node->accept (visitor) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "be_visitor_interface_ss::"
                             "visit_interface - "
                             "codegen for thru_poa collocated class failed\n"),
                            -1);
        }
      delete visitor;
    }

  if (idl_global->gen_direct_collocation ())
    {
      be_visitor_context ctx (*this->ctx_);
      if (this->ctx_->state () == TAO_CodeGen::TAO_AMI_HANDLER_SERVANT_CS)
        ctx.state (TAO_CodeGen::TAO_AMI_HANDLER_INTERFACE_THRU_POA_COLLOCATED_CS);
      else
        ctx.state (TAO_CodeGen::TAO_INTERFACE_DIRECT_COLLOCATED_SS);
      be_visitor *visitor = tao_cg->make_visitor (&ctx);
      if (!visitor)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "be_visitor_interface_ss::"
                             "visit_interface - "
                             "Bad visitor for direct collocated class\n"),
                            -1);
        }

      if (node->accept (visitor) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "be_visitor_interface_ss::"
                             "visit_interface - "
                             "codegen for direct collocated class failed\n"),
                            -1);
        }
      delete visitor;
    }

  *os << "\n\n";

  return 0;
}