#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lib.h"
#include "parse.h"
#include "cwchash/hashtable.h"
static struct hashtable *macro_table;
static DEFINE_HASHTABLE_INSERT(do_insert_macro, struct position, struct string_list);
static DEFINE_HASHTABLE_SEARCH(do_search_macro, struct position, struct string_list);
static inline unsigned int position_hash(void *_pos)
{
struct position *pos = _pos;
return pos->line | (pos->pos << 22) | (pos->stream << 18);
}
static inline int equalkeys(void *_pos1, void *_pos2)
{
struct position *pos1 = _pos1;
struct position *pos2 = _pos2;
return pos1->line == pos2->line && pos1->pos == pos2->pos &&
pos1->stream == pos2->stream;
}
static void insert_macro_string(struct string_list **str_list, char *new)
{
add_ptr_list(str_list, new);
}
void store_macro_pos(struct token *token)
{
struct string_list *list;
if (!macro_table)
macro_table = create_hashtable(5000, position_hash, equalkeys);
list = do_search_macro(macro_table, &token->pos);
insert_macro_string(&list, token->ident->name);
do_insert_macro(macro_table, &token->pos, list);
}
char *get_macro_name(struct position pos)
{
struct string_list *list;
if (!macro_table)
return NULL;
list = do_search_macro(macro_table, &pos);
return first_ptr_list((struct ptr_list *)list);
}
char *get_inner_macro(struct position pos)
{
struct string_list *list;
if (!macro_table)
return NULL;
list = do_search_macro(macro_table, &pos);
return last_ptr_list((struct ptr_list *)list);
}
struct string_list *get_all_macros(struct position pos)
{
if (!macro_table)
return NULL;
return do_search_macro(macro_table, &pos);
}