aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tokenize.c
diff options
authorLinus Torvalds <torvalds@home.transmeta.com>2003-03-14 22:22:29 -0700
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-07 20:59:17 -0700
commitfe8ebbfbcd9da4c02541a2917031c6df7b4f9261 (patch)
tree1f0a09a0d5c7ead27016ea4e1139fb3860ab828e /tokenize.c
parenta3b3237dadefa3b9bed18ab35c4a1dae2ec354bc (diff)
downloadsparse-dev-fe8ebbfbcd9da4c02541a2917031c6df7b4f9261.tar.gz
Initialize 'struct', 'union' and 'enum' built-ins.
Diffstat (limited to 'tokenize.c')
-rw-r--r--tokenize.c68
1 files changed, 44 insertions, 24 deletions
diff --git a/tokenize.c b/tokenize.c
index b2d58db3..1ca9acfb 100644
--- a/tokenize.c
+++ b/tokenize.c
@@ -484,6 +484,11 @@ static int get_one_special(int c, action_t *action)
#define IDENT_HASH_BITS (10)
#define IDENT_HASH_SIZE (1<<IDENT_HASH_BITS)
#define IDENT_HASH_MASK (IDENT_HASH_SIZE-1)
+
+#define ident_hash_init(c) (c)
+#define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
+#define ident_hash_end(hash) ((((hash) >> IDENT_HASH_BITS) + (hash)) & IDENT_HASH_MASK)
+
static struct ident *hash_table[IDENT_HASH_SIZE];
int ident_hit, ident_miss;
@@ -517,56 +522,71 @@ void show_identifier_stats(void)
}
}
-static struct ident *create_hashed_ident(const char *name, int len, unsigned long hash)
+static struct ident *alloc_ident(const char *name, int len)
{
struct ident *ident;
- hash = ((hash >> IDENT_HASH_BITS) + hash) & IDENT_HASH_MASK;
- ident = hash_table[hash];
- while (ident) {
- if (ident->len == len && !memcmp(ident->name, name, len)) {
- ident_hit++;
- return ident;
- }
- ident = ident->next;
- }
-
ident = malloc(offsetof(struct ident,name) + len);
if (!ident)
die("Out of memory for identifiers");
-
ident->symbol = NULL;
ident->len = len;
memcpy(ident->name, name, len);
+ return ident;
+}
+
+static struct ident * insert_hash(struct ident *ident, unsigned long hash)
+{
ident->next = hash_table[hash];
hash_table[hash] = ident;
ident_miss++;
return ident;
}
-#define ident_hash_init(c) (c)
-#define ident_hash_add(oldhash,c) ((oldhash)*11 + (c))
-#define ident_hash_end(hash) (hash)
+static struct ident *create_hashed_ident(const char *name, int len, unsigned long hash)
+{
+ struct ident *ident;
-struct token *built_in_token(int stream, const char *name)
+ ident = hash_table[hash];
+ while (ident) {
+ if (ident->len == len && !memcmp(ident->name, name, len)) {
+ ident_hit++;
+ return ident;
+ }
+ ident = ident->next;
+ }
+
+ return insert_hash(alloc_ident(name, len), hash);
+}
+
+struct ident *hash_ident(struct ident *ident)
{
- int len = 1;
+ int n;
unsigned long hash;
- struct token *token;
- const unsigned char *p = (const unsigned char *)name;
+ const unsigned char *p = (const unsigned char *)ident->name;
hash = ident_hash_init(*p++);
- for (;;) {
+ n = ident->len;
+ while (--n) {
unsigned int i = *p++;
- if (!i)
- break;
hash = ident_hash_add(hash, i);
- len++;
}
hash = ident_hash_end(hash);
+ insert_hash(ident, hash);
+}
+
+struct ident *built_in_ident(const char *name)
+{
+ return hash_ident(alloc_ident(name, strlen(name)));
+}
+
+struct token *built_in_token(int stream, const char *name)
+{
+ struct token *token;
+
token = alloc_token(stream, 0, 0);
token->type = TOKEN_IDENT;
- token->ident = create_hashed_ident(name, len, hash);
+ token->ident = built_in_ident(name);
return token;
}