summaryrefslogtreecommitdiff
path: root/modules/TAO/TAO_IDL/be/be_visitor_operation/operation.cpp
blob: 9b3ce96287c718b0acae7e0054408ed0ec6e10fa (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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//
// $Id$
//

// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    operation.cpp
//
// = DESCRIPTION
//    Visitor generating code for Operation in the stubs file.
//
// = AUTHOR
//    Aniruddha Gokhale
//
// ============================================================================

ACE_RCSID (be_visitor_operation,
           operation,
           "$Id$")

// ************************************************************
// Generic Operation visitor
// ************************************************************

be_visitor_operation::be_visitor_operation (be_visitor_context *ctx)
  : be_visitor_scope (ctx)
{
}

be_visitor_operation::~be_visitor_operation (void)
{
}

// Is the operation return type void?
int
be_visitor_operation::void_return_type (be_type *bt)
{
  if (bt->node_type () == AST_Decl::NT_pre_defined)
    {
      be_predefined_type * const bpd =
        be_predefined_type::narrow_from_decl (bt);

      if (!bpd)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "TAO_IDL (%N:%l) "
                           "be_predefined_type::narrow_from_decl "
                           "failed\n"),
                          0);

      AST_PredefinedType::PredefinedType const pdt = bpd->pt ();

      if (pdt == AST_PredefinedType::PT_void)
        {
          return 1;
        }
    }

  return 0;
}

int
be_visitor_operation::has_param_type (be_operation *node,
                                      AST_Argument::Direction dir)
{
  return (node->count_arguments_with_direction (dir) != 0);
}

size_t
be_visitor_operation::count_non_out_parameters (be_operation *node)
{
  // @@ Once the valuetype issue discussed below is fixed we can
  //    replace this routine with:
  //
  // return node->count_arguments_with_direction (AST_Argument::dir_IN
  //                                              | AST_Argument::dir_INOUT);
  //
  size_t count = 0;

  // Initialize an iterator to iterate thru our scope.
  for (UTL_ScopeActiveIterator si (node, UTL_Scope::IK_decls);
       !si.is_done ();
       si.next ())
    {
      be_argument *bd =
        be_argument::narrow_from_decl (si.item ());

      // We do not generate insertion operators for valuetypes
      // yet. Do not include them in the count.
      be_valuetype *vt =
        be_valuetype::narrow_from_decl (bd->field_type ());

      if (bd && (bd->direction () != AST_Argument::dir_OUT) && !vt)
        {
          ++count;
        }
    }

  return count;
}

int
be_visitor_operation::is_amh_exception_holder (be_interface *node)
{
  int is_an_amh_exception_holder = 0;
  const char *amh_underbar = "AMH_";
  const char *node_name = node->local_name ();

  if( amh_underbar[0] == node_name[0] &&
      amh_underbar[1] == node_name[1] &&
      amh_underbar[2] == node_name[2] &&
      amh_underbar[3] == node_name[3]
      ) // node name starts with "AMH_"
    {
      //ACE_DEBUG ((LM_DEBUG, "Passed first test of amh_excepholder\n"));
      const char *last_E = ACE_OS::strrchr (node->full_name (), 'E');
      if (last_E != 0
          && ACE_OS::strcmp (last_E, "ExceptionHolder") == 0)
        {
          //ACE_DEBUG ((LM_DEBUG, "be_visitor_operation: Passed second test of amh_excepholder\n"));
          is_an_amh_exception_holder = 1;
        }
    }

  return is_an_amh_exception_holder;
}

int
be_visitor_operation::gen_raise_exception (const char *exception_name,
                                           const char *exception_arguments)
{
  TAO_OutStream *os = this->ctx_->stream ();

  *os << "throw "
      << exception_name << "(" << exception_arguments << ");";

  return 0;
}

