]> git.saurik.com Git - bison.git/blame_incremental - src/symtab.h
Fix push parsing memory leak reported by Brandon Lucia at
[bison.git] / src / symtab.h
... / ...
CommitLineData
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 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.
12
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.
17
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
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23/**
24 * \file symtab.h
25 * \brief Manipulating ::symbol.
26 */
27
28#ifndef SYMTAB_H_
29# define SYMTAB_H_
30
31# include "assoc.h"
32# include "location.h"
33# include "scan-code.h"
34# include "uniqstr.h"
35
36/*----------.
37| Symbols. |
38`----------*/
39
40/** Symbol classes. */
41typedef enum
42{
43 unknown_sym, /**< Undefined. */
44 token_sym, /**< Terminal. */
45 nterm_sym /**< Non-terminal. */
46} symbol_class;
47
48
49/** Internal token numbers. */
50typedef int symbol_number;
51#define SYMBOL_NUMBER_MAXIMUM INT_MAX
52
53
54typedef struct symbol symbol;
55
56/* When extending this structure, be sure to complete
57 symbol_check_alias_consistency. */
58struct symbol
59{
60 /** The key, name of the symbol. */
61 uniqstr tag;
62 /** The location of its first occurrence. */
63 location location;
64
65 /** Its \c \%type. */
66 uniqstr type_name;
67 /** Its \c \%type's location. */
68 location type_location;
69
70 /** Any \c \%destructor declared specifically for this symbol.
71
72 Access this field only through <tt>symbol</tt>'s interface functions. For
73 example, if <tt>symbol::destructor = NULL</tt>, a default \c \%destructor
74 or a per-type \c \%destructor might be appropriate, and
75 \c symbol_destructor_get will compute the correct one. */
76 code_props destructor;
77
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 */
82 code_props printer;
83
84 symbol_number number;
85 location prec_location;
86 int prec;
87 assoc assoc;
88 int user_token_number;
89
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
92 identifier-symbol pair for an alias. */
93 symbol *alias;
94 symbol_class class;
95 bool declared;
96};
97
98/** Undefined user number. */
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. */
108#define NUMBER_UNDEFINED (-1)
109
110/** Print a symbol (for debugging). */
111void symbol_print (symbol *s, FILE *f);
112
113/** Fetch (or create) the symbol associated to KEY. */
114symbol *symbol_from_uniqstr (const uniqstr key, location loc);
115
116/** Fetch (or create) the symbol associated to KEY. */
117symbol *symbol_get (const char *key, location loc);
118
119/** Generate a dummy nonterminal.
120
121 Its name cannot conflict with the user's names. */
122symbol *dummy_symbol_get (location loc);
123
124/** Is this a dummy nonterminal? */
125bool symbol_is_dummy (const symbol *sym);
126
127/** Declare the new symbol \c sym. Make it an alias of \c symval. */
128void symbol_make_alias (symbol *sym, symbol *symval, location loc);
129
130/** Set the \c type_name associated with \c sym.
131
132 Do nothing if passed 0 as \c type_name. */
133void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
134
135/** Set the \c destructor associated with \c sym. */
136void symbol_destructor_set (symbol *sym, code_props const *destructor);
137
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. */
140code_props const *symbol_destructor_get (symbol const *sym);
141
142/** Set the \c printer associated with \c sym. */
143void symbol_printer_set (symbol *sym, code_props const *printer);
144
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. */
147code_props const *symbol_printer_get (symbol const *sym);
148
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. */
153void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
154
155/** Set the \c class associated with \c sym. */
156void symbol_class_set (symbol *sym, symbol_class class, location loc,
157 bool declaring);
158
159/** Set the \c user_token_number associated with \c sym. */
160void symbol_user_token_number_set (symbol *sym, int user_number, location loc);
161
162
163/** The error token. */
164extern symbol *errtoken;
165/** The token for unknown tokens. */
166extern symbol *undeftoken;
167/** The end of input token. */
168extern symbol *endtoken;
169/** The genuine start symbol.
170
171 $accept: start-symbol $end */
172extern symbol *accept;
173
174/** The user start symbol. */
175extern symbol *startsymbol;
176/** The location of the \c \%start declaration. */
177extern location startsymbol_location;
178
179
180/*-----------------.
181| Semantic types. |
182`-----------------*/
183
184/** A semantic type and its associated \c \%destructor and \c \%printer.
185
186 Access the fields of this struct only through the interface functions in
187 this file. \sa symbol::destructor */
188typedef struct {
189 /** The key, name of the semantic type. */
190 uniqstr tag;
191
192 /** Any \c %destructor declared for this semantic type. */
193 code_props destructor;
194 /** Any \c %printer declared for this semantic type. */
195 code_props printer;
196} semantic_type;
197
198/** Fetch (or create) the semantic type associated to KEY. */
199semantic_type *semantic_type_from_uniqstr (const uniqstr key);
200
201/** Fetch (or create) the semantic type associated to KEY. */
202semantic_type *semantic_type_get (const char *key);
203
204/** Set the \c destructor associated with \c type. */
205void semantic_type_destructor_set (semantic_type *type,
206 code_props const *destructor);
207
208/** Set the \c printer associated with \c type. */
209void semantic_type_printer_set (semantic_type *type,
210 code_props const *printer);
211
212/*----------------------------------.
213| Symbol and semantic type tables. |
214`----------------------------------*/
215
216/** Create the symbol and semantic type tables. */
217void symbols_new (void);
218
219/** Free all the memory allocated for symbols and semantic types. */
220void symbols_free (void);
221
222/** Check that all the symbols are defined.
223
224 Report any undefined symbols and consider them nonterminals. */
225void symbols_check_defined (void);
226
227/** Sanity checks and #token_translations construction.
228
229 Perform various sanity checks, assign symbol numbers, and set up
230 #token_translations. */
231void symbols_pack (void);
232
233
234/*---------------------------------------.
235| Default %destructor's and %printer's. |
236`---------------------------------------*/
237
238/** Set the default \c \%destructor for tagged values. */
239void default_tagged_destructor_set (code_props const *destructor);
240/** Set the default \c \%destructor for tagless values. */
241void default_tagless_destructor_set (code_props const *destructor);
242
243/** Set the default \c \%printer for tagged values. */
244void default_tagged_printer_set (code_props const *printer);
245/** Set the default \c \%printer for tagless values. */
246void default_tagless_printer_set (code_props const *printer);
247
248#endif /* !SYMTAB_H_ */