Home | History | Annotate | Download | only in src
      1 /* Definitions for symtab.c and callers, part of Bison.
      2 
      3    Copyright (C) 1984, 1989, 1992, 2000-2002, 2004-2007, 2009-2012 Free
      4    Software Foundation, Inc.
      5 
      6    This file is part of Bison, the GNU Compiler Compiler.
      7 
      8    This program is free software: you can redistribute it and/or modify
      9    it under the terms of the GNU General Public License as published by
     10    the Free Software Foundation, either version 3 of the License, or
     11    (at your option) any later version.
     12 
     13    This program is distributed in the hope that it will be useful,
     14    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16    GNU General Public License for more details.
     17 
     18    You should have received a copy of the GNU General Public License
     19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     20 
     21 /**
     22  * \file symtab.h
     23  * \brief Manipulating ::symbol.
     24  */
     25 
     26 #ifndef SYMTAB_H_
     27 # define SYMTAB_H_
     28 
     29 # include "assoc.h"
     30 # include "location.h"
     31 # include "scan-code.h"
     32 # include "uniqstr.h"
     33 
     34 /*----------.
     35 | Symbols.  |
     36 `----------*/
     37 
     38 /** Symbol classes.  */
     39 typedef enum
     40 {
     41   unknown_sym,          /**< Undefined.  */
     42   token_sym,		/**< Terminal. */
     43   nterm_sym		/**< Non-terminal. */
     44 } symbol_class;
     45 
     46 
     47 /** Internal token numbers. */
     48 typedef int symbol_number;
     49 #define SYMBOL_NUMBER_MAXIMUM INT_MAX
     50 
     51 
     52 typedef struct symbol symbol;
     53 
     54 /* When extending this structure, be sure to complete
     55    symbol_check_alias_consistency.  */
     56 struct symbol
     57 {
     58   /** The key, name of the symbol.  */
     59   uniqstr tag;
     60   /** The location of its first occurrence.  */
     61   location location;
     62 
     63   /** Its \c \%type.
     64 
     65       Beware that this is the type_name as was entered by the user,
     66       including silly things such as "]" if she entered "%token <]> t".
     67       Therefore, when outputting type_name to M4, be sure to escape it
     68       into "@}".  See quoted_output for instance.  */
     69   uniqstr type_name;
     70 
     71   /** Its \c \%type's location.  */
     72   location type_location;
     73 
     74   /** Any \c \%destructor declared specifically for this symbol.
     75 
     76       Access this field only through <tt>symbol</tt>'s interface
     77       functions.  For example, if <tt>symbol::destructor = NULL</tt>, a
     78       default \c \%destructor or a per-type \c \%destructor might be
     79       appropriate, and \c symbol_destructor_get will compute the
     80       correct one.  */
     81   code_props destructor;
     82 
     83   /** Any \c \%printer declared specifically for this symbol.
     84 
     85       Access this field only through <tt>symbol</tt>'s interface functions.
     86       \sa symbol::destructor  */
     87   code_props printer;
     88 
     89   symbol_number number;
     90   location prec_location;
     91   int prec;
     92   assoc assoc;
     93   int user_token_number;
     94 
     95   /* Points to the other in the symbol-string pair for an alias.
     96      Special value USER_NUMBER_HAS_STRING_ALIAS in the symbol half of the
     97      symbol-string pair for an alias.  */
     98   symbol *alias;
     99   symbol_class class;
    100   bool declared;
    101 };
    102 
    103 /** Undefined user number.  */
    104 #define USER_NUMBER_UNDEFINED -1
    105 
    106 /* `symbol->user_token_number == USER_NUMBER_HAS_STRING_ALIAS' means
    107    this symbol has a literal string alias.  For instance, `%token foo
    108    "foo"' has `"foo"' numbered regularly, and `foo' numbered as
    109    USER_NUMBER_HAS_STRING_ALIAS.  */
    110 #define USER_NUMBER_HAS_STRING_ALIAS -9991
    111 
    112 /* Undefined internal token number.  */
    113 #define NUMBER_UNDEFINED (-1)
    114 
    115 /** Print a symbol (for debugging). */
    116 void symbol_print (symbol *s, FILE *f);
    117 
    118 /** Fetch (or create) the symbol associated to KEY.  */
    119 symbol *symbol_from_uniqstr (const uniqstr key, location loc);
    120 
    121 /** Fetch (or create) the symbol associated to KEY.  */
    122 symbol *symbol_get (const char *key, location loc);
    123 
    124 /** Generate a dummy nonterminal.
    125 
    126    Its name cannot conflict with the user's names.  */
    127 symbol *dummy_symbol_get (location loc);
    128 
    129 /** Is this a dummy nonterminal?  */
    130 bool symbol_is_dummy (const symbol *sym);
    131 
    132 /**
    133  * Make \c str the literal string alias of \c sym.  Copy token number,
    134  * symbol number, and type from \c sym to \c str.
    135  */
    136 void symbol_make_alias (symbol *sym, symbol *str, location loc);
    137 
    138 /** Set the \c type_name associated with \c sym.
    139 
    140     Do nothing if passed 0 as \c type_name.  */
    141 void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
    142 
    143 /** Set the \c destructor associated with \c sym.  */
    144 void symbol_destructor_set (symbol *sym, code_props const *destructor);
    145 
    146 /** Get the computed \c \%destructor for \c sym, which was initialized with
    147     \c code_props_none_init if there's no \c \%destructor.  */
    148 code_props const *symbol_destructor_get (symbol const *sym);
    149 
    150 /** Set the \c printer associated with \c sym.  */
    151 void symbol_printer_set (symbol *sym, code_props const *printer);
    152 
    153 /** Get the computed \c \%printer for \c sym, which was initialized with
    154     \c code_props_none_init if there's no \c \%printer.  */
    155 code_props const *symbol_printer_get (symbol const *sym);
    156 
    157 /* Set the \c precedence associated with \c sym.
    158 
    159    Ensure that \a symbol is a terminal.
    160    Do nothing if invoked with \c undef_assoc as \c assoc.  */
    161 void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
    162 
    163 /** Set the \c class associated with \c sym.  */
    164 void symbol_class_set (symbol *sym, symbol_class class, location loc,
    165 		       bool declaring);
    166 
    167 /** Set the \c user_token_number associated with \c sym.  */
    168 void symbol_user_token_number_set (symbol *sym, int user_number, location loc);
    169 
    170 
    171 /** The error token. */
    172 extern symbol *errtoken;
    173 /** The token for unknown tokens.  */
    174 extern symbol *undeftoken;
    175 /** The end of input token.  */
    176 extern symbol *endtoken;
    177 /** The genuine start symbol.
    178 
    179    $accept: start-symbol $end */
    180 extern symbol *accept;
    181 
    182 /** The user start symbol. */
    183 extern symbol *startsymbol;
    184 /** The location of the \c \%start declaration.  */
    185 extern location startsymbol_location;
    186 
    187 
    188 /*-----------------.
    189 | Semantic types.  |
    190 `-----------------*/
    191 
    192 /** A semantic type and its associated \c \%destructor and \c \%printer.
    193 
    194    Access the fields of this struct only through the interface functions in
    195    this file.  \sa symbol::destructor  */
    196 typedef struct {
    197   /** The key, name of the semantic type.  */
    198   uniqstr tag;
    199 
    200   /** Any \c %destructor declared for this semantic type.  */
    201   code_props destructor;
    202   /** Any \c %printer declared for this semantic type.  */
    203   code_props printer;
    204 } semantic_type;
    205 
    206 /** Fetch (or create) the semantic type associated to KEY.  */
    207 semantic_type *semantic_type_from_uniqstr (const uniqstr key);
    208 
    209 /** Fetch (or create) the semantic type associated to KEY.  */
    210 semantic_type *semantic_type_get (const char *key);
    211 
    212 /** Set the \c destructor associated with \c type.  */
    213 void semantic_type_destructor_set (semantic_type *type,
    214                                    code_props const *destructor);
    215 
    216 /** Set the \c printer associated with \c type.  */
    217 void semantic_type_printer_set (semantic_type *type,
    218                                 code_props const *printer);
    219 
    220 /*----------------------------------.
    221 | Symbol and semantic type tables.  |
    222 `----------------------------------*/
    223 
    224 /** Create the symbol and semantic type tables.  */
    225 void symbols_new (void);
    226 
    227 /** Free all the memory allocated for symbols and semantic types.  */
    228 void symbols_free (void);
    229 
    230 /** Check that all the symbols are defined.
    231 
    232     Report any undefined symbols and consider them nonterminals.  */
    233 void symbols_check_defined (void);
    234 
    235 /** Sanity checks and #token_translations construction.
    236 
    237    Perform various sanity checks, assign symbol numbers, and set up
    238    #token_translations.  */
    239 void symbols_pack (void);
    240 
    241 
    242 /*---------------------------------------.
    243 | Default %destructor's and %printer's.  |
    244 `---------------------------------------*/
    245 
    246 /** Set the default \c \%destructor for tagged values.  */
    247 void default_tagged_destructor_set (code_props const *destructor);
    248 /** Set the default \c \%destructor for tagless values.  */
    249 void default_tagless_destructor_set (code_props const *destructor);
    250 
    251 /** Set the default \c \%printer for tagged values.  */
    252 void default_tagged_printer_set (code_props const *printer);
    253 /** Set the default \c \%printer for tagless values.  */
    254 void default_tagless_printer_set (code_props const *printer);
    255 
    256 #endif /* !SYMTAB_H_ */
    257