int
be_visitor_operation::gen_stub_operation_body (
    be_operation *node,
    be_type *return_type
  )
{
  be_interface *intf = this->ctx_->attribute ()
    ? be_interface::narrow_from_scope (this->ctx_->attribute ()->defined_in ())
    : be_interface::narrow_from_scope (node->defined_in ());

  if (!intf)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_operation::"
                         "gen_stub_operation_body - "
                         "bad interface scope\n"),
                        -1);
    }

  TAO_OutStream *os = this->ctx_->stream ();
  be_visitor_context ctx;

  *os << be_nl << "{" << be_idt_nl;

  if (node->has_native ()) // native exists => no stub
    {
      if (this->gen_raise_exception ("::CORBA::MARSHAL",
                                     "") == -1)
        {
          ACE_ERROR_RETURN ((
              LM_ERROR,
              "(%N:%l) be_visitor_operation_cs::"
              "visit_operation - "
              "codegen for native exception failed\n"
            ),
            -1
          );
        }

      *os << be_uidt_nl << "}";

      return 0;
    }

  if (!node->is_abstract ())
    {
      // If the object is lazily evaluated the proxy broker might well
      // be null.  Initialize it now.
      *os << "if (!this->is_evaluated ())" << be_idt_nl
          << "{" << be_idt_nl
          << "::CORBA::Object::tao_object_initialize (this);"
          << be_uidt_nl
          << "}" << be_uidt_nl << be_nl;
      if (be_global->gen_direct_collocation() || be_global->gen_thru_poa_collocation ())
        {
            *os << "if (this->the" << intf->base_proxy_broker_name () << "_ == 0)"
                << be_idt_nl
                << "{" << be_idt_nl
                << intf->flat_name () << "_setup_collocation ();"
                << be_uidt_nl
                << "}" << be_uidt_nl << be_nl;
        }
    }

  // Declare return type helper class.
  *os << "TAO::Arg_Traits< ";

  this->gen_arg_template_param_name (node,
                                     return_type,
                                     os);

  *os << ">::"
      << (node->flags () == AST_Operation::OP_oneway &&
          be_global->use_clonable_in_args() ? "clonable_" : "")
      << "ret_val _tao_retval;";

  // Declare the argument helper classes.
  this->gen_stub_body_arglist (node, os);

  *os << be_nl << be_nl
      << "TAO::Argument *_the_tao_operation_signature [] =" << be_idt_nl
      << "{" << be_idt_nl
      << "&_tao_retval";

  AST_Argument *arg = 0;

  for (UTL_ScopeActiveIterator arg_list_iter (node, UTL_Scope::IK_decls);
       ! arg_list_iter.is_done ();
       arg_list_iter.next ())
    {
      arg = AST_Argument::narrow_from_decl (arg_list_iter.item ());

      *os << "," << be_nl
          << "&_tao_" << arg->local_name ();
    }

  *os << be_uidt_nl
      << "};" << be_uidt;

  if (this->gen_pre_stub_info (node) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_operation_cs::"
                         "visit_operation - "
                         "codegen for exceptiondata failed\n"),
                        -1);
    }

  // Use the name without the possible '_cxx_' here.
  ACE_CDR::ULong tmp_len =
    ACE_OS::strlen (node->original_local_name ()->get_string ());

  *os << be_nl << be_nl
      << "TAO::" << (node->is_abstract () ? "AbstractBase_" : "" )
      << "Invocation_Adapter _tao_call (" << be_idt << be_idt_nl
      << "this," << be_nl
      << "_the_tao_operation_signature," << be_nl
      << node->argument_count () + 1 << "," << be_nl
      << "\"";

  // Check if we are an attribute node in disguise.
  if (this->ctx_->attribute ())
    {
      // If we are a attribute node, add th elength of the operation
      // name.
      tmp_len += 5;

      // Now check if we are a "get" or "set" operation.
      if (node->nmembers () == 1)
        {
          *os << "_set_";
        }
      else
        {
          *os << "_get_";
        }
    }

  // original_local_name() strips off the leading '_cxx_' if any.
  *os << node->original_local_name () << "\"," << be_nl
      << tmp_len << "," << be_nl;

  if (be_global->gen_direct_collocation() || be_global->gen_thru_poa_collocation ())
    {
      *os << "this->the" << intf->base_proxy_broker_name () << "_";
    }
  else
    {
      *os << "0";
    }

  if (node->flags () == AST_Operation::OP_oneway)
    {
      *os << "," << be_nl
          << "TAO::TAO_ONEWAY_INVOCATION";
    }

  *os << be_uidt_nl
      << ");" << be_uidt;

  *os << be_nl << be_nl;

  // Since oneways cannot raise user exceptions, we have that
  // case covered as well.
  if (node->exceptions ())
    {
      *os << "_tao_call.invoke (" << be_idt << be_idt_nl
          << "_tao_" << node->flat_name ()
          << "_exceptiondata," << be_nl
          << node->exceptions ()->length () << be_uidt_nl
          << ");" << be_uidt;
    }
  else
    {
      *os << "_tao_call.invoke (0, 0);";
    }

  if (!this->void_return_type (return_type))
    {
      *os << be_nl << be_nl
          << "return _tao_retval.retn ();";
    }

  *os << be_uidt_nl << "}";

  return 0;
}

int
be_visitor_operation::gen_pre_stub_info (
    be_operation *node
  )
{
  be_visitor_context ctx = *this->ctx_;
  be_visitor_operation_exceptlist_cs visitor (&ctx);

  if (node->accept (&visitor) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) "
                         "be_visitor_operation_cs::"
                         "gen_pre_stub_info - "
                         "Exceptionlist generation error\n"),
                        -1);
    }

  return 0;
}

int
be_visitor_operation::gen_raise_interceptor_exception (
    be_type *bt,
    const char *excep,
    const char *completion_status
  )
{
  TAO_OutStream *os = this->ctx_->stream ();

  if (this->void_return_type (bt))
    {
      *os << "throw " << excep << "(" << completion_status << ");";
    }
  else
    {
      if (bt->size_type () == AST_Type::VARIABLE
          || bt->base_node_type () == AST_Decl::NT_array)
        {
          *os << "TAO_INTERCEPTOR_THROW_RETURN (" << be_idt << be_idt_nl
              << excep << " (" << be_idt << be_idt_nl
              << completion_status << be_uidt_nl
              << ")," << be_uidt_nl
              <<  "0" << be_uidt_nl
              << ");" << be_uidt;
        }
      else
        {
          *os << "TAO_INTERCEPTOR_THROW_RETURN (" << be_idt << be_idt_nl
              << excep << " (" << be_idt << be_idt_nl
              << completion_status << be_uidt_nl
              << ")," << be_uidt_nl
              <<  "_tao_retval" << be_uidt_nl
              << ");" << be_uidt;
        }
    }

  return 0;
}

