/*****************************************************************/
/**                                                             **/
/**             (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           **/
/**                                                             **/
/*****************************************************************/

#include "decls.h"
#include "pnode.h"
#include "abs.h"
#include "symtab.h"


void act_give_accept ( symbol_element *inst )

     /* When encountering a give, the instance's interface's operations
      * come into scope */
{
  interface_element *interf;

  if (inst) { /* allow graceful recovery */
     if ((interf = inst->get_interf()) == NULL) {
       report_error("internal error",
		    "No Interface for instance \"%s\".",
		    inst->get_name());
       exit(1);
     }
     Symbol_Table->attach_scope(SCOP_OPS,interf->operations_ptr);
     scopedepth++;
  }
}


void act_operation ( operation_element *op )

     /* similar to act_give_accept, this routine brings
      * the operation's names into scope */
{
    interface_element *interf;

  if (op) { /* allow graceful recovery */
    if ((interf = op->get_interf()) == NULL) {
       report_error("internal error",
		    "No Interface for operation \"%s\".",
		    op->get_name());
       exit(1);
    }
    Symbol_Table->attach_scope(SCOP_NAMES,interf->names_ptr);
    scopedepth++;
  }
}

void act_decl(int cflag, 
		   int dflag, 
		   int tflag, 
		   interface_element *ieptr,
		   decl_element *deptr)
{ 
 instance_element *tmpinst;
 char *ident;

 if (cflag && dflag) {
   BUG_CHECK("Declaration is both Control and Data ?!");
 }
 else {	
    ident = deptr->get_name();
    if (!cflag && !dflag) {
       if (tflag) {
	 Symbol_Table->add_typedef(ident);
       }
       else {
	 Symbol_Table->add_ident(ident);
       }
    }
    else {
       if (cflag) {
	 tmpinst = new instance_element(ident,scopedepth,
					ieptr,TYPE_CONTROL);
	 instance_list_ptr->insert(tmpinst);
       }
       else {
	 tmpinst = new instance_element(ident,scopedepth,
					ieptr,TYPE_DATA);
	 instance_list_ptr->insert(tmpinst);
       }
       Symbol_Table->add_inst(ident,ieptr);
    }
 }
}

void act_config()

     /* This simply writes out the information gathered from cracking
      * the params.dat (config) file. */

{
  fprintf(prim,"\n#include <elros/preprocessor.h>\n");
  fprintf(prim,"#define __RO_SEND_BUFFER_SIZE %d\n",
	  global_SEND_BUFFER_SIZE);
  fprintf(prim,"#define __RO_RECEIVE_BUFFER_SIZE %d\n",
	  global_RECEIVE_BUFFER_SIZE);
  put_out__ROc_information();
}


