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

#ifndef _DECLS_H
#define _DECLS_H

/*
 * see the the "globals" section of this file for an explaination
 * of _NO_EXTERN  */

#include <stdio.h>
#include <stdarg.h>
#include <elros/typeinfo.h>
#include "pnode.h"

/*
 *        ==========================================================
 *                      C O N S T A N T S
 *        ==========================================================
 */


/* first, some basics... */

#ifndef TRUE
#define	TRUE (1==1)
#endif

#ifndef FALSE
#define	FALSE (1==0)
#endif

/* debugging defs */

#define YYDEBUG 1
#define YYDEBUG_LEXER_TEXT (yytext)

/* other constants */

#define MAX_BIND_ID         10
#define NOT_A_KEY  0  /* must be a value that 
			 is not a possible token */

/*
 *        ==========================================================
 *                              M A C R O S
 *        ==========================================================
 */

#ifndef GEN_LINEDIR
#define GEN_LINEDIR						\
{ if (global.line_dirs) fprintf(out,"\n# %d \"%s\"\n", linedir_line, linedir_file); }
#endif

#ifndef FPRINTF
#define FPRINTF	GEN_LINEDIR fprintf
#endif

#define INDENTX3(A)	     { int i; for (i=0;i<(A);i++)	\
					 fprintf(yyout,"|  "); }
#define INDENT(B,A)	     { int i; for (i=0;i<(A);i++)	\
					 fprintf((B),"   "); }
#define PINDENT(F,A)     { int __i; for (__i=0;__i<(A);__i++)	\
					 fprintf((F),"|     "); }

#define BUG_CHECK(arg) 					\
	{ report_error("internal error","%s\n",arg); exit(1); }
/*
 *        ==========================================================
 *                              T Y P E S
 *        ==========================================================
 */

/* The different kinds of symbols, enumerated */

typedef 
  enum {
    SYM_UNKNOWN, 
    SYM_IDENT, 
    SYM_TYPEDEF, 
    SYM_INST,
    SYM_INTER,
    SYM_NAME,
    SYM_OP,
    SYM_TYPE
    }
SymKind;


/* This type enumerates the possible "states" of the lexer with respect
 * to determining how to tokenize a name (IDENTIFIER, TYPE_NAME, etc..) */

typedef 
  enum { 
    MODE_IDENT, 
    MODE_INSTANCE, 
    MODE_INTERFACE, 
    MODE_ELROS_TYPE, 
    MODE_LOOKUP 
    }
ID_Mode;


typedef unsigned int __RO_int32; /* copied from instance.h */
typedef int boolean;

/*
 *        ==========================================================
 *                          E N U M S 
 *        ==========================================================
 */

typedef enum 
   {KIND_UNKNOWN,KIND_INVOKE,KIND_RESULT,KIND_ERROR,KIND_REJECT,KIND_CALL} op_kind;
typedef enum 
   {TYPE_UNKNOWN,TYPE_CONTROL,TYPE_DATA} inst_type;
typedef enum 
   {MODE_UNKNOWN,MODE_IN,MODE_OUT,MODE_INOUT} arg_mode;
typedef enum {
   PARADIGM_UNKNOWN,PARADIGM_SET,PARADIGM_SEQUENCE } paradigm;
typedef enum {
   ARRAYKIND_UNKNOWN,ARRAYKIND_STATIC,ARRAYKIND_DYNAMIC } arraykind;
typedef enum {
   NOTHING_SEEN,IMPLICIT_SEEN,EXPLICIT_SEEN } implicitness;
enum Handler_Masks {
     MASK_ZERO		=0,
     MASK_COM_ERROR	=1,
     MASK_INTERNAL_ERROR=2,
     MASK_PROTOCOL_ERROR=4,
     MASK_MISSING_OP	=8,
     MASK_MISSING_ARG	=16,
     MASK_BAD_OP_ID	=32,
     MASK_NAME_ERROR	=64,
     MASK_DONE		=128
     };

/*
 *        ==========================================================
 *                          P R O T O T Y P E S
 *        ==========================================================
 */

/*
 *        ==========================================================
 *                          C L A S S E S
 *        ==========================================================
 */

