Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / TAO_IDL / be / be_visitor_home / home_svs.cpp
blob9f6f2a969bb975c4070420974598abf6dc935269
2 //=============================================================================
3 /**
4 * @file home_svs.cpp
6 * Visitor generating code for Homes in the servant source.
8 * @author Jeff Parsons
9 */
10 //=============================================================================
12 #include "home.h"
14 be_visitor_home_svs::be_visitor_home_svs (be_visitor_context *ctx)
15 : be_visitor_scope (ctx),
16 node_ (0),
17 comp_ (0),
18 os_ (*ctx->stream ()),
19 export_macro_ (be_global->svnt_export_macro ()),
20 for_finder_ (false)
22 /// All existing CIAO examples set the servant export values in the CIDL
23 /// compiler to equal the IDL compiler's skel export values. Below is a
24 /// partial effort to decouple them, should be completely decoupled
25 /// sometime. See comment in codegen.cpp, line 1173.
26 if (export_macro_ == "")
28 export_macro_ = be_global->skel_export_macro ();
32 be_visitor_home_svs::~be_visitor_home_svs (void)
36 int
37 be_visitor_home_svs::visit_home (be_home *node)
39 if (node->imported ())
41 return 0;
44 node_ = node;
45 comp_ = node_->managed_component ();
47 /// Use 'CIAO_' + component's flat name.
48 os_ << be_nl_2
49 << "namespace CIAO_" << comp_->flat_name () << "_Impl" << be_nl
50 << "{" << be_idt;
52 if (this->gen_servant_class () == -1)
54 ACE_ERROR_RETURN ((LM_ERROR,
55 ACE_TEXT ("be_visitor_home_svs::")
56 ACE_TEXT ("visit_home - ")
57 ACE_TEXT ("gen_servant_class() failed\n")),
58 -1);
61 this->gen_entrypoint ();
63 os_ << be_uidt_nl
64 << "}";
66 return 0;
69 int
70 be_visitor_home_svs::visit_operation (be_operation *node)
72 be_visitor_operation_svs v (this->ctx_);
73 v.scope (node_);
74 return v.visit_operation (node);
77 int
78 be_visitor_home_svs::visit_attribute (be_attribute *node)
80 be_visitor_attribute v (this->ctx_);
81 v.op_scope (node_);
82 return v.visit_attribute (node);
85 int
86 be_visitor_home_svs::visit_argument (be_argument *node)
88 os_ << node->local_name ();
90 if (! this->last_node (node))
92 os_ << "," << be_nl;
95 return 0;
98 int
99 be_visitor_home_svs::visit_factory (be_factory *node)
101 // An inherited factory/finder needs to return the managed
102 // component of the home where it is defined.
103 be_home *h =
104 dynamic_cast<be_home*> (node->defined_in ());
106 AST_Component *c = h->managed_component ();
108 os_ << be_nl_2
109 << "::" << c->name () << "_ptr" << be_nl
110 << node_->original_local_name ()->get_string ()
111 << "_Servant::" << node->local_name ();
113 be_visitor_operation_arglist al_visitor (this->ctx_);
114 al_visitor.unused (this->for_finder_);
116 if (al_visitor.visit_factory (node) == -1)
118 ACE_ERROR_RETURN ((LM_ERROR,
119 "be_visitor_factory_svs::"
120 "visit_factory - "
121 "codegen for argument list failed\n"),
122 -1);
125 os_ << be_nl
126 << "{" << be_idt_nl;
128 if (this->for_finder_)
130 os_ << "throw ::CORBA::NO_IMPLEMENT (CORBA::OMGVMCID | 8,"
131 << be_nl
132 << " CORBA::COMPLETED_NO);";
134 else
136 ACE_CString comp_sname_str (
137 ScopeAsDecl (comp_->defined_in ())->full_name ());
138 const char *comp_sname = comp_sname_str.c_str ();
139 const char *comp_lname = comp_->local_name ()->get_string ();
140 const char *global = (comp_sname_str == "" ? "" : "::");
142 ACE_CString sname_str (ScopeAsDecl (node_->defined_in ())->full_name ());
144 os_ << sname_str << global << "CCM_" << node_->original_local_name ()
145 << "_var executor = " << be_idt_nl
146 << sname_str << global << "CCM_" << node_->original_local_name ()
147 << "::_duplicate (this->executor_.in ());" << be_uidt
148 << be_nl_2;
150 os_ << "if ( ::CORBA::is_nil (executor.in ()))" << be_idt_nl
151 << "{"<< be_idt_nl
152 << "throw ::CORBA::INV_OBJREF ();" << be_uidt_nl
153 << "}" << be_uidt << be_nl_2;
155 os_ << "::Components::EnterpriseComponent_var _ciao_ec ="
156 << be_idt_nl
157 << "executor->" << node->local_name () << " (";
159 if (node->argument_count () > 0)
161 os_ << be_idt_nl;
163 if (this->visit_scope (node) != 0)
165 ACE_ERROR_RETURN ((LM_ERROR,
166 "be_visitor_factory_svs::"
167 "visit_factory - "
168 "codegen for scope failed\n"),
169 -1);
172 os_ << be_uidt;
175 os_ << ");" << be_uidt << be_nl_2
176 << global << comp_sname << "::CCM_" << comp_lname
177 << "_var _ciao_comp =" << be_idt_nl
178 << global << comp_sname << "::CCM_" << comp_lname
179 << "::_narrow (_ciao_ec.in ());" << be_uidt << be_nl_2
180 << "if ( ::CORBA::is_nil (_ciao_ec.in ()))" << be_idt_nl
181 << "{" << be_idt_nl
182 << "throw ::Components::CreateFailure ();" << be_uidt_nl
183 << "}" << be_uidt << be_nl_2
185 << "return this->_ciao_activate_component "
186 << "(_ciao_comp.in ());";
189 os_ << be_uidt_nl
190 << "}";
192 // In case it was set for the call above.
193 this->for_finder_ = false;
195 return 0;
199 be_visitor_home_svs::visit_finder (be_finder *node)
201 this->for_finder_ = true;
202 return this->visit_factory (node);
206 be_visitor_home_svs::gen_servant_class (void)
208 AST_Decl *scope = ScopeAsDecl (node_->defined_in ());
209 ACE_CString sname_str (scope->full_name ());
210 const char *sname = sname_str.c_str ();
212 // Avoid '_cxx_' prefix.
213 const char *lname =
214 node_->original_local_name ()->get_string ();
216 const char *clname = comp_->local_name ()->get_string ();
217 const char *global = (sname_str == "" ? "" : "::");
219 os_ << be_nl
220 << lname << "_Servant::"
221 << lname << "_Servant (" << be_idt << be_idt_nl
222 << global << sname << "::CCM_" << lname << "_ptr exe," << be_nl
223 << "const char * ins_name," << be_nl
224 << "::CIAO::" << be_global->ciao_container_type ()
225 << "_Container_ptr c)" << be_uidt_nl
226 << ": ::CIAO::Home_Servant_Impl_Base ()," << be_idt_nl
227 << "::CIAO::"
228 << "Home_Servant_Impl<" << be_idt_nl
229 << "::" << node_->full_skel_name () << "," << be_nl
230 << global << sname << "::CCM_" << lname << "," << be_nl
231 << clname << "_Servant," << be_nl
232 << "::CIAO::" << be_global->ciao_container_type () << "_Container> (exe, c, ins_name)"
233 << be_uidt << be_uidt << be_uidt_nl
234 << "{" << be_nl
235 << "}";
237 os_ << be_nl_2
238 << lname << "_Servant::~" << lname << "_Servant (void)"
239 << be_nl
240 << "{" << be_nl
241 << "}";
243 if (this->node_->has_rw_attributes ())
245 os_ << be_nl_2
246 << "void" << be_nl
247 << lname << "_Servant::set_attributes (" << be_idt_nl
248 << "const ::Components::ConfigValues & descr)"
249 << be_uidt_nl
250 << "{" << be_idt_nl;
252 os_ << "for ( ::CORBA::ULong i = 0; i < descr.length (); ++i)"
253 << be_idt_nl
254 << "{" << be_idt_nl
255 << "const char * descr_name = descr[i]->name ();"
256 << be_nl
257 << "::CORBA::Any & descr_value = descr[i]->value ();";
259 be_visitor_home_attr_set as_visitor (this->ctx_);
261 if (as_visitor.visit_home (node_) == -1)
263 ACE_ERROR_RETURN ((LM_ERROR,
264 "home_svs::"
265 "gen_servant_class - "
266 "attr init visitor failed\n"),
267 -1);
270 os_ << be_uidt_nl
271 << "}" << be_uidt << be_uidt_nl
272 << "}";
275 AST_Type *pk = node_->primary_key ();
277 if (pk != 0)
279 os_ << be_nl_2
280 << "::" << comp_->name () << "_ptr" << be_nl
281 << lname << "_Servant::create (" << be_idt_nl
282 << "::" << pk->name () << " * /* key */)" << be_uidt_nl
283 << "{" << be_idt_nl
284 << "throw ::CORBA::NO_IMPLEMENT (CORBA::OMGVMCID | 8,"
285 << be_nl
286 << " CORBA::COMPLETED_NO);"
287 << be_uidt_nl
288 << "}";
290 if (!be_global->gen_lwccm ())
292 os_ << be_nl_2
293 << "::" << comp_->name () << "_ptr" << be_nl
294 << lname << "_Servant::find_by_primary_key (" << be_idt_nl
295 << "::" << pk->name () << " * /* key */)" << be_uidt_nl
296 << "{" << be_idt_nl
297 << "throw ::CORBA::NO_IMPLEMENT (CORBA::OMGVMCID | 8,"
298 << be_nl
299 << " CORBA::COMPLETED_NO);"
300 << be_uidt_nl
301 << "}";
304 os_ << be_nl_2
305 << "void" << be_nl
306 << lname << "_Servant::remove (" << be_idt_nl
307 << "::" << pk->name () << " * /* key */)" << be_uidt_nl
308 << "{" << be_idt_nl
309 << "throw ::CORBA::NO_IMPLEMENT (CORBA::OMGVMCID | 8,"
310 << be_nl
311 << " CORBA::COMPLETED_NO);"
312 << be_uidt_nl
313 << "}";
315 if (!be_global->gen_lwccm ())
317 os_ << be_nl_2
318 << "::" << pk->name () << " *" << be_nl
319 << lname << "_Servant::get_primary_key (" << be_idt_nl
320 << "::" << comp_->name () << "_ptr /* comp */)" << be_uidt_nl
321 << "{" << be_idt_nl
322 << "throw ::CORBA::NO_IMPLEMENT (CORBA::OMGVMCID | 8,"
323 << be_nl
324 << " CORBA::COMPLETED_NO);"
325 << be_uidt_nl
326 << "}";
330 be_home *h = node_;
332 while (h != 0)
334 if (this->visit_scope (h) != 0)
336 ACE_ERROR_RETURN ((LM_ERROR,
337 ACE_TEXT ("be_visitor_home_svs::")
338 ACE_TEXT ("gen_servant_class - ")
339 ACE_TEXT ("codegen for scope failed\n")),
340 -1);
343 for (long i = 0; i < h->n_inherits (); ++i)
345 // A closure of all the supported interfaces is stored
346 // in the base class 'pd_inherits_flat' member.
347 be_interface *bi =
348 dynamic_cast<be_interface*> (h->inherits ()[i]);
350 bi->get_insert_queue ().reset ();
351 bi->get_del_queue ().reset ();
352 bi->get_insert_queue ().enqueue_tail (bi);
354 Home_Op_Attr_Generator op_attr_gen (this);
356 int status =
357 bi->traverse_inheritance_graph (op_attr_gen,
358 &os_,
359 false,
360 false);
362 if (status == -1)
364 ACE_ERROR_RETURN ((LM_ERROR,
365 ACE_TEXT ("be_visitor_home_svs::")
366 ACE_TEXT ("gen_servant_class - ")
367 ACE_TEXT ("traverse_inheritance_graph() ")
368 ACE_TEXT ("failed for %s\n"),
369 bi->full_name ()),
370 -1);
375 h = dynamic_cast<be_home*> (h->base_home ());
378 return 0;
381 void
382 be_visitor_home_svs::gen_entrypoint (void)
384 ACE_CString sname_str (
385 ScopeAsDecl (node_->defined_in ())->full_name ());
386 const char *sname = sname_str.c_str ();
388 // Avoid _cxx_ prefix.
389 const char *lname =
390 node_->original_local_name ()->get_string ();
392 const char *global = (sname_str == "" ? "" : "::");
394 os_ << be_nl_2
395 << "extern \"C\" " << export_macro_.c_str ()
396 << " ::PortableServer::Servant" << be_nl
397 << "create_" << node_->flat_name ()
398 << "_Servant (" << be_idt_nl
399 << "::Components::HomeExecutorBase_ptr p," << be_nl
400 << "::CIAO::" << be_global->ciao_container_type ()
401 << "_Container_ptr c," << be_nl
402 << "const char * ins_name)" << be_uidt_nl
403 << "{" << be_idt_nl
404 << "::PortableServer::Servant retval = 0;" << be_nl
405 << global << sname << "::CCM_" << lname
406 << "_var x =" << be_idt_nl
407 << global << sname << "::CCM_" << lname
408 << "::_narrow (p);" << be_uidt_nl << be_nl
409 << "if (! ::CORBA::is_nil (x.in ()))" << be_idt_nl
410 << "{" << be_idt_nl
411 << "ACE_NEW_NORETURN (retval," << be_nl
412 << " " << lname << "_Servant (" << be_idt_nl
413 << " x.in ()," << be_nl
414 << " ins_name," << be_nl
415 << " c));" << be_uidt << be_uidt_nl
416 << "}" << be_uidt_nl << be_nl
417 << "return retval;" << be_uidt_nl
418 << "}";
421 //=======================================================
423 Home_Op_Attr_Generator::Home_Op_Attr_Generator (
424 be_visitor_scope * visitor)
425 : visitor_ (visitor)
430 Home_Op_Attr_Generator::emit (be_interface * /* derived_interface */,
431 TAO_OutStream * /* os */,
432 be_interface * base_interface)
434 return visitor_->visit_scope (base_interface);
437 // ==========================================================
439 be_visitor_home_attr_set::be_visitor_home_attr_set (
440 be_visitor_context *ctx)
441 : be_visitor_decl (ctx)
445 be_visitor_home_attr_set::~be_visitor_home_attr_set (void)
450 be_visitor_home_attr_set::visit_home (be_home *node)
452 if (node == 0)
454 return 0;
457 for (UTL_ScopeActiveIterator i (node, UTL_Scope::IK_decls);
458 !i.is_done ();
459 i.next ())
461 be_decl *d = dynamic_cast<be_decl*> (i.item ());
463 if (d->accept (this) == -1)
465 ACE_ERROR_RETURN ((LM_ERROR,
466 ACE_TEXT ("be_visitor_home_attr_set")
467 ACE_TEXT ("::visit_home - ")
468 ACE_TEXT ("accept () failed\n")),
469 -1);
473 be_home *h = dynamic_cast<be_home*> (node->base_home ());
475 return this->visit_home (h);
479 be_visitor_home_attr_set::visit_attribute (be_attribute *node)
481 be_visitor_attribute_ccm_init v (this->ctx_);
483 if (v.visit_attribute (node) == -1)
485 ACE_ERROR_RETURN ((LM_ERROR,
486 ACE_TEXT ("be_visitor_home_attr_set")
487 ACE_TEXT ("::visit_attribute - ")
488 ACE_TEXT ("ccm attr init visitor ")
489 ACE_TEXT ("failed\n")),
490 -1);
493 return 0;