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 /** The current ident module implementation. */
27 static ident_if_t impl;
30 * Stores a string in the ident module and returns a handle for the string.
32 * @param handle the handle for the set
33 * @param str the string which shall be stored
35 * @return id - a handle for the generated ident
37 * Default implementation using libfirm sets.
39 static ident *set_new_id_from_chars(void *handle, const char *str, int len)
44 return (ident *)set_hinsert0(id_set, str, len, ID_HASH(str, len));
48 * Stores a string in the ident module and returns a handle for the string.
50 * @param handle the handle for the set
51 * @param str the string (or whatever) which shall be stored
52 * @param len the length of the data in bytes
54 * Default implementation using libfirm sets.
56 static ident *set_new_id_from_str(void *handle, const char *str)
59 return (ident *)set_new_id_from_chars(handle, str, strlen(str));
63 * Returns a string represented by an ident.
65 * @param handle the handle for the set
68 * Default implementation using libfirm sets.
70 static const char *set_get_id_str(void *handle, ident *id)
72 struct set_entry *entry = (struct set_entry *)id;
74 return (const char *)entry->dptr;
78 * Returns the length of the string represented by an ident.
80 * @param handle the handle for the set
83 * Default implementation using libfirm sets.
85 static int set_get_id_strlen(void *handle, ident *id)
87 struct set_entry *entry = (struct set_entry *)id;
93 * Default implementation using libfirm sets.
95 void set_finish_ident(void *handle) {
102 * Default implementation if no new_id_from_str() is provided.
104 static ident *def_new_id_from_str(void *handle, const char *str)
106 return impl.new_id_from_chars(handle, str, strlen(str));
110 * Default implementation if no get_id_strlen() is provided.
112 static int def_get_id_strlen(void *handle, ident *id)
114 return strlen(impl.get_id_str(handle, id));
117 /* Initialize the ident module. */
118 void init_ident(ident_if_t *id_if, int initial_n_idents)
121 memcpy(&impl, id_if, sizeof(impl));
123 if (! impl.new_id_from_str)
124 impl.new_id_from_str = def_new_id_from_str;
125 if (! impl.get_id_strlen)
126 impl.get_id_strlen = def_get_id_strlen;
129 impl.new_id_from_str = set_new_id_from_str;
130 impl.new_id_from_chars = set_new_id_from_chars;
131 impl.get_id_str = set_get_id_str;
132 impl.get_id_strlen = set_get_id_strlen;
133 impl.finish_ident = set_finish_ident;
135 impl.handle = new_set(memcmp, initial_n_idents);
139 ident *new_id_from_str(const char *str)
142 return impl.new_id_from_str(impl.handle, str);
145 ident *new_id_from_chars(const char *str, int len)
148 return impl.new_id_from_chars(impl.handle, str, len);
151 const char *get_id_str(ident *id)
153 return impl.get_id_str(impl.handle, id);
156 int get_id_strlen(ident *id)
158 return impl.get_id_strlen(impl.handle, id);
161 void finish_ident(void) {
162 if (impl.finish_ident)
163 impl.finish_ident(impl.handle);
166 int id_is_prefix(ident *prefix, ident *id)
168 if (get_id_strlen(prefix) > get_id_strlen(id)) return 0;
169 return 0 == memcmp(get_id_str(prefix), get_id_str(id), get_id_strlen(prefix));
172 int id_is_suffix(ident *suffix, ident *id)
174 int suflen = get_id_strlen(suffix);
175 int idlen = get_id_strlen(id);
178 if (suflen > idlen) return 0;
180 part = get_id_str(id);
181 part = part + (idlen - suflen);
183 return 0 == memcmp(get_id_str(suffix), part, suflen);
186 int id_contains_char(ident *id, char c)
188 return strchr(get_id_str(id), c) != NULL;
191 int print_id (ident *id)
193 return printf("%s", get_id_str(id));
196 int fprint_id (FILE *F, ident *id)
198 return fprintf(F, "%s", get_id_str(id));