]> git.saurik.com Git - bison.git/blame - src/symtab.c
Rename all the `bucket's as `symbol_t'.
[bison.git] / src / symtab.c
CommitLineData
40675e7c 1/* Symbol table manager for Bison,
72a23c97 2 Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
40675e7c 3
95e36146 4 This file is part of Bison, the GNU Compiler Compiler.
40675e7c 5
95e36146
AD
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
40675e7c 10
95e36146
AD
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
40675e7c 15
95e36146
AD
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
40675e7c
DM
20
21
40675e7c 22#include "system.h"
72a23c97 23#include "hash.h"
40675e7c
DM
24#include "symtab.h"
25#include "gram.h"
26
72a23c97
AD
27/*---------------------------------.
28| Create a new symbol, named TAG. |
29`---------------------------------*/
1e9798d5 30
db8837cb
AD
31static symbol_t *
32symbol_new (const char *tag)
1e9798d5 33{
db8837cb 34 symbol_t *res = XMALLOC (symbol_t, 1);
1e9798d5 35
1e9798d5
AD
36 res->tag = xstrdup (tag);
37 res->type_name = NULL;
d9b739c3 38 res->number = -1;
1e9798d5
AD
39 res->prec = 0;
40 res->assoc = right_assoc;
6b7e85b9 41 res->user_token_number = SUNDEF;
1e9798d5
AD
42 res->alias = NULL;
43 res->class = unknown_sym;
44
72a23c97
AD
45 if (getenv ("DEBUG"))
46 fprintf (stderr, "Creating: nsyms = %d, ntokens = %d: %s\n",
47 nsyms, ntokens, tag);
1e9798d5
AD
48 nsyms++;
49
50 return res;
51}
52
40675e7c 53
72a23c97
AD
54/*------------.
55| Free THIS. |
56`------------*/
57
58static void
db8837cb 59symbol_free (symbol_t *this)
40675e7c 60{
72a23c97
AD
61#if 0
62 /* This causes crashes because one string can appear more
63 than once. */
64 XFREE (this->type_name);
65#endif
66 XFREE (this->tag);
67 XFREE (this);
68}
69
70
71
72/*----------------------.
db8837cb 73| A symbol_t hash table. |
72a23c97 74`----------------------*/
40675e7c 75
db8837cb 76/* Initial capacity of symbols hash table. */
72a23c97
AD
77#define HT_INITIAL_CAPACITY 257
78
db8837cb 79static struct hash_table *symbol_table = NULL;
72a23c97
AD
80
81static bool
db8837cb 82hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2)
72a23c97
AD
83{
84 return strcmp (m1->tag, m2->tag) ? FALSE : TRUE;
85}
86
87static unsigned int
db8837cb 88hash_symbol_t (const symbol_t *m, unsigned int tablesize)
72a23c97
AD
89{
90 return hash_string (m->tag, tablesize);
91}
92
93
94/*-------------------------------.
db8837cb 95| Create the symbol_t hash table. |
72a23c97
AD
96`-------------------------------*/
97
98void
db8837cb 99symbols_new (void)
72a23c97 100{
db8837cb 101 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
72a23c97 102 NULL,
db8837cb
AD
103 (Hash_hasher) hash_symbol_t,
104 (Hash_comparator) hash_compare_symbol_t,
105 (Hash_data_freer) symbol_free);
40675e7c
DM
106}
107
108
1e9798d5
AD
109/*----------------------------------------------------------------.
110| Find the symbol named KEY, and return it. If it does not exist |
111| yet, create it. |
112`----------------------------------------------------------------*/
113
db8837cb 114symbol_t *
4a120d45 115getsym (const char *key)
40675e7c 116{
db8837cb
AD
117 symbol_t probe;
118 symbol_t *entry;
40675e7c 119
72a23c97 120 (const char *) probe.tag = key;
db8837cb 121 entry = hash_lookup (symbol_table, &probe);
40675e7c 122
72a23c97 123 if (!entry)
40675e7c 124 {
72a23c97 125 /* First insertion in the hash. */
db8837cb
AD
126 entry = symbol_new (key);
127 hash_insert (symbol_table, entry);
40675e7c 128 }
72a23c97
AD
129 return entry;
130}
40675e7c 131
40675e7c 132
72a23c97 133/*-------------------.
db8837cb 134| Free the symbols. |
72a23c97
AD
135`-------------------*/
136
137void
db8837cb 138symbols_free (void)
72a23c97 139{
db8837cb 140 hash_free (symbol_table);
40675e7c
DM
141}
142
143
72a23c97 144/*---------------------------------------------------------------.
db8837cb 145| Look for undefined symbols, report an error, and consider them |
72a23c97
AD
146| terminals. |
147`---------------------------------------------------------------*/
148
40675e7c 149void
db8837cb 150symbols_do (symbol_processor processor, void *processor_data)
40675e7c 151{
db8837cb 152 hash_do_for_each (symbol_table,
72a23c97
AD
153 (Hash_processor) processor,
154 processor_data);
40675e7c 155}