1 /* Symbol table manager for Bison,
2 Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
27 /*---------------------------------.
28 | Create a new symbol, named TAG. |
29 `---------------------------------*/
32 bucket_new (const char *tag
)
34 bucket
*res
= XMALLOC (bucket
, 1);
36 res
->tag
= xstrdup (tag
);
37 res
->type_name
= NULL
;
40 res
->assoc
= right_assoc
;
41 res
->user_token_number
= SUNDEF
;
43 res
->class = unknown_sym
;
46 fprintf (stderr
, "Creating: nsyms = %d, ntokens = %d: %s\n",
59 bucket_free (bucket
*this)
62 /* This causes crashes because one string can appear more
64 XFREE (this->type_name
);
72 /*----------------------.
73 | A bucket hash table. |
74 `----------------------*/
76 /* Initial capacity of buckets hash table. */
77 #define HT_INITIAL_CAPACITY 257
79 static struct hash_table
*bucket_table
= NULL
;
82 hash_compare_bucket (const bucket
*m1
, const bucket
*m2
)
84 return strcmp (m1
->tag
, m2
->tag
) ? FALSE
: TRUE
;
88 hash_bucket (const bucket
*m
, unsigned int tablesize
)
90 return hash_string (m
->tag
, tablesize
);
94 /*-------------------------------.
95 | Create the bucket hash table. |
96 `-------------------------------*/
101 bucket_table
= hash_initialize (HT_INITIAL_CAPACITY
,
103 (Hash_hasher
) hash_bucket
,
104 (Hash_comparator
) hash_compare_bucket
,
105 (Hash_data_freer
) bucket_free
);
109 /*----------------------------------------------------------------.
110 | Find the symbol named KEY, and return it. If it does not exist |
112 `----------------------------------------------------------------*/
115 getsym (const char *key
)
120 (const char *) probe
.tag
= key
;
121 entry
= hash_lookup (bucket_table
, &probe
);
125 /* First insertion in the hash. */
126 entry
= bucket_new (key
);
127 hash_insert (bucket_table
, entry
);
133 /*-------------------.
134 | Free the buckets. |
135 `-------------------*/
140 hash_free (bucket_table
);
144 /*---------------------------------------------------------------.
145 | Look for undefined buckets, report an error, and consider them |
147 `---------------------------------------------------------------*/
150 buckets_do (bucket_processor processor
, void *processor_data
)
152 hash_do_for_each (bucket_table
,
153 (Hash_processor
) processor
,