gnulib: update
[bison.git] / src / symtab.h
blob11a3a6fc98eb0e39b8e95fecc53e7ce6e643ac30
1 /* Definitions for symtab.c and callers, part of Bison.
3 Copyright (C) 1984, 1989, 1992, 2000-2002, 2004-2015, 2018-2021 Free
4 Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
21 /**
22 * \file symtab.h
23 * \brief Manipulating ::symbol.
26 #ifndef SYMTAB_H_
27 # define SYMTAB_H_
29 # include "assoc.h"
30 # include "location.h"
31 # include "scan-code.h"
32 # include "uniqstr.h"
34 /*----------.
35 | Symbols. |
36 `----------*/
38 /** Symbol classes. */
39 typedef enum
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;
53 /** Internal token numbers. */
54 typedef int symbol_number;
55 # define SYMBOL_NUMBER_MAXIMUM INT_MAX
58 typedef struct symbol symbol;
59 typedef struct sym_content sym_content;
61 /* Declaration status of a symbol.
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".
68 When status are checked at the end, "declared" symbols are fine,
69 "used" symbols trigger warnings, otherwise it's an error. */
71 typedef enum
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;
83 enum code_props_type
85 destructor = 0,
86 printer = 1,
88 typedef enum code_props_type code_props_type;
90 enum { CODE_PROPS_SIZE = 2 };
92 struct symbol
94 /** The key, name of the symbol. */
95 uniqstr tag;
97 /** The "defining" location. */
98 location location;
100 /** Whether this symbol is translatable. */
101 bool translatable;
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;
111 /** Points to the other in the symbol-string pair for an alias. */
112 symbol *alias;
114 /** Whether this symbol is the alias of another or not. */
115 bool is_alias;
117 /** All the info about the pointed symbol is there. */
118 sym_content *content;
121 struct sym_content
123 /** The main symbol that denotes this content (it contains the
124 possible alias). */
125 symbol *symbol;
127 /** Its \c \%type.
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;
135 /** Its \c \%type's location. */
136 location type_loc;
138 /** Any \c \%destructor (resp. \%printer) declared specifically for this
139 symbol.
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];
147 symbol_number number;
148 location prec_loc;
149 int prec;
150 assoc assoc;
152 /** Token code, possibly specified by the user (%token FOO 42). */
153 int code;
155 symbol_class class;
156 declaration_status status;
159 /** Fetch (or create) the symbol associated to KEY. */
160 symbol *symbol_from_uniqstr (const uniqstr key, location loc);
162 /** Fetch (or create) the symbol associated to KEY. */
163 symbol *symbol_get (const char *key, location loc);
165 /** Generate a dummy nonterminal.
167 Its name cannot conflict with the user's names. */
168 symbol *dummy_symbol_get (location loc);
171 /*--------------------.
172 | Methods on symbol. |
173 `--------------------*/
175 /** Print a symbol (for debugging). */
176 void symbol_print (symbol const *s, FILE *f);
178 /** Is this a dummy nonterminal? */
179 bool symbol_is_dummy (symbol const *sym);
181 /** The name of the code_props type: "\%destructor" or "\%printer". */
182 char const *code_props_type_string (code_props_type kind);
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);
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.
194 void symbol_make_alias (symbol *sym, symbol *str, location loc);
197 * This symbol is used as the lhs of a rule. Record this location
198 * as definition point, if not already done.
200 void symbol_location_as_lhs_set (symbol *sym, location loc);
202 /** Set the \c type_name associated with \c sym.
204 Do nothing if passed 0 as \c type_name. */
205 void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
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);
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);
216 /** Set the \c precedence associated with \c sym.
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);
222 /** Set the \c class associated with \c sym.
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);
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);
235 /*------------------.
236 | Special symbols. |
237 `------------------*/
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.
247 $accept: start-symbol $end */
248 extern symbol *acceptsymbol;
250 /** Whether a symbol declared with a type tag. */
251 extern bool tag_seen;
254 /*-------------------.
255 | Symbol Relations. |
256 `-------------------*/
258 /* The symbol relations are represented by a directed graph. */
260 /* The id of a node */
261 typedef int graphid;
263 typedef struct symgraphlink symgraphlink;
265 struct symgraphlink
267 /** The second \c symbol or group of a precedence relation.
268 * See \c symgraph. */
269 graphid id;
271 symgraphlink *next;
274 /* Symbol precedence graph, to store the used precedence relations between
275 * symbols. */
277 typedef struct symgraph symgraph;
279 struct symgraph
281 /** Identifier for the node: equal to the number of the symbol. */
282 graphid id;
284 /** The list of related symbols that have a smaller precedence. */
285 symgraphlink *succ;
287 /** The list of related symbols that have a greater precedence. */
288 symgraphlink *pred;
291 /** Register a new precedence relation as used. */
293 void register_precedence (graphid first, graphid snd);
295 /** Print a warning for each symbol whose precedence and/or associativity
296 * is useless. */
298 void print_precedence_warnings (void);
300 /*----------------------.
301 | Symbol associativity |
302 `----------------------*/
304 void register_assoc (graphid i, graphid j);
306 /*-----------------.
307 | Semantic types. |
308 `-----------------*/
310 /** A semantic type and its associated \c \%destructor and \c \%printer.
312 Access the fields of this struct only through the interface functions in
313 this file. \sa symbol::destructor */
314 typedef struct {
315 /** The key, name of the semantic type. */
316 uniqstr tag;
318 /** The location of its first occurrence. */
319 location location;
321 /** Its status : "undeclared", "used" or "declared".
322 It cannot be "needed". */
323 declaration_status status;
325 /** Any \c %destructor and %printer declared for this
326 semantic type. */
327 code_props props[CODE_PROPS_SIZE];
329 } semantic_type;
331 /** Fetch (or create) the semantic type associated to KEY. */
332 semantic_type *semantic_type_from_uniqstr (const uniqstr key,
333 const location *loc);
335 /** Fetch (or create) the semantic type associated to KEY. */
336 semantic_type *semantic_type_get (const char *key, const location *loc);
338 /** Set the \c destructor or \c printer associated with \c type. */
339 void semantic_type_code_props_set (semantic_type *type,
340 code_props_type kind,
341 code_props const *code);
343 /*----------------------------------.
344 | Symbol and semantic type tables. |
345 `----------------------------------*/
347 /** Create the symbol and semantic type tables, and the built-in
348 symbols. */
349 void symbols_new (void);
351 /** Free all the memory allocated for symbols and semantic types. */
352 void symbols_free (void);
354 /** Check that all the symbols are defined.
356 Report any undefined symbols and consider them nonterminals. */
357 void symbols_check_defined (void);
359 /** Sanity checks and #token_translations construction.
361 Perform various sanity checks, assign symbol numbers, and set up
362 #token_translations. */
363 void symbols_pack (void);
365 #endif /* !SYMTAB_H_ */