]> git.saurik.com Git - bison.git/blob - src/symtab.h
* GNUmakefile: Switch to coreutils's version.
[bison.git] / src / symtab.h
1 /* Definitions for symtab.c and callers, part of Bison.
2
3 Copyright (C) 1984, 1989, 1992, 2000, 2001, 2002, 2004, 2005, 2006, 2007
4 Free Software 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 /* When extending this structure, be sure to complete
55 symbol_check_alias_consistency. */
56 struct symbol
57 {
58 /** The key, name of the symbol. */
59 uniqstr tag;
60 /** The location of its first occurrence. */
61 location location;
62
63 /** Its \c \%type. */
64 uniqstr type_name;
65 /** Its \c \%type's location. */
66 location type_location;
67
68 /** Any \c \%destructor declared specifically for this symbol.
69
70 Access this field only through <tt>symbol</tt>'s interface functions. For
71 example, if <tt>symbol::destructor = NULL</tt>, a default \c \%destructor
72 or a per-type \c \%destructor might be appropriate, and
73 \c symbol_destructor_get will compute the correct one. */
74 code_props destructor;
75
76 /** Any \c \%printer declared specifically for this symbol.
77
78 Access this field only through <tt>symbol</tt>'s interface functions.
79 \sa symbol::destructor */
80 code_props printer;
81
82 symbol_number number;
83 location prec_location;
84 int prec;
85 assoc assoc;
86 int user_token_number;
87
88 /* Points to the other in the identifier-symbol pair for an alias.
89 Special value USER_NUMBER_ALIAS in the identifier half of the
90 identifier-symbol pair for an alias. */
91 symbol *alias;
92 symbol_class class;
93 bool declared;
94 };
95
96 /** Undefined user number. */
97 #define USER_NUMBER_UNDEFINED -1
98
99 /* `symbol->user_token_number == USER_NUMBER_ALIAS' means this symbol
100 *has* (not is) a string literal alias. For instance, `%token foo
101 "foo"' has `"foo"' numbered regularly, and `foo' numbered as
102 USER_NUMBER_ALIAS. */
103 #define USER_NUMBER_ALIAS -9991
104
105 /* Undefined internal token number. */
106 #define NUMBER_UNDEFINED (-1)
107
108 /** Print a symbol (for debugging). */
109 void symbol_print (symbol *s, FILE *f);
110
111 /** Fetch (or create) the symbol associated to KEY. */
112 symbol *symbol_from_uniqstr (const uniqstr key, location loc);
113
114 /** Fetch (or create) the symbol associated to KEY. */
115 symbol *symbol_get (const char *key, location loc);
116
117 /** Generate a dummy nonterminal.
118
119 Its name cannot conflict with the user's names. */
120 symbol *dummy_symbol_get (location loc);
121
122 /** Is this a dummy nonterminal? */
123 bool symbol_is_dummy (const symbol *sym);
124
125 /** Declare the new symbol \c sym. Make it an alias of \c symval. */
126 void symbol_make_alias (symbol *sym, symbol *symval, location loc);
127
128 /** Set the \c type_name associated with \c sym.
129
130 Do nothing if passed 0 as \c type_name. */
131 void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
132
133 /** Set the \c destructor associated with \c sym. */
134 void symbol_destructor_set (symbol *sym, code_props const *destructor);
135
136 /** Get the computed \c \%destructor for \c sym, which was initialized with
137 \c code_props_none_init if there's no \c \%destructor. */
138 code_props const *symbol_destructor_get (symbol const *sym);
139
140 /** Set the \c printer associated with \c sym. */
141 void symbol_printer_set (symbol *sym, code_props const *printer);
142
143 /** Get the computed \c \%printer for \c sym, which was initialized with
144 \c code_props_none_init if there's no \c \%printer. */
145 code_props const *symbol_printer_get (symbol const *sym);
146
147 /* Set the \c precedence associated with \c sym.
148
149 Ensure that \a symbol is a terminal.
150 Do nothing if invoked with \c undef_assoc as \c assoc. */
151 void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
152
153 /** Set the \c class associated with \c sym. */
154 void symbol_class_set (symbol *sym, symbol_class class, location loc,
155 bool declaring);
156
157 /** Set the \c user_token_number associated with \c sym. */
158 void symbol_user_token_number_set (symbol *sym, int user_number, location loc);
159
160
161 /** The error token. */
162 extern symbol *errtoken;
163 /** The token for unknown tokens. */
164 extern symbol *undeftoken;
165 /** The end of input token. */
166 extern symbol *endtoken;
167 /** The genuine start symbol.
168
169 $accept: start-symbol $end */
170 extern symbol *accept;
171
172 /** The user start symbol. */
173 extern symbol *startsymbol;
174 /** The location of the \c \%start declaration. */
175 extern location startsymbol_location;
176
177
178 /*-----------------.
179 | Semantic types. |
180 `-----------------*/
181
182 /** A semantic type and its associated \c \%destructor and \c \%printer.
183
184 Access the fields of this struct only through the interface functions in
185 this file. \sa symbol::destructor */
186 typedef struct {
187 /** The key, name of the semantic type. */
188 uniqstr tag;
189
190 /** Any \c %destructor declared for this semantic type. */
191 code_props destructor;
192 /** Any \c %printer declared for this semantic type. */
193 code_props printer;
194 } semantic_type;
195
196 /** Fetch (or create) the semantic type associated to KEY. */
197 semantic_type *semantic_type_from_uniqstr (const uniqstr key);
198
199 /** Fetch (or create) the semantic type associated to KEY. */
200 semantic_type *semantic_type_get (const char *key);
201
202 /** Set the \c destructor associated with \c type. */
203 void semantic_type_destructor_set (semantic_type *type,
204 code_props const *destructor);
205
206 /** Set the \c printer associated with \c type. */
207 void semantic_type_printer_set (semantic_type *type,
208 code_props const *printer);
209
210 /*----------------------------------.
211 | Symbol and semantic type tables. |
212 `----------------------------------*/
213
214 /** Create the symbol and semantic type tables. */
215 void symbols_new (void);
216
217 /** Free all the memory allocated for symbols and semantic types. */
218 void symbols_free (void);
219
220 /** Check that all the symbols are defined.
221
222 Report any undefined symbols and consider them nonterminals. */
223 void symbols_check_defined (void);
224
225 /** Sanity checks and #token_translations construction.
226
227 Perform various sanity checks, assign symbol numbers, and set up
228 #token_translations. */
229 void symbols_pack (void);
230
231
232 /*---------------------------------------.
233 | Default %destructor's and %printer's. |
234 `---------------------------------------*/
235
236 /** Set the default \c \%destructor for tagged values. */
237 void default_tagged_destructor_set (code_props const *destructor);
238 /** Set the default \c \%destructor for tagless values. */
239 void default_tagless_destructor_set (code_props const *destructor);
240
241 /** Set the default \c \%printer for tagged values. */
242 void default_tagged_printer_set (code_props const *printer);
243 /** Set the default \c \%printer for tagless values. */
244 void default_tagless_printer_set (code_props const *printer);
245
246 #endif /* !SYMTAB_H_ */