11f5207bJohn Levon/*
21f5207bJohn Levon * sparse/macro_table.c
31f5207bJohn Levon *
41f5207bJohn Levon * Copyright (C) 2010 Dan Carpenter.
51f5207bJohn Levon *
61f5207bJohn Levon * Permission is hereby granted, free of charge, to any person obtaining a copy
71f5207bJohn Levon * of this software and associated documentation files (the "Software"), to deal
81f5207bJohn Levon * in the Software without restriction, including without limitation the rights
91f5207bJohn Levon * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
101f5207bJohn Levon * copies of the Software, and to permit persons to whom the Software is
111f5207bJohn Levon * furnished to do so, subject to the following conditions:
121f5207bJohn Levon *
131f5207bJohn Levon * The above copyright notice and this permission notice shall be included in
141f5207bJohn Levon * all copies or substantial portions of the Software.
151f5207bJohn Levon *
161f5207bJohn Levon * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171f5207bJohn Levon * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181f5207bJohn Levon * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
191f5207bJohn Levon * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201f5207bJohn Levon * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211f5207bJohn Levon * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
221f5207bJohn Levon * THE SOFTWARE.
231f5207bJohn Levon */
241f5207bJohn Levon
251f5207bJohn Levon#include <stdlib.h>
261f5207bJohn Levon#include <stdio.h>
271f5207bJohn Levon#include <string.h>
281f5207bJohn Levon#include "lib.h"
291f5207bJohn Levon#include "parse.h"
301f5207bJohn Levon#include "cwchash/hashtable.h"
311f5207bJohn Levon
321f5207bJohn Levonstatic struct hashtable *macro_table;
331f5207bJohn Levon
34c85f09cJohn Levonstatic DEFINE_HASHTABLE_INSERT(do_insert_macro, struct position, struct string_list);
35c85f09cJohn Levonstatic DEFINE_HASHTABLE_SEARCH(do_search_macro, struct position, struct string_list);
361f5207bJohn Levon
371f5207bJohn Levonstatic inline unsigned int position_hash(void *_pos)
381f5207bJohn Levon{
391f5207bJohn Levon	struct position *pos = _pos;
401f5207bJohn Levon
41c85f09cJohn Levon	return pos->line | (pos->pos << 22) | (pos->stream << 18);
421f5207bJohn Levon}
431f5207bJohn Levon
441f5207bJohn Levonstatic inline int equalkeys(void *_pos1, void *_pos2)
451f5207bJohn Levon{
461f5207bJohn Levon	struct position *pos1 = _pos1;
471f5207bJohn Levon	struct position *pos2 = _pos2;
481f5207bJohn Levon
491f5207bJohn Levon	return pos1->line == pos2->line && pos1->pos == pos2->pos &&
501f5207bJohn Levon		pos1->stream == pos2->stream;
511f5207bJohn Levon}
521f5207bJohn Levon
53c85f09cJohn Levonstatic void insert_macro_string(struct string_list **str_list, char *new)
54c85f09cJohn Levon{
55c85f09cJohn Levon	add_ptr_list(str_list, new);
56c85f09cJohn Levon}
57c85f09cJohn Levon
581f5207bJohn Levonvoid store_macro_pos(struct token *token)
591f5207bJohn Levon{
60c85f09cJohn Levon	struct string_list *list;
61c85f09cJohn Levon
621f5207bJohn Levon	if (!macro_table)
631f5207bJohn Levon		macro_table = create_hashtable(5000, position_hash, equalkeys);
641f5207bJohn Levon
65c85f09cJohn Levon	list = do_search_macro(macro_table, &token->pos);
66c85f09cJohn Levon	insert_macro_string(&list, token->ident->name);
671f5207bJohn Levon
68c85f09cJohn Levon	do_insert_macro(macro_table, &token->pos, list);
691f5207bJohn Levon}
701f5207bJohn Levon
711f5207bJohn Levonchar *get_macro_name(struct position pos)
721f5207bJohn Levon{
73c85f09cJohn Levon	struct string_list *list;
74c85f09cJohn Levon
75c85f09cJohn Levon	if (!macro_table)
76c85f09cJohn Levon		return NULL;
77c85f09cJohn Levon	list = do_search_macro(macro_table, &pos);
78c85f09cJohn Levon	return first_ptr_list((struct ptr_list *)list);
79c85f09cJohn Levon}
80c85f09cJohn Levon
81c85f09cJohn Levonchar *get_inner_macro(struct position pos)
82c85f09cJohn Levon{
83c85f09cJohn Levon	struct string_list *list;
84c85f09cJohn Levon
85c85f09cJohn Levon	if (!macro_table)
86c85f09cJohn Levon		return NULL;
87c85f09cJohn Levon	list = do_search_macro(macro_table, &pos);
88c85f09cJohn Levon	return last_ptr_list((struct ptr_list *)list);
89c85f09cJohn Levon}
90c85f09cJohn Levon
91c85f09cJohn Levonstruct string_list *get_all_macros(struct position pos)
92c85f09cJohn Levon{
93c85f09cJohn Levon	if (!macro_table)
94c85f09cJohn Levon		return NULL;
951f5207bJohn Levon	return do_search_macro(macro_table, &pos);
961f5207bJohn Levon}
97