]> git.saurik.com Git - bison.git/blame - src/symtab.h
Require default %destructor/%printer to be declared using
[bison.git] / src / symtab.h
CommitLineData
2cc6b612
PE
1/* Definitions for symtab.c and callers, part of Bison.
2
073f9288 3 Copyright (C) 1984, 1989, 1992, 2000, 2001, 2002, 2004, 2005, 2006
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
PE
32# include "location.h"
33# include "uniqstr.h"
8efe435c 34
2f1afb73
AD
35/*----------.
36| Symbols. |
37`----------*/
f7d4d87a 38
2073e1b6 39/** Symbol classes. */
d7020c20
AD
40typedef enum
41{
2073e1b6
AD
42 unknown_sym, /**< Undefined. */
43 token_sym, /**< Terminal. */
44 nterm_sym /**< Non-terminal. */
d7020c20 45} symbol_class;
340ef489 46
b87f8b21 47
2073e1b6 48/** Internal token numbers. */
f6fbd3da
PE
49typedef int symbol_number;
50#define SYMBOL_NUMBER_MAXIMUM INT_MAX
5fbb0954 51
5fbb0954 52
7486e51b 53typedef struct symbol symbol;
df09ef2e
AD
54
55/* When extending this structure, be sure to complete
56 symbol_check_alias_consistency. */
7486e51b 57struct symbol
340ef489 58{
2073e1b6 59 /** The key, name of the symbol. */
7486e51b 60 uniqstr tag;
2073e1b6 61 /** The location of its first occurrence. */
7486e51b 62 location location;
1e0bab92 63
ec5479ce 64 /** Its \c \%type. */
7486e51b 65 uniqstr type_name;
ec5479ce 66 /** Its \c \%type's location. */
df09ef2e
AD
67 location type_location;
68
ec5479ce
JD
69 /** Any \c \%destructor declared specifically for this symbol.
70
71 Access this field only through <tt>symbol</tt>'s interface functions. For
72 example, if <tt>symbol::destructor = NULL</tt>, the default
73 \c \%destructor or a per-type \c \%destructor might be appropriate, and
74 \c symbol_destructor_get will compute the correct one. */
c1432f65 75 const char *destructor;
ec5479ce
JD
76
77 /** The location of \c symbol::destructor.
78
79 Access this field only through <tt>symbol</tt>'s interface functions.
80 \sa symbol::destructor */
7486e51b 81 location destructor_location;
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 */
c1432f65 87 const char *printer;
ec5479ce
JD
88
89 /** The location of \c symbol::printer.
90
91 Access this field only through <tt>symbol</tt>'s interface functions.
92 \sa symbol::destructor */
7486e51b 93 location printer_location;
ee000ba4 94
7486e51b 95 symbol_number number;
df09ef2e 96 location prec_location;
f6fbd3da 97 int prec;
7486e51b 98 assoc assoc;
62a3e4f0 99 int user_token_number;
3f96f4dc 100
b87f8b21
AD
101 /* Points to the other in the identifier-symbol pair for an alias.
102 Special value USER_NUMBER_ALIAS in the identifier half of the
1e9798d5 103 identifier-symbol pair for an alias. */
7486e51b 104 symbol *alias;
d7020c20 105 symbol_class class;
073f9288 106 bool declared;
db8837cb
AD
107};
108
2073e1b6
AD
109
110/** Undefined user number. */
b87f8b21
AD
111#define USER_NUMBER_UNDEFINED -1
112
113/* `symbol->user_token_number == USER_NUMBER_ALIAS' means this symbol
114 *has* (not is) a string literal alias. For instance, `%token foo
115 "foo"' has `"foo"' numbered regularly, and `foo' numbered as
116 USER_NUMBER_ALIAS. */
117#define USER_NUMBER_ALIAS -9991
118
119/* Undefined internal token number. */
2cc6b612 120#define NUMBER_UNDEFINED (-1)
b87f8b21 121
2073e1b6 122/** Print a symbol (for debugging). */
22dda0f0 123void symbol_print (symbol *s, FILE *f);
f7d4d87a 124
2073e1b6 125/** Fetch (or create) the symbol associated to KEY. */
203b9274
AD
126symbol *symbol_from_uniqstr (const uniqstr key, location loc);
127
2073e1b6 128/** Fetch (or create) the symbol associated to KEY. */
7486e51b 129symbol *symbol_get (const char *key, location loc);
39f41916 130
2073e1b6
AD
131/** Generate a dummy nonterminal.
132
133 Its name cannot conflict with the user's names. */
7486e51b 134symbol *dummy_symbol_get (location loc);
2f1afb73 135
4d7370cb
JD
136/** Is this a dummy nonterminal? */
137bool symbol_is_dummy (const symbol *sym);
138
2073e1b6 139/** Declare the new symbol \c sym. Make it an alias of \c symval. */
7486e51b 140void symbol_make_alias (symbol *sym, symbol *symval, location loc);
2f1afb73 141
2073e1b6
AD
142/** Set the \c type_name associated with \c sym.
143
144 Do nothing if passed 0 as \c type_name. */
7486e51b 145void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
3ae2b51f 146
2073e1b6 147/** Set the \c destructor associated with \c sym. */
c1432f65 148void symbol_destructor_set (symbol *sym, const char *destructor, location loc);
9280d3ef 149
ec5479ce
JD
150/** Get the computed \c \%destructor for \c sym, or \c NULL if none. */
151const char *symbol_destructor_get (symbol *sym);
152
153/** Get the grammar location of the computed \c \%destructor for \c sym.
154
155 \pre <tt>symbol_destructor_get (sym) != NULL</tt> */
156location symbol_destructor_location_get (symbol *sym);
157
2073e1b6 158/** Set the \c printer associated with \c sym. */
c1432f65 159void symbol_printer_set (symbol *sym, const char *printer, location loc);
366eea36 160
ec5479ce
JD
161/** Get the computed \c \%printer for \c sym, or \c NULL if none. */
162const char *symbol_printer_get (symbol *sym);
163
164/** Get the grammar location of the computed \c \%printer for \c sym.
165
166 \pre <tt>symbol_printer_get (sym) != NULL</tt> */
167location symbol_printer_location_get (symbol *sym);
168
2073e1b6
AD
169/* Set the \c precedence associated with \c sym.
170
171 Ensure that \a symbol is a terminal.
172 Do nothing if invoked with \c undef_assoc as \c assoc. */
7486e51b 173void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
3ae2b51f 174
2073e1b6 175/** Set the \c class associated with \c sym. */
073f9288
PE
176void symbol_class_set (symbol *sym, symbol_class class, location loc,
177 bool declaring);
44536b35 178
2073e1b6 179/** Set the \c user_token_number associated with \c sym. */
7486e51b 180void symbol_user_token_number_set (symbol *sym, int user_number, location loc);
44536b35
AD
181
182
2073e1b6 183/** The error token. */
7486e51b 184extern symbol *errtoken;
2073e1b6 185/** The token for unknown tokens. */
7486e51b 186extern symbol *undeftoken;
2073e1b6 187/** The end of input token. */
7486e51b 188extern symbol *endtoken;
2073e1b6
AD
189/** The genuine start symbol.
190
191 $accept: start-symbol $end */
7486e51b 192extern symbol *accept;
2073e1b6
AD
193
194/** The user start symbol. */
7486e51b 195extern symbol *startsymbol;
ec5479ce 196/** The location of the \c \%start declaration. */
7486e51b 197extern location startsymbol_location;
f7d4d87a 198
340ef489 199
2f1afb73
AD
200/*---------------.
201| Symbol table. |
202`---------------*/
203
204
2073e1b6 205/** Create the symbol table. */
d33cb3ae 206void symbols_new (void);
2f1afb73 207
2073e1b6 208/** Free all the memory allocated for symbols. */
d33cb3ae 209void symbols_free (void);
340ef489 210
2073e1b6
AD
211/** Check that all the symbols are defined.
212
213 Report any undefined symbols and consider them nonterminals. */
d33cb3ae 214void symbols_check_defined (void);
2f1afb73 215
2073e1b6
AD
216/** Sanity checks and #token_translations construction.
217
218 Perform various sanity checks, assign symbol numbers, and set up
219 #token_translations. */
d33cb3ae 220void symbols_pack (void);
2f1afb73 221
ec5479ce
JD
222
223/*-----------------------------------.
224| Default %destructor and %printer. |
225`-----------------------------------*/
226
227/** Set the default \c \%destructor. */
228void default_destructor_set (const char *destructor, location loc);
229
230/** Set the default \c \%printer. */
231void default_printer_set (const char *printer, location loc);
232
340ef489 233#endif /* !SYMTAB_H_ */