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.
26 /* for debugging only, not the real implementation */
28 char reserved[sizeof(unsigned) + sizeof(size_t)];
32 /** The current ident module implementation. */
33 static ident_if_t impl;
36 * Stores a string in the ident module and returns a handle for the string.
38 * @param handle the handle for the set
39 * @param str the string which shall be stored
40 * @param len length of str in bytes
42 * @return id - a handle for the generated ident
44 * Default implementation using libfirm sets.
46 static ident *set_new_id_from_chars(void *handle, const char *str, int len)
50 /* GL: Who added this assert? And why? */
52 return (ident *)set_hinsert0(id_set, str, len, ID_HASH(str, len));
56 * Stores a string in the ident module and returns a handle for the string.
58 * @param handle the handle for the set
59 * @param str the string (or whatever) which shall be stored
61 * Default implementation using libfirm sets.
63 static ident *set_new_id_from_str(void *handle, const char *str)
66 return (ident *)set_new_id_from_chars(handle, str, strlen(str));
70 * Returns a string represented by an ident.
72 * @param handle the handle for the set
75 * Default implementation using libfirm sets.
77 static const char *set_get_id_str(void *handle, ident *id)
79 struct set_entry *entry = (struct set_entry *)id;
81 return (const char *)entry->dptr;
85 * Returns the length of the string represented by an ident.
87 * @param handle the handle for the set
90 * Default implementation using libfirm sets.
92 static int set_get_id_strlen(void *handle, ident *id)
94 struct set_entry *entry = (struct set_entry *)id;
100 * Default implementation using libfirm sets.
102 void set_finish_ident(void *handle) {
103 set *id_set = handle;
109 * Default implementation if no new_id_from_str() is provided.
111 static ident *def_new_id_from_str(void *handle, const char *str)
113 return impl.new_id_from_chars(handle, str, strlen(str));
117 * Default implementation if no get_id_strlen() is provided.
119 static int def_get_id_strlen(void *handle, ident *id)
121 return strlen(impl.get_id_str(handle, id));
124 /* Initialize the ident module. */
125 void init_ident(ident_if_t *id_if, int initial_n_idents)
128 memcpy(&impl, id_if, sizeof(impl));
130 if (! impl.new_id_from_str)
131 impl.new_id_from_str = def_new_id_from_str;
132 if (! impl.get_id_strlen)
133 impl.get_id_strlen = def_get_id_strlen;
136 impl.new_id_from_str = set_new_id_from_str;
137 impl.new_id_from_chars = set_new_id_from_chars;
138 impl.get_id_str = set_get_id_str;
139 impl.get_id_strlen = set_get_id_strlen;
140 impl.finish_ident = set_finish_ident;
142 impl.handle = new_set(memcmp, initial_n_idents);
146 ident *new_id_from_str(const char *str)
149 return impl.new_id_from_str(impl.handle, str);
152 ident *new_id_from_chars(const char *str, int len)
155 return impl.new_id_from_chars(impl.handle, str, len);
158 const char *get_id_str(ident *id)
160 return impl.get_id_str(impl.handle, id);
163 int get_id_strlen(ident *id)
165 return impl.get_id_strlen(impl.handle, id);
168 void finish_ident(void) {
169 if (impl.finish_ident)
170 impl.finish_ident(impl.handle);
173 int id_is_prefix(ident *prefix, ident *id)
175 if (get_id_strlen(prefix) > get_id_strlen(id)) return 0;
176 return 0 == memcmp(get_id_str(prefix), get_id_str(id), get_id_strlen(prefix));
179 int id_is_suffix(ident *suffix, ident *id)
181 int suflen = get_id_strlen(suffix);
182 int idlen = get_id_strlen(id);
185 if (suflen > idlen) return 0;
187 part = get_id_str(id);
188 part = part + (idlen - suflen);
190 return 0 == memcmp(get_id_str(suffix), part, suflen);
193 int id_contains_char(ident *id, char c)
195 return strchr(get_id_str(id), c) != NULL;
198 int print_id (ident *id)
200 return printf("%s", get_id_str(id));
203 int fprint_id (FILE *F, ident *id)
205 return fprintf(F, "%s", get_id_str(id));