]> git.saurik.com Git - bison.git/blame - src/symtab.h
version 3.0
[bison.git] / src / symtab.h
CommitLineData
2cc6b612
PE
1/* Definitions for symtab.c and callers, part of Bison.
2
7d6bad19 3 Copyright (C) 1984, 1989, 1992, 2000-2002, 2004-2013 Free Software
575619af 4 Foundation, Inc.
f7d4d87a 5
340ef489 6 This file is part of Bison, the GNU Compiler Compiler.
f7d4d87a 7
f16b0819 8 This program is free software: you can redistribute it and/or modify
340ef489 9 it under the terms of the GNU General Public License as published by
f16b0819
PE
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
f7d4d87a 12
f16b0819 13 This program is distributed in the hope that it will be useful,
340ef489
AD
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.
f7d4d87a 17
340ef489 18 You should have received a copy of the GNU General Public License
f16b0819 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
f7d4d87a 20
2073e1b6
AD
21/**
22 * \file symtab.h
23 * \brief Manipulating ::symbol.
24 */
25
340ef489
AD
26#ifndef SYMTAB_H_
27# define SYMTAB_H_
f7d4d87a 28
a945ec39 29# include "assoc.h"
7486e51b 30# include "location.h"
95021767 31# include "scan-code.h"
7486e51b 32# include "uniqstr.h"
8efe435c 33
2f1afb73
AD
34/*----------.
35| Symbols. |
36`----------*/
f7d4d87a 37
2073e1b6 38/** Symbol classes. */
d7020c20
AD
39typedef enum
40{
2073e1b6 41 unknown_sym, /**< Undefined. */
e9690142
JD
42 token_sym, /**< Terminal. */
43 nterm_sym /**< Non-terminal. */
d7020c20 44} symbol_class;
340ef489 45
b87f8b21 46
2073e1b6 47/** Internal token numbers. */
f6fbd3da 48typedef int symbol_number;
a99ec53e 49# define SYMBOL_NUMBER_MAXIMUM INT_MAX
5fbb0954 50
5fbb0954 51
7486e51b 52typedef struct symbol symbol;
df09ef2e 53
3b0b682f
AD
54/* Declaration status of a symbol.
55
56 First, it is "undeclared". Then, if "undeclared" and used in a
3edfae04
AD
57 %printer/%destructor, it is "used". If not "declared" but used in
58 a rule, it is "needed". Finally, if declared (via a rule for
59 nonterminals, or %token), it is "declared".
3b0b682f
AD
60
61 When status are checked at the end, "declared" symbols are fine,
3edfae04 62 "used" symbols trigger warnings, otherwise it's an error. */
3b0b682f 63
b921d92f
VS
64typedef enum
65 {
3b0b682f
AD
66 /** Used in the input file for an unknown reason (error). */
67 undeclared,
68 /** Used by %destructor/%printer but not defined (warning). */
69 used,
70 /** Used in the gramar (rules) but not defined (error). */
71 needed,
72 /** Defined with %type or %token (good). */
73 declared,
b921d92f
VS
74 } status;
75
71da68b3
VS
76typedef enum code_props_type code_props_type;
77enum code_props_type
78 {
79 destructor = 0,
80 printer = 1,
81 };
82
83enum { CODE_PROPS_SIZE = 2 };
84
df09ef2e
AD
85/* When extending this structure, be sure to complete
86 symbol_check_alias_consistency. */
7486e51b 87struct symbol
340ef489 88{
2073e1b6 89 /** The key, name of the symbol. */
7486e51b 90 uniqstr tag;
2073e1b6 91 /** The location of its first occurrence. */
7486e51b 92 location location;
1e0bab92 93
9a86ee60
AD
94 /** Its \c \%type.
95
96 Beware that this is the type_name as was entered by the user,
97 including silly things such as "]" if she entered "%token <]> t".
98 Therefore, when outputting type_name to M4, be sure to escape it
99 into "@}". See quoted_output for instance. */
7486e51b 100 uniqstr type_name;
9c46ba16 101
ec5479ce 102 /** Its \c \%type's location. */
df09ef2e
AD
103 location type_location;
104
71da68b3
VS
105 /** Any \c \%destructor (resp. \%printer) declared specificially for this
106 symbol.
ec5479ce 107
71da68b3 108 Access this field only through <tt>symbol</tt>'s interface functions. For
4323e0da 109 example, if <tt>symbol::destructor = NULL</tt> (resp. <tt>symbol::printer
71da68b3 110 = NULL</tt>), a default \c \%destructor (resp. \%printer) or a per-type
4323e0da 111 \c symbol_destructor_printer_get will compute the correct one. */
71da68b3 112 code_props props[CODE_PROPS_SIZE];
ee000ba4 113
7486e51b 114 symbol_number number;
df09ef2e 115 location prec_location;
f6fbd3da 116 int prec;
7486e51b 117 assoc assoc;
62a3e4f0 118 int user_token_number;
3f96f4dc 119
dfaa4860
JD
120 /* Points to the other in the symbol-string pair for an alias.
121 Special value USER_NUMBER_HAS_STRING_ALIAS in the symbol half of the
122 symbol-string pair for an alias. */
7486e51b 123 symbol *alias;
d7020c20 124 symbol_class class;
b921d92f 125 status status;
db8837cb
AD
126};
127
2073e1b6 128/** Undefined user number. */
a99ec53e 129# define USER_NUMBER_UNDEFINED -1
b87f8b21 130
45eebca4
AD
131/* 'symbol->user_token_number == USER_NUMBER_HAS_STRING_ALIAS' means
132 this symbol has a literal string alias. For instance, '%token foo
133 "foo"' has '"foo"' numbered regularly, and 'foo' numbered as
dfaa4860 134 USER_NUMBER_HAS_STRING_ALIAS. */
a99ec53e 135# define USER_NUMBER_HAS_STRING_ALIAS -9991
b87f8b21
AD
136
137/* Undefined internal token number. */
a99ec53e 138# define NUMBER_UNDEFINED (-1)
b87f8b21 139
2073e1b6 140/** Fetch (or create) the symbol associated to KEY. */
203b9274
AD
141symbol *symbol_from_uniqstr (const uniqstr key, location loc);
142
2073e1b6 143/** Fetch (or create) the symbol associated to KEY. */
7486e51b 144symbol *symbol_get (const char *key, location loc);
39f41916 145
2073e1b6
AD
146/** Generate a dummy nonterminal.
147
148 Its name cannot conflict with the user's names. */
7486e51b 149symbol *dummy_symbol_get (location loc);
2f1afb73 150
aea10ef4
AD
151
152/*--------------------.
153| Methods on symbol. |
154`--------------------*/
155
156/** Print a symbol (for debugging). */
c5289832 157void symbol_print (symbol const *s, FILE *f);
aea10ef4 158
4d7370cb
JD
159/** Is this a dummy nonterminal? */
160bool symbol_is_dummy (const symbol *sym);
161
6a0655d9
AD
162/** The name of the code_props type: "\%destructor" or "\%printer". */
163char const *code_props_type_string (code_props_type kind);
164
165/** The name of the symbol that can be used as an identifier.
aea10ef4
AD
166 ** Consider the alias if needed.
167 ** Return 0 if there is none (e.g., the symbol is only defined as
168 ** a string). */
169uniqstr symbol_id_get (symbol const *sym);
170
dfaa4860
JD
171/**
172 * Make \c str the literal string alias of \c sym. Copy token number,
173 * symbol number, and type from \c sym to \c str.
174 */
175void symbol_make_alias (symbol *sym, symbol *str, location loc);
2f1afb73 176
2073e1b6
AD
177/** Set the \c type_name associated with \c sym.
178
179 Do nothing if passed 0 as \c type_name. */
7486e51b 180void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
3ae2b51f 181
71da68b3
VS
182/** Set the \c \%destructor or \c \%printer associated with \c sym. */
183void symbol_code_props_set (symbol *sym, code_props_type kind,
184 code_props const *destructor);
db06f0ce 185
71da68b3 186/** Get the computed \c \%destructor or \c %printer for \c sym, which was
0560fa24
AD
187 initialized with \c code_props_none_init if there's no \c \%destructor or
188 \c %printer. */
70946cff 189code_props *symbol_code_props_get (symbol *sym, code_props_type kind);
ec5479ce 190
0560fa24 191/** Set the \c precedence associated with \c sym.
2073e1b6 192
0560fa24
AD
193 Ensure that \a symbol is a terminal.
194 Do nothing if invoked with \c undef_assoc as \c assoc. */
7486e51b 195void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
3ae2b51f 196
2073e1b6 197/** Set the \c class associated with \c sym. */
073f9288 198void symbol_class_set (symbol *sym, symbol_class class, location loc,
e9690142 199 bool declaring);
44536b35 200
2073e1b6 201/** Set the \c user_token_number associated with \c sym. */
7486e51b 202void symbol_user_token_number_set (symbol *sym, int user_number, location loc);
44536b35
AD
203
204
aea10ef4
AD
205
206/*------------------.
207| Special symbols. |
208`------------------*/
209
2073e1b6 210/** The error token. */
7486e51b 211extern symbol *errtoken;
2073e1b6 212/** The token for unknown tokens. */
7486e51b 213extern symbol *undeftoken;
2073e1b6 214/** The end of input token. */
7486e51b 215extern symbol *endtoken;
2073e1b6
AD
216/** The genuine start symbol.
217
218 $accept: start-symbol $end */
7486e51b 219extern symbol *accept;
2073e1b6
AD
220
221/** The user start symbol. */
7486e51b 222extern symbol *startsymbol;
ec5479ce 223/** The location of the \c \%start declaration. */
7486e51b 224extern location startsymbol_location;
f7d4d87a 225
340ef489 226
284bc49c
VT
227
228/*-------------------.
229| Symbol Relations. |
230`-------------------*/
231
232/* The symbol relations are represented by a directed graph. */
233
234/* The id of a node */
235typedef int graphid;
236
237typedef struct symgraphlink symgraphlink;
238
239struct symgraphlink
240{
241 /** The second \c symbol or group of a precedence relation.
242 * See \c symgraph. */
243 graphid id;
244
245 symgraphlink *next;
246};
247
248/* Symbol precedence graph, to store the used precedence relations between
249 * symbols. */
250
251typedef struct symgraph symgraph;
252
253struct symgraph
254{
255 /** Identifier for the node: equal to the number of the symbol. */
256 graphid id;
257
258 /** The list of related symbols that have a smaller precedence. */
259 symgraphlink *succ;
260
261 /** The list of related symbols that have a greater precedence. */
262 symgraphlink *pred;
263};
264
265/** Register a new precedence relation as used. */
266
267void register_precedence (graphid first, graphid snd);
268
cc2235ac
VT
269/** Print a warning for each symbol whose precedence and/or associativity
270 * is useless. */
284bc49c
VT
271
272void print_precedence_warnings (void);
273
e8f7155d
VT
274/*----------------------.
275| Symbol associativity |
276`----------------------*/
277
cc2235ac 278void register_assoc (graphid i, graphid j);
e8f7155d 279
b2a0b7ca
JD
280/*-----------------.
281| Semantic types. |
282`-----------------*/
283
284/** A semantic type and its associated \c \%destructor and \c \%printer.
db06f0ce 285
b2a0b7ca
JD
286 Access the fields of this struct only through the interface functions in
287 this file. \sa symbol::destructor */
db06f0ce 288typedef struct {
b2a0b7ca
JD
289 /** The key, name of the semantic type. */
290 uniqstr tag;
291
9641b918
VS
292 /** The location of its first occurence. */
293 location location;
294
295 /** Its status : "undeclared", "used" or "declared".
296 It cannot be "needed". */
297 status status;
298
71da68b3
VS
299 /** Any \c %destructor and %printer declared for this
300 semantic type. */
301 code_props props[CODE_PROPS_SIZE];
302
b2a0b7ca
JD
303} semantic_type;
304
305/** Fetch (or create) the semantic type associated to KEY. */
9641b918
VS
306semantic_type *semantic_type_from_uniqstr (const uniqstr key,
307 const location *loc);
b2a0b7ca
JD
308
309/** Fetch (or create) the semantic type associated to KEY. */
9641b918 310semantic_type *semantic_type_get (const char *key, const location *loc);
b2a0b7ca 311
71da68b3
VS
312/** Set the \c destructor or \c printer associated with \c type. */
313void semantic_type_code_props_set (semantic_type *type,
314 code_props_type kind,
315 code_props const *code);
2f1afb73 316
b2a0b7ca
JD
317/*----------------------------------.
318| Symbol and semantic type tables. |
319`----------------------------------*/
2f1afb73 320
b2a0b7ca 321/** Create the symbol and semantic type tables. */
d33cb3ae 322void symbols_new (void);
2f1afb73 323
b2a0b7ca 324/** Free all the memory allocated for symbols and semantic types. */
d33cb3ae 325void symbols_free (void);
340ef489 326
2073e1b6
AD
327/** Check that all the symbols are defined.
328
329 Report any undefined symbols and consider them nonterminals. */
d33cb3ae 330void symbols_check_defined (void);
2f1afb73 331
2073e1b6
AD
332/** Sanity checks and #token_translations construction.
333
334 Perform various sanity checks, assign symbol numbers, and set up
335 #token_translations. */
d33cb3ae 336void symbols_pack (void);
2f1afb73 337
340ef489 338#endif /* !SYMTAB_H_ */