//
// field_ch.cpp,v 1.28 2002/01/31 19:27:19 parsons Exp
//

// ============================================================================
//
// = LIBRARY
//    TAO IDL
//
// = FILENAME
//    field_ch.cpp
//
// = DESCRIPTION
//    Visitor generating code for Field node in the client header
//
// = AUTHOR
//    Aniruddha Gokhale
//
// ============================================================================

#include        "idl.h"
#include        "idl_extern.h"
#include        "be.h"

#include "be_visitor_field.h"

ACE_RCSID(be_visitor_field, field_ch, "field_ch.cpp,v 1.28 2002/01/31 19:27:19 parsons Exp")


// **********************************************
//  visitor for field in the client header file
// **********************************************

// Constructor.
be_visitor_field_ch::be_visitor_field_ch (be_visitor_context *ctx)
  : be_visitor_decl (ctx)
{
}

// Destructor.
be_visitor_field_ch::~be_visitor_field_ch (void)
{
}

// Visit the field node.
int
be_visitor_field_ch::visit_field (be_field *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  be_type *bt;

  // First generate the type information.
  bt = be_type::narrow_from_decl (node->field_type ());

  if (!bt)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_field_ch::"
                         "visit_field - "
                         "Bad field type\n"),
                        -1);
    }

  this->ctx_->node (node);

  if (bt->accept (this) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_field_ch::"
                         "visit_field - "
                         "codegen for field type failed\n"),
                        -1);
    }

  // Now output the field name.
  *os << " " << node->local_name () << ";" << be_nl;

  return 0;
}

// =Visit operations on all possible data types  that a field can be.

// Visit array type.
int
be_visitor_field_ch::visit_array (be_array *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  be_type *bt;

  if (this->ctx_->alias ())
    {
      bt = this->ctx_->alias ();
    }
  else
    {
      bt = node;
    }

  // If not a typedef and we are defined in the use scope, we must be defined.
  if (!this->ctx_->alias ()
      && node->is_child (this->ctx_->scope ()))
    {
      // This is the case for anonymous arrays.

      // Instantiate a visitor context with a copy of our context. This info
      // will be modified based on what type of node we are visiting
      be_visitor_context ctx (*this->ctx_);
      ctx.node (node); // set the node to be the node being visited. The scope
                       // is still the same

      // First generate the array declaration
      ctx.state (TAO_CodeGen::TAO_ARRAY_CH);
      be_visitor *visitor = tao_cg->make_visitor (&ctx);

      if (!visitor)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_field_ch::"
                             "visit_array - "
                             "Bad visitor\n"),
                            -1);
        }

      if (node->accept (visitor) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_field_ch::"
                             "visit_array - "
                             "codegen failed\n"),
                            -1);
        }

      delete visitor;

      // Having defined all array type and its supporting operations, now
      // generate the actual variable that is a field of the structure.
      *os << "_" << bt->local_name ();
    }
  else
    {
      // This was a typedefed array.
      // ACE_NESTED_CLASS macro generated by nested_type_name
      // is not necessary in all cases.
      if (this->ctx_->sub_state () != TAO_CodeGen::TAO_USE_FULL_NAME)
        {
          *os << bt->nested_type_name (this->ctx_->scope ());
        }
      else
        {
          *os << bt->name ();
        }
    }

  return 0;
}

// Visit enum type.
int
be_visitor_field_ch::visit_enum (be_enum *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  be_type *bt;

  if (this->ctx_->alias ())
    {
      bt = this->ctx_->alias ();
    }
  else
    {
      bt = node;
    }

  // If not a typedef and we are defined in the use scope, we must be defined.
  if (!this->ctx_->alias () // not a typedef
      && node->is_child (this->ctx_->scope ()))
    {
      // Instantiate a visitor context with a copy of our context. This info
      // will be modified based on what type of node we are visiting.
      be_visitor_context ctx (*this->ctx_);
      ctx.node (node);

      // First generate the enum declaration.
      ctx.state (TAO_CodeGen::TAO_ENUM_CH);
      be_visitor *visitor = tao_cg->make_visitor (&ctx);

      if (!visitor)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_field_ch::"
                             "visit_enum - "
                             "Bad visitor\n"),
                            -1);
        }

      if (node->accept (visitor) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_field_ch::"
                             "visit_enum - "
                             "codegen failed\n"),
                            -1);
        }

      delete visitor;
    }

  // Now use this enum as a "type" for the subsequent declarator.
  // ACE_NESTED_CLASS macro generated by nested_type_name
  // is not necessary in all cases.
  if (this->ctx_->sub_state () != TAO_CodeGen::TAO_USE_FULL_NAME)
    {
      *os << bt->nested_type_name (this->ctx_->scope ());
    }
  else
    {
      *os << bt->name ();
    }

  return 0;
}

