From d2397bae93f29d259eb15755d2ebe876071692e1 Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Thu, 13 Feb 2003 14:43:03 +0000 Subject: [PATCH] Simplifyed ident handling: Idents now are stored zero terminated, so id_to_str() returns a zero terminated string! [r766] --- ir/ident/ident.c | 96 +++++++++++------------------------------------- ir/ident/ident.h | 13 +++++-- 2 files changed, 32 insertions(+), 77 deletions(-) diff --git a/ir/ident/ident.c b/ir/ident/ident.c index ca8288330..2a73dc50b 100644 --- a/ir/ident/ident.c +++ b/ir/ident/ident.c @@ -19,10 +19,6 @@ #include "misc.h" #include "set.h" -/* Caution: strings _not_ zero-terminated! */ -#define ID_FROM_STR(str, len) \ - (assert ((len) > 0), \ - (const set_entry *)set_hinsert (id_set, (str), (len), ID_HASH ((str), (len)))) #define ID_TO_STR(id) ((const char *)&(id)->dptr[0]) #define ID_TO_STRLEN(id) ((id)->size) #define ID_TO_HASH(id) ((long)(id) + (id)->hash) @@ -37,95 +33,38 @@ # define id_stats() ((void)0) #endif -extern set *id_set; - -#define XX_USER(name) ident *id_##name; -#define XX_INTERNAL(name, str) XX_USER(name) -#undef XX_USER -#undef XX_INTERNAL static set *id_set; -#if 0 /* nowhere used */ -static ident * -new_id_derived (const char *pfx, ident *id) -{ - int pfx_len = strlen (pfx); - int len = pfx_len + ID_TO_STRLEN (id); - char *str = alloca (len); - - memcpy (str, pfx, pfx_len); - memcpy (str+pfx_len, ID_TO_STR (id), ID_TO_STRLEN (id)); - return ID_FROM_STR (str, pfx_len + ID_TO_STRLEN (id)); -} - -static ident * -new_id_internal (void) +void id_init(void) { - static char str[] = "_0000000"; - int i; - - i = sizeof (str) - 2; - while (++str[i] == '9'+1) { - str[i--] = '0'; - /* if following assertion fails, we get called far too often ;-) */ - assert (i >= 0); - } - assert (('0' <= str[i]) && (str[i] <= '9')); - - return ID_FROM_STR (str, sizeof (str) - 1); + id_set = new_set(memcmp, TUNE_NIDENTS); } - -static bool -id_is_internal (ident *id) -{ - assert (ID_TO_STRLEN (id)); - return !!ispunct (ID_TO_STR(id)[0]); -} -#endif - - -int -ident_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN) -{ - ident *id = XP_GETARG (ident *, 0); - return XPMR (ID_TO_STR (id), ID_TO_STRLEN (id)); -} - - -void -id_init (void) -{ - id_set = new_set (memcmp, TUNE_NIDENTS); - -#define XX_USER(name) id_##name = ID_FROM_STR(#name, sizeof(#name)- 1); -#define XX_INTERNAL(name, str) id_##name = ID_FROM_STR((str), sizeof((str))-1); -#undef XX_USER -#undef XX_INTERNAL -} - - INLINE ident *id_from_str (const char *str, int len) { assert (len > 0); - return (ident *)set_hinsert(id_set, str, len, ID_HASH(str, len)); + return set_hinsert0(id_set, str, len, ID_HASH(str, len)); } -INLINE const char *id_to_str (ident *id) { +INLINE const char *id_to_str(ident *id) +{ return (const char *)id->dptr; } -INLINE int id_to_strlen(ident *id) { +INLINE int id_to_strlen(ident *id) +{ return id->size; } -int id_is_prefix (ident *prefix, ident *id) { +int id_is_prefix (ident *prefix, ident *id) +{ if (id_to_strlen(prefix) > id_to_strlen(id)) return 0; return 0 == memcmp(prefix->dptr, id->dptr, id_to_strlen(prefix)); } -int id_is_suffix (ident *suffix, ident *id) { +int id_is_suffix (ident *suffix, ident *id) +{ int suflen = id_to_strlen(suffix); int idlen = id_to_strlen(id); char *part; @@ -138,10 +77,19 @@ int id_is_suffix (ident *suffix, ident *id) { return 0 == memcmp(suffix->dptr, part, suflen); } -int print_id (ident *id) { +int print_id (ident *id) +{ return xprintf("%I", id); } -int fprint_id (FILE *F, ident *id) { +int fprint_id (FILE *F, ident *id) +{ return xfprintf(F, "%I", id); } + +int +ident_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN) +{ + ident *id = XP_GETARG (ident *, 0); + return XPMR (ID_TO_STR (id), ID_TO_STRLEN (id)); +} diff --git a/ir/ident/ident.h b/ir/ident/ident.h index 5bebf9b7e..46b31fb23 100644 --- a/ir/ident/ident.h +++ b/ir/ident/ident.h @@ -26,10 +26,17 @@ /* Identifiers */ +/** + * Initialises the ident handling. + * + * Must be called before any id_*() function can be called. + */ +void id_init(void); + /** * The abstract data type ident. * - * An ident repraesents an unique string. The == operator + * An ident represents an unique string. The == operator * is sufficient to compare two idents. */ typedef const struct set_entry ident; @@ -52,8 +59,8 @@ INLINE ident *id_from_str (const char *str, int len); /** * Returns a string represented by an ident. * - * Returns the string represented by id. This string is not - * NULL terminated! The string may not be changed. + * Returns the string represented by id. This string is + * NULL terminated. The string may not be changed. * * @param id - the ident * -- 2.20.1