]>
Commit | Line | Data |
---|---|---|
2cc6b612 PE |
1 | /* Definitions for symtab.c and callers, part of Bison. |
2 | ||
95021767 | 3 | Copyright (C) 1984, 1989, 1992, 2000, 2001, 2002, 2004, 2005, 2006, 2007 |
03b31c0c | 4 | Free Software Foundation, Inc. |
f7d4d87a | 5 | |
340ef489 | 6 | This file is part of Bison, the GNU Compiler Compiler. |
f7d4d87a | 7 | |
340ef489 AD |
8 | Bison 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 2, or (at your option) | |
11 | any later version. | |
f7d4d87a | 12 | |
340ef489 AD |
13 | Bison 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. | |
f7d4d87a | 17 | |
340ef489 AD |
18 | You should have received a copy of the GNU General Public License |
19 | along with Bison; see the file COPYING. If not, write to | |
0fb669f9 PE |
20 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
f7d4d87a | 22 | |
2073e1b6 AD |
23 | /** |
24 | * \file symtab.h | |
25 | * \brief Manipulating ::symbol. | |
26 | */ | |
27 | ||
340ef489 AD |
28 | #ifndef SYMTAB_H_ |
29 | # define SYMTAB_H_ | |
f7d4d87a | 30 | |
a945ec39 | 31 | # include "assoc.h" |
7486e51b | 32 | # include "location.h" |
95021767 | 33 | # include "scan-code.h" |
7486e51b | 34 | # include "uniqstr.h" |
8efe435c | 35 | |
2f1afb73 AD |
36 | /*----------. |
37 | | Symbols. | | |
38 | `----------*/ | |
f7d4d87a | 39 | |
2073e1b6 | 40 | /** Symbol classes. */ |
d7020c20 AD |
41 | typedef enum |
42 | { | |
2073e1b6 AD |
43 | unknown_sym, /**< Undefined. */ |
44 | token_sym, /**< Terminal. */ | |
45 | nterm_sym /**< Non-terminal. */ | |
d7020c20 | 46 | } symbol_class; |
340ef489 | 47 | |
b87f8b21 | 48 | |
2073e1b6 | 49 | /** Internal token numbers. */ |
f6fbd3da PE |
50 | typedef int symbol_number; |
51 | #define SYMBOL_NUMBER_MAXIMUM INT_MAX | |
5fbb0954 | 52 | |
5fbb0954 | 53 | |
7486e51b | 54 | typedef struct symbol symbol; |
df09ef2e AD |
55 | |
56 | /* When extending this structure, be sure to complete | |
57 | symbol_check_alias_consistency. */ | |
7486e51b | 58 | struct symbol |
340ef489 | 59 | { |
2073e1b6 | 60 | /** The key, name of the symbol. */ |
7486e51b | 61 | uniqstr tag; |
2073e1b6 | 62 | /** The location of its first occurrence. */ |
7486e51b | 63 | location location; |
1e0bab92 | 64 | |
ec5479ce | 65 | /** Its \c \%type. */ |
7486e51b | 66 | uniqstr type_name; |
ec5479ce | 67 | /** Its \c \%type's location. */ |
df09ef2e AD |
68 | location type_location; |
69 | ||
ec5479ce JD |
70 | /** Any \c \%destructor declared specifically for this symbol. |
71 | ||
72 | Access this field only through <tt>symbol</tt>'s interface functions. For | |
12e35840 JD |
73 | example, if <tt>symbol::destructor = NULL</tt>, a default \c \%destructor |
74 | or a per-type \c \%destructor might be appropriate, and | |
ec5479ce | 75 | \c symbol_destructor_get will compute the correct one. */ |
95021767 | 76 | code_props destructor; |
df09ef2e | 77 | |
ec5479ce JD |
78 | /** Any \c \%printer declared specifically for this symbol. |
79 | ||
80 | Access this field only through <tt>symbol</tt>'s interface functions. | |
81 | \sa symbol::destructor */ | |
95021767 | 82 | code_props printer; |
ee000ba4 | 83 | |
7486e51b | 84 | symbol_number number; |
df09ef2e | 85 | location prec_location; |
f6fbd3da | 86 | int prec; |
7486e51b | 87 | assoc assoc; |
62a3e4f0 | 88 | int user_token_number; |
3f96f4dc | 89 | |
b87f8b21 AD |
90 | /* Points to the other in the identifier-symbol pair for an alias. |
91 | Special value USER_NUMBER_ALIAS in the identifier half of the | |
1e9798d5 | 92 | identifier-symbol pair for an alias. */ |
7486e51b | 93 | symbol *alias; |
d7020c20 | 94 | symbol_class class; |
073f9288 | 95 | bool declared; |
db8837cb AD |
96 | }; |
97 | ||
2073e1b6 | 98 | /** Undefined user number. */ |
b87f8b21 AD |
99 | #define USER_NUMBER_UNDEFINED -1 |
100 | ||
101 | /* `symbol->user_token_number == USER_NUMBER_ALIAS' means this symbol | |
102 | *has* (not is) a string literal alias. For instance, `%token foo | |
103 | "foo"' has `"foo"' numbered regularly, and `foo' numbered as | |
104 | USER_NUMBER_ALIAS. */ | |
105 | #define USER_NUMBER_ALIAS -9991 | |
106 | ||
107 | /* Undefined internal token number. */ | |
2cc6b612 | 108 | #define NUMBER_UNDEFINED (-1) |
b87f8b21 | 109 | |
2073e1b6 | 110 | /** Print a symbol (for debugging). */ |
22dda0f0 | 111 | void symbol_print (symbol *s, FILE *f); |
f7d4d87a | 112 | |
2073e1b6 | 113 | /** Fetch (or create) the symbol associated to KEY. */ |
203b9274 AD |
114 | symbol *symbol_from_uniqstr (const uniqstr key, location loc); |
115 | ||
2073e1b6 | 116 | /** Fetch (or create) the symbol associated to KEY. */ |
7486e51b | 117 | symbol *symbol_get (const char *key, location loc); |
39f41916 | 118 | |
2073e1b6 AD |
119 | /** Generate a dummy nonterminal. |
120 | ||
121 | Its name cannot conflict with the user's names. */ | |
7486e51b | 122 | symbol *dummy_symbol_get (location loc); |
2f1afb73 | 123 | |
4d7370cb JD |
124 | /** Is this a dummy nonterminal? */ |
125 | bool symbol_is_dummy (const symbol *sym); | |
126 | ||
2073e1b6 | 127 | /** Declare the new symbol \c sym. Make it an alias of \c symval. */ |
7486e51b | 128 | void symbol_make_alias (symbol *sym, symbol *symval, location loc); |
2f1afb73 | 129 | |
2073e1b6 AD |
130 | /** Set the \c type_name associated with \c sym. |
131 | ||
132 | Do nothing if passed 0 as \c type_name. */ | |
7486e51b | 133 | void symbol_type_set (symbol *sym, uniqstr type_name, location loc); |
3ae2b51f | 134 | |
2073e1b6 | 135 | /** Set the \c destructor associated with \c sym. */ |
95021767 | 136 | void symbol_destructor_set (symbol *sym, code_props const *destructor); |
db06f0ce | 137 | |
95021767 JD |
138 | /** Get the computed \c \%destructor for \c sym, which was initialized with |
139 | \c code_props_none_init if there's no \c \%destructor. */ | |
140 | code_props const *symbol_destructor_get (symbol const *sym); | |
ec5479ce | 141 | |
2073e1b6 | 142 | /** Set the \c printer associated with \c sym. */ |
95021767 | 143 | void symbol_printer_set (symbol *sym, code_props const *printer); |
366eea36 | 144 | |
95021767 JD |
145 | /** Get the computed \c \%printer for \c sym, which was initialized with |
146 | \c code_props_none_init if there's no \c \%printer. */ | |
147 | code_props const *symbol_printer_get (symbol const *sym); | |
ec5479ce | 148 | |
2073e1b6 AD |
149 | /* Set the \c precedence associated with \c sym. |
150 | ||
151 | Ensure that \a symbol is a terminal. | |
152 | Do nothing if invoked with \c undef_assoc as \c assoc. */ | |
7486e51b | 153 | void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc); |
3ae2b51f | 154 | |
2073e1b6 | 155 | /** Set the \c class associated with \c sym. */ |
073f9288 PE |
156 | void symbol_class_set (symbol *sym, symbol_class class, location loc, |
157 | bool declaring); | |
44536b35 | 158 | |
2073e1b6 | 159 | /** Set the \c user_token_number associated with \c sym. */ |
7486e51b | 160 | void symbol_user_token_number_set (symbol *sym, int user_number, location loc); |
44536b35 AD |
161 | |
162 | ||
2073e1b6 | 163 | /** The error token. */ |
7486e51b | 164 | extern symbol *errtoken; |
2073e1b6 | 165 | /** The token for unknown tokens. */ |
7486e51b | 166 | extern symbol *undeftoken; |
2073e1b6 | 167 | /** The end of input token. */ |
7486e51b | 168 | extern symbol *endtoken; |
2073e1b6 AD |
169 | /** The genuine start symbol. |
170 | ||
171 | $accept: start-symbol $end */ | |
7486e51b | 172 | extern symbol *accept; |
2073e1b6 AD |
173 | |
174 | /** The user start symbol. */ | |
7486e51b | 175 | extern symbol *startsymbol; |
ec5479ce | 176 | /** The location of the \c \%start declaration. */ |
7486e51b | 177 | extern location startsymbol_location; |
f7d4d87a | 178 | |
340ef489 | 179 | |
b2a0b7ca JD |
180 | /*-----------------. |
181 | | Semantic types. | | |
182 | `-----------------*/ | |
183 | ||
184 | /** A semantic type and its associated \c \%destructor and \c \%printer. | |
db06f0ce | 185 | |
b2a0b7ca JD |
186 | Access the fields of this struct only through the interface functions in |
187 | this file. \sa symbol::destructor */ | |
db06f0ce | 188 | typedef struct { |
b2a0b7ca JD |
189 | /** The key, name of the semantic type. */ |
190 | uniqstr tag; | |
191 | ||
192 | /** Any \c %destructor declared for this semantic type. */ | |
95021767 | 193 | code_props destructor; |
b2a0b7ca | 194 | /** Any \c %printer declared for this semantic type. */ |
95021767 | 195 | code_props printer; |
b2a0b7ca JD |
196 | } semantic_type; |
197 | ||
198 | /** Fetch (or create) the semantic type associated to KEY. */ | |
199 | semantic_type *semantic_type_from_uniqstr (const uniqstr key); | |
200 | ||
201 | /** Fetch (or create) the semantic type associated to KEY. */ | |
202 | semantic_type *semantic_type_get (const char *key); | |
203 | ||
204 | /** Set the \c destructor associated with \c type. */ | |
95021767 JD |
205 | void semantic_type_destructor_set (semantic_type *type, |
206 | code_props const *destructor); | |
b2a0b7ca JD |
207 | |
208 | /** Set the \c printer associated with \c type. */ | |
95021767 JD |
209 | void semantic_type_printer_set (semantic_type *type, |
210 | code_props const *printer); | |
2f1afb73 | 211 | |
b2a0b7ca JD |
212 | /*----------------------------------. |
213 | | Symbol and semantic type tables. | | |
214 | `----------------------------------*/ | |
2f1afb73 | 215 | |
b2a0b7ca | 216 | /** Create the symbol and semantic type tables. */ |
d33cb3ae | 217 | void symbols_new (void); |
2f1afb73 | 218 | |
b2a0b7ca | 219 | /** Free all the memory allocated for symbols and semantic types. */ |
d33cb3ae | 220 | void symbols_free (void); |
340ef489 | 221 | |
2073e1b6 AD |
222 | /** Check that all the symbols are defined. |
223 | ||
224 | Report any undefined symbols and consider them nonterminals. */ | |
d33cb3ae | 225 | void symbols_check_defined (void); |
2f1afb73 | 226 | |
2073e1b6 AD |
227 | /** Sanity checks and #token_translations construction. |
228 | ||
229 | Perform various sanity checks, assign symbol numbers, and set up | |
230 | #token_translations. */ | |
d33cb3ae | 231 | void symbols_pack (void); |
2f1afb73 | 232 | |
ec5479ce | 233 | |
12e35840 JD |
234 | /*---------------------------------------. |
235 | | Default %destructor's and %printer's. | | |
236 | `---------------------------------------*/ | |
ec5479ce | 237 | |
12e35840 | 238 | /** Set the default \c \%destructor for tagged values. */ |
95021767 | 239 | void default_tagged_destructor_set (code_props const *destructor); |
12e35840 | 240 | /** Set the default \c \%destructor for tagless values. */ |
95021767 | 241 | void default_tagless_destructor_set (code_props const *destructor); |
ec5479ce | 242 | |
12e35840 | 243 | /** Set the default \c \%printer for tagged values. */ |
95021767 | 244 | void default_tagged_printer_set (code_props const *printer); |
12e35840 | 245 | /** Set the default \c \%printer for tagless values. */ |
95021767 | 246 | void default_tagless_printer_set (code_props const *printer); |
ec5479ce | 247 | |
340ef489 | 248 | #endif /* !SYMTAB_H_ */ |