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 typedef const struct set_entry ident_s;
28 /** The current ident module implementation. */
29 static ident_if_t impl;
32 * Stores a string in the ident module and returns a handle for the string.
34 * @param handle the handle for the set
35 * @param str the string which shall be stored
37 * @return id - a handle for the generated ident
39 * Default implementation using libfirm sets.
41 static ident *set_new_id_from_chars(void *handle, const char *str, int len)
45 /* GL: Who added this assert? And why? */
47 return (ident *)set_hinsert0(id_set, str, len, ID_HASH(str, len));
51 * Stores a string in the ident module and returns a handle for the string.
53 * @param handle the handle for the set
54 * @param str the string (or whatever) which shall be stored
55 * @param len the length of the data in bytes
57 * Default implementation using libfirm sets.
59 static ident *set_new_id_from_str(void *handle, const char *str)
62 return (ident *)set_new_id_from_chars(handle, str, strlen(str));
66 * Returns a string represented by an ident.
68 * @param handle the handle for the set
71 * Default implementation using libfirm sets.
73 static const char *set_get_id_str(void *handle, ident *id)
75 struct set_entry *entry = (struct set_entry *)id;
77 return (const char *)entry->dptr;
81 * Returns the length of the string represented by an ident.
83 * @param handle the handle for the set
86 * Default implementation using libfirm sets.
88 static int set_get_id_strlen(void *handle, ident *id)
90 struct set_entry *entry = (struct set_entry *)id;
96 * Default implementation using libfirm sets.
98 void set_finish_ident(void *handle) {
105 * Default implementation if no new_id_from_str() is provided.
107 static ident *def_new_id_from_str(void *handle, const char *str)
109 return impl.new_id_from_chars(handle, str, strlen(str));
113 * Default implementation if no get_id_strlen() is provided.
115 static int def_get_id_strlen(void *handle, ident *id)
117 return strlen(impl.get_id_str(handle, id));
120 /* Initialize the ident module. */
121 void init_ident(ident_if_t *id_if, int initial_n_idents)
124 memcpy(&impl, id_if, sizeof(impl));
126 if (! impl.new_id_from_str)
127 impl.new_id_from_str = def_new_id_from_str;
128 if (! impl.get_id_strlen)
129 impl.get_id_strlen = def_get_id_strlen;
132 impl.new_id_from_str = set_new_id_from_str;
133 impl.new_id_from_chars = set_new_id_from_chars;
134 impl.get_id_str = set_get_id_str;
135 impl.get_id_strlen = set_get_id_strlen;
136 impl.finish_ident = set_finish_ident;
138 impl.handle = new_set(memcmp, initial_n_idents);
142 ident *new_id_from_str(const char *str)
145 return impl.new_id_from_str(impl.handle, str);
148 ident *new_id_from_chars(const char *str, int len)
151 return impl.new_id_from_chars(impl.handle, str, len);
154 const char *get_id_str(ident *id)
156 return impl.get_id_str(impl.handle, id);
159 int get_id_strlen(ident *id)
161 return impl.get_id_strlen(impl.handle, id);
164 void finish_ident(void) {
165 if (impl.finish_ident)
166 impl.finish_ident(impl.handle);
169 int id_is_prefix(ident *prefix, ident *id)
171 if (get_id_strlen(prefix) > get_id_strlen(id)) return 0;
172 return 0 == memcmp(get_id_str(prefix), get_id_str(id), get_id_strlen(prefix));
175 int id_is_suffix(ident *suffix, ident *id)
177 int suflen = get_id_strlen(suffix);
178 int idlen = get_id_strlen(id);
181 if (suflen > idlen) return 0;
183 part = get_id_str(id);
184 part = part + (idlen - suflen);
186 return 0 == memcmp(get_id_str(suffix), part, suflen);
189 int id_contains_char(ident *id, char c)
191 return strchr(get_id_str(id), c) != NULL;
194 int print_id (ident *id)
196 return printf("%s", get_id_str(id));
199 int fprint_id (FILE *F, ident *id)
201 return fprintf(F, "%s", get_id_str(id));