Go to the documentation of this file.00001
00002
00003
00004 #include "ast_enum.h"
00005 #include "ast_expression.h"
00006 #include "ast_union.h"
00007 #include "ast_union_branch.h"
00008 #include "ast_union_label.h"
00009 #include "utl_identifier.h"
00010
00011 #include "ifr_adding_visitor_union.h"
00012 #include "ifr_adding_visitor_structure.h"
00013
00014 #include "tao/AnyTypeCode/Any_Unknown_IDL_Type.h"
00015 #include "tao/CDR.h"
00016
00017 ACE_RCSID (IFR_Service,
00018 ifr_adding_visitor_union,
00019 "$Id: ifr_adding_visitor_union.cpp 83731 2008-11-13 21:54:42Z fields_t $")
00020
00021 ifr_adding_visitor_union::ifr_adding_visitor_union (AST_Decl *scope)
00022 : ifr_adding_visitor (scope)
00023 {
00024 }
00025
00026 ifr_adding_visitor_union::~ifr_adding_visitor_union (void)
00027 {
00028 }
00029
00030
00031 int
00032 ifr_adding_visitor_union::visit_scope (UTL_Scope *node)
00033 {
00034
00035
00036 if (node->scope_node_type () != AST_Decl::NT_union)
00037 {
00038 return ifr_adding_visitor::visit_scope (node);
00039 }
00040
00041 AST_Union *u = AST_Union::narrow_from_scope (node);
00042
00043 CORBA::ULong nfields = static_cast<CORBA::ULong> (u->nfields ());
00044
00045 this->members_.length (nfields);
00046
00047 AST_Field **f = 0;
00048
00049
00050 CORBA::ULong index = 0;
00051
00052 try
00053 {
00054
00055 for (CORBA::ULong i = 0; i < nfields; ++i)
00056 {
00057 if (u->field (f, i) != 0)
00058 {
00059 ACE_ERROR_RETURN ((
00060 LM_ERROR,
00061 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::")
00062 ACE_TEXT ("visit_scope -")
00063 ACE_TEXT (" field node access failed\n")
00064 ),
00065 -1
00066 );
00067 }
00068
00069 AST_Type *ft = (*f)->field_type ();
00070
00071 bool defined_here = ft->is_child (this->scope_);
00072
00073
00074
00075 if (defined_here)
00076 {
00077 if (ft->node_type () == AST_Decl::NT_union)
00078 {
00079
00080
00081
00082 ifr_adding_visitor_union visitor (ft);
00083
00084 if (ft->ast_accept (&visitor) == -1)
00085 {
00086 ACE_ERROR_RETURN ((
00087 LM_ERROR,
00088 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::")
00089 ACE_TEXT ("visit_scope -")
00090 ACE_TEXT (" failed to accept visitor\n")),
00091 -1);
00092 }
00093
00094 this->ir_current_ =
00095 CORBA::IDLType::_duplicate (visitor.ir_current ());
00096 }
00097 else
00098 {
00099 if (ft->ast_accept (this) == -1)
00100 {
00101 ACE_ERROR_RETURN ((
00102 LM_ERROR,
00103 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::")
00104 ACE_TEXT ("visit_scope -")
00105 ACE_TEXT (" failed to accept visitor\n")),
00106 -1);
00107 }
00108 }
00109 }
00110 else
00111 {
00112
00113 this->get_referenced_type (ft);
00114 }
00115
00116
00117
00118 AST_UnionLabel *case_label = 0;
00119 AST_UnionBranch *ub = AST_UnionBranch::narrow_from_decl (*f);
00120 unsigned long len = ub->label_list_length ();
00121
00122
00123
00124
00125
00126 if (len > 1)
00127 {
00128 this->members_.length (this->members_.length () + len - 1);
00129 }
00130
00131 for (unsigned long j = 0; j < len; ++j)
00132 {
00133 case_label = ub->label (j);
00134
00135
00136 if (case_label->label_kind () == AST_UnionLabel::UL_label)
00137 {
00138 AST_Expression::AST_ExprValue *ev =
00139 case_label->label_val ()->ev ();
00140
00141
00142
00143 if (u->disc_type ()->node_type () == AST_Decl::NT_enum)
00144 {
00145 TAO_OutputCDR cdr;
00146 cdr.write_ulong (ev->u.ulval);
00147 TAO_InputCDR in_cdr (cdr);
00148 TAO::Unknown_IDL_Type *unk = 0;
00149 ACE_NEW_RETURN (unk,
00150 TAO::Unknown_IDL_Type (
00151 this->disc_tc_.in (),
00152 in_cdr),
00153 -1);
00154 this->members_[index].label.replace (unk);
00155 }
00156 else
00157 {
00158 this->load_any (ev,
00159 this->members_[index].label);
00160 }
00161 }
00162 else
00163 {
00164 this->members_[index].label <<= CORBA::Any::from_octet (0);
00165 }
00166
00167 this->members_[index].name =
00168 CORBA::string_dup ((*f)->local_name ()->get_string ());
00169
00170
00171
00172 this->members_[index].type =
00173 CORBA::TypeCode::_duplicate (CORBA::_tc_void);
00174
00175 this->members_[index++].type_def =
00176 CORBA::IDLType::_duplicate (this->ir_current_.in ());
00177 }
00178 }
00179 }
00180 catch (const CORBA::Exception& ex)
00181 {
00182 ex._tao_print_exception (
00183 ACE_TEXT (
00184 "ifr_adding_visitor_union::visit_scope"));
00185
00186 return -1;
00187 }
00188
00189 return 0;
00190 }
00191
00192 int
00193 ifr_adding_visitor_union::visit_structure (AST_Structure *node)
00194 {
00195 try
00196 {
00197
00198 CORBA::Contained_var prev_def =
00199 be_global->repository ()->lookup_id (node->repoID ());
00200
00201
00202 if (CORBA::is_nil (prev_def.in ()))
00203 {
00204 ifr_adding_visitor_structure visitor (node);
00205 int retval = visitor.visit_structure (node);
00206
00207 if (retval == 0)
00208 {
00209
00210 this->ir_current_ =
00211 CORBA::IDLType::_duplicate (visitor.ir_current ());
00212 }
00213
00214 return retval;
00215 }
00216 else
00217 {
00218
00219
00220
00221
00222 if (!node->ifr_added ())
00223 {
00224 prev_def->destroy ();
00225
00226
00227 return this->visit_structure (node);
00228 }
00229
00230 this->ir_current_ =
00231 CORBA::IDLType::_narrow (prev_def.in ());
00232 }
00233 }
00234 catch (const CORBA::Exception& ex)
00235 {
00236 ex._tao_print_exception (
00237 ACE_TEXT (
00238 "ifr_adding_visitor_union::visit_structure"));
00239
00240 return -1;
00241 }
00242
00243 return 0;
00244 }
00245
00246 int
00247 ifr_adding_visitor_union::visit_enum (AST_Enum *node)
00248 {
00249 try
00250 {
00251
00252 CORBA::Contained_var prev_def =
00253 be_global->repository ()->lookup_id (node->repoID ());
00254
00255
00256 if (CORBA::is_nil (prev_def.in ()))
00257 {
00258 CORBA::ULong member_count =
00259 static_cast<CORBA::ULong> (node->member_count ());
00260
00261 CORBA::EnumMemberSeq members (member_count);
00262 members.length (member_count);
00263
00264 UTL_ScopedName *member_name = 0;
00265
00266
00267 for (CORBA::ULong i = 0; i < member_count; ++i)
00268 {
00269 member_name = node->value_to_name (i);
00270
00271 members[i] =
00272 CORBA::string_dup (
00273 member_name->last_component ()->get_string ());
00274 }
00275
00276 this->ir_current_ =
00277 be_global->repository ()->create_enum (
00278 node->repoID (),
00279 node->local_name ()->get_string (),
00280 node->version (),
00281 members
00282 );
00283
00284 node->ifr_added (true);
00285 }
00286 else
00287 {
00288
00289
00290
00291
00292 if (!node->ifr_added ())
00293 {
00294 prev_def->destroy ();
00295
00296
00297 return this->visit_enum (node);
00298 }
00299
00300 this->ir_current_ =
00301 CORBA::IDLType::_narrow (prev_def.in ());
00302 }
00303 }
00304 catch (const CORBA::Exception& ex)
00305 {
00306 ex._tao_print_exception (
00307 ACE_TEXT (
00308 "ifr_adding_visitor_union::visit_enum"));
00309
00310 return -1;
00311 }
00312
00313 return 0;
00314 }
00315
00316 int
00317 ifr_adding_visitor_union::visit_union (AST_Union *node)
00318 {
00319 try
00320 {
00321
00322 if (node->disc_type ()->ast_accept (this) == -1)
00323 {
00324 ACE_ERROR_RETURN ((
00325 LM_ERROR,
00326 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::")
00327 ACE_TEXT ("visit_union -")
00328 ACE_TEXT (" visit of discriminator failed\n")),
00329 -1);
00330 }
00331
00332 this->disc_tc_ = this->ir_current_->type ();
00333
00334 CORBA::UnionDef_var union_def;
00335 CORBA::Contained_var prev_def =
00336 be_global->repository ()->lookup_id (node->repoID ());
00337
00338 if (CORBA::is_nil (prev_def.in ()))
00339 {
00340 CORBA::UnionMemberSeq dummyMembers;
00341 dummyMembers.length (0);
00342 CORBA::Container_ptr current_scope = CORBA::Container::_nil ();
00343
00344 if (be_global->ifr_scopes ().top (current_scope) != 0)
00345 {
00346 ACE_ERROR_RETURN ((
00347 LM_ERROR,
00348 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::")
00349 ACE_TEXT ("visit_union -")
00350 ACE_TEXT (" scope stack is empty\n")),
00351 -1);
00352 }
00353
00354
00355 union_def =
00356 current_scope->create_union (
00357 node->repoID (),
00358 node->local_name ()->get_string (),
00359 node->version (),
00360 this->ir_current_.in (),
00361 dummyMembers
00362 );
00363
00364 if (be_global->ifr_scopes ().push (union_def.in ()) != 0)
00365 {
00366 ACE_ERROR_RETURN ((
00367 LM_ERROR,
00368 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::")
00369 ACE_TEXT ("visit_union -")
00370 ACE_TEXT (" scope push failed\n")
00371 ),
00372 -1
00373 );
00374 }
00375
00376
00377 if (this->add_members (node, union_def.in ()) == -1)
00378 {
00379 ACE_ERROR_RETURN ((
00380 LM_ERROR,
00381 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::visit_union -")
00382 ACE_TEXT (" visit_scope failed\n")),
00383 -1);
00384 }
00385
00386 this->ir_current_ = CORBA::IDLType::_narrow (union_def.in ());
00387
00388 CORBA::Container_ptr used_scope =
00389 CORBA::Container::_nil ();
00390
00391
00392 if (be_global->ifr_scopes ().pop (used_scope) != 0)
00393 {
00394 ACE_ERROR_RETURN ((
00395 LM_ERROR,
00396 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::")
00397 ACE_TEXT ("visit_union -")
00398 ACE_TEXT (" scope pop failed\n")
00399 ),
00400 -1
00401 );
00402 }
00403 }
00404 else
00405 {
00406
00407
00408
00409
00410 union_def = CORBA::UnionDef::_narrow (prev_def.in ());
00411 union_def->discriminator_type_def (this->ir_current_.in ());
00412
00413 if (be_global->ifr_scopes ().push (union_def.in ()) != 0)
00414 {
00415 ACE_ERROR_RETURN ((
00416 LM_ERROR,
00417 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::")
00418 ACE_TEXT ("visit_union -")
00419 ACE_TEXT (" scope push failed\n")
00420 ),
00421 -1
00422 );
00423 }
00424
00425 if (this->add_members (node, union_def.in ()) == -1)
00426 {
00427 ACE_ERROR_RETURN ((
00428 LM_ERROR,
00429 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::")
00430 ACE_TEXT ("visit_union -")
00431 ACE_TEXT (" visit_scope failed\n")),
00432 -1);
00433 }
00434
00435 this->ir_current_ = CORBA::IDLType::_narrow (prev_def.in ());
00436
00437 CORBA::Container_ptr used_scope =
00438 CORBA::Container::_nil ();
00439
00440
00441 if (be_global->ifr_scopes ().pop (used_scope) != 0)
00442 {
00443 ACE_ERROR_RETURN ((
00444 LM_ERROR,
00445 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::")
00446 ACE_TEXT ("visit_union -")
00447 ACE_TEXT (" scope pop failed\n")
00448 ),
00449 -1
00450 );
00451 }
00452 }
00453 }
00454 catch (const CORBA::Exception& ex)
00455 {
00456 ex._tao_print_exception (
00457 ACE_TEXT (
00458 "ifr_adding_visitor_union::visit_union"));
00459
00460 return -1;
00461 }
00462
00463 return 0;
00464 }
00465
00466 CORBA::IDLType_ptr
00467 ifr_adding_visitor_union::ir_current (void) const
00468 {
00469 return this->ir_current_.in ();
00470 }
00471
00472 int
00473 ifr_adding_visitor_union::add_members (AST_Union *node,
00474 CORBA::UnionDef_ptr union_def)
00475 {
00476 if (this->visit_scope (node) == -1)
00477 {
00478 ACE_ERROR_RETURN ((
00479 LM_ERROR,
00480 ACE_TEXT ("(%N:%l) ifr_adding_visitor_union::visit_union -")
00481 ACE_TEXT (" visit_scope failed\n")),
00482 -1);
00483 }
00484
00485
00486 this->ir_current_= CORBA::UnionDef::_duplicate (union_def);
00487 union_def->members (this->members_);
00488
00489 node->ifr_added (true);
00490 return 0;
00491 }