/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_argument_c[] = "@(#)argument.C	1.10 10/26/92 10:09:15 LLNL ";


#include <stdio.h>
#include <string.h>
#include "argument.h"
#include "gen_code.h"


argument::argument(char *v, char *l1, char *l2)
{
   name = NULL;
   type = NULL;
   mode = MODE_UNKNOWN;
   value_expression = v;
   length_expression1 = l1;
   length_expression2 = l2;
   list_position = 0;
   num_id = NULL;
}
argument::argument(char *v, char *l1, char *l2, name_element *n)
{
   name = n->get_name();
   type = n->get_type_element_ptr();
   mode = MODE_UNKNOWN;
   value_expression = v;
   length_expression1 = l1;
   length_expression2 = l2;
   list_position = 0;
   num_id = NULL;
}
argument::argument(param_element *p)
{
   if (p->get_name_ptr()) {
      name = p->get_name_ptr()->get_name();
      type = p->get_name_ptr()->get_type_element_ptr();
   }
   else if(p->get_type_element_ptr()) {
      name = NULL;
      type = p->get_type_element_ptr();
   }
   else
      BUG_CHECK("bad field in param_element");
   mode = p->get_mode();
   value_expression = NULL;
   length_expression1 = NULL;
   length_expression2 = NULL;
   list_position = p->get_list_position();
   num_id = p->get_num_id();
}
argument::argument(param_element *p, argument *a)
{
   name = NULL;
   if (p->get_name_ptr()) {
      type = p->get_name_ptr()->get_type_element_ptr();
   }
   else if(p->get_type_element_ptr()) {
      type = p->get_type_element_ptr();
   }
   else
      BUG_CHECK("bad field in param_element");
   mode = p->get_mode();
   value_expression = a ? a->value_expression : NULL;
   length_expression1 = a ? a->length_expression1 : NULL;
   length_expression2 = a ? a->length_expression2 : NULL;
   list_position = p->get_list_position();
   num_id = p->get_num_id();
}
void argument::gen_sendP(FILE *out, char *e_label)
{
   boolean possible_literal;
   abstr_kind k;

   if (!type)
      return;
   fprintf(out,"\n");
   INDENT(out,1);
   fprintf(out,"/* ARGUMENT */\n");
   if (value_expression)
   {
      if (global.gen_log)
      {
         INDENT(out,1);
         fprintf(out,"{\n");
	 k = type->abstr_type_ptr->kind;  
	 possible_literal =
	   k==AKIND_BOOLEAN ||
	   k==AKIND_INTEGER ||
	   k==AKIND_ENUMERATED ||
	   k==AKIND_REAL ||
	   k==AKIND_VISIBLESTRING ||
	   k==AKIND_GRAPHICSTRING ||
	   k==AKIND_PRINTABLESTRING ||
	   k==AKIND_NUMERICSTRING;

         if (possible_literal && !type->abstr_type_ptr->pointer_already)
         {
            type->abstr_type_ptr->gen_type(out,2,"__RO_value");
            fprintf(out,";\n");
            INDENT(out,2);
            fprintf(out,"__RO_value = (%s);\n",value_expression);
            
            gen_log_type(out,2,GETINSTNAME,num_id,"__RO_value",
		type->abstr_type_ptr->print_name,type->abstr_type_ptr->pointer_already,e_label);
         }
         else
            gen_log_type(out,2,GETINSTNAME,num_id,value_expression,
		type->abstr_type_ptr->print_name,type->abstr_type_ptr->pointer_already,e_label);
         INDENT(out,1);
         fprintf(out,"}\n");
      }
      type->gen_sendP(out,
		      1,
		      value_expression,
		      length_expression1,
		      GETINSTNAME,
		      GETCONSTNAME,
		      GETEXCEPTIONLAB);
      INDENT(out,1);
      fprintf(out,"/* End of ARGUMENT */\n\n");;
   }
}

void argument::gen_getP(FILE *out, boolean ellipses, char *e_label)
{
   fprintf(out,"\n");
   INDENT(out,4);
   fprintf(out,"/* ARGUMENT */\n");
   type->gen_leadingTAG_cases(out,4);

   if (value_expression)
   {
      INDENT(out,5);
      fprintf(out,"if (__RO_bits[%d] & %d)\n",list_position/8,1<<list_position%8);
      gen_exception(out,
		    6,
		    "__ROx_ASN_DUPLICATEARG",
		    e_label,
		    GETINSTNAME);
      INDENT(out,5);
      fprintf(out,"else\n",list_position,1);
      INDENT(out,6);
      fprintf(out,"__RO_bits[%d] |= %d;\n",list_position/8,1<<list_position%8);
   }

   if (value_expression || ellipses) {
      boolean iswrapped = type->get_rules_ptr()->length() > 0;

      if ((type->abstr_type_ptr->get_kind() == AKIND_ANY) && iswrapped)
         /* Wrapping is not part of the Any value */
         gen_getheader(out,5,e_label,GETINSTNAME);

      type->gen_getP(out,
                     5,
                     value_expression,
                     length_expression2
                        ?length_expression2
                        :length_expression1,
                     e_label,
                     GETINSTNAME);

      if ((type->abstr_type_ptr->get_kind() == AKIND_ANY) && iswrapped)
         /* Finish consuming wrapping */
         gen_getheader(out,5,e_label,GETINSTNAME);
   }
   else
   {
      INDENT(out,5);
      fprintf(out,"{__RO_RAISE_MISSING_ARG(%s,__RO_operation,__RO_type,%s);}\n",
		GETINSTNAME,e_label);
   }
   if (value_expression) {
      if (global.gen_log)
         gen_log_type(out,
                      5,
		      GETINSTNAME,
		      num_id,
		      value_expression,
		      type->abstr_type_ptr->print_name,
		      type->abstr_type_ptr->pointer_already,
		      e_label);
   }
   INDENT(out,5);
   fprintf(out,"break;\n");
   INDENT(out,4);
   fprintf(out," /* End of ARGUMENT */\n\n");;
}

argument_list::argument_list()
{
   pos_or_named = TYPE_PARAM_LIST; 
   ellipsed = FALSE;
   unwrapped_Any_present = FALSE;
}
argument_list::argument_list(int p)
{
   pos_or_named = p; 
   ellipsed = FALSE;
   unwrapped_Any_present = FALSE;
}
boolean argument_list::search_name(char *key, argument *&target)

{
   link *l;

   this->reset();
   while (this->next(l))
   {
      argument *a = (argument *)l;
      if (!strcmp(a->name,key))
      {
         target = a;
         return(TRUE);
      }
   }
   return(FALSE);
}
