]> git.saurik.com Git - bison.git/blame - src/uniqstr.c
(conflict_report):
[bison.git] / src / uniqstr.c
CommitLineData
ac72f3e9
PE
1/* Keep a unique copy of strings.
2
5bab03fc
PE
3 Copyright (C) 2002 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "system.h"
23
24#include <error.h>
25#include <hash.h>
26#include <quotearg.h>
27
28#include "uniqstr.h"
29
30/*-----------------------.
31| A uniqstr hash table. |
32`-----------------------*/
33
34/* Initial capacity of uniqstr hash table. */
35#define HT_INITIAL_CAPACITY 257
36
37static struct hash_table *uniqstrs_table = NULL;
38
39/*-------------------------------------.
40| Create the uniqstr for S if needed. |
41`-------------------------------------*/
42
43uniqstr
ac72f3e9 44uniqstr_new (char const *s)
5bab03fc
PE
45{
46 uniqstr res = hash_lookup (uniqstrs_table, s);
47 if (!res)
48 {
49 /* First insertion in the hash. */
50 res = xstrdup (s);
51 hash_insert (uniqstrs_table, res);
52 }
53 return res;
54}
55
56
57/*------------------------------.
58| Abort if S is not a uniqstr. |
59`------------------------------*/
60
61void
ac72f3e9 62uniqstr_assert (char const *s)
5bab03fc
PE
63{
64 if (!hash_lookup (uniqstrs_table, s))
65 {
66 error (0, 0, "not a uniqstr: %s", quotearg (s));
67 abort ();
68 }
69}
70
71
72/*--------------------.
73| Print the uniqstr. |
74`--------------------*/
75
ac72f3e9
PE
76static inline bool
77uniqstr_print (uniqstr s)
5bab03fc
PE
78{
79 fprintf (stderr, "%s\n", s);
80 return true;
81}
82
ac72f3e9
PE
83static bool
84uniqstr_print_processor (void *s, void *null ATTRIBUTE_UNUSED)
85{
86 return uniqstr_print (s);
87}
88
5bab03fc
PE
89\f
90/*-----------------------.
91| A uniqstr hash table. |
92`-----------------------*/
93
94static bool
ac72f3e9 95hash_compare_uniqstr (void const *m1, void const *m2)
5bab03fc
PE
96{
97 return strcmp (m1, m2) == 0;
98}
99
100static unsigned int
ac72f3e9 101hash_uniqstr (void const *m, unsigned int tablesize)
5bab03fc
PE
102{
103 return hash_string (m, tablesize);
104}
105
5bab03fc
PE
106/*----------------------------.
107| Create the uniqstrs table. |
108`----------------------------*/
109
110void
111uniqstrs_new (void)
112{
113 uniqstrs_table = hash_initialize (HT_INITIAL_CAPACITY,
114 NULL,
ac72f3e9
PE
115 hash_uniqstr,
116 hash_compare_uniqstr,
117 free);
5bab03fc
PE
118}
119
120
121/*-------------------------------------.
122| Perform a task on all the uniqstrs. |
123`-------------------------------------*/
124
125static void
ac72f3e9 126uniqstrs_do (Hash_processor processor, void *processor_data)
5bab03fc 127{
ac72f3e9 128 hash_do_for_each (uniqstrs_table, processor, processor_data);
5bab03fc
PE
129}
130
131
132/*-----------------.
133| Print them all. |
134`-----------------*/
135
136void
137uniqstrs_print (void)
138{
ac72f3e9 139 uniqstrs_do (uniqstr_print_processor, NULL);
5bab03fc
PE
140}
141
142
143/*--------------------.
144| Free the uniqstrs. |
145`--------------------*/
146
147void
148uniqstrs_free (void)
149{
150 hash_free (uniqstrs_table);
151}