// Visit interface type.
int
be_visitor_field_ch::visit_interface (be_interface *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  be_type *bt;

  if (this->ctx_->alias ())
    {
      bt = this->ctx_->alias ();
    }
  else
    {
      bt = node;
    }

  // If not a typedef and we are defined in the use scope, we must be defined.
  // ACE_NESTED_CLASS macro generated by nested_type_name
  // is not necessary in all cases.
  if (this->ctx_->sub_state () != TAO_CodeGen::TAO_USE_FULL_NAME)
    {
      *os << bt->nested_type_name (this->ctx_->scope (), "_var");
    }
  else
    {
      *os << bt->name () << "_var";
    }

  return 0;
}

// Visit interface forward type.
int
be_visitor_field_ch::visit_interface_fwd (be_interface_fwd *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  be_type *bt;

  if (this->ctx_->alias ())
    {
      bt = this->ctx_->alias ();
    }
  else
    {
      bt = node;
    }

  // If not a typedef and we are defined in the use scope, we must be defined.
  // ACE_NESTED_CLASS macro generated by nested_type_name
  // is not necessary in all cases.
  if (this->ctx_->sub_state () != TAO_CodeGen::TAO_USE_FULL_NAME)
    {
      *os << bt->nested_type_name (this->ctx_->scope (), "_var");
    }
  else
    {
      *os << bt->name () << "_var";
    }

  return 0;
}

// Visit valuetype type.
int
be_visitor_field_ch::visit_valuetype (be_valuetype *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  be_type *bt;

  if (this->ctx_->alias ())
    {
      bt = this->ctx_->alias ();
    }
  else
    {
      bt = node;
    }

  // If not a typedef and we are defined in the use scope, we must be defined.
  // ACE_NESTED_CLASS macro generated by nested_type_name
  // is not necessary in all cases.
  if (this->ctx_->sub_state () != TAO_CodeGen::TAO_USE_FULL_NAME)
    {
      *os << bt->nested_type_name (this->ctx_->scope (), "_var");
    }
  else
    {
      *os << bt->name () << "_var";
    }

  return 0;
}

// Visit valuetype forward type.
int
be_visitor_field_ch::visit_valuetype_fwd (be_valuetype_fwd *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  be_type *bt;

  if (this->ctx_->alias ())
    {
      bt = this->ctx_->alias ();
    }
  else
    {
      bt = node;
    }

  // If not a typedef and we are defined in the use scope, we must be defined.
  // ACE_NESTED_CLASS macro generated by nested_type_name
  // is not necessary in all cases.
  if (this->ctx_->sub_state () != TAO_CodeGen::TAO_USE_FULL_NAME)
    {
      *os << bt->nested_type_name (this->ctx_->scope (), "_var");
    }
  else
    {
      *os << bt->name () << "_var";
    }

  return 0;
}

// Visit predefined type.
int
be_visitor_field_ch::visit_predefined_type (be_predefined_type *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  be_type *bt;

  if (this->ctx_->alias ())
    {
      bt = this->ctx_->alias ();
    }
  else
    {
      bt = node;
    }

  // If not a typedef and we are defined in the use scope, we must be defined.
  if (node->pt () == AST_PredefinedType::PT_pseudo)
    {
      // Check if we are dealing with a CORBA::Object,
      if (!ACE_OS::strcmp (node->local_name ()->get_string (), "Object"))
        {
          *os << bt->name () << "_var";
        }
      else
        {
          // ACE_NESTED_CLASS macro generated by nested_type_name
          // is not necessary in all cases.
          if (bt->defined_in ()->scope_node_type () == AST_Decl::NT_interface)
            {
              *os << bt->nested_type_name (this->ctx_->scope (), "_var");
            }
          else
            {
              *os << bt->name () << "_var";
            }
        }
    }
  else
    {
      // ACE_NESTED_CLASS macro generated by nested_type_name
      // is not necessary in all cases.
      if (this->ctx_->sub_state () != TAO_CodeGen::TAO_USE_FULL_NAME)
        {
          *os << bt->nested_type_name (this->ctx_->scope ());
        }
      else
        {
          *os << bt->name ();
        }
    }

  return 0;
}

