2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Hash table to store names.
23 * @author Goetz Lindenmaier
39 /* for debugging only, not the real implementation */
41 char reserved[sizeof(unsigned) + sizeof(size_t)];
45 /** The current ident module implementation. */
46 static ident_if_t impl;
49 * Stores a string in the ident module and returns a handle for the string.
51 * @param handle the handle for the set
52 * @param str the string which shall be stored
53 * @param len length of str in bytes
55 * @return id - a handle for the generated ident
57 * Default implementation using libfirm sets.
59 static ident *set_new_id_from_chars(void *handle, const char *str, int len)
63 return (ident *)set_hinsert0(id_set, str, len, ID_HASH(unsigned char, str, len));
67 * Stores a string in the ident module and returns a handle for the string.
69 * @param handle the handle for the set
70 * @param str the string (or whatever) which shall be stored
72 * Default implementation using libfirm sets.
74 static ident *set_new_id_from_str(void *handle, const char *str)
77 return set_new_id_from_chars(handle, str, strlen(str));
81 * Returns a string represented by an ident.
83 * @param handle the handle for the set
86 * Default implementation using libfirm sets.
88 static const char *set_get_id_str(void *handle, ident *id)
90 struct set_entry *entry = (struct set_entry *)id;
93 return (const char *)entry->dptr;
97 * Returns the length of the string represented by an ident.
99 * @param handle the handle for the set
100 * @param id the ident
102 * Default implementation using libfirm sets.
104 static int set_get_id_strlen(void *handle, ident *id)
106 struct set_entry *entry = (struct set_entry *)id;
113 * Default implementation using libfirm sets.
115 static void set_finish_ident(void *handle)
117 set *id_set = handle;
123 * Default implementation if no new_id_from_str() is provided.
125 static ident *def_new_id_from_str(void *handle, const char *str)
127 return impl.new_id_from_chars(handle, str, strlen(str));
131 * Default implementation if no get_id_strlen() is provided.
133 static int def_get_id_strlen(void *handle, ident *id)
135 return strlen(impl.get_id_str(handle, id));
138 /* Initialize the ident module. */
139 void init_ident(ident_if_t *id_if, int initial_n_idents)
142 memcpy(&impl, id_if, sizeof(impl));
144 if (! impl.new_id_from_str)
145 impl.new_id_from_str = def_new_id_from_str;
146 if (! impl.get_id_strlen)
147 impl.get_id_strlen = def_get_id_strlen;
149 impl.new_id_from_str = set_new_id_from_str;
150 impl.new_id_from_chars = set_new_id_from_chars;
151 impl.get_id_str = set_get_id_str;
152 impl.get_id_strlen = set_get_id_strlen;
153 impl.finish_ident = set_finish_ident;
155 /* it's ok to use memcmp here, we check only strings */
156 impl.handle = new_set(memcmp, initial_n_idents);
160 ident *new_id_from_str(const char *str)
163 return impl.new_id_from_str(impl.handle, str);
166 ident *new_id_from_chars(const char *str, int len)
168 return impl.new_id_from_chars(impl.handle, str, len);
171 const char *get_id_str(ident *id)
173 return impl.get_id_str(impl.handle, id);
176 int get_id_strlen(ident *id)
178 return impl.get_id_strlen(impl.handle, id);
181 void finish_ident(void)
183 if (impl.finish_ident)
184 impl.finish_ident(impl.handle);
187 int id_is_prefix(ident *prefix, ident *id)
189 if (get_id_strlen(prefix) > get_id_strlen(id)) return 0;
190 return 0 == memcmp(get_id_str(prefix), get_id_str(id), get_id_strlen(prefix));
193 int id_is_suffix(ident *suffix, ident *id)
195 int suflen = get_id_strlen(suffix);
196 int idlen = get_id_strlen(id);
199 if (suflen > idlen) return 0;
201 part = get_id_str(id);
202 part = part + (idlen - suflen);
204 return 0 == memcmp(get_id_str(suffix), part, suflen);
207 int id_contains_char(ident *id, char c)
209 return strchr(get_id_str(id), c) != NULL;
212 ident *id_unique(const char *tag)
214 static unsigned unique_id = 0;
217 snprintf(buf, sizeof(buf), tag, unique_id);
219 return new_id_from_str(buf);