class symbol_table;
class tree_link;
class symbol_element;
class symbol_list;
class primitive_list;
class printer_list;
class printer_element;
class type_element_list;
class contains_recur_list;
class contains_recur_element;
class interface_element;
class operation_element;
class conc_part_element;
class type_element;
class name_element;
class instance_element;
class abstr_type_element;
class rules_element;
class param_element;
class modal_param;
class modal_list;
class bytearray;
class constant_generator;
class optable_generator;
class encoding_rules;
class parameter_list;
class interface_list;
class instance_list;
class active_instance_element;
class active_instance_list;
class argument;
class argument_list;

/* first, decls for all the 'yy' functions */

int   yylex    ( );
int   yyparse  ( );
int   yywrap   ( );          // found in epp.l

void  yyerror  ( char * );                 // found in tail.y
void  yywhere  ( );                        // found in tail.y
void  report_error  (char *label,
		     char *fmt, ... );     // found in tail.y


/* functions used for initialization and such - ones that aren't 
 * called during the actual parsing */

void  crack_execute_line  	( char** );
void  init      		( char** );
void  nomem     		( );

/* these are the functions found in epp.l that are for lexing */

int  lexIdColon      ( );
int  lexIdAtSign     ( );
int  lexKeyword      ( char * );
int  lexInsideIspec  ( char *, interface_element * );

int  scope_resolve   ( );
int  identify        ( char *, int =FALSE );

void make_string         ( );
void squeeze_underscores ( );


/* this group is roughly the parsing actions */

void  act_decl		( int, int, int, interface_element *, decl_element *);
void  act_fundecl       ( );
void  act_config        ( );
void  act_scope_open    ( );
void  act_scope_close   ( );

void  act_give_accept   ( symbol_element * );
void  act_operation     ( operation_element * );
void  dump_expbuf	( int );
void  clear_expbuf	( int );
void  gen_startsend	( FILE *, char *, instance_element *);
void  gen_activateinst_log  ( FILE *, char *, instance_element *);
void  gen_startrec	( FILE *, char *, char *, instance_element *);
void  do_ontype_stmt    (boolean,type_element,char*,char*);
void  do_decode_stmt4   (boolean);
void  do_call_stmt1_a   (operation_element*);
void  do_call_stmt1_b   (operation_element*, instance_element*,
		         argument_list *, char *, char *);
void  do_give_stmt      ();
void  do_give2          (symbol_element *, instance_element *);
char *do_interface_header(char*,boolean,boolean,boolean,int);
void  do_ispec          (char*);
char *negative_string   (char *);
operation_element *do_op_def(int,char*,char*,parameter_list*);
void  put_out__ROc_information ( );
void  do_encode_stmt    ( );
void  do_encode_stmt2   (instance_element *);
void  do_type_stmt      (type_element *,char *,char *);


/*
 *        ==========================================================
 *                          G L O B A L S
 *        ==========================================================
 */


/* We define STORAGE depending upon _NO_EXTERN. This allows us to use
 * just this one file as both the external decl's and the global def's.
 * this is a pretty common technique. We must remember, of course, to
 * #include this file with _NO_EXTERN defined when compiling some source
 * file. Currently, header.y (which becomes the top of parser.c via YACC)
 * is the file in which we do so. CAUTION - always remember to #include 
 * this file in a such a manner *exactly* once! */

#ifdef _NO_EXTERN
#define STORAGE /* nothing */
#else
#define STORAGE extern
#endif


/* globals dealing with files, and line numbers while lexing */

STORAGE FILE  *out;
STORAGE FILE  *prim;
STORAGE FILE  *types;
STORAGE FILE  *oldyyin;

STORAGE char  *input_file;
STORAGE char  *oldinput_file;
STORAGE char  *output_file;
STORAGE char  *output_file2;
STORAGE char  *types_file;
STORAGE char  *parameters_dat_file;

STORAGE int    linedir_line;
STORAGE char   *linedir_file;
STORAGE int    linedir_file_size;

/* globals indicating "where you are" at different 
   moments during the parse */

