Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / TAO_IDL / be / be_visitor_field / field_ch.cpp
blobfcdf4dfbff86b2aaaf0eef91eaacff2a6f82e7c4
2 //=============================================================================
3 /**
4 * @file field_ch.cpp
6 * Visitor generating code for Field node in the client header
8 * @author Aniruddha Gokhale
9 */
10 //=============================================================================
12 #include "field.h"
13 #include "be_visitor_enum/enum_ch.h"
14 #include "be_visitor_sequence/sequence_ch.h"
15 #include "be_visitor_map/map_ch.h"
16 #include "nr_extern.h"
18 // **********************************************
19 // Visitor for field in the client header file.
20 // **********************************************
22 be_visitor_field_ch::be_visitor_field_ch (be_visitor_context *ctx)
23 : be_visitor_decl (ctx)
27 be_visitor_field_ch::~be_visitor_field_ch ()
31 int
32 be_visitor_field_ch::visit_field (be_field *node)
34 TAO_OutStream *os = this->ctx_->stream ();
35 be_type *bt = dynamic_cast<be_type*> (node->field_type ());
37 if (!bt)
39 ACE_ERROR_RETURN ((LM_ERROR,
40 "(%N:%l) be_visitor_field_ch::"
41 "visit_field - "
42 "Bad field type\n"),
43 -1);
46 this->ctx_->node (node);
48 *os << be_nl;
50 if (bt->accept (this) == -1)
52 ACE_ERROR_RETURN ((LM_ERROR,
53 "(%N:%l) be_visitor_field_ch::"
54 "visit_field - "
55 "codegen for field type failed\n"),
56 -1);
59 // Now output the field name.
60 *os << " " << node->local_name () << ";";
62 return 0;
65 // Visit operations on all possible data types that a field can be.
67 int
68 be_visitor_field_ch::visit_array (be_array *node)
70 TAO_OutStream *os = this->ctx_->stream ();
71 be_type *bt = nullptr;
73 if (this->ctx_->alias ())
75 bt = this->ctx_->alias ();
77 else
79 bt = node;
82 // If not a typedef and we are defined in the use scope, we must be defined.
83 if (!this->ctx_->alias ()
84 && node->is_child (this->ctx_->scope ()->decl ()))
86 // This is the case for anonymous arrays.
88 // Instantiate a visitor context with a copy of our context. This info
89 // will be modified based on what type of node we are visiting
90 be_visitor_context ctx (*this->ctx_);
91 ctx.node (node); // set the node to be the node being visited. The scope
92 // is still the same
94 // First generate the array declaration
95 ctx.state (TAO_CodeGen::TAO_ARRAY_CH);
96 be_visitor_array_ch visitor (&ctx);
98 if (node->accept (&visitor) == -1)
100 ACE_ERROR_RETURN ((LM_ERROR,
101 "(%N:%l) be_visitor_field_ch::"
102 "visit_array - "
103 "codegen failed\n"),
104 -1);
107 ctx.state (TAO_CodeGen::TAO_ROOT_CH);
109 // Having defined all array type and its supporting operations, now
110 // generate the actual variable that is a field of the structure.
111 *os << be_nl_2 << "_" << bt->local_name ();
113 else
115 // This was a typedefed array.
116 UTL_Scope *holds_container =
117 this->ctx_->scope ()->decl ()->defined_in ();
118 AST_Decl *hc_decl = ScopeAsDecl (holds_container);
120 if (hc_decl->node_type () != AST_Decl::NT_module)
122 *os << bt->nested_type_name (this->ctx_->scope ()->decl ());
124 else
126 *os << bt->name ();
130 return 0;
134 be_visitor_field_ch::visit_enum (be_enum *node)
136 TAO_OutStream *os = this->ctx_->stream ();
137 be_type *bt = nullptr;
139 if (this->ctx_->alias ())
141 bt = this->ctx_->alias ();
143 else
145 bt = node;
148 // If not a typedef and we are defined in the use scope, we must be defined.
149 if (!this->ctx_->alias () // not a typedef
150 && node->is_child (this->ctx_->scope ()->decl ()))
152 // Instantiate a visitor context with a copy of our context. This info
153 // will be modified based on what type of node we are visiting.
154 be_visitor_context ctx (*this->ctx_);
155 ctx.node (node);
157 // First generate the enum declaration.
158 be_visitor_enum_ch visitor (&ctx);
160 if (node->accept (&visitor) == -1)
162 ACE_ERROR_RETURN ((LM_ERROR,
163 "(%N:%l) be_visitor_field_ch::"
164 "visit_enum - "
165 "codegen failed\n"),
166 -1);
170 // This was a typedefed array.
171 UTL_Scope *holds_container =
172 this->ctx_->scope ()->decl ()->defined_in ();
173 AST_Decl *hc_decl = ScopeAsDecl (holds_container);
175 if (hc_decl->node_type () != AST_Decl::NT_module)
177 *os << bt->nested_type_name (this->ctx_->scope ()->decl ());
179 else
181 *os << bt->name ();
184 return 0;
188 be_visitor_field_ch::visit_interface (be_interface *node)
190 return this->emit_common (node);
194 be_visitor_field_ch::visit_interface_fwd (be_interface_fwd *node)
196 return this->emit_common (node);
200 be_visitor_field_ch::visit_valuebox (be_valuebox *node)
202 return this->emit_common (node);
206 be_visitor_field_ch::visit_valuetype (be_valuetype *node)
208 return this->emit_common (node);
212 be_visitor_field_ch::visit_valuetype_fwd (be_valuetype_fwd *node)
214 return this->emit_common (node);
219 be_visitor_field_ch::emit_common (be_type *node)
221 TAO_OutStream *os = this->ctx_->stream ();
222 be_type *bt = nullptr;
224 if (this->ctx_->alias ())
226 bt = this->ctx_->alias ();
228 else
230 bt = node;
233 UTL_Scope *holds_container =
234 this->ctx_->scope ()->decl ()->defined_in ();
235 AST_Decl *hc_decl = ScopeAsDecl (holds_container);
237 if (hc_decl->node_type () != AST_Decl::NT_module)
239 *os << bt->nested_type_name (this->ctx_->scope ()->decl (), "_var");
241 else
243 *os << bt->name () << "_var";
246 return 0;
250 be_visitor_field_ch::visit_predefined_type (be_predefined_type *node)
252 TAO_OutStream *os = this->ctx_->stream ();
253 be_type *bt = nullptr;
254 be_typedef *td = this->ctx_->alias ();
256 if (td != nullptr)
258 bt = td;
260 else
262 bt = node;
265 *os << bt->nested_type_name (this->ctx_->scope ()->decl ());
267 switch (node->pt ())
269 case AST_PredefinedType::PT_object:
270 case AST_PredefinedType::PT_abstract:
271 case AST_PredefinedType::PT_pseudo:
272 *os << "_var";
273 break;
274 case AST_PredefinedType::PT_value:
275 *os << " *";
276 break;
277 default:
278 break;
281 return 0;
285 be_visitor_field_ch::visit_sequence (be_sequence *node)
287 TAO_OutStream *os = this->ctx_->stream ();
288 be_type *bt = nullptr;
290 if (this->ctx_->alias ())
292 bt = this->ctx_->alias ();
294 else
296 bt = node;
299 if (!this->ctx_->alias ()
300 && node->is_child (this->ctx_->scope ()->decl ()))
302 // Put the field node into the (anonymous) sequence node, to be
303 // used later for unique name generation.
304 be_field *member_node =
305 dynamic_cast<be_field*> (this->ctx_->node ());
306 node->field_node (member_node);
308 // This was already generated in the corresponding valuetype class.
309 if (this->ctx_->state () != TAO_CodeGen::TAO_VALUETYPE_OBV_CH)
311 be_visitor_context ctx (*this->ctx_);
312 ctx.node (node);
314 // First generate the sequence declaration.
315 be_visitor_sequence_ch visitor (&ctx);
317 if (node->accept (&visitor) == -1)
319 ACE_ERROR_RETURN ((LM_ERROR,
320 "(%N:%l) be_visitor_field_ch::"
321 "visit_sequence - "
322 "codegen failed\n"),
323 -1);
327 // If we are being reused by valutype, this would get generated
328 // in the private section of the OBV_xx class, so we must
329 // generate the typedef for that case elsewhere.
330 AST_Decl::NodeType snt =
331 this->ctx_->scope ()->decl ()->node_type ();
333 if (snt != AST_Decl::NT_valuetype && snt != AST_Decl::NT_eventtype)
335 // Generate the anonymous sequence member typedef.
336 be_decl *bs = this->ctx_->scope ()->decl ();
338 TAO_INSERT_COMMENT (os);
340 *os << "typedef " << bt->nested_type_name (bs)
341 << " _" << this->ctx_->node ()->local_name ()
342 << "_seq;" << be_nl;
346 be_typedef *tdef = dynamic_cast<be_typedef*> (bt);
348 // This was a typedefed array.
349 UTL_Scope *holds_container =
350 this->ctx_->scope ()->decl ()->defined_in ();
351 AST_Decl *hc_decl = ScopeAsDecl (holds_container);
353 if (hc_decl->node_type () != AST_Decl::NT_module
354 || !tdef)
356 *os << bt->nested_type_name (this->ctx_->scope ()->decl ());
358 else
360 *os << bt->name ();
363 return 0;
367 be_visitor_field_ch::visit_map (be_map *node)
369 TAO_OutStream *os = this->ctx_->stream ();
370 be_type *bt = nullptr;
371 if (this->ctx_->alias ())
373 bt = this->ctx_->alias ();
375 else
377 bt = node;
380 if (!this->ctx_->alias ()
381 && node->is_child (this->ctx_->scope ()->decl ()))
383 // Put the field node into the (anonymous) sequence node, to be
384 // used later for unique name generation.
385 be_field *member_node =
386 dynamic_cast<be_field*> (this->ctx_->node ());
387 node->field_node (member_node);
389 // This was already generated in the corresponding valuetype class.
390 if (this->ctx_->state () != TAO_CodeGen::TAO_VALUETYPE_OBV_CH)
392 be_visitor_context ctx (*this->ctx_);
393 ctx.node (node);
395 // First generate the map declaration.
396 be_visitor_map_ch visitor (&ctx);
397 if (node->accept (&visitor) == -1)
399 ACE_ERROR_RETURN ((LM_ERROR,
400 "(%N:%l) be_visitor_field_ch::"
401 "visit_map - "
402 "codegen failed\n"),
403 -1);
407 // If we are being reused by valutype, this would get generated
408 // in the private section of the OBV_xx class, so we must
409 // generate the typedef for that case elsewhere.
410 AST_Decl::NodeType snt =
411 this->ctx_->scope ()->decl ()->node_type ();
413 if (snt != AST_Decl::NT_valuetype && snt != AST_Decl::NT_eventtype)
415 // Generate the anonymous sequence member typedef.
416 be_decl *bs = this->ctx_->scope ()->decl ();
418 TAO_INSERT_COMMENT (os);
420 *os << "typedef " << bt->nested_type_name (bs)
421 << " _" << this->ctx_->node ()->local_name ()
422 << "_map;" << be_nl;
426 be_typedef *tdef = dynamic_cast<be_typedef*> (bt);
428 UTL_Scope *holds_container =
429 this->ctx_->scope ()->decl ()->defined_in ();
430 AST_Decl *hc_decl = ScopeAsDecl (holds_container);
432 if (hc_decl->node_type () != AST_Decl::NT_module
433 || !tdef)
435 *os << bt->nested_type_name (this->ctx_->scope ()->decl ());
437 else
439 *os << bt->name ();
442 return 0;
446 be_visitor_field_ch::visit_string (be_string *node)
448 TAO_OutStream *os = this->ctx_->stream ();
450 if (node->width () == (long) sizeof (char))
452 *os << "::TAO::String_Manager";
454 else
456 *os << "::TAO::WString_Manager";
459 return 0;
463 be_visitor_field_ch::visit_structure (be_structure *node)
465 TAO_OutStream *os = this->ctx_->stream ();
466 be_type *bt = nullptr;
468 if (this->ctx_->alias ())
470 bt = this->ctx_->alias ();
472 else
474 bt = node;
477 if (!this->ctx_->alias () // not a typedef
478 && node->is_child (this->ctx_->scope ()->decl ()))
480 be_visitor_context ctx (*this->ctx_);
481 ctx.node (node);
483 be_visitor_structure_ch visitor (&ctx);
485 if (node->accept (&visitor) == -1)
487 ACE_ERROR_RETURN ((LM_ERROR,
488 "(%N:%l) be_visitor_field_ch::"
489 "visit_struct - "
490 "codegen failed\n"),
491 -1);
495 // This was a typedefed array.
496 UTL_Scope *holds_container =
497 this->ctx_->scope ()->decl ()->defined_in ();
498 AST_Decl *hc_decl = ScopeAsDecl (holds_container);
500 if (hc_decl->node_type () != AST_Decl::NT_module)
502 *os << bt->nested_type_name (this->ctx_->scope ()->decl ());
504 else
506 *os << bt->name ();
509 return 0;
513 be_visitor_field_ch::visit_structure_fwd (be_structure_fwd *node)
515 be_structure *s =
516 dynamic_cast<be_structure*> (node->full_definition ());
518 return this->visit_structure (s);
522 be_visitor_field_ch::visit_typedef (be_typedef *node)
524 this->ctx_->alias (node);
526 // Make a decision based on the primitive base type.
527 be_type *bt = node->primitive_base_type ();
529 if (!bt || (bt->accept (this) == -1))
531 ACE_ERROR_RETURN ((LM_ERROR,
532 "(%N:%l) be_visitor_field_ch::"
533 "visit_typedef - "
534 "Bad primitive type\n"),
535 -1);
538 // Reset the alias.
539 this->ctx_->alias (nullptr);
540 return 0;
544 be_visitor_field_ch::visit_union (be_union *node)
546 TAO_OutStream *os = this->ctx_->stream ();
547 be_type *bt = nullptr;
549 if (this->ctx_->alias ())
551 bt = this->ctx_->alias ();
553 else
555 bt = node;
558 if (!this->ctx_->alias ()
559 && node->is_child (this->ctx_->scope ()->decl ()))
561 be_visitor_context ctx (*this->ctx_);
562 ctx.node (node);
564 be_visitor_union_ch visitor (&ctx);
566 if (node->accept (&visitor) == -1)
568 ACE_ERROR_RETURN ((LM_ERROR,
569 "(%N:%l) be_visitor_field_ch::"
570 "visit_union - "
571 "codegen failed\n"),
572 -1);
576 *os << be_nl_2;
578 // This was a typedefed array.
579 UTL_Scope *holds_container =\
580 this->ctx_->scope ()->decl ()->defined_in ();
581 AST_Decl *hc_decl = ScopeAsDecl (holds_container);
583 if (hc_decl->node_type () != AST_Decl::NT_module)
585 *os << bt->nested_type_name (this->ctx_->scope ()->decl ());
587 else
589 *os << bt->name ();
592 return 0;
596 be_visitor_field_ch::visit_union_fwd (be_union_fwd *node)
598 be_union *u =
599 dynamic_cast<be_union*> (node->full_definition ());
601 return this->visit_union (u);
605 be_visitor_field_ch::visit_component (
606 be_component *node
609 return this->visit_interface (node);
613 be_visitor_field_ch::visit_component_fwd (
614 be_component_fwd *node
617 return this->visit_interface_fwd (node);
621 be_visitor_field_ch::visit_eventtype (
622 be_eventtype *node
625 return this->visit_valuetype (node);
629 be_visitor_field_ch::visit_eventtype_fwd (
630 be_eventtype_fwd *node
633 return this->visit_valuetype_fwd (node);