]>
Commit | Line | Data |
---|---|---|
1 | /* Definitions for symtab.c and callers, part of Bison. | |
2 | ||
3 | Copyright (C) 1984, 1989, 1992, 2000-2002, 2004-2012 Free Software | |
4 | 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 /**< Non-terminal. */ | |
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 | ||
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 | ||
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 gramar (rules) but not defined (error). */ | |
72 | needed, | |
73 | /** Defined with %type or %token (good). */ | |
74 | declared, | |
75 | } status; | |
76 | ||
77 | typedef enum code_props_type code_props_type; | |
78 | enum code_props_type | |
79 | { | |
80 | destructor = 0, | |
81 | printer = 1, | |
82 | }; | |
83 | ||
84 | enum { CODE_PROPS_SIZE = 2 }; | |
85 | ||
86 | /* When extending this structure, be sure to complete | |
87 | symbol_check_alias_consistency. */ | |
88 | struct symbol | |
89 | { | |
90 | /** The key, name of the symbol. */ | |
91 | uniqstr tag; | |
92 | /** The location of its first occurrence. */ | |
93 | location location; | |
94 | ||
95 | /** Its \c \%type. */ | |
96 | uniqstr type_name; | |
97 | /** Its \c \%type's location. */ | |
98 | location type_location; | |
99 | ||
100 | /** Any \c \%destructor (resp. \%printer) declared specificially for this | |
101 | symbol. | |
102 | ||
103 | Access this field only through <tt>symbol</tt>'s interface functions. For | |
104 | Example, if <tt>symbol::destructor = NULL</tt> (resp. <tt>symbol::printer | |
105 | = NULL</tt>), a default \c \%destructor (resp. \%printer) or a per-type | |
106 | \c symbol_destructor_printer_get will compute the corect one. */ | |
107 | code_props props[CODE_PROPS_SIZE]; | |
108 | ||
109 | symbol_number number; | |
110 | location prec_location; | |
111 | int prec; | |
112 | assoc assoc; | |
113 | int user_token_number; | |
114 | ||
115 | /* Points to the other in the symbol-string pair for an alias. | |
116 | Special value USER_NUMBER_HAS_STRING_ALIAS in the symbol half of the | |
117 | symbol-string pair for an alias. */ | |
118 | symbol *alias; | |
119 | symbol_class class; | |
120 | status status; | |
121 | }; | |
122 | ||
123 | /** Undefined user number. */ | |
124 | #define USER_NUMBER_UNDEFINED -1 | |
125 | ||
126 | /* `symbol->user_token_number == USER_NUMBER_HAS_STRING_ALIAS' means | |
127 | this symbol has a literal string alias. For instance, `%token foo | |
128 | "foo"' has `"foo"' numbered regularly, and `foo' numbered as | |
129 | USER_NUMBER_HAS_STRING_ALIAS. */ | |
130 | #define USER_NUMBER_HAS_STRING_ALIAS -9991 | |
131 | ||
132 | /* Undefined internal token number. */ | |
133 | #define NUMBER_UNDEFINED (-1) | |
134 | ||
135 | /** Fetch (or create) the symbol associated to KEY. */ | |
136 | symbol *symbol_from_uniqstr (const uniqstr key, location loc); | |
137 | ||
138 | /** Fetch (or create) the symbol associated to KEY. */ | |
139 | symbol *symbol_get (const char *key, location loc); | |
140 | ||
141 | /** Generate a dummy nonterminal. | |
142 | ||
143 | Its name cannot conflict with the user's names. */ | |
144 | symbol *dummy_symbol_get (location loc); | |
145 | ||
146 | ||
147 | /*--------------------. | |
148 | | Methods on symbol. | | |
149 | `--------------------*/ | |
150 | ||
151 | /** Print a symbol (for debugging). */ | |
152 | void symbol_print (symbol *s, FILE *f); | |
153 | ||
154 | /** Is this a dummy nonterminal? */ | |
155 | bool symbol_is_dummy (const symbol *sym); | |
156 | ||
157 | /** The name of the code_props type: "\%destructor" or "\%printer". */ | |
158 | char const *code_props_type_string (code_props_type kind); | |
159 | ||
160 | /** The name of the symbol that can be used as an identifier. | |
161 | ** Consider the alias if needed. | |
162 | ** Return 0 if there is none (e.g., the symbol is only defined as | |
163 | ** a string). */ | |
164 | uniqstr symbol_id_get (symbol const *sym); | |
165 | ||
166 | /** | |
167 | * Make \c str the literal string alias of \c sym. Copy token number, | |
168 | * symbol number, and type from \c sym to \c str. | |
169 | */ | |
170 | void symbol_make_alias (symbol *sym, symbol *str, location loc); | |
171 | ||
172 | /** Set the \c type_name associated with \c sym. | |
173 | ||
174 | Do nothing if passed 0 as \c type_name. */ | |
175 | void symbol_type_set (symbol *sym, uniqstr type_name, location loc); | |
176 | ||
177 | /** Set the \c \%destructor or \c \%printer associated with \c sym. */ | |
178 | void symbol_code_props_set (symbol *sym, code_props_type kind, | |
179 | code_props const *destructor); | |
180 | ||
181 | /** Get the computed \c \%destructor or \c %printer for \c sym, which was | |
182 | initialized with \c code_props_none_init if there's no \c \%destructor or | |
183 | \c %printer. */ | |
184 | code_props const *symbol_code_props_get (symbol const *sym, | |
185 | code_props_type kind); | |
186 | ||
187 | /* Set the \c precedence associated with \c sym. | |
188 | ||
189 | Ensure that \a symbol is a terminal. | |
190 | Do nothing if invoked with \c undef_assoc as \c assoc. */ | |
191 | void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc); | |
192 | ||
193 | /** Set the \c class associated with \c sym. */ | |
194 | void symbol_class_set (symbol *sym, symbol_class class, location loc, | |
195 | bool declaring); | |
196 | ||
197 | /** Set the \c user_token_number associated with \c sym. */ | |
198 | void symbol_user_token_number_set (symbol *sym, int user_number, location loc); | |
199 | ||
200 | ||
201 | ||
202 | /*------------------. | |
203 | | Special symbols. | | |
204 | `------------------*/ | |
205 | ||
206 | /** The error token. */ | |
207 | extern symbol *errtoken; | |
208 | /** The token for unknown tokens. */ | |
209 | extern symbol *undeftoken; | |
210 | /** The end of input token. */ | |
211 | extern symbol *endtoken; | |
212 | /** The genuine start symbol. | |
213 | ||
214 | $accept: start-symbol $end */ | |
215 | extern symbol *accept; | |
216 | ||
217 | /** The user start symbol. */ | |
218 | extern symbol *startsymbol; | |
219 | /** The location of the \c \%start declaration. */ | |
220 | extern location startsymbol_location; | |
221 | ||
222 | ||
223 | /*-----------------. | |
224 | | Semantic types. | | |
225 | `-----------------*/ | |
226 | ||
227 | /** A semantic type and its associated \c \%destructor and \c \%printer. | |
228 | ||
229 | Access the fields of this struct only through the interface functions in | |
230 | this file. \sa symbol::destructor */ | |
231 | typedef struct { | |
232 | /** The key, name of the semantic type. */ | |
233 | uniqstr tag; | |
234 | ||
235 | /** The location of its first occurence. */ | |
236 | location location; | |
237 | ||
238 | /** Its status : "undeclared", "used" or "declared". | |
239 | It cannot be "needed". */ | |
240 | status status; | |
241 | ||
242 | /** Any \c %destructor and %printer declared for this | |
243 | semantic type. */ | |
244 | code_props props[CODE_PROPS_SIZE]; | |
245 | ||
246 | } semantic_type; | |
247 | ||
248 | /** Fetch (or create) the semantic type associated to KEY. */ | |
249 | semantic_type *semantic_type_from_uniqstr (const uniqstr key, | |
250 | const location *loc); | |
251 | ||
252 | /** Fetch (or create) the semantic type associated to KEY. */ | |
253 | semantic_type *semantic_type_get (const char *key, const location *loc); | |
254 | ||
255 | /** Set the \c destructor or \c printer associated with \c type. */ | |
256 | void semantic_type_code_props_set (semantic_type *type, | |
257 | code_props_type kind, | |
258 | code_props const *code); | |
259 | ||
260 | /*----------------------------------. | |
261 | | Symbol and semantic type tables. | | |
262 | `----------------------------------*/ | |
263 | ||
264 | /** Create the symbol and semantic type tables. */ | |
265 | void symbols_new (void); | |
266 | ||
267 | /** Free all the memory allocated for symbols and semantic types. */ | |
268 | void symbols_free (void); | |
269 | ||
270 | /** Check that all the symbols are defined. | |
271 | ||
272 | Report any undefined symbols and consider them nonterminals. */ | |
273 | void symbols_check_defined (void); | |
274 | ||
275 | /** Sanity checks and #token_translations construction. | |
276 | ||
277 | Perform various sanity checks, assign symbol numbers, and set up | |
278 | #token_translations. */ | |
279 | void symbols_pack (void); | |
280 | ||
281 | ||
282 | /*---------------------------------------. | |
283 | | Default %destructor's and %printer's. | | |
284 | `---------------------------------------*/ | |
285 | ||
286 | /** Set the default \c \%destructor or \c \%printer for tagged values. */ | |
287 | void default_tagged_code_props_set (code_props_type kind, | |
288 | code_props const *code); | |
289 | /** Set the default \c \%destructor or \c \%printer for tagless values. */ | |
290 | void default_tagless_code_props_set (code_props_type kind, | |
291 | code_props const *destructor); | |
292 | ||
293 | #endif /* !SYMTAB_H_ */ |