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