typecode_defn.cpp

来自「这是广泛使用的通信开源项目,对于大容量,高并发的通讯要求完全能够胜任,他广泛可用」· C++ 代码 · 共 2,059 行 · 第 1/5 页

CPP
2,059
字号
//
// typecode_defn.cpp,v 1.58 2003/12/09 19:49:41 parsons Exp
//

// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    typecode_defn.cpp
//
// = DESCRIPTION
//    Visitor generating code for TypeCode definitions for types.
//
// = AUTHOR
//    Aniruddha Gokhale
//
// ============================================================================

ACE_RCSID (be_visitor_typecode,
           typecode_defn,
           "typecode_defn.cpp,v 1.58 2003/12/09 19:49:41 parsons Exp")

// This is an implementation of C++ "scoped lock" idiom in order to
// avoid repetitive code.
class Scoped_Compute_Queue_Guard
{
public:
  Scoped_Compute_Queue_Guard (be_visitor_typecode_defn* );
  ~Scoped_Compute_Queue_Guard (void);

private:
  be_visitor_typecode_defn* customer_;
};

Scoped_Compute_Queue_Guard::Scoped_Compute_Queue_Guard (
    be_visitor_typecode_defn* customer
  )
  :customer_ (customer)
{
  if (customer_ != 0)
    {
      // Reset the compute queue to set the stage for computing our
      // encapsulation length.
      customer_->queue_reset (customer_->compute_queue_);
    }
}

Scoped_Compute_Queue_Guard::~Scoped_Compute_Queue_Guard (void)
{
  if (customer_ != 0)
    {
      // Reset the compute queue since we must not affect computation of other
      // nodes.
      customer_->queue_reset (customer_->compute_queue_);
    }
}

// ******************************************************
// TypeCode Definitions
// ******************************************************

be_visitor_typecode_defn::be_visitor_typecode_defn (be_visitor_context *ctx)
    : be_visitor_scope (ctx),
      computed_tc_size_ (0),
      computed_encap_len_ (0),
      computed_scope_encap_len_ (0),
      tc_offset_ (0),
      index_ (-1)

{
}

be_visitor_typecode_defn::~be_visitor_typecode_defn (void)
{
  this->queue_reset (this->tc_queue_);
  this->queue_reset (this->compute_queue_);
}

int
be_visitor_typecode_defn::visit_members (AST_Structure *node)
{
  this->elem_number_ = 0;
  AST_Field **member_ptr = 0;
  size_t count = node->nfields ();
  be_decl *enclosing = be_decl::narrow_from_decl (node);

  for (size_t i = 0; i < count; ++i)
    {
      node->field (member_ptr, i);

      be_decl *bd = be_decl::narrow_from_decl (*member_ptr);

      // Set the node to be visited.
      this->ctx_->node (bd);

      // Reset this with every iteration - it might be changed
      // when it comes around again.
      this->ctx_->scope (enclosing);

      this->elem_number_++;

      // Do any pre processing using the next item info.
      if (this->pre_process (bd) == -1)
        {
          ACE_ERROR_RETURN ((
              LM_ERROR,
              "(%N:%l) be_visitor_typecode_defn::visit_members - "
              "pre processing failed\n"
            ),
            -1
          );
        }

      // Send the visitor.
      if (bd == 0 || bd->accept (this) == -1)
        {
          ACE_ERROR_RETURN ((
              LM_ERROR,
              "(%N:%l) be_visitor_typecode_defn::visit_members - "
              "codegen for scope failed\n"
            ),
            -1
          );

        }

      // Do any post processing using this item info.
      if (this->post_process (bd) == -1)
        {
          ACE_ERROR_RETURN ((
              LM_ERROR,
              "(%N:%l) be_visitor_typecode_defn::visit_members - "
              "post processing failed\n"
            ),
            -1
          );
        }
    }

  return 0;
}

int
be_visitor_typecode_defn::visit_members (be_valuetype *node)
{
  this->elem_number_ = 0;

  for (UTL_ScopeActiveIterator si (node, UTL_Scope::IK_decls);
       !si.is_done ();
       si.next())
    {
      AST_Decl *d = si.item ();

      if (!d)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_typecode_defn::visit_members - "
                             "bad node in this scope\n"),
                            -1);
        }

      AST_Field *field = AST_Field::narrow_from_decl (d);

      if (!field)
        {
          continue;
        }

      be_decl *bd = be_decl::narrow_from_decl (d);

      // Set the scope node as "node" in which the code is being
      // generated so that elements in the node's scope can use it
      // for code generation
      this->ctx_->scope (node->decl ());

      // Set the node to be visited.
      this->ctx_->node (bd);
      this->elem_number_++;

      // Do any pre processing using the next item info.
      if (this->pre_process (bd) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_typecode_defn::"
                             "visit_members - "
                             "pre processing failed\n"
                             ),
                            -1);
        }

      // Send the visitor.
      if (bd == 0 || bd->accept (this) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_typecode_defn::"
                             "visit_members - "
                             "codegen for scope failed\n"
                             ), -1);
        }

      // Do any post processing using this item info.
      if (this->post_process (bd) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_typecode_defn::"
                             "visit_members - "
                             "post processing failed\n"
                             ), -1);
        }
    }

  return 0;
}


// The following needs to be done to deal until the MSVC compiler's broken
// handling of namespaces is fixed (hopefully forthcoming in version 7).
int
be_visitor_typecode_defn::gen_nested_namespace_begin (be_module *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  char *item_name = 0;

  for (UTL_IdListActiveIterator i (node->name ()); !i.is_done (); i.next ())
    {
      item_name = i.item ()->get_string ();

      if (ACE_OS::strcmp (item_name, "") != 0)
        {
          // Leave the outermost root scope.
          *os << "namespace " << item_name << be_nl
              << "{" << be_idt_nl;
        }
    }

  return 0;
}

// The following needs to be done to deal until the MSVC compiler's broken
// handling of namespaces is fixed (hopefully forthcoming in version 7).
int
be_visitor_typecode_defn::gen_nested_namespace_end (be_module *node)
{
  TAO_OutStream *os = this->ctx_->stream ();

  for (UTL_IdListActiveIterator i (node->name ()); !i.is_done (); i.next ())
    {
      if (ACE_OS::strcmp (i.item ()->get_string (), "") != 0)
        {
          // Leave the outermost root scope.
          *os << be_uidt_nl << "}";
        }
    }

  return 0;
}

// The visit methods will be called for the top-level node whose typecode is
// being generated.

int
be_visitor_typecode_defn::visit_type (be_type *node)
{
  TAO_OutStream *os = this->ctx_->stream ();

  // reset the queue
  this->queue_reset (this->tc_queue_);
  this->tc_offset_ = 0;

  // Insert node into tc_queue_ in case the node is involved in
  // some form of recursion.
  if (this->queue_insert (this->tc_queue_, node, this->tc_offset_) == 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_typecode_defn::"
                         "visit_type - "
                         "queue insert failed\n"),
                        -1);
    }

  *os << be_nl << be_nl << "// TAO_IDL - Generated from" << be_nl
      << "// " << __FILE__ << ":" << __LINE__ << be_nl << be_nl;

  // Generate the typecode information here
  *os << "static const CORBA::Long _oc_";

  // Flat name generation.
  *os << node->flat_name ();

  *os << "[] =" << be_nl;
  *os << "{" << be_idt_nl;

  // Add the sizeof the enum tk_* and the encap length that we do not put into
  // this array but which will exist in the CDR buffer.

  this->tc_offset_ = 4 + 4;

  // Note that we just need the parameters here and hence we generate the
  // encapsulation for the parameters.
  this->ctx_->sub_state (TAO_CodeGen::TAO_TC_DEFN_ENCAPSULATION);

  if (node->accept (this) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_typecode_defn::"
                         "visit_type - "
                         "codegen for typecode encapsulation failed\n"),
                        -1);
    }

  *os << be_uidt << "};" << be_nl << be_nl;

  // Type code definition.
  *os << "static CORBA::TypeCode _tc_TAO_tc_";

  // Flat name generation.
  *os << node->flat_name ();

  *os << " (" << be_idt << be_idt_nl;

  switch (node->node_type ())
    {
    case AST_Decl::NT_array:
      *os << "CORBA::tk_array";
      break;
    case AST_Decl::NT_enum:
      *os << "CORBA::tk_enum";
      break;
    case AST_Decl::NT_except:
      *os << "CORBA::tk_except";
      break;
    case AST_Decl::NT_interface:
      *os << "CORBA::tk_objref";
      break;
    case AST_Decl::NT_valuetype:
      *os << "CORBA::tk_value";
      break;
    case AST_Decl::NT_eventtype:
      *os << "CORBA::tk_event";
      break;
    case AST_Decl::NT_component:
      *os << "CORBA::tk_component";
      break;
    case AST_Decl::NT_sequence:
      *os << "CORBA::tk_sequence";
      break;
    case AST_Decl::NT_struct:
      *os << "CORBA::tk_struct";
      break;
    case AST_Decl::NT_typedef:
      *os << "CORBA::tk_alias";
      break;
    case AST_Decl::NT_union:
      *os << "CORBA::tk_union";
      break;
    default:
      return -1; // error
    }

  *os << "," << be_nl
      << "sizeof (_oc_";

  // Flat name generation.
  *os <<  node->flat_name ();

  *os << ")," << be_nl
      << "(char *) &_oc_";

  // Flat name generation.
  *os <<  node->flat_name ();

  // Name generation.
  *os << "," << be_nl
      << "0," << be_nl
      << "sizeof (" << node->name () << ")" << be_uidt_nl
      << ");" << be_uidt_nl << be_nl;

  // Is our enclosing scope a module? We need this check because for
  // platforms that support namespaces, the typecode must be declared
  // extern.
  if (node->is_nested () &&
      node->defined_in ()->scope_node_type () == AST_Decl::NT_module)
    {
      be_module *module = be_module::narrow_from_scope (node->defined_in ());

      if (!module || (this->gen_nested_namespace_begin (module) == -1))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "be_visitor_typecode_defn::visit_type - "
                             "Error parsing nested name\n"),
                            -1);
        }

      *os << "::CORBA::TypeCode_ptr _tc_"
          << node->local_name ()
          << " =" << be_idt_nl
          << "&_tc_TAO_tc_"
          << node->flat_name () << ";"
          << be_uidt;

      if (this->gen_nested_namespace_end (module) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "be_visitor_typecode_defn::visit_type - "
                             "Error parsing nested name\n"),
                            -1);
        }
    }
  else
    {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?