3 * File name: ir/common/ident.c
4 * Purpose: Hash table to store names.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
30 #ifdef FIRM_ENABLE_WCHAR
37 /* for debugging only, not the real implementation */
39 char reserved[sizeof(unsigned) + sizeof(size_t)];
43 /** The current ident module implementation. */
44 static ident_if_t impl;
47 * Stores a string in the ident module and returns a handle for the string.
49 * @param handle the handle for the set
50 * @param str the string which shall be stored
51 * @param len length of str in bytes
53 * @return id - a handle for the generated ident
55 * Default implementation using libfirm sets.
57 static ident *set_new_id_from_chars(void *handle, const char *str, int len)
61 /* GL: Who added this assert? And why? */
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 (ident *)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;
92 return (const char *)entry->dptr;
96 * Returns the length of the string represented by an ident.
98 * @param handle the handle for the set
101 * Default implementation using libfirm sets.
103 static int set_get_id_strlen(void *handle, ident *id)
105 struct set_entry *entry = (struct set_entry *)id;
111 * Default implementation using libfirm sets.
113 void set_finish_ident(void *handle) {
114 set *id_set = handle;
120 * Default implementation if no new_id_from_str() is provided.
122 static ident *def_new_id_from_str(void *handle, const char *str)
124 return impl.new_id_from_chars(handle, str, strlen(str));
128 * Default implementation if no get_id_strlen() is provided.
130 static int def_get_id_strlen(void *handle, ident *id)
132 return strlen(impl.get_id_str(handle, id));
135 #ifdef FIRM_ENABLE_WCHAR
137 * Stores a wide character string in the ident module and returns a
138 * handle for the string.
140 * @param handle the handle for the set
141 * @param wstr the wide character string which shall be stored
142 * @param len length of wstr
144 * @return id - a handle for the generated ident
146 * Default implementation using libfirm sets.
148 static ident *set_new_id_from_wchars(void *handle, const wchar_t *wstr, int len)
150 set *id_set = handle;
153 /* can't use hinsert0 here, so copy and add a 0 */
154 tmp = alloca((len + 1) * sizeof(*tmp));
155 memcpy(tmp, wstr, len * sizeof(*tmp));
158 return (ident *)set_hinsert(id_set, tmp, (len + 1) * sizeof(wchar_t), ID_HASH(wchar_t, tmp, len));
162 * Stores a wide character string in the ident module and
163 * returns a handle for the string.
165 * @param handle the handle for the set
166 * @param wstr the wide character string which shall be stored
168 * Default implementation using libfirm sets.
170 static ident *set_new_id_from_wcs(void *handle, const wchar_t *wstr)
173 return (ident *)set_new_id_from_wchars(handle, wstr, wcslen(wstr));
177 * Returns a wide character string represented by an ident.
179 * @param handle the handle for the set
180 * @param id the ident
182 * Default implementation using libfirm sets.
184 static const wchar_t *set_get_id_wcs(void *handle, ident *id)
186 struct set_entry *entry = (struct set_entry *)id;
188 return (const wchar_t *)entry->dptr;
192 * Returns the length of the string represented by an ident.
194 * @param handle the handle for the set
195 * @param id the ident
197 * Default implementation using libfirm sets.
199 static int set_get_id_wcslen(void *handle, ident *id)
201 struct set_entry *entry = (struct set_entry *)id;
203 /* len + \0 is stored for wchar_t */
204 return entry->size / sizeof(wchar_t) - 1;
208 * Default implementation if no new_id_from_wcs() is provided.
210 static ident *def_new_id_from_wcs(void *handle, const wchar_t *wstr)
212 return impl.new_id_from_wchars(handle, wstr, wcslen(wstr));
216 * Default implementation if no new_id_from_wchars() is provided.
218 static ident *def_new_id_from_wchars(void *handle, const wchar_t *wstr, int len)
220 return impl.new_id_from_chars(handle, (const char *)wstr, (len + 1) * sizeof(wchar_t));
224 * Default implementation if no get_id_wcs() is provided.
226 static const wchar_t *def_get_id_wcs(void *handle, ident *id)
228 return (const wchar_t *)impl.get_id_str(handle, id);
232 * Default implementation if no get_id_wcslen() is provided.
234 static int def_get_id_wcslen(void *handle, ident *id)
236 return wcslen(impl.get_id_wcs(handle, id));
238 #endif /* FIRM_ENABLE_WCHAR */
240 /* Initialize the ident module. */
241 void init_ident(ident_if_t *id_if, int initial_n_idents)
244 memcpy(&impl, id_if, sizeof(impl));
246 if (! impl.new_id_from_str)
247 impl.new_id_from_str = def_new_id_from_str;
248 if (! impl.get_id_strlen)
249 impl.get_id_strlen = def_get_id_strlen;
251 #ifdef FIRM_ENABLE_WCHAR
252 if (! impl.new_id_from_wcs)
253 impl.new_id_from_wcs = def_new_id_from_wcs;
254 if (! impl.new_id_from_wchars)
255 impl.new_id_from_wchars = def_new_id_from_wchars;
256 if (! impl.get_id_wcs)
257 impl.get_id_wcs = def_get_id_wcs;
258 if (! impl.get_id_wcslen)
259 impl.get_id_wcslen = def_get_id_wcslen;
260 #endif /* FIRM_ENABLE_WCHAR */
263 impl.new_id_from_str = set_new_id_from_str;
264 impl.new_id_from_chars = set_new_id_from_chars;
265 impl.get_id_str = set_get_id_str;
266 impl.get_id_strlen = set_get_id_strlen;
267 impl.finish_ident = set_finish_ident;
268 #ifdef FIRM_ENABLE_WCHAR
269 impl.new_id_from_wcs = set_new_id_from_wcs;
270 impl.new_id_from_wchars = set_new_id_from_wchars;
271 impl.get_id_wcs = set_get_id_wcs;
272 impl.get_id_wcslen = set_get_id_wcslen;
273 #endif /* FIRM_ENABLE_WCHAR */
275 impl.handle = new_set(memcmp, initial_n_idents);
279 ident *new_id_from_str(const char *str)
282 return impl.new_id_from_str(impl.handle, str);
285 ident *new_id_from_chars(const char *str, int len)
288 return impl.new_id_from_chars(impl.handle, str, len);
291 const char *get_id_str(ident *id)
293 return impl.get_id_str(impl.handle, id);
296 int get_id_strlen(ident *id)
298 return impl.get_id_strlen(impl.handle, id);
301 void finish_ident(void) {
302 if (impl.finish_ident)
303 impl.finish_ident(impl.handle);
306 int id_is_prefix(ident *prefix, ident *id)
308 if (get_id_strlen(prefix) > get_id_strlen(id)) return 0;
309 return 0 == memcmp(get_id_str(prefix), get_id_str(id), get_id_strlen(prefix));
312 int id_is_suffix(ident *suffix, ident *id)
314 int suflen = get_id_strlen(suffix);
315 int idlen = get_id_strlen(id);
318 if (suflen > idlen) return 0;
320 part = get_id_str(id);
321 part = part + (idlen - suflen);
323 return 0 == memcmp(get_id_str(suffix), part, suflen);
326 int id_contains_char(ident *id, char c)
328 return strchr(get_id_str(id), c) != NULL;
331 #ifdef FIRM_ENABLE_WCHAR
333 ident *new_id_from_wcs (const wchar_t *str)
336 return impl.new_id_from_wcs(impl.handle, str);
339 ident *new_id_from_wchars (const wchar_t *str, int len)
342 return impl.new_id_from_wchars(impl.handle, str, len);
345 const wchar_t *get_id_wcs(ident *id)
347 return impl.get_id_wcs(impl.handle, id);
350 int get_id_wcslen(ident *id)
352 return impl.get_id_wcslen(impl.handle, id);
355 int id_contains_wchar (ident *id, wchar_t c)
357 return wcschr(get_id_wcs(id), c) != NULL;
360 #endif /* FIRM_ENABLE_WCHAR */