1 /* Definitions for symtab.c and callers, part of Bison. 2 3 Copyright (C) 1984, 1989, 1992, 2000-2002, 2004-2015, 2018-2021 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 <https://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 /** Undefined. */ 42 unknown_sym, 43 /** Declared with %type: same as Undefined, but triggered a Wyacc if 44 applied to a terminal. */ 45 pct_type_sym, 46 /** Terminal. */ 47 token_sym, 48 /** Nonterminal. */ 49 nterm_sym 50 } symbol_class; 51 52 53 /** Internal token numbers. */ 54 typedef int symbol_number; 55 # define SYMBOL_NUMBER_MAXIMUM INT_MAX 56 57 58 typedef struct symbol symbol; 59 typedef struct sym_content sym_content; 60 61 /* Declaration status of a symbol. 62 63 First, it is "undeclared". Then, if "undeclared" and used in a 64 %printer/%destructor, it is "used". If not "declared" but used in 65 a rule, it is "needed". Finally, if declared (via a rule for 66 nonterminals, or %token), it is "declared". 67 68 When status are checked at the end, "declared" symbols are fine, 69 "used" symbols trigger warnings, otherwise it's an error. */ 70 71 typedef enum 72 { 73 /** Used in the input file for an unknown reason (error). */ 74 undeclared, 75 /** Used by %destructor/%printer but not defined (warning). */ 76 used, 77 /** Used in the grammar (rules) but not defined (error). */ 78 needed, 79 /** Defined with %type or %token (good). */ 80 declared, 81 } declaration_status; 82 83 enum code_props_type 84 { 85 destructor = 0, 86 printer = 1, 87 }; 88 typedef enum code_props_type code_props_type; 89 90 enum { CODE_PROPS_SIZE = 2 }; 91 92 struct symbol 93 { 94 /** The key, name of the symbol. */ 95 uniqstr tag; 96 97 /** The "defining" location. */ 98 location location; 99 100 /** Whether this symbol is translatable. */ 101 bool translatable; 102 103 /** Whether \a location is about the first uses as left-hand side 104 symbol of a rule (true), or simply the first occurrence (e.g., 105 in a %type, or as a rhs symbol of a rule). The former type of 106 location is more natural in error messages. This Boolean helps 107 moving from location of the first occurrence to first use as 108 lhs. */ 109 bool location_of_lhs; 110 111 /** Points to the other in the symbol-string pair for an alias. */ 112 symbol *alias; 113 114 /** Whether this symbol is the alias of another or not. */ 115 bool is_alias; 116 117 /** All the info about the pointed symbol is there. */ 118 sym_content *content; 119 }; 120 121 struct sym_content 122 { 123 /** The main symbol that denotes this content (it contains the 124 possible alias). */ 125 symbol *symbol; 126 127 /** Its \c \%type. 128 129 Beware that this is the type_name as was entered by the user, 130 including silly things such as "]" if she entered "%token <]> t". 131 Therefore, when outputting type_name to M4, be sure to escape it 132 into "@}". See quoted_output for instance. */ 133 uniqstr type_name; 134 135 /** Its \c \%type's location. */ 136 location type_loc; 137 138 /** Any \c \%destructor (resp. \%printer) declared specifically for this 139 symbol. 140 141 Access this field only through <tt>symbol</tt>'s interface functions. For 142 example, if <tt>symbol::destructor = NULL</tt> (resp. <tt>symbol::printer 143 = NULL</tt>), a default \c \%destructor (resp. \%printer) or a per-type 144 \c symbol_destructor_printer_get will compute the correct one. */ 145 code_props props[CODE_PROPS_SIZE]; 146 147 symbol_number number; 148 location prec_loc; 149 int prec; 150 assoc assoc; 151 152 /** Token code, possibly specified by the user (%token FOO 42). */ 153 int code; 154 155 symbol_class class; 156 declaration_status status; 157 }; 158 159 /** Fetch (or create) the symbol associated to KEY. */ 160 symbol *symbol_from_uniqstr (const uniqstr key, location loc); 161 162 /** Fetch (or create) the symbol associated to KEY. */ 163 symbol *symbol_get (const char *key, location loc); 164 165 /** Generate a dummy nonterminal. 166 167 Its name cannot conflict with the user's names. */ 168 symbol *dummy_symbol_get (location loc); 169 170 171 /*--------------------. 172 | Methods on symbol. | 173 `--------------------*/ 174 175 /** Print a symbol (for debugging). */ 176 void symbol_print (symbol const *s, FILE *f); 177 178 /** Is this a dummy nonterminal? */ 179 bool symbol_is_dummy (symbol const *sym); 180 181 /** The name of the code_props type: "\%destructor" or "\%printer". */ 182 char const *code_props_type_string (code_props_type kind); 183 184 /** The name of the symbol that can be used as an identifier. 185 ** Consider the alias if needed. 186 ** Return 0 if there is none (e.g., the symbol is only defined as 187 ** a string). */ 188 uniqstr symbol_id_get (symbol const *sym); 189 190 /** 191 * Make \c str the literal string alias of \c sym. Copy token number, 192 * symbol number, and type from \c sym to \c str. 193 */ 194 void symbol_make_alias (symbol *sym, symbol *str, location loc); 195 196 /** 197 * This symbol is used as the lhs of a rule. Record this location 198 * as definition point, if not already done. 199 */ 200 void symbol_location_as_lhs_set (symbol *sym, location loc); 201 202 /** Set the \c type_name associated with \c sym. 203 204 Do nothing if passed 0 as \c type_name. */ 205 void symbol_type_set (symbol *sym, uniqstr type_name, location loc); 206 207 /** Set the \c \%destructor or \c \%printer associated with \c sym. */ 208 void symbol_code_props_set (symbol *sym, code_props_type kind, 209 code_props const *destructor); 210 211 /** Get the computed \c \%destructor or \c %printer for \c sym, which was 212 initialized with \c code_props_none_init if there's no \c \%destructor or 213 \c %printer. */ 214 code_props *symbol_code_props_get (symbol *sym, code_props_type kind); 215 216 /** Set the \c precedence associated with \c sym. 217 218 Ensure that \a symbol is a terminal. 219 Do nothing if invoked with \c undef_assoc as \c assoc. */ 220 void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc); 221 222 /** Set the \c class associated with \c sym. 223 224 Whether \c declaring means whether this class definition comes 225 from %nterm or %token (but not %type, prec/assoc, etc.). A symbol 226 can have "declaring" set only at most once. */ 227 void symbol_class_set (symbol *sym, symbol_class class, location loc, 228 bool declaring); 229 230 /** Set the token \c code of \c sym, specified by the user at \c loc. */ 231 void symbol_code_set (symbol *sym, int code, location loc); 232 233 234 235 /*------------------. 236 | Special symbols. | 237 `------------------*/ 238 239 /** The error token. */ 240 extern symbol *errtoken; 241 /** The token for unknown tokens. */ 242 extern symbol *undeftoken; 243 /** The end of input token. */ 244 extern symbol *eoftoken; 245 /** The genuine start symbol. 246 247 $accept: start-symbol $end */ 248 extern symbol *acceptsymbol; 249 250 /** The user start symbol. */ 251 extern symbol *startsymbol; 252 /** The location of the \c \%start declaration. */ 253 extern location startsymbol_loc; 254 255 /** Whether a symbol declared with a type tag. */ 256 extern bool tag_seen; 257 258 259 /*-------------------. 260 | Symbol Relations. | 261 `-------------------*/ 262 263 /* The symbol relations are represented by a directed graph. */ 264 265 /* The id of a node */ 266 typedef int graphid; 267 268 typedef struct symgraphlink symgraphlink; 269 270 struct symgraphlink 271 { 272 /** The second \c symbol or group of a precedence relation. 273 * See \c symgraph. */ 274 graphid id; 275 276 symgraphlink *next; 277 }; 278 279 /* Symbol precedence graph, to store the used precedence relations between 280 * symbols. */ 281 282 typedef struct symgraph symgraph; 283 284 struct symgraph 285 { 286 /** Identifier for the node: equal to the number of the symbol. */ 287 graphid id; 288 289 /** The list of related symbols that have a smaller precedence. */ 290 symgraphlink *succ; 291 292 /** The list of related symbols that have a greater precedence. */ 293 symgraphlink *pred; 294 }; 295 296 /** Register a new precedence relation as used. */ 297 298 void register_precedence (graphid first, graphid snd); 299 300 /** Print a warning for each symbol whose precedence and/or associativity 301 * is useless. */ 302 303 void print_precedence_warnings (void); 304 305 /*----------------------. 306 | Symbol associativity | 307 `----------------------*/ 308 309 void register_assoc (graphid i, graphid j); 310 311 /*-----------------. 312 | Semantic types. | 313 `-----------------*/ 314 315 /** A semantic type and its associated \c \%destructor and \c \%printer. 316 317 Access the fields of this struct only through the interface functions in 318 this file. \sa symbol::destructor */ 319 typedef struct { 320 /** The key, name of the semantic type. */ 321 uniqstr tag; 322 323 /** The location of its first occurrence. */ 324 location location; 325 326 /** Its status : "undeclared", "used" or "declared". 327 It cannot be "needed". */ 328 declaration_status status; 329 330 /** Any \c %destructor and %printer declared for this 331 semantic type. */ 332 code_props props[CODE_PROPS_SIZE]; 333 334 } semantic_type; 335 336 /** Fetch (or create) the semantic type associated to KEY. */ 337 semantic_type *semantic_type_from_uniqstr (const uniqstr key, 338 const location *loc); 339 340 /** Fetch (or create) the semantic type associated to KEY. */ 341 semantic_type *semantic_type_get (const char *key, const location *loc); 342 343 /** Set the \c destructor or \c printer associated with \c type. */ 344 void semantic_type_code_props_set (semantic_type *type, 345 code_props_type kind, 346 code_props const *code); 347 348 /*----------------------------------. 349 | Symbol and semantic type tables. | 350 `----------------------------------*/ 351 352 /** Create the symbol and semantic type tables, and the built-in 353 symbols. */ 354 void symbols_new (void); 355 356 /** Free all the memory allocated for symbols and semantic types. */ 357 void symbols_free (void); 358 359 /** Check that all the symbols are defined. 360 361 Report any undefined symbols and consider them nonterminals. */ 362 void symbols_check_defined (void); 363 364 /** Sanity checks and #token_translations construction. 365 366 Perform various sanity checks, assign symbol numbers, and set up 367 #token_translations. */ 368 void symbols_pack (void); 369 370 #endif /* !SYMTAB_H_ */ 371