/*****************************************************************/
/**                                                             **/
/**             (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 _SYMTAB_H
#define _SYMTAB_H

#include "misc.h"
#include "list.h"
#include "decls.h"
#include "abs.h"

/* how much to expand a hash_table when it gets too full - this is also
 * the reciprocal of the fraction that determines when to expand [e.g a
 * value of 4 means expand the table to 4 times its size when it's 1/4
 * used up] */

#define HASHTAB_EXPANSION  4


/* The different initial sizes for hash_tables, depending how deep they
 * are. The file scope is likely to be very big, a function scope
 * can also be large, but an inner scope is not likely to have alot of
 * declarations. Also, an interface_table may need a fair amount of symbols,
 * but an interface_index is not likely too big. */

#define SYMTAB_SIZE_FILE      4096
#define SYMTAB_SIZE_FUNCTION  1024
#define SYMTAB_SIZE_INNER       64

#define INTERTAB_SIZE         1024

#define INTERDEX_SIZE          256

/*  the different kinds of scopes */

typedef 
  enum {
    SCOP_STD,
    SCOP_NAMES,
    SCOP_OPS,
    SCOP_UNKNOWN 
    }
ScopeKind;

/* a macro to help with error messages, and such */

#define NameOfSymKind(s) \
  ((s)==SYM_IDENT?       " C identifer (not a typedef'ed name) " : \
   ((s)==SYM_TYPEDEF?    " C typedef'ed name " : \
    ((s)==SYM_INST?      " instance variable " : \
     ((s)==SYM_INTER?    " interface name (a C global) " : \
      ((s)==SYM_NAME?    " parameter name " : \
       ((s)==SYM_OP?     " operation name " : \
	((s)==SYM_TYPE?  " user defined Elros type " :\
	                 " <unknown symbol> ")))))))

/* some distinguished scope levels */

#define SCOPLEV_GLOBAL   1
#define SCOPLEV_UNKNOWN  (SCOPLEV_GLOBAL - 1)


/* mask bits used to tell what kinds of symbols are desired for any
 * given lookup */

#define SMASK_IDENT    0x0001
#define SMASK_TYPEDEF  0x0002
#define SMASK_INST     0x0004
#define SMASK_INTER    0x0008
#define SMASK_TYPE     0x0010
#define SMASK_OP       0x0020
#define SMASK_NAME     0x0040

/* ... and the common settings for these masks */

#define SMASK_ANY      0xffff
#define SMASK_C        0x000f
#define SMASK_STD      0x001f
#define SMASK_NONSTD   0x0060


/* this class is basically a hash table that expands when it 
 * becomes too full.See the code for hash_table::add() 
 * for more info about expansion */

class hash_table {
  
 private:
  
  int               size;           /* current size of this table */
  int               fillcount;      /* current count of entries   */
  int               fillimit;       /* when to expand             */
  symbol_element  **contents;       /* the entries                */

  /* These routines are only for use within the class. They abstract out
   * the actual deciding where to place an entry, and then placing it */

  int   hashfun ( char *, int );
  void  hashit  (  symbol_element *, symbol_element **, int );

 public:

  /* adding and lookups are the main operations (other than 
   * con/destruction. Above, hashit() is just a subroutine of add(), 
   * and hashfun() is the hashing function. */

  void            add     ( symbol_element * ); 
  symbol_element *lookup  ( char * );

  hash_table( int );

  ~hash_table()
    {
      delete (contents);
    }
  
  /* this next routine is for the use of a symbol_table, look there
   * for how it's used. It allows you to exchange an entry in the
   * table with a new one. DANGEROUS if misused - you must ensure that
   * the new entry hashes to the same place. This condition is met
   * when a symbol_table uses this routine */

  symbol_element *swap_in( symbol_element *, symbol_element * );

  /* clean out all the entries in the table */

  void purge();

  /* a debugging function */

  void dump();
};



/* this class is just a way to link hash_table's into chains, which is 
 * needed for the symbol_table. Notice that it works for both the
 * typical "stack of scopes" operation, as well as for when we must 
 * "sneak in" a particular interface's set of OPERATION_NAME's - for
 * instance, inside a give statement. However, depending on which of
 * of these two uses is in effect, we may or may not have to free the
 * hash_table when the scope closes - need to set a flag. */

class HashLink {

  /* all members private, only a symbol_table may use them */

  friend class symbol_table;

 private:

  hash_table  *contents;   /* "local" info */
  symbol_list *symlist;    /* for pointing into interface_element's */
  HashLink    *prev;
  ScopeKind    kind;

  HashLink ( ScopeKind k )
    {
      contents   = NULL;
      symlist    = NULL;
      prev       = NULL;
      kind       = k;
    }

  ~HashLink() { /* nop */ }
};
      

class symbol_table {

 private:

  HashLink   *contents;     /* a chain of hash_tables (used as a stack) */
  HashLink   *last_look;    /* the table where the last lookup was found */
  int         scope_level;  /* the length of the chain */

  /* These are some private routines involved with lookups */

  symbol_element *lookup_driver   ( char *, HashLink *, int );
  symbol_element *standard_lookup ( char *, hash_table *, int );
  symbol_element *external_lookup ( char *, symbol_list *, int );

 public:

  /* the basic operations are scoping, inserting, and looking-up */

  void open_scope   ( );
  void attach_scope ( ScopeKind, symbol_list * );
  void close_scope  ( );

  void add         ( symbol_element * );

  void add_ident( char *nam );
  void add_inter( char *nam );
  void add_typedef( char *nam );
  void add_type( char *nam, interface_element *ifc );
  void add_inst( char *nam, interface_element *ifc );

  symbol_element *lookup ( char *name, int mask =SMASK_ANY )
    {
      /* simply do a lookup starting from the current innermost 
       * scope (i.e. the 'contents' member of the symbol table) for
       * the indicated kinds of symbols */

      return lookup_driver( name, contents, mask );
    }

  int is_defined_type (char *name)
  {
     symbol_element *temp;
     temp = lookup(name);
     if (temp && temp->get_kind() == SYM_TYPE)
        return (1);
     else
        return (0);
  }
       

  /* con/destruction - the destructor is not inline-able with 2.0 */

  symbol_table()
    {
      contents    = NULL;
      scope_level = SCOPLEV_UNKNOWN;
    }

  ~symbol_table();

  /* a debugging function */

  void dump();
};

    
#endif