STORAGE int in_interface_spec;
STORAGE int in_config_file;
STORAGE int in_names_sect;
STORAGE int in_op_def_sect;
STORAGE int config_just_ended;

STORAGE int scopedepth;
STORAGE int decldepth;
STORAGE int nested_constr_lev;


/* communication bewteen the lexer and parser, or between different
 * moments in the parse... */

STORAGE ID_Mode IdentMode;
STORAGE int	changeable_mode;

STORAGE int no_lex_echo;
STORAGE int no_labels_now;

STORAGE int lookup;

STORAGE int  cdtflag_valid;  /* flag to indicate if the following flags valid */
STORAGE int  is_control;     /* flags to indicate if an identifier is a       */
STORAGE int  is_data;        /* control instance variable, a data instance    */
STORAGE int  is_typedef;     /* variable, of a typedef namecontrol vs. data   */

STORAGE arg_mode   current_mode;  // in, out, inout.


/* Things dealing with bind_id's ... */

STORAGE char *bind_type[MAX_BIND_ID];
STORAGE int   bind_type_count;


/* the main data structures */

STORAGE symbol_table   *Symbol_Table;

class globals 
{
public:

   char *interface_name;
   char *constant_table_name;
   char *information_name;
   boolean gen_log;
   boolean line_dirs;
   interface_element *anon_interfptr;
   boolean tagging;
   boolean explicit_tagging;
   boolean freeable;
   int kind_of_args;
   int error_count;

   globals()
   {
      interface_name = NULL;
      constant_table_name = NULL;
      information_name = NULL;
      gen_log = TRUE;           /* generate logging calls 
				   by default */
      line_dirs = FALSE;	/* put out line_dirs after
				   clean up */
      tagging = FALSE;          /* noautotag by default */
      explicit_tagging = TRUE;  /* explicit tagging 
				   by default */
      kind_of_args = 0;		/* no args by default */
      error_count = 0;          /* number of errors reported */
   }
   ~globals(){}

};

STORAGE globals global;

STORAGE decl_element	      *last_deptr;
STORAGE int		      typedef_seen;
STORAGE int		      control_seen;
STORAGE int		      data_seen;
STORAGE interface_element     *ieptr;
STORAGE interface_list        *interface_list_ptr;
STORAGE instance_list         *instance_list_ptr;
STORAGE active_instance_list  *active_instances_ptr;
STORAGE primitive_list        *primitive_list_ptr;
STORAGE printer_list          *printer_list_ptr;
STORAGE type_element_list     *forward_types_ptr;
STORAGE type_element_list     *incomplete_types_ptr;
STORAGE type_element_list     *freeable_types_ptr;


/* globals set by parameter file */

STORAGE int  global_SEND_BUFFER_SIZE;
STORAGE int  global_RECEIVE_BUFFER_SIZE;

/* typedefs from typeinfo.h */

STORAGE __RO_type_lengths __ROc_Real_lengths[3];
STORAGE __RO_type_lengths __ROc_Integer_lengths[5];
STORAGE __RO_type_lengths __ROc_Boolean_lengths[5];
STORAGE __RO_Endian_enum  global_ENDIANESS;

/* Currently I have no general mechanism to deal with initialized
 * aggregate globals with this decls.h approach. Since they're not
 * too numerous, it seems OK to handle them on a case-by-case basis.
 * The keyword tables, for instance, emply "KEYTAB_C" similarly to the
 * "_NO_EXTERN" used here.
 *
 * non-aggregate types (scalars) are simply initialized at the top of
 * the init() function... */

#ifdef _NO_EXTERN

__RO_Type_Info global_params = 
   {
      __ROc_Real_lengths,
      __ROc_Integer_lengths,
      __ROc_Boolean_lengths,
      __ROm_BIG_ENDIAN
   };

#else

extern __RO_Type_Info global_params;

#endif


/* This buffer is needed for code generation. */

STORAGE char *expbuf;
STORAGE int last_bufoffset;
STORAGE int max_bufsize;


/* finally, 'yy' globals that are defined in the C code output from
 * lex. So, these are 'extern' instead of 'STORAGE' */

extern FILE *yyin;
extern FILE *yyout;
extern int   yylineno;
extern char yytext[];


#endif