void
be_visitor_operation::gen_stub_body_arglist (be_operation *node,
                                             TAO_OutStream *os,
                                             bool ami)
{
  AST_Argument *arg = 0;
  UTL_ScopeActiveIterator arg_decl_iter (node, UTL_Scope::IK_decls);

  if (ami)
    {
      // Skip the reply handler (first argument).
      arg_decl_iter.next ();
    }

  for (; ! arg_decl_iter.is_done (); arg_decl_iter.next ())
    {
      arg = AST_Argument::narrow_from_decl (arg_decl_iter.item ());

      *os << be_nl
          << "TAO::Arg_Traits< ";

      this->gen_arg_template_param_name (arg,
                                         arg->field_type (),
                                         os);

      *os << ">::";

      switch (arg->direction ())
        {
          case AST_Argument::dir_IN:
            *os << "in";
            if (be_global->use_clonable_in_args() &&
                node->flags () == AST_Operation::OP_oneway)
              {
                *os << "_clonable";
              }
            break;
          case AST_Argument::dir_INOUT:
            *os << "inout";
            break;
          case AST_Argument::dir_OUT:
            *os << "out";
          default:
            break;
        }

      *os << "_arg_val _tao_" << arg->local_name () << " ("
          << arg->local_name () << ");";
    }
}

void
be_visitor_operation::gen_arg_template_param_name (AST_Decl *scope,
                                                   AST_Type *bt,
                                                   TAO_OutStream *os)
{
  AST_Typedef *alias = 0;

  if (bt->node_type () == AST_Decl::NT_typedef)
    {
      alias = AST_Typedef::narrow_from_decl (bt);
    }

  AST_Decl::NodeType nt = bt->unaliased_type ()->node_type ();
  ACE_CDR::ULong bound = 0;

  if (nt == AST_Decl::NT_string || nt == AST_Decl::NT_wstring)
    {
      AST_String *s =
        AST_String::narrow_from_decl (bt->unaliased_type  ());
      bound = s->max_size ()->ev ()->u.ulval;

      // If the (w)string is unbounded, code is generated below by the
      // last line of this method, whether bt is a typedef or not.
      if (bound > 0)
        {
          *os << "::TAO::";

          if (alias != 0)
            {
              *os << alias->local_name () << "_" << bound;
            }
           else
            {
              // If we have an unaliased, bounded (w)string parameter,
              // we know that be_visitor_arg_traits has used an empty
              // struct of a type constructed from the flat name of
              // the parameter's 'enclosing scope' (the parameter
              // identifier for parameters, and the operation identifier
              // for return types) to get a unique type for the
              // Arg_Traits<> template parameter. So we generate it
              // the same way here, using the 'scope' argument's
              // flat name.
              *os << scope->flat_name ();
            }

          return;
        }
    }

  // We need the unaliased type name to make the code block below
  // work correctly.
  AST_Type *ut = bt->unaliased_type ();
  nt = ut->node_type ();

  // For the four predefined types below, we use the helper struct
  // type, in order to disambiguate the template parameter.
  if (nt == AST_Decl::NT_pre_defined)
    {
      AST_PredefinedType *pdt =
        AST_PredefinedType::narrow_from_decl (ut);

      switch (pdt->pt ())
        {
          case AST_PredefinedType::PT_boolean:
            *os << "::ACE_InputCDR::to_boolean";
            return;
          case AST_PredefinedType::PT_octet:
            *os << "::ACE_InputCDR::to_octet";
            return;
          case AST_PredefinedType::PT_char:
            *os << "::ACE_InputCDR::to_char";
            return;
          case AST_PredefinedType::PT_wchar:
            *os << "::ACE_InputCDR::to_wchar";
            return;
          case AST_PredefinedType::PT_void:
            break;
          default:
            *os << "::";
            break;
        }
    }
  else if (nt != AST_Decl::NT_string)
    {
      // We are unbounded, since the bounded case is handled
      // above. In this case, we want to generate 'char *'
      // without the leading double colon.
      *os << "::";
    }
    
  /// For now, keep a list of system operation or arg names
  /// that may not be remapped. May decide later to regnerate
  /// ORB code for alt mapping as well.  
  ACE_CString repo_id (scope->repoID ());
  bool sys_val = (repo_id == "IDL:repository_id:1.0");
    
  // For types other than the 4 above, don't unalias the type name
  // in case it is a sequence or array.
  if (nt == AST_Decl::NT_string && bound == 0)
    {
      if (be_global->alt_mapping () && !sys_val)
        {
          *os << "std::string";
        }
      else
        {
          *os << "char *";
        }
    }
  else
    {
      *os << bt->name ();
    }

  if (nt == AST_Decl::NT_array)
    {
      *os << "_tag";
    }
}