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