home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / FTNCHK32.ZIP / symtab.h < prev    next >
C/C++ Source or Header  |  1993-02-16  |  12KB  |  438 lines

  1. /* symtab.h:
  2.  
  3.     Shared declarations for symbol-table routines.  Note: uses
  4.     declarations in defs.h.
  5.  
  6.     Copyright (C) 1992 by Robert K. Moniot.
  7.     This program is free software.  Permission is granted to
  8.     modify it and/or redistribute it, retaining this notice.
  9.     No guarantees accompany this software.
  10.  
  11.  
  12. */
  13.  
  14. #ifdef SYMTAB            /* "home" for variables is symtab.c */
  15. #define SYM_SHARED
  16. #else
  17. #define SYM_SHARED extern
  18. #endif
  19.  
  20.         /* Definitions of symbol table information */
  21.  
  22. /*    Token subclasses (classes are in tokdefs.h)
  23.  */
  24.  
  25. #define relop_EQ    0
  26. #define relop_NE    1
  27. #define relop_LE    2
  28. #define relop_LT    3
  29. #define relop_GE    4
  30. #define relop_GT    5
  31.  
  32.  
  33.  
  34.     /* Storage Class types for variables, consts, and externals */
  35. #define class_VAR 0
  36. #define class_SUBPROGRAM 1
  37. #define class_COMMON_BLOCK 2
  38. #define class_STMT_FUNCTION 3
  39. #define class_LABEL 4
  40. #define class_NAMELIST 5
  41.  
  42.  
  43.     /* Data types for variables, consts, and externals */
  44.     /* N.B. 0 thru 7 are wired into lookup tables in exprtype.c */
  45. #define type_UNDECL 0
  46. #define type_ERROR 0        /* for result of erroneous operation */
  47. #define type_INTEGER 1
  48. #define type_REAL 2
  49. #define type_DP 3
  50. #define type_COMPLEX 4
  51. #define type_LOGICAL 5
  52. #define type_STRING 6
  53. #define type_HOLLERITH 7
  54. #define type_GENERIC 8
  55. #define type_SUBROUTINE 9
  56. #define type_COMMON_BLOCK 10
  57. #define type_PROGRAM 11
  58. #define type_BLOCK_DATA 12
  59. #define type_LABEL 13
  60. #define type_NAMELIST 14
  61.  
  62.                 /* test for types usable in exprs */
  63. #define is_computational_type(t) ((t) <= 7)
  64.                 /* test for arith, char, or logical type */
  65. #define is_const_type(t) ((datatype_of(t) > 0) && (datatype_of(t) <= 6))
  66.  
  67.  
  68. typedef unsigned char BYTE;
  69.  
  70.  
  71.         /* Array of class and type name translations */
  72. #ifdef SYMTAB
  73. char *class_name[] = {
  74.     "",
  75.     "subprog",
  76.     "common",
  77.     "stmt fun",
  78.     "label",
  79.     "namelist",
  80. };
  81. char *type_name[] = {
  82.     "undf",
  83.     "intg",
  84.     "real",
  85.     "dble",
  86.     "cplx",
  87.     "logl",
  88.     "char",
  89.     "holl",
  90.     "genr",
  91.     "subr",
  92.     "comm",
  93.     "prog",
  94.     "data",
  95.     "labl",
  96.     "naml",
  97. };
  98.  
  99.         /* Typical sizes of objects of each data type */
  100. BYTE type_size[]={        /* for use in check_mixed_common */
  101.     0, /*undf*/
  102.     4, /*intg*/
  103.     4, /*real*/
  104.     8, /*dble*/
  105.     8, /*cplx*/
  106.     4, /*logl*/
  107.     1, /*char*/
  108.     4, /*holl*/
  109.     0, /*genr*/
  110.     0, /*subr*/
  111.     0, /*comm*/
  112.     0, /*prog*/
  113.     0, /*data*/
  114.     0, /*labl*/
  115.     0, /*naml*/
  116. };
  117.  
  118. #else
  119. extern char *class_name[];
  120. extern char *type_name[];
  121. extern BYTE type_size[];
  122. #endif
  123.  
  124.  
  125.         /* implicit and default typing lookup table */
  126. SYM_SHARED
  127. int implicit_type[26];    /* indexed by [char - 'A'] */
  128.  
  129.  
  130.  
  131.     /* Declaration of Token data structure.  N.B. do not change without
  132.        consulting preamble of fortran.y for uses with nonterminals.
  133.      */
  134. struct tokstruct {
  135.     union {
  136.         long integer;
  137.         double dbl;
  138.         char *string;
  139.     } value;
  140.     struct tokstruct *next_token;
  141.     long class,subclass;
  142.     unsigned line_num;    /* Line and column where token occurred */
  143.     unsigned col_num : 8;
  144. };
  145.  
  146. typedef struct tokstruct Token;
  147.  
  148. #define YYSTYPE Token    /* Type defn for yylval and Yacc stack */
  149.  
  150.  
  151.  
  152. SYM_SHARED
  153. unsigned long loc_symtab_top,    /* Next avail spot in local symbol table */
  154.    glob_symtab_top;        /* Ditto global */
  155.  
  156. SYM_SHARED
  157. char strspace[STRSPACESZ];    /* String space for storing identifiers */
  158.     /* Stringspace is partitioned into local (growing from bottom up)
  159.        and global (growing from top down). */
  160.  
  161. SYM_SHARED
  162. unsigned long loc_str_top,    /* Top of local stringspace */
  163.    glob_str_bot;        /* Bottom of global stringspace */
  164.  
  165. SYM_SHARED
  166.   unsigned long token_space_top;    /* Top of token space */
  167. SYM_SHARED
  168.   Token tokenspace[TOKENSPACESZ];
  169.  
  170.  
  171.         /* Define names for anonymous things */
  172. #ifdef SYMTAB
  173. char *blank_com_name = "%BLANK",  /* id for blank common entry in symtab */
  174.      *unnamed_prog="%MAIN",      /* id for unnamed program module */
  175.      *unnamed_block_data="%DAT00";  /* id for unnamed block data module */
  176. int  block_data_number=0;       /* count of multiple anonymous block data */
  177. #else
  178. extern char *blank_com_name,
  179.         *unnamed_prog,
  180.         *unnamed_block_data;
  181. extern int block_data_number;
  182. #endif
  183.  
  184.                 /* Symbol table argument list declarations */
  185.  
  186. typedef union {        /* InfoUnion: misc info about symtab entry */
  187.          unsigned long array_dim;    /* array size and no. of dims */
  188.          struct ALHead *arglist;    /* ptr to func/subr argument list */
  189.          struct CMHead *comlist;    /* ptr to common block list */
  190.          struct TLHead *toklist;  /* ptr to token list */
  191.          struct IInfo *intrins_info;/* ptr to intrinsic func info */
  192.          long int_value;        /* value of integer parameter */
  193. } InfoUnion;
  194.  
  195. typedef struct {    /* ArgListElement: holds subprog argument data */
  196.     InfoUnion info;
  197.     BYTE type;
  198.     unsigned is_lvalue: 1,
  199.          set_flag: 1,
  200.          assigned_flag: 1,
  201.          used_before_set: 1,
  202.          array_var: 1,
  203.          array_element: 1,
  204.          declared_external: 1;
  205. } ArgListElement;
  206.  
  207.  
  208. typedef struct ALHead {        /* ArgListHeader: head node of argument list */
  209.     BYTE type;
  210.     short numargs;
  211.     ArgListElement *arg_array;
  212.     struct gSymtEntry *module;
  213.     char *filename,*topfile;
  214.     unsigned
  215.          line_num,
  216.          is_defn: 1,
  217.          is_call: 1,
  218.          external_decl: 1,    /* EXTERNAL decl, not arg list */
  219.              actual_arg: 1;    /* subprog passed as arg */
  220.     struct ALHead *next;
  221. } ArgListHeader;
  222.  
  223.         /* Symbol table common block list declarations */
  224.  
  225. typedef struct {    /* ComListElement: holds common var data */
  226.     unsigned long dimen_info;
  227.     BYTE type;
  228. } ComListElement;
  229.  
  230. typedef struct CMHead {    /* ComListHeader: head node of common var list */
  231.     short numargs;
  232.     short flags;
  233.     unsigned line_num;
  234.     ComListElement *com_list_array;
  235.     struct gSymtEntry *module;
  236.     char *filename,*topfile;
  237.     struct CMHead *next;
  238. } ComListHeader;
  239.  
  240.  
  241. typedef struct TLHead {    /* TokenListHeader: head node of token list */
  242.     Token *tokenlist;
  243.     struct TLHead *next;
  244.     char *filename;
  245.     unsigned line_num;
  246.     unsigned
  247.       external_decl:1,
  248.       actual_arg:1;
  249. } TokenListHeader;
  250.  
  251.  
  252.             /* Structure for intrinsic-function info */
  253. typedef struct IInfo{
  254.     char *name;
  255.     short num_args,arg_type,result_type;
  256. } IntrinsInfo;
  257.  
  258.  
  259.             /* Structure for call-tree child list */
  260. typedef struct childlist {
  261.   struct gSymtEntry *child;    /* Pointer to child's symtab entry */
  262.   struct childlist *next;/* Pointer to next child on list */
  263. } ChildList;
  264.  
  265.         /*  Identifier symbol table declaration */
  266.  
  267.  
  268. typedef struct lSymtEntry{
  269.     char *name;             /* Identifier name in stringspace */
  270.     InfoUnion info;
  271.     struct lSymtEntry *equiv_link;    /* Link for equivalence lists */
  272.     BYTE  type;        /* Type & storage class: see macros below */
  273.             /* Flags */
  274.     unsigned
  275.          used_flag: 1,
  276.          set_flag: 1,
  277.          assigned_flag: 1,
  278.          used_before_set: 1,
  279.          is_current_module: 1,
  280.          library_module: 1,
  281.          array_var: 1,
  282.          common_var: 1,
  283.          entry_point: 1,
  284.          parameter: 1,
  285.          argument: 1,
  286.          external: 1,
  287.          intrinsic: 1,
  288.          invoked_as_func: 1,
  289.          defined_in_include: 1,
  290.          declared_external: 1;
  291. } Lsymtab;
  292.  
  293. typedef struct gSymtEntry{    /* Global symbol table element */
  294.     char *name;             /* Identifier name in stringspace */
  295.     InfoUnion info;
  296.     union {
  297.       struct childlist *child_list; /* List of callees (for module) */
  298.       struct gSymtEntry *module; /* Module (for interior entry) */
  299.     } link;
  300.     BYTE  type;        /* Type & storage class: see macros below */
  301.             /* Flags */
  302.     unsigned
  303.          used_flag: 1,
  304.          set_flag: 1,
  305.          assigned_flag: 1,
  306.          used_before_set: 1,
  307.          library_module: 1,
  308.          internal_entry: 1,    /* entry point other than at the top */
  309.          invoked_as_func: 1,
  310.          visited: 1,       /* this entry point is in call tree */
  311.          visited_somewhere: 1, /* some entry point of module is in call tree */
  312.          defined_in_include: 1,
  313.          declared_external: 1;
  314. } Gsymtab;
  315.  
  316.  
  317.  
  318.         /* Macro to zero out symbol table entry */
  319.  
  320. #define clear_symtab_entry(S) {register int i;\
  321.                  for(i=0;i<sizeof(*S);i++)((char*)S)[i]=0;}
  322.  
  323.  
  324.     /* These macros pack and unpack datatype and storage class in type
  325.        field of symbol table entry. Datatype is least 4 bits. */
  326.  
  327. #define datatype_of(TYPE) ((TYPE) & 0xF)
  328. #define storage_class_of(TYPE) ((TYPE) >> 4)
  329. #define type_byte(SCLASS,DTYPE) (((SCLASS)<<4) + (DTYPE))
  330.  
  331.  
  332.     /* This macro is for pattern matching in flag checking */
  333.  
  334. #define flag_combo(A,B,C) (((A)<<2) | ((B)<<1) | (C))
  335.  
  336.  
  337.     /* These macros are for dimensions & sizes of arrays */
  338.  
  339. #define array_dims(dim_info) ((dim_info)&0xF)
  340. #define array_size(dim_info) ((dim_info)>>4)
  341. #define array_dim_info(dim,size) (((long)(size)<<4)+(dim))
  342.  
  343.  
  344.  
  345.         /* Defns used by expression type propagation mechanisms
  346.            in fortran.y and exprtype.c  The flags go in token.subclass
  347.          */
  348.  
  349. #define make_true(flag,x) ((x) |= (flag))        /* x.flag <-- true   */
  350. #define make_false(flag,x) ((x) &= ~(flag))        /* x.flag <-- false  */
  351. #define is_true(flag,x) ((x) & (flag))            /* x.flag == true?   */
  352. #define copy_flag(flag,x,y)  ((x) |= ((y)&(flag)))    /* x.flag <-- y.flag */
  353.  
  354. #define ID_EXPR 0x1        /* a variable */
  355. #define LVALUE_EXPR 0x2        /* assignable */
  356. #define CONST_EXPR 0x4        /* compile-time constant */
  357. #define NUM_CONST 0x8        /* a number */
  358. #define ARRAY_ID_EXPR 0x10    /* an array or array element */
  359. #define INT_QUOTIENT_EXPR 0x20    /* contains INT/INT */
  360. #define STMT_FUNCTION_EXPR 0x40
  361. #define PARAMETER_EXPR 0x80    /* == CONST_EXPR or intrinsic funcs */
  362. #define SET_FLAG 0x100        /* these are for id's and lvalues */
  363. #define ASSIGNED_FLAG 0x200
  364. #define USED_BEFORE_SET 0x400
  365. #define COMMA_FLAG  0x800        /* keeps track of extra or missing commas
  366.                    in exprlists */
  367. #define COMPLEX_FLAG 0x1000    /* keeps track of complex_const_allowed */
  368.  
  369.  
  370. SYM_SHARED
  371. Lsymtab    loc_symtab[LOCSYMTABSZ];
  372. SYM_SHARED
  373. Gsymtab glob_symtab[GLOBSYMTABSZ];
  374.  
  375.         /*  Identifier hashtable declaration  */
  376.  
  377. SYM_SHARED
  378. struct {
  379.     char    *name;        /* Identifier name in stringspace */
  380.     Lsymtab    *loc_symtab,    /* Local symtab entry for vars etc. */
  381.         *com_loc_symtab;/* Local symtab entry for common blocks */
  382.     Gsymtab    *glob_symtab,    /* Global symtab entry for vars etc. */
  383.         *com_glob_symtab;/* Global symtab entry for common blocks */
  384. } hashtab[HASHSZ];
  385.  
  386.  
  387.  
  388.         /* Shared routines */
  389.  
  390.             /* in fortran.y/fortran.c */
  391. void
  392. check_seq_header();
  393.  
  394.             /* in prsymtab.c */
  395. void
  396. debug_symtabs(), print_loc_symbols();
  397.  
  398.             /* in symtab.c */
  399. void
  400. call_func(), call_subr(), declare_type(), def_arg_name(),
  401. def_array_dim(), def_com_block(), def_com_variable(),
  402. def_equiv_name(), def_ext_name(), def_function(), def_intrins_name(),
  403. def_namelist(), def_namelist_item(), def_parameter(),
  404. def_stmt_function(), do_ASSIGN(), do_assigned_GOTO(), do_ENTRY(),
  405. do_RETURN(), equivalence(),
  406. init_globals(), init_symtab(),
  407. process_lists(), ref_array(), ref_namelist(), ref_variable(),
  408. set_implicit_type(),
  409. stmt_function_stmt(),use_actual_arg(), use_implied_do_index(),
  410. use_io_keyword(), use_special_open_keywd(),
  411. use_lvalue(), use_parameter(),
  412. use_function_arg(), use_variable();
  413.  
  414. Token
  415. *new_token();
  416.  
  417. Lsymtab
  418.  *install_local();
  419. Gsymtab
  420.  *install_global();
  421.  
  422. unsigned
  423. hash_lookup();
  424.  
  425. int
  426. def_curr_module(), get_type(), int_expr_value();
  427.  
  428. char *
  429. token_name();
  430.                 /* in hash.c (now symtab.c) */
  431. unsigned long
  432. hash(), kwd_hash(), rehash();
  433.  
  434.             /* in symtab2.c */
  435. void                /* exprtype routines */
  436. binexpr_type(),unexpr_type(),assignment_stmt_type(),
  437. func_ref_expr(),primary_id_expr();
  438.