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