void put_out__ROc_information()
{

        /* Put out __ROc_xxx_information */

   fprintf(prim,"\nstatic __RO_type_lengths __ROc_Real_lengths[3] = \n");
   fprintf(prim,"{ {%d,%d,%d}, {%d,%d,%d}, {%d,%d,%d} };\n",
		  __ROc_Real_lengths[0].size,
		  __ROc_Real_lengths[0].maxlength,
		  __ROc_Real_lengths[0].maxlength2,
		  __ROc_Real_lengths[1].size,
		  __ROc_Real_lengths[1].maxlength,
		  __ROc_Real_lengths[1].maxlength2,
		  __ROc_Real_lengths[2].size,
		  __ROc_Real_lengths[2].maxlength,
		  __ROc_Real_lengths[2].maxlength2
		  );

   fprintf(prim,"\nstatic __RO_type_lengths __ROc_Integer_lengths[5] = \n");
   fprintf(prim,"{{%d,%d,%d},{%d,%d,%d},{%d,%d,%d},{%d,%d,%d},{%d,%d,%d} };\n",
		  __ROc_Integer_lengths[0].size,
		  __ROc_Integer_lengths[0].maxlength,
		  __ROc_Integer_lengths[0].maxlength2,
		  __ROc_Integer_lengths[1].size,
		  __ROc_Integer_lengths[1].maxlength,
		  __ROc_Integer_lengths[1].maxlength2,
		  __ROc_Integer_lengths[2].size,
		  __ROc_Integer_lengths[2].maxlength,
		  __ROc_Integer_lengths[2].maxlength2,
		  __ROc_Integer_lengths[3].size,
		  __ROc_Integer_lengths[3].maxlength,
		  __ROc_Integer_lengths[3].maxlength2,
		  __ROc_Integer_lengths[4].size,
		  __ROc_Integer_lengths[4].maxlength,
		  __ROc_Integer_lengths[4].maxlength2
		  );
   fprintf(prim,"\nstatic __RO_type_lengths __ROc_Boolean_lengths[5] = \n");
   fprintf(prim,"{{%d,%d,%d},{%d,%d,%d},{%d,%d,%d},{%d,%d,%d},{%d,%d,%d} };\n",
		  __ROc_Boolean_lengths[0].size,
		  __ROc_Boolean_lengths[0].maxlength,
		  __ROc_Boolean_lengths[0].maxlength2,
		  __ROc_Boolean_lengths[1].size,
		  __ROc_Boolean_lengths[1].maxlength,
		  __ROc_Boolean_lengths[1].maxlength2,
		  __ROc_Boolean_lengths[2].size,
		  __ROc_Boolean_lengths[2].maxlength,
		  __ROc_Boolean_lengths[2].maxlength2,
		  __ROc_Boolean_lengths[3].size,
		  __ROc_Boolean_lengths[3].maxlength,
		  __ROc_Boolean_lengths[3].maxlength2,
		  __ROc_Boolean_lengths[4].size,
		  __ROc_Boolean_lengths[4].maxlength,
		  __ROc_Boolean_lengths[4].maxlength2
		  );
   fprintf(prim,"\nstatic __RO_Type_Info __ROc_type_information = \n");
   fprintf(prim,"{ __ROc_Real_lengths,\n");
   fprintf(prim,"  __ROc_Integer_lengths,\n");
   fprintf(prim,"  __ROc_Boolean_lengths,\n");
   if (global_ENDIANESS == __ROm_BIG_ENDIAN)
      fprintf(prim,"\t__ROm_BIG_ENDIAN\n};\n\n",0);
   else
      fprintf(prim,"\t__ROm_LITTLE_ENDIAN\n};\n\n",0);
}


void act_fundecl()

     /* This function handles scoping just after we've seen a 
      * function decl [something like id( <args> ) c.f. declarator2 in
      * ansi.y]. 
      *
      * Normally, this will be a "top-level" decl, and we 
      * will *not* adjust the scoping because we don't yet know wether
      * it's 
      *   (1) an extern decl    [ void f(int i, int j); ] or 
      *   (2) a function header [ void f(int i, int j) { <body } ],
      * c.f. the last few rules in ansi.y. In case 1, the scope ends
      * and i,j are no longer visible. In case 2, i,j are still around
      * because the scope continues on until the end of the function.
      *
      * The less likely case is a "nested" decl (not "top-level"), in
      * which case we do know that the scope ends:
      *
      *    void f(int i, int j, int g(int k, int k2), int k )
      *                                            ^
      * At the caret, the scope for g's args ends, so we close_scope and
      * k, k2 are no longer visible, allowing the final "int k" arg of
      * f. The above is a legal ANSI decl or function header.  */

{
  /* we check for depth 2 because depth 1 is the outermost scope, or
   * "file" scope. so a "top-level" decl within the outermost scope
   * is depth 2 - any deeper decl is a nested decl */

  if (scopedepth > 2) act_scope_close();
}


void act_scope_open()

     /* Takes care of entering a new scope */
{
  Symbol_Table->open_scope();
  scopedepth++;
}

void act_scope_close()

     /* Cleans up when exiting a scope */
{

  /* First, some "elros" clean-up */

  instance_list_ptr->delete_instances(scopedepth);

  /* and the "standard" stuff */

  Symbol_Table->close_scope();
  scopedepth--;
}
