]> git.saurik.com Git - bison.git/blame - src/symtab.h
Merge branch 'maint'
[bison.git] / src / symtab.h
CommitLineData
2cc6b612
PE
1/* Definitions for symtab.c and callers, part of Bison.
2
34136e65 3 Copyright (C) 1984, 1989, 1992, 2000-2002, 2004-2012 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
PE
48typedef int symbol_number;
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
57 %printer/%destructor, it is "used". If not "declared" by used in a
58 rule, it is "needed". Finally, if declared (via a rule for
59 nonterminals, or %oken), it is "declared".
60
61 When status are checked at the end, "declared" symbols are fine,
62 "used" symbols trigger warnings, otherwise it's an error.
63*/
64
b921d92f
VS
65typedef enum
66 {
3b0b682f
AD
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 gramar (rules) but not defined (error). */
72 needed,
73 /** Defined with %type or %token (good). */
74 declared,
b921d92f
VS
75 } status;
76
71da68b3
VS
77typedef enum code_props_type code_props_type;
78enum code_props_type
79 {
80 destructor = 0,
81 printer = 1,
82 };
83
84enum { CODE_PROPS_SIZE = 2 };
85
df09ef2e
AD
86/* When extending this structure, be sure to complete
87 symbol_check_alias_consistency. */
7486e51b 88struct symbol
340ef489 89{
2073e1b6 90 /** The key, name of the symbol. */
7486e51b 91 uniqstr tag;
2073e1b6 92 /** The location of its first occurrence. */
7486e51b 93 location location;
1e0bab92 94
9a86ee60
AD
95 /** Its \c \%type.
96
97 Beware that this is the type_name as was entered by the user,
98 including silly things such as "]" if she entered "%token <]> t".
99 Therefore, when outputting type_name to M4, be sure to escape it
100 into "@}". See quoted_output for instance. */
7486e51b 101 uniqstr type_name;
9c46ba16 102
ec5479ce 103 /** Its \c \%type's location. */
df09ef2e
AD
104 location type_location;
105
71da68b3
VS
106 /** Any \c \%destructor (resp. \%printer) declared specificially for this
107 symbol.
ec5479ce 108
71da68b3 109 Access this field only through <tt>symbol</tt>'s interface functions. For
4323e0da 110 example, if <tt>symbol::destructor = NULL</tt> (resp. <tt>symbol::printer
71da68b3 111 = NULL</tt>), a default \c \%destructor (resp. \%printer) or a per-type
4323e0da 112 \c symbol_destructor_printer_get will compute the correct one. */
71da68b3 113 code_props props[CODE_PROPS_SIZE];
ee000ba4 114
7486e51b 115 symbol_number number;
df09ef2e 116 location prec_location;
f6fbd3da 117 int prec;
7486e51b 118 assoc assoc;
62a3e4f0 119 int user_token_number;
3f96f4dc 120
dfaa4860
JD
121 /* Points to the other in the symbol-string pair for an alias.
122 Special value USER_NUMBER_HAS_STRING_ALIAS in the symbol half of the
123 symbol-string pair for an alias. */
7486e51b 124 symbol *alias;
d7020c20 125 symbol_class class;
b921d92f 126 status status;
db8837cb
AD
127};
128
2073e1b6 129/** Undefined user number. */
b87f8b21
AD
130#define USER_NUMBER_UNDEFINED -1
131
dfaa4860
JD
132/* `symbol->user_token_number == USER_NUMBER_HAS_STRING_ALIAS' means
133 this symbol has a literal string alias. For instance, `%token foo
b87f8b21 134 "foo"' has `"foo"' numbered regularly, and `foo' numbered as
dfaa4860
JD
135 USER_NUMBER_HAS_STRING_ALIAS. */
136#define USER_NUMBER_HAS_STRING_ALIAS -9991
b87f8b21
AD
137
138/* Undefined internal token number. */
2cc6b612 139#define NUMBER_UNDEFINED (-1)
b87f8b21 140
2073e1b6 141/** Fetch (or create) the symbol associated to KEY. */
203b9274
AD
142symbol *symbol_from_uniqstr (const uniqstr key, location loc);
143
2073e1b6 144/** Fetch (or create) the symbol associated to KEY. */
7486e51b 145symbol *symbol_get (const char *key, location loc);
39f41916 146
2073e1b6
AD
147/** Generate a dummy nonterminal.
148
149 Its name cannot conflict with the user's names. */
7486e51b 150symbol *dummy_symbol_get (location loc);
2f1afb73 151
aea10ef4
AD
152
153/*--------------------.
154| Methods on symbol. |
155`--------------------*/
156
157/** Print a symbol (for debugging). */
c5289832 158void symbol_print (symbol const *s, FILE *f);
aea10ef4 159
4d7370cb
JD
160/** Is this a dummy nonterminal? */
161bool symbol_is_dummy (const symbol *sym);
162
6a0655d9
AD
163/** The name of the code_props type: "\%destructor" or "\%printer". */
164char const *code_props_type_string (code_props_type kind);
165
166/** The name of the symbol that can be used as an identifier.
aea10ef4
AD
167 ** Consider the alias if needed.
168 ** Return 0 if there is none (e.g., the symbol is only defined as
169 ** a string). */
170uniqstr symbol_id_get (symbol const *sym);
171
dfaa4860
JD
172/**
173 * Make \c str the literal string alias of \c sym. Copy token number,
174 * symbol number, and type from \c sym to \c str.
175 */
176void symbol_make_alias (symbol *sym, symbol *str, location loc);
2f1afb73 177
2073e1b6
AD
178/** Set the \c type_name associated with \c sym.
179
180 Do nothing if passed 0 as \c type_name. */
7486e51b 181void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
3ae2b51f 182
71da68b3
VS
183/** Set the \c \%destructor or \c \%printer associated with \c sym. */
184void symbol_code_props_set (symbol *sym, code_props_type kind,
185 code_props const *destructor);
db06f0ce 186
71da68b3 187/** Get the computed \c \%destructor or \c %printer for \c sym, which was
0560fa24
AD
188 initialized with \c code_props_none_init if there's no \c \%destructor or
189 \c %printer. */
70946cff 190code_props *symbol_code_props_get (symbol *sym, code_props_type kind);
ec5479ce 191
0560fa24 192/** Set the \c precedence associated with \c sym.
2073e1b6 193
0560fa24
AD
194 Ensure that \a symbol is a terminal.
195 Do nothing if invoked with \c undef_assoc as \c assoc. */
7486e51b 196void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
3ae2b51f 197
2073e1b6 198/** Set the \c class associated with \c sym. */
073f9288 199void symbol_class_set (symbol *sym, symbol_class class, location loc,
e9690142 200 bool declaring);
44536b35 201
2073e1b6 202/** Set the \c user_token_number associated with \c sym. */
7486e51b 203void symbol_user_token_number_set (symbol *sym, int user_number, location loc);
44536b35
AD
204
205
aea10ef4
AD
206
207/*------------------.
208| Special symbols. |
209`------------------*/
210
2073e1b6 211/** The error token. */
7486e51b 212extern symbol *errtoken;
2073e1b6 213/** The token for unknown tokens. */
7486e51b 214extern symbol *undeftoken;
2073e1b6 215/** The end of input token. */
7486e51b 216extern symbol *endtoken;
2073e1b6
AD
217/** The genuine start symbol.
218
219 $accept: start-symbol $end */
7486e51b 220extern symbol *accept;
2073e1b6
AD
221
222/** The user start symbol. */
7486e51b 223extern symbol *startsymbol;
ec5479ce 224/** The location of the \c \%start declaration. */
7486e51b 225extern location startsymbol_location;
f7d4d87a 226
340ef489 227
b2a0b7ca
JD
228/*-----------------.
229| Semantic types. |
230`-----------------*/
231
232/** A semantic type and its associated \c \%destructor and \c \%printer.
db06f0ce 233
b2a0b7ca
JD
234 Access the fields of this struct only through the interface functions in
235 this file. \sa symbol::destructor */
db06f0ce 236typedef struct {
b2a0b7ca
JD
237 /** The key, name of the semantic type. */
238 uniqstr tag;
239
9641b918
VS
240 /** The location of its first occurence. */
241 location location;
242
243 /** Its status : "undeclared", "used" or "declared".
244 It cannot be "needed". */
245 status status;
246
71da68b3
VS
247 /** Any \c %destructor and %printer declared for this
248 semantic type. */
249 code_props props[CODE_PROPS_SIZE];
250
b2a0b7ca
JD
251} semantic_type;
252
253/** Fetch (or create) the semantic type associated to KEY. */
9641b918
VS
254semantic_type *semantic_type_from_uniqstr (const uniqstr key,
255 const location *loc);
b2a0b7ca
JD
256
257/** Fetch (or create) the semantic type associated to KEY. */
9641b918 258semantic_type *semantic_type_get (const char *key, const location *loc);
b2a0b7ca 259
71da68b3
VS
260/** Set the \c destructor or \c printer associated with \c type. */
261void semantic_type_code_props_set (semantic_type *type,
262 code_props_type kind,
263 code_props const *code);
2f1afb73 264
b2a0b7ca
JD
265/*----------------------------------.
266| Symbol and semantic type tables. |
267`----------------------------------*/
2f1afb73 268
b2a0b7ca 269/** Create the symbol and semantic type tables. */
d33cb3ae 270void symbols_new (void);
2f1afb73 271
b2a0b7ca 272/** Free all the memory allocated for symbols and semantic types. */
d33cb3ae 273void symbols_free (void);
340ef489 274
2073e1b6
AD
275/** Check that all the symbols are defined.
276
277 Report any undefined symbols and consider them nonterminals. */
d33cb3ae 278void symbols_check_defined (void);
2f1afb73 279
2073e1b6
AD
280/** Sanity checks and #token_translations construction.
281
282 Perform various sanity checks, assign symbol numbers, and set up
283 #token_translations. */
d33cb3ae 284void symbols_pack (void);
2f1afb73 285
340ef489 286#endif /* !SYMTAB_H_ */