X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=typedef.c;h=d9be47817df95489a427cc26b268afa6496ca1d3;hb=db879bd626af0003840ee32df41cf17714680ac8;hp=e84300a88a0c2c8ac801d266342431348c14ebaf;hpb=639fc8a32b35174fc4c2347fd38f76147093d6f4;p=xonotic%2Fgmqcc.git diff --git a/typedef.c b/typedef.c index e84300a..d9be478 100644 --- a/typedef.c +++ b/typedef.c @@ -1,6 +1,6 @@ /* * Copyright (C) 2012 - * Dale Weiler + * Dale Weiler * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in @@ -20,71 +20,60 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include -#include /* replace if stdint.h doesn't exist! */ -#include #include "gmqcc.h" static typedef_node *typedef_table[1024]; + void typedef_init() { - int i; - for(i = 0; i < sizeof(typedef_table)/sizeof(*typedef_table); i++) - typedef_table[i] = NULL; + int i; + for(i = 0; i < sizeof(typedef_table)/sizeof(*typedef_table); i++) + typedef_table[i] = NULL; } -unsigned int typedef_hash(const char *s) { - unsigned int hash = 0; - unsigned int size = strlen(s); - unsigned int iter; - - for (iter = 0; iter < size; iter++) { - hash += s[iter]; - hash += (hash << 10); - hash ^= (hash >> 6); - } - hash += (hash << 3); - hash ^= (hash >> 11); - hash += (hash << 15); - - return hash % 1024; +uint32_t typedef_hash(const char *s) { + return util_crc32(s, strlen(s), 1024); } typedef_node *typedef_find(const char *s) { - unsigned int hash = typedef_hash(s); - typedef_node *find = typedef_table[hash]; - return find; + unsigned int hash = typedef_hash(s); + typedef_node *find = typedef_table[hash]; + return find; } void typedef_clear() { - int i; - for(i = 1024; i > 0; i--) - if(typedef_table[i]) - mem_d(typedef_table[i]); + int i; + for(i = 1024; i > 0; i--) { + if(typedef_table[i]) { + mem_d(typedef_table[i]->name); + mem_d(typedef_table[i]); + } + } } -int typedef_add(const char *from, const char *to) { - unsigned int hash = typedef_hash(to); - typedef_node *find = typedef_table[hash]; - if (find) - return error(ERROR_PARSE, "typedef for %s already exists or conflicts\n", to); - - /* check if the type exists first */ - if (strncmp(from, "float", sizeof("float")) == 0 || - strncmp(from, "vector", sizeof("vector")) == 0 || - strncmp(from, "string", sizeof("string")) == 0 || - strncmp(from, "entity", sizeof("entity")) == 0 || - strncmp(from, "void", sizeof("void")) == 0) { - - typedef_table[hash] = mem_a(sizeof(typedef_node)); - typedef_table[hash]->name = strdup(from); - return -100; - } else { - /* search the typedefs for it (typedef-a-typedef?) */ - typedef_node *find = typedef_table[typedef_hash(from)]; - if (find) { - typedef_table[hash] = mem_a(sizeof(typedef_node)); - typedef_table[hash]->name = strdup(find->name); - return -100; - } - } - return error(ERROR_PARSE, "cannot typedef `%s` (not a type)\n", from); +int typedef_add(struct lex_file *file, const char *from, const char *to) { + unsigned int hash = typedef_hash(to); + typedef_node *find = typedef_table[hash]; + + if (find) + return error(file, ERROR_PARSE, "typedef for %s already exists or conflicts\n", to); + + /* check if the type exists first */ + if (strncmp(from, "float", sizeof("float")) == 0 || + strncmp(from, "vector", sizeof("vector")) == 0 || + strncmp(from, "string", sizeof("string")) == 0 || + strncmp(from, "entity", sizeof("entity")) == 0 || + strncmp(from, "void", sizeof("void")) == 0) { + + typedef_table[hash] = mem_a(sizeof(typedef_node)); + typedef_table[hash]->name = util_strdup(from); + return -100; + } else { + /* search the typedefs for it (typedef-a-typedef?) */ + typedef_node *find = typedef_table[typedef_hash(from)]; + if (find) { + typedef_table[hash] = mem_a(sizeof(typedef_node)); + typedef_table[hash]->name = util_strdup(find->name); + return -100; + } + } + return error(file, ERROR_PARSE, "cannot typedef `%s` (not a type)\n", from); }