// Visit sequence type.
int
be_visitor_field_ch::visit_sequence (be_sequence *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  be_type *bt;

  if (this->ctx_->alias ())
    {
      bt = this->ctx_->alias ();
    }
  else
    {
      bt = node;
    }

  // If not a typedef and we are defined in the use scope, we must be defined
  if (!this->ctx_->alias () // not a typedef
      && node->is_child (this->ctx_->scope ()))
    {
      // Instantiate a visitor context with a copy of our context. This info
      // will be modified based on what type of node we are visiting.
      be_visitor_context ctx (*this->ctx_);
      ctx.node (node);

      // First generate the sequence declaration.
      ctx.state (TAO_CodeGen::TAO_SEQUENCE_CH);
      be_visitor *visitor = tao_cg->make_visitor (&ctx);

      if (!visitor)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_field_ch::"
                             "visit_sequence - "
                             "Bad visitor\n"),
                            -1);
        }

      if (node->accept (visitor) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_field_ch::"
                             "visit_sequence - "
                             "codegen failed\n"),
                            -1);
        }

      delete visitor;

      // If we are being reused by valutype, this would get generated
      // in the private section of the OBV_xx class, so we must
      // generate the typedef for that case elsewhere.
      if (this->ctx_->scope ()->node_type () != AST_Decl::NT_interface)
        {
          // Generate the anonymous sequence member typedef
          // but we must protect against certain versions of g++.
          // This provides a consistent name to use instead of the
          // implementation-specific name.
          be_decl *bs = this->ctx_->scope ();
          *os << "\n#if !defined (__GNUC__) || !defined (ACE_HAS_GNUG_PRE_2_8)"
              << be_nl
              << "typedef " << bt->nested_type_name (bs)
              << " _" << this->ctx_->node ()->local_name ()
              << "_seq;\n";
          *os << "#endif /* ! __GNUC__ || ACE_HAS_GNUG_PRE_2_8 */\n" << be_nl;
        }
    }

  // ACE_NESTED_CLASS macro generated by nested_type_name
  // is not necessary in all cases.
  be_typedef *tdef = be_typedef::narrow_from_decl (bt);

  if (this->ctx_->sub_state () != TAO_CodeGen::TAO_USE_FULL_NAME
      || !tdef)
    {
      *os << bt->nested_type_name (this->ctx_->scope ());
    }
  else
    {
      *os << bt->name ();
    }

  return 0;
}

// Visit string type.
int
be_visitor_field_ch::visit_string (be_string *node)
{
  TAO_OutStream *os; // output stream

  os = this->ctx_->stream ();

  // Set the right type.
  if (node->width () == (long) sizeof (char))
    {
      *os << "TAO_String_Manager";
    }
  else
    {
      *os << "TAO_WString_Manager";
    }

  return 0;
}

// Visit structure type.
int
be_visitor_field_ch::visit_structure (be_structure *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  be_type *bt;

  if (this->ctx_->alias ())
    {
      bt = this->ctx_->alias ();
    }
  else
    {
      bt = node;
    }

  // If not a typedef and we are defined in the use scope, we must be defined.
  if (!this->ctx_->alias () // not a typedef
      && node->is_child (this->ctx_->scope ()))
    {
      // Instantiate a visitor context with a copy of our context. This info
      // will be modified based on what type of node we are visiting.
      be_visitor_context ctx (*this->ctx_);
      ctx.node (node);

      // First generate the struct declaration.
      ctx.state (TAO_CodeGen::TAO_STRUCT_CH);
      be_visitor *visitor = tao_cg->make_visitor (&ctx);

      if (!visitor)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_field_ch::"
                             "visit_struct - "
                             "Bad visitor\n"),
                            -1);
        }

      if (node->accept (visitor) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_field_ch::"
                             "visit_struct - "
                             "codegen failed\n"),
                            -1);
        }

      delete visitor;
    }

  // ACE_NESTED_CLASS macro generated by nested_type_name
  // is not necessary in all cases.
  if (this->ctx_->sub_state () != TAO_CodeGen::TAO_USE_FULL_NAME)
    {
      *os << bt->nested_type_name (this->ctx_->scope ());
    }
  else
    {
      *os << bt->name ();
    }

  return 0;
}

// Visit typedefed type.
int
be_visitor_field_ch::visit_typedef (be_typedef *node)
{
  this->ctx_->alias (node);

  // Make a decision based on the primitive base type.
  be_type *bt = node->primitive_base_type ();

  if (!bt || (bt->accept (this) == -1))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%N:%l) be_visitor_field_ch::"
                         "visit_typedef - "
                         "Bad primitive type\n"),
                        -1);
    }

  // Reset the alias.
  this->ctx_->alias (0);
  return 0;
}

// visit union type
int
be_visitor_field_ch::visit_union (be_union *node)
{
  TAO_OutStream *os = this->ctx_->stream ();
  be_type *bt;

  if (this->ctx_->alias ())
    {
      bt = this->ctx_->alias ();
    }
  else
    {
      bt = node;
    }

  // If not a typedef and we are defined in the use scope, we must be defined.
  if (!this->ctx_->alias () // not a typedef
      && node->is_child (this->ctx_->scope ()))
    {
      // Instantiate a visitor context with a copy of our context. This info
      // will be modified based on what type of node we are visiting.
      be_visitor_context ctx (*this->ctx_);
      ctx.node (node);

      // First generate the union declaration.
      ctx.state (TAO_CodeGen::TAO_UNION_CH);
      be_visitor *visitor = tao_cg->make_visitor (&ctx);

      if (!visitor)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_field_ch::"
                             "visit_union - "
                             "Bad visitor\n"),
                            -1);
        }

      if (node->accept (visitor) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%N:%l) be_visitor_field_ch::"
                             "visit_union - "
                             "codegen failed\n"),
                            -1);
        }

      delete visitor;
    }

  // ACE_NESTED_CLASS macro generated by nested_type_name
  // is not necessary in all cases.
  if (this->ctx_->sub_state () != TAO_CodeGen::TAO_USE_FULL_NAME)
    {
      *os << bt->nested_type_name (this->ctx_->scope ());
    }
  else
    {
      *os << bt->name ();
    }

  return 0;
}
