dds_visitor.cpp

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Distributed under the OpenDDS License.
00005  * See: http://www.opendds.org/license.html
00006  */
00007 
00008 #include "ast_argument.h"
00009 #include "ast_attribute.h"
00010 #include "ast_component_fwd.h"
00011 #include "ast_enum.h"
00012 #include "ast_enum_val.h"
00013 #include "ast_eventtype.h"
00014 #include "ast_eventtype_fwd.h"
00015 #include "ast_exception.h"
00016 #include "ast_factory.h"
00017 #include "ast_home.h"
00018 #include "ast_interface.h"
00019 #include "ast_module.h"
00020 #include "ast_native.h"
00021 #include "ast_operation.h"
00022 #include "ast_predefined_type.h"
00023 #include "ast_root.h"
00024 #include "ast_sequence.h"
00025 #include "ast_structure.h"
00026 #include "ast_union.h"
00027 #include "ast_valuetype.h"
00028 #include "ast_valuetype_fwd.h"
00029 #include "utl_identifier.h"
00030 #include "utl_string.h"
00031 #include "utl_exceptlist.h"
00032 #include "utl_err.h"
00033 #include "nr_extern.h"
00034 
00035 #include "dds_visitor.h"
00036 #include "metaclass_generator.h"
00037 #include "ts_generator.h"
00038 #include "marshal_generator.h"
00039 #include "keys_generator.h"
00040 #include "wireshark_generator.h"
00041 #include "itl_generator.h"
00042 #include "v8_generator.h"
00043 #include "langmap_generator.h"
00044 
00045 #include <iostream>
00046 #include <vector>
00047 #include <fstream>
00048 
00049 using namespace std;
00050 
00051 namespace {
00052 
00053   marshal_generator mar_gen_;
00054   keys_generator key_gen_;
00055   ts_generator ts_gen_;
00056   metaclass_generator mc_gen_;
00057   wireshark_generator ws_gen_;
00058   itl_generator itl_gen_;
00059   v8_generator v8_gen_;
00060   langmap_generator lm_gen_;
00061 
00062   dds_generator* generators_[] = {&mar_gen_, &key_gen_, &ts_gen_, &mc_gen_, &ws_gen_, &itl_gen_};
00063   const size_t N_MAP = sizeof(generators_) / sizeof(generators_[0]);
00064 
00065   composite_generator gen_target_(&generators_[0], &generators_[N_MAP]);
00066 
00067   template <typename T>
00068   void scope2vector(vector<T*>& v, UTL_Scope* s, AST_Decl::NodeType nt)
00069   {
00070     UTL_ScopeActiveIterator it(s, UTL_Scope::IK_decls);
00071 
00072     for (; !it.is_done(); it.next()) {
00073       AST_Decl* item = it.item();
00074 
00075       if (item->node_type() == nt) {
00076         v.push_back(T::narrow_from_decl(item));
00077       }
00078     }
00079   }
00080 
00081   bool field_check_anon(AST_Field* f, const char* ct, const char* cn)
00082   {
00083     AST_Decl::NodeType nt = f->field_type()->node_type();
00084     if (nt == AST_Decl::NT_array || nt == AST_Decl::NT_sequence) {
00085       std::cerr << "ERROR: field " << f->local_name()->get_string()
00086                 << " in " << ct << " " << cn << " has an anonymous type.\n";
00087       return false;
00088     }
00089     return true;
00090   }
00091 } // namespace
00092 
00093 dds_visitor::dds_visitor(AST_Decl* scope, bool java_ts_only)
00094   : scope_(scope), error_(false), java_ts_only_(java_ts_only)
00095 {
00096   if (be_global->v8()) {
00097     gen_target_.add_generator(&v8_gen_);
00098   }
00099   if (be_global->language_mapping() != BE_GlobalData::LANGMAP_NONE) {
00100     gen_target_.add_generator(&lm_gen_);
00101     lm_gen_.init();
00102   }
00103 }
00104 
00105 dds_visitor::~dds_visitor()
00106 {
00107 }
00108 
00109 //visit_* functions
00110 
00111 int
00112 dds_visitor::visit_root(AST_Root* node)
00113 {
00114   error_ = false;
00115 
00116   gen_target_.gen_prologue();
00117   if (this->visit_scope(node) == -1) {
00118     ACE_ERROR_RETURN((LM_ERROR,
00119                       ACE_TEXT("(%N:%l) dds_visitor::visit_root -")
00120                       ACE_TEXT(" visit_scope failed\n")), -1);
00121   }
00122   gen_target_.gen_epilogue();
00123 
00124   return (error_) ? -1 : 0;
00125 }
00126 
00127 int
00128 dds_visitor::visit_scope(UTL_Scope* node)
00129 {
00130   if (node->nmembers() > 0) {
00131     UTL_ScopeActiveIterator si(node, UTL_Scope::IK_decls);
00132     AST_Decl* d = 0;
00133 
00134     while (!si.is_done()) {
00135       d = si.item();
00136 
00137       if (d == 0) {
00138         ACE_ERROR_RETURN((LM_ERROR,
00139                           ACE_TEXT("(%N:%l) dds_visitor::visit_")
00140                           ACE_TEXT("scope - bad node in this scope\n")), -1);
00141       }
00142 
00143       if (d->node_type() == AST_Decl::NT_pre_defined) {
00144         si.next();
00145         continue;
00146       }
00147 
00148       if (d->ast_accept(this) == -1) {
00149         ACE_ERROR_RETURN((LM_ERROR,
00150                           ACE_TEXT("(%N:%l) dds_visitor::visit_")
00151                           ACE_TEXT("scope - failed to accept visitor\n")), -1);
00152       }
00153 
00154       si.next();
00155     }
00156   }
00157 
00158   return 0;
00159 }
00160 
00161 int
00162 dds_visitor::visit_module(AST_Module* node)
00163 {
00164   const char* name = node->local_name()->get_string();
00165 
00166   BE_Comment_Guard g("MODULE", name);
00167 
00168   ACE_UNUSED_ARG(g);
00169 
00170   if (this->visit_scope(node) == -1) {
00171     ACE_ERROR_RETURN((LM_ERROR,
00172                       ACE_TEXT("(%N:%l) dds_visitor::visit_module -")
00173                       ACE_TEXT(" visit_scope failed\n")), -1);
00174   }
00175 
00176   return 0;
00177 }
00178 
00179 int
00180 dds_visitor::visit_interface(AST_Interface* node)
00181 {
00182   const char* name = node->local_name()->get_string();
00183 
00184   BE_Comment_Guard g("INTERFACE", name);
00185 
00186   ACE_UNUSED_ARG(g);
00187 
00188   vector<AST_Interface*> inherits(node->n_inherits());
00189   for (int i = 0; i < node->n_inherits(); ++i) {
00190     inherits[i] = dynamic_cast<AST_Interface*>(node->inherits()[i]);
00191   }
00192 
00193   vector<AST_Interface*> inherits_flat(node->inherits_flat(),
00194                                        node->inherits_flat()
00195                                        + node->n_inherits_flat());
00196 
00197   vector<AST_Attribute*> attrs;
00198 
00199   scope2vector(attrs, node, AST_Decl::NT_attr);
00200 
00201   vector<AST_Operation*> ops;
00202 
00203   scope2vector(ops, node, AST_Decl::NT_op);
00204 
00205   if (!java_ts_only_) {
00206     error_ |= !gen_target_.gen_interf(node, node->name(), node->is_local(),
00207                                       inherits, inherits_flat, attrs, ops,
00208                                       node->repoID());
00209   }
00210 
00211   if (this->visit_scope(node) == -1) {
00212     ACE_ERROR_RETURN((LM_ERROR,
00213                       ACE_TEXT("(%N:%l) dds_visitor::visit_interface ")
00214                       ACE_TEXT("- visit_scope failed\n")), -1);
00215   }
00216 
00217   return 0;
00218 }
00219 
00220 int
00221 dds_visitor::visit_structure(AST_Structure* node)
00222 {
00223   const char* name = node->local_name()->get_string();
00224 
00225   BE_Comment_Guard g("STRUCT", name);
00226 
00227   ACE_UNUSED_ARG(g);
00228 
00229   size_t nfields = node->nfields();
00230   vector<AST_Field*> fields;
00231   fields.reserve(nfields);
00232 
00233   for (CORBA::ULong i = 0; i < nfields; ++i) {
00234     AST_Field** f;
00235     node->field(f, i);
00236     if (!field_check_anon(*f, "struct", name)) {
00237       error_ = true;
00238       return -1;
00239     }
00240     fields.push_back(*f);
00241   }
00242 
00243   if (!java_ts_only_) {
00244     error_ |= !gen_target_.gen_struct(node, node->name(), fields,
00245                                       node->size_type(), node->repoID());
00246   }
00247 
00248   if (be_global->java()) {
00249     java_ts_generator::generate(node->name());
00250   }
00251 
00252   return 0;
00253 }
00254 
00255 int
00256 dds_visitor::visit_exception(AST_Exception* node)
00257 {
00258   if (node->imported()) {
00259     return 0;
00260   }
00261 
00262   const char* name = node->local_name()->get_string();
00263 
00264   BE_Comment_Guard g("EXCEPTION", name);
00265 
00266   ACE_UNUSED_ARG(g);
00267 
00268   return 0;
00269 }
00270 
00271 int
00272 dds_visitor::visit_typedef(AST_Typedef* node)
00273 {
00274   const char* name = node->local_name()->get_string();
00275 
00276   BE_Comment_Guard g("TYPEDEF", name);
00277 
00278   ACE_UNUSED_ARG(g);
00279 
00280   if (!java_ts_only_) {
00281     error_ |= !gen_target_.gen_typedef(node, node->name(), node->base_type(),
00282                                        node->repoID());
00283   }
00284 
00285   return 0;
00286 }
00287 
00288 int
00289 dds_visitor::visit_enum(AST_Enum* node)
00290 {
00291   const char* name = node->local_name()->get_string();
00292 
00293   BE_Comment_Guard g("ENUM", name);
00294 
00295   ACE_UNUSED_ARG(g);
00296 
00297   vector<AST_EnumVal*> contents;
00298 
00299   scope2vector(contents, node, AST_Decl::NT_enum_val);
00300 
00301   if (!java_ts_only_) {
00302     error_ |= !gen_target_.gen_enum(node, node->name(), contents, node->repoID());
00303   }
00304 
00305   return 0;
00306 }
00307 
00308 int
00309 dds_visitor::visit_interface_fwd(AST_InterfaceFwd* node)
00310 {
00311   const char* name = node->local_name()->get_string();
00312   BE_Comment_Guard g("INTERFACE-FWD", name);
00313   ACE_UNUSED_ARG(g);
00314 
00315   if (!java_ts_only_) {
00316     error_ |= !gen_target_.gen_interf_fwd(node->name());
00317   }
00318 
00319   return 0;
00320 }
00321 
00322 int
00323 dds_visitor::visit_structure_fwd(AST_StructureFwd* node)
00324 {
00325   const char* name = node->local_name()->get_string();
00326   BE_Comment_Guard g("STRUCT-FWD", name);
00327 
00328   if (!java_ts_only_) {
00329     error_ |= !gen_target_.gen_struct_fwd(node->name(), node->size_type());
00330   }
00331 
00332   return 0;
00333 }
00334 
00335 int
00336 dds_visitor::visit_constant(AST_Constant* node)
00337 {
00338   const char* name = node->local_name()->get_string();
00339 
00340   BE_Comment_Guard g("CONST", name);
00341 
00342   ACE_UNUSED_ARG(g);
00343 
00344   AST_Decl* d = ScopeAsDecl(node->defined_in());
00345 
00346   bool nested = d && (d->node_type() == AST_Decl::NT_interface);
00347 
00348   if (!java_ts_only_) {
00349     error_ |= !gen_target_.gen_const(node->name(), nested, node);
00350   }
00351 
00352   return 0;
00353 }
00354 
00355 int
00356 dds_visitor::visit_native(AST_Native* node)
00357 {
00358   const char* name = node->local_name()->get_string();
00359 
00360   BE_Comment_Guard g("NATIVE", name);
00361 
00362   ACE_UNUSED_ARG(g);
00363 
00364   if (!java_ts_only_) {
00365     error_ |= !gen_target_.gen_native(node, node->name(), node->repoID());
00366   }
00367 
00368   return 0;
00369 }
00370 
00371 int
00372 dds_visitor::visit_union(AST_Union* node)
00373 {
00374   const char* name = node->local_name()->get_string();
00375 
00376   BE_Comment_Guard g("UNION", name);
00377 
00378   ACE_UNUSED_ARG(g);
00379 
00380   vector<AST_UnionBranch*> branches;
00381 
00382   size_t nfields = node->nfields();
00383 
00384   branches.reserve(nfields);
00385 
00386   for (CORBA::ULong i = 0; i < nfields; ++i) {
00387     AST_Field** f;
00388     node->field(f, i);
00389     if (!field_check_anon(*f, "union", name)) {
00390       error_ = true;
00391       return -1;
00392     }
00393 
00394     AST_UnionBranch* ub = AST_UnionBranch::narrow_from_decl(*f);
00395 
00396     if (!ub) {
00397       std::cerr << "ERROR - expected union to contain UnionBranches\n";
00398       error_ = true;
00399       return 0;
00400     }
00401 
00402     branches.push_back(ub);
00403   }
00404 
00405   if (!java_ts_only_) {
00406     error_ |= !gen_target_.gen_union(node, node->name(), branches, node->disc_type(),
00407                                      node->repoID());
00408   }
00409 
00410   return 0;
00411 }
00412 
00413 // *** All methods below here are unimplemented (or trivially implemented) ***
00414 
00415 int
00416 dds_visitor::visit_sequence(AST_Sequence*)
00417 {
00418   //sequences always appear as typedefs, see visit_typedef ()
00419   return 0;
00420 }
00421 
00422 int
00423 dds_visitor::visit_operation(AST_Operation*)
00424 {
00425   // operations are taken care of by visit_interface()
00426   return 0;
00427 }
00428 
00429 int
00430 dds_visitor::visit_field(AST_Field*)
00431 {
00432   // fields are taken care of by visit_interface() for arguments and attributes
00433   return 0;
00434 }
00435 
00436 int
00437 dds_visitor::visit_attribute(AST_Attribute*)
00438 {
00439   // attributes are taken care of by visit_interface ()
00440   return 0;
00441 }
00442 
00443 int
00444 dds_visitor::visit_array(AST_Array*)
00445 {
00446   //arrays always appear as typedefs, see visit_typedef ()
00447   return 0;
00448 }
00449 
00450 //begin IDL syntactic elements that are not currently supported
00451 
00452 int
00453 dds_visitor::visit_valuetype(AST_ValueType*)
00454 {
00455   return 0;
00456 }
00457 
00458 int
00459 dds_visitor::visit_valuetype_fwd(AST_ValueTypeFwd*)
00460 {
00461   return 0;
00462 }
00463 
00464 int
00465 dds_visitor::visit_component(AST_Component*)
00466 {
00467   return 0;
00468 }
00469 
00470 int
00471 dds_visitor::visit_component_fwd(AST_ComponentFwd*)
00472 {
00473   return 0;
00474 }
00475 
00476 int
00477 dds_visitor::visit_eventtype(AST_EventType*)
00478 {
00479   return 0;
00480 }
00481 
00482 int
00483 dds_visitor::visit_eventtype_fwd(AST_EventTypeFwd*)
00484 {
00485   return 0;
00486 }
00487 
00488 int
00489 dds_visitor::visit_home(AST_Home*)
00490 {
00491   return 0;
00492 }
00493 
00494 int
00495 dds_visitor::visit_factory(AST_Factory*)
00496 {
00497   return 0;
00498 }
00499 
00500 //no need to implement these at this level
00501 
00502 int
00503 dds_visitor::visit_predefined_type(AST_PredefinedType*)
00504 {
00505   return 0;
00506 }
00507 
00508 int
00509 dds_visitor::visit_string(AST_String*)
00510 {
00511   return 0;
00512 }
00513 
00514 int
00515 dds_visitor::visit_union_fwd(AST_UnionFwd* node)
00516 {
00517   const char* name = node->local_name()->get_string();
00518   BE_Comment_Guard g("UNION-FWD", name);
00519 
00520   if (!java_ts_only_) {
00521     error_ |= !gen_target_.gen_union_fwd(node, node->name(), node->size_type());
00522   }
00523 
00524   return 0;
00525 }
00526 
00527 int dds_visitor::visit_union_branch(AST_UnionBranch*)
00528 {
00529   return 0;
00530 }
00531 
00532 int dds_visitor::visit_union_label(AST_UnionLabel*)
00533 {
00534   return 0;
00535 }
00536 
00537 int dds_visitor::visit_enum_val(AST_EnumVal*)
00538 {
00539   return 0;
00540 }
00541 
00542 int dds_visitor::visit_expression(AST_Expression*)
00543 {
00544   return 0;
00545 }
00546 
00547 int dds_visitor::visit_type(AST_Type*)
00548 {
00549   return 0;
00550 }
00551 
00552 int dds_visitor::visit_argument(AST_Argument*)
00553 {
00554   return 0;
00555 }
00556 
00557 int dds_visitor::visit_decl(AST_Decl*)
00558 {
00559   return 0;
00560 }
00561 
00562 int dds_visitor::visit_valuebox(AST_ValueBox*)
00563 {
00564   return 0;
00565 }
00566 
00567 int
00568 dds_visitor::visit_template_module (AST_Template_Module*)
00569 {
00570   return 0;
00571 }
00572 
00573 int
00574 dds_visitor::visit_template_module_inst (AST_Template_Module_Inst*)
00575 {
00576   return 0;
00577 }
00578 
00579 int
00580 dds_visitor::visit_template_module_ref (AST_Template_Module_Ref*)
00581 {
00582   return 0;
00583 }
00584 
00585 int
00586 dds_visitor::visit_param_holder(AST_Param_Holder*)
00587 {
00588   return 0;
00589 }
00590 
00591 int dds_visitor::visit_porttype(AST_PortType*)
00592 {
00593   return 0;
00594 }
00595 
00596 int dds_visitor::visit_provides(AST_Provides*)
00597 {
00598   return 0;
00599 }
00600 
00601 int dds_visitor::visit_uses(AST_Uses*)
00602 {
00603   return 0;
00604 }
00605 
00606 int dds_visitor::visit_publishes(AST_Publishes*)
00607 {
00608   return 0;
00609 }
00610 
00611 int dds_visitor::visit_emits(AST_Emits*)
00612 {
00613   return 0;
00614 }
00615 
00616 int dds_visitor::visit_consumes(AST_Consumes*)
00617 {
00618   return 0;
00619 }
00620 
00621 int dds_visitor::visit_extended_port(AST_Extended_Port*)
00622 {
00623   return 0;
00624 }
00625 
00626 int dds_visitor::visit_mirror_port(AST_Mirror_Port*)
00627 {
00628   return 0;
00629 }
00630 
00631 int dds_visitor::visit_connector(AST_Connector*)
00632 {
00633   return 0;
00634 }
00635 
00636 int dds_visitor::visit_finder(AST_Finder*)
00637 {
00638   return 0;
00639 }

Generated on Fri Feb 12 20:05:22 2016 for OpenDDS by  doxygen 1.4.7