Commit 01bea684 authored by Matthias Braun's avatar Matthias Braun
Browse files

Simplify ident implementation

Remove some unnecessary/unused functions. Simplify the implementation to
pass around const char* (which is a little bit faster and also nicer in
gdb)
parent fb350c51
......@@ -42,8 +42,12 @@ typedef struct dbg_info dbg_info;
* Source Type Reference */
typedef struct type_dbg_info type_dbg_info;
/** @ingroup ir_ident
* Identifier */
typedef struct ident ident;
* Identifier
*
* @note This is currently defined as a normal C string, but you should not
* rely on that and always use get_id_str() before using it as a C string.
*/
typedef const char ident;
/** @ingroup ir_node
* Procedure Graph Node */
typedef struct ir_node ir_node;
......
......@@ -55,41 +55,6 @@ FIRM_API ident *new_id_from_chars(const char *str, size_t len);
*/
FIRM_API const char *get_id_str(ident *id);
/**
* Returns the length of the string represented by an ident.
*
* @param id the ident
* @return len the length of the string
*/
FIRM_API size_t get_id_strlen(ident *id);
/**
* Test if @p prefix is a prefix of ident @p id.
*
* @param prefix the prefix
* @param id the ident
* @returns 1 if @p prefix is prefix of @p id, 0 otherwise
*/
FIRM_API int id_is_prefix(ident *prefix, ident *id);
/**
* Test if @p suffix is a suffix of ident @p id.
*
* @param suffix the suffix
* @param id the ident
* @returns 1 if @p suffix is suffix of @p id, 0 otherwise
*/
FIRM_API int id_is_suffix(ident *suffix, ident *id);
/**
* Test if identifier contains a given character.
*
* @param id the ident
* @param c the character
* @returns 1 if character is contained, 0 otherwise
*/
FIRM_API int id_contains_char(ident *id, char c);
/**
* helper function for creating unique idents. It contains an internal counter
* and replaces a "%u" inside the tag with the counter.
......
......@@ -32,9 +32,9 @@ void init_ident(void)
ident *new_id_from_chars(const char *str, size_t len)
{
unsigned hash = hash_data((const unsigned char*)str, len);
ident *result = (ident*) set_hinsert0(id_set, str, len, hash);
return result;
unsigned hash = hash_data((const unsigned char*)str, len);
set_entry *result = set_hinsert0(id_set, str, len, hash);
return (ident*)result->dptr;
}
ident *new_id_from_str(const char *str)
......@@ -43,16 +43,9 @@ ident *new_id_from_str(const char *str)
return new_id_from_chars(str, strlen(str));
}
const char *get_id_str(ident *id)
const char *(get_id_str)(ident *id)
{
struct set_entry *entry = (struct set_entry*) id;
return (const char*) entry->dptr;
}
size_t get_id_strlen(ident *id)
{
struct set_entry *entry = (struct set_entry*) id;
return entry->size;
return get_id_str_(id);
}
void finish_ident(void)
......@@ -61,34 +54,6 @@ void finish_ident(void)
id_set = NULL;
}
int id_is_prefix(ident *prefix, ident *id)
{
size_t prefix_len = get_id_strlen(prefix);
if (prefix_len > get_id_strlen(id))
return 0;
return 0 == memcmp(get_id_str(prefix), get_id_str(id), prefix_len);
}
int id_is_suffix(ident *suffix, ident *id)
{
size_t suflen = get_id_strlen(suffix);
size_t idlen = get_id_strlen(id);
const char *part;
if (suflen > idlen)
return 0;
part = get_id_str(id);
part = part + (idlen - suflen);
return 0 == memcmp(get_id_str(suffix), part, suflen);
}
int id_contains_char(ident *id, char c)
{
return strchr(get_id_str(id), c) != NULL;
}
ident *id_unique(const char *tag)
{
static unsigned unique_id = 0;
......
......@@ -13,6 +13,13 @@
#include "ident.h"
#define get_id_str(x) get_id_str_(x)
static inline const char *get_id_str_(ident *ident)
{
return ident;
}
/**
* Initialize the ident module.
*/
......
......@@ -30,54 +30,47 @@ static inline ident *mangle_type(const ir_type *tp)
return tp->name;
}
/* Returns a new ident that represents 'firstscnd'. */
ident *id_mangle(ident *first, ident *scnd)
static void obstack_grow_ident(struct obstack *obst, ident *id)
{
char *cp;
int len;
ident *res;
const char *c = get_id_str(id);
for ( ; *c != '\0'; ++c) {
obstack_1grow(obst, *c);
}
}
obstack_grow(&mangle_obst, get_id_str(first), get_id_strlen(first));
obstack_grow(&mangle_obst, get_id_str(scnd), get_id_strlen(scnd));
len = obstack_object_size(&mangle_obst);
cp = (char*)obstack_finish(&mangle_obst);
res = new_id_from_chars(cp, len);
obstack_free(&mangle_obst, cp);
static ident *new_ident_from_obst(struct obstack *obst)
{
size_t len = obstack_object_size(obst);
char *string = (char*)obstack_finish(obst);
ident *res = new_id_from_chars(string, len);
obstack_free(obst, string);
return res;
}
/* Returns a new ident that represents 'firstscnd'. */
ident *id_mangle(ident *first, ident *scnd)
{
obstack_grow_ident(&mangle_obst, first);
obstack_grow_ident(&mangle_obst, scnd);
return new_ident_from_obst(&mangle_obst);
}
/** Returns a new ident that represents 'prefixscndsuffix'. */
ident *id_mangle3(const char *prefix, ident *scnd, const char *suffix)
{
char *cp;
int len;
ident *res;
obstack_grow(&mangle_obst, prefix, strlen(prefix));
obstack_grow(&mangle_obst, get_id_str(scnd), get_id_strlen(scnd));
obstack_grow_ident(&mangle_obst, scnd);
obstack_grow(&mangle_obst, suffix, strlen(suffix));
len = obstack_object_size(&mangle_obst);
cp = (char*)obstack_finish(&mangle_obst);
res = new_id_from_chars(cp, len);
obstack_free(&mangle_obst, cp);
return res;
return new_ident_from_obst(&mangle_obst);
}
/** Returns a new ident that represents first<c>scnd. */
static ident *id_mangle_3(ident *first, char c, ident *scnd)
{
char *cp;
int len;
ident *res;
obstack_grow(&mangle_obst, get_id_str(first), get_id_strlen(first));
obstack_grow_ident(&mangle_obst, first);
obstack_1grow(&mangle_obst, c);
obstack_grow(&mangle_obst,get_id_str(scnd),get_id_strlen(scnd));
len = obstack_object_size(&mangle_obst);
cp = (char*)obstack_finish(&mangle_obst);
res = new_id_from_chars(cp, len);
obstack_free(&mangle_obst, cp);
return res;
obstack_grow_ident(&mangle_obst, scnd);
return new_ident_from_obst(&mangle_obst);
}
/* Returns a new ident that represents first_scnd. */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment