]> git.saurik.com Git - bison.git/blame - src/symtab.h
Factor the tables.
[bison.git] / src / symtab.h
CommitLineData
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
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
39typedef enum
40{
2073e1b6
AD
41 unknown_sym, /**< Undefined. */
42 token_sym, /**< Terminal. */
43 nterm_sym /**< Non-terminal. */
d7020c20 44} symbol_class;
340ef489 45
b87f8b21 46
2073e1b6 47/** Internal token numbers. */
f6fbd3da
PE
48typedef int symbol_number;
49#define SYMBOL_NUMBER_MAXIMUM INT_MAX
5fbb0954 50
5fbb0954 51
7486e51b 52typedef struct symbol symbol;
df09ef2e
AD
53
54/* When extending this structure, be sure to complete
55 symbol_check_alias_consistency. */
7486e51b 56struct symbol
340ef489 57{
2073e1b6 58 /** The key, name of the symbol. */
7486e51b 59 uniqstr tag;
2073e1b6 60 /** The location of its first occurrence. */
7486e51b 61 location location;
1e0bab92 62
ec5479ce 63 /** Its \c \%type. */
7486e51b 64 uniqstr type_name;
ec5479ce 65 /** Its \c \%type's location. */
df09ef2e
AD
66 location type_location;
67
ec5479ce
JD
68 /** Any \c \%destructor declared specifically for this symbol.
69
70 Access this field only through <tt>symbol</tt>'s interface functions. For
12e35840
JD
71 example, if <tt>symbol::destructor = NULL</tt>, a default \c \%destructor
72 or a per-type \c \%destructor might be appropriate, and
ec5479ce 73 \c symbol_destructor_get will compute the correct one. */
95021767 74 code_props destructor;
df09ef2e 75
ec5479ce
JD
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 */
95021767 80 code_props printer;
ee000ba4 81
7486e51b 82 symbol_number number;
df09ef2e 83 location prec_location;
f6fbd3da 84 int prec;
7486e51b 85 assoc assoc;
62a3e4f0 86 int user_token_number;
3f96f4dc 87
b87f8b21
AD
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
1e9798d5 90 identifier-symbol pair for an alias. */
7486e51b 91 symbol *alias;
d7020c20 92 symbol_class class;
073f9288 93 bool declared;
db8837cb
AD
94};
95
2073e1b6 96/** Undefined user number. */
b87f8b21
AD
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. */
2cc6b612 106#define NUMBER_UNDEFINED (-1)
b87f8b21 107
2073e1b6 108/** Print a symbol (for debugging). */
22dda0f0 109void symbol_print (symbol *s, FILE *f);
f7d4d87a 110
2073e1b6 111/** Fetch (or create) the symbol associated to KEY. */
203b9274
AD
112symbol *symbol_from_uniqstr (const uniqstr key, location loc);
113
2073e1b6 114/** Fetch (or create) the symbol associated to KEY. */
7486e51b 115symbol *symbol_get (const char *key, location loc);
39f41916 116
2073e1b6
AD
117/** Generate a dummy nonterminal.
118
119 Its name cannot conflict with the user's names. */
7486e51b 120symbol *dummy_symbol_get (location loc);
2f1afb73 121
4d7370cb
JD
122/** Is this a dummy nonterminal? */
123bool symbol_is_dummy (const symbol *sym);
124
2073e1b6 125/** Declare the new symbol \c sym. Make it an alias of \c symval. */
7486e51b 126void symbol_make_alias (symbol *sym, symbol *symval, location loc);
2f1afb73 127
2073e1b6
AD
128/** Set the \c type_name associated with \c sym.
129
130 Do nothing if passed 0 as \c type_name. */
7486e51b 131void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
3ae2b51f 132
41d7a5f2
PE
133/** Get the \c class string associated with \c sym. */
134const char *symbol_class_get_string (symbol *sym);
135
2073e1b6 136/** Set the \c destructor associated with \c sym. */
95021767 137void symbol_destructor_set (symbol *sym, code_props const *destructor);
db06f0ce 138
95021767
JD
139/** Get the computed \c \%destructor for \c sym, which was initialized with
140 \c code_props_none_init if there's no \c \%destructor. */
141code_props const *symbol_destructor_get (symbol const *sym);
ec5479ce 142
2073e1b6 143/** Set the \c printer associated with \c sym. */
95021767 144void symbol_printer_set (symbol *sym, code_props const *printer);
366eea36 145
95021767
JD
146/** Get the computed \c \%printer for \c sym, which was initialized with
147 \c code_props_none_init if there's no \c \%printer. */
148code_props const *symbol_printer_get (symbol const *sym);
ec5479ce 149
2073e1b6
AD
150/* Set the \c precedence associated with \c sym.
151
152 Ensure that \a symbol is a terminal.
153 Do nothing if invoked with \c undef_assoc as \c assoc. */
7486e51b 154void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
3ae2b51f 155
2073e1b6 156/** Set the \c class associated with \c sym. */
073f9288
PE
157void symbol_class_set (symbol *sym, symbol_class class, location loc,
158 bool declaring);
44536b35 159
2073e1b6 160/** Set the \c user_token_number associated with \c sym. */
7486e51b 161void symbol_user_token_number_set (symbol *sym, int user_number, location loc);
44536b35
AD
162
163
2073e1b6 164/** The error token. */
7486e51b 165extern symbol *errtoken;
2073e1b6 166/** The token for unknown tokens. */
7486e51b 167extern symbol *undeftoken;
2073e1b6 168/** The end of input token. */
7486e51b 169extern symbol *endtoken;
2073e1b6
AD
170/** The genuine start symbol.
171
172 $accept: start-symbol $end */
7486e51b 173extern symbol *accept;
2073e1b6
AD
174
175/** The user start symbol. */
7486e51b 176extern symbol *startsymbol;
ec5479ce 177/** The location of the \c \%start declaration. */
7486e51b 178extern location startsymbol_location;
f7d4d87a 179
340ef489 180
b2a0b7ca
JD
181/*-----------------.
182| Semantic types. |
183`-----------------*/
184
185/** A semantic type and its associated \c \%destructor and \c \%printer.
db06f0ce 186
b2a0b7ca
JD
187 Access the fields of this struct only through the interface functions in
188 this file. \sa symbol::destructor */
db06f0ce 189typedef struct {
b2a0b7ca
JD
190 /** The key, name of the semantic type. */
191 uniqstr tag;
192
193 /** Any \c %destructor declared for this semantic type. */
95021767 194 code_props destructor;
b2a0b7ca 195 /** Any \c %printer declared for this semantic type. */
95021767 196 code_props printer;
b2a0b7ca
JD
197} semantic_type;
198
199/** Fetch (or create) the semantic type associated to KEY. */
200semantic_type *semantic_type_from_uniqstr (const uniqstr key);
201
202/** Fetch (or create) the semantic type associated to KEY. */
203semantic_type *semantic_type_get (const char *key);
204
205/** Set the \c destructor associated with \c type. */
95021767
JD
206void semantic_type_destructor_set (semantic_type *type,
207 code_props const *destructor);
b2a0b7ca
JD
208
209/** Set the \c printer associated with \c type. */
95021767
JD
210void semantic_type_printer_set (semantic_type *type,
211 code_props const *printer);
2f1afb73 212
b2a0b7ca
JD
213/*----------------------------------.
214| Symbol and semantic type tables. |
215`----------------------------------*/
2f1afb73 216
b2a0b7ca 217/** Create the symbol and semantic type tables. */
d33cb3ae 218void symbols_new (void);
2f1afb73 219
b2a0b7ca 220/** Free all the memory allocated for symbols and semantic types. */
d33cb3ae 221void symbols_free (void);
340ef489 222
2073e1b6
AD
223/** Check that all the symbols are defined.
224
225 Report any undefined symbols and consider them nonterminals. */
d33cb3ae 226void symbols_check_defined (void);
2f1afb73 227
2073e1b6
AD
228/** Sanity checks and #token_translations construction.
229
230 Perform various sanity checks, assign symbol numbers, and set up
231 #token_translations. */
d33cb3ae 232void symbols_pack (void);
2f1afb73 233
ec5479ce 234
12e35840
JD
235/*---------------------------------------.
236| Default %destructor's and %printer's. |
237`---------------------------------------*/
ec5479ce 238
12e35840 239/** Set the default \c \%destructor for tagged values. */
95021767 240void default_tagged_destructor_set (code_props const *destructor);
12e35840 241/** Set the default \c \%destructor for tagless values. */
95021767 242void default_tagless_destructor_set (code_props const *destructor);
ec5479ce 243
12e35840 244/** Set the default \c \%printer for tagged values. */
95021767 245void default_tagged_printer_set (code_props const *printer);
12e35840 246/** Set the default \c \%printer for tagless values. */
95021767 247void default_tagless_printer_set (code_props const *printer);
ec5479ce 248
340ef489 249#endif /* !SYMTAB_H_ */