Oops. Bad scoping for iterators and switch() statements. We didn't
[smatch.git] / lib.h
blobe1684f8f9bdc6f117be6c478746cfd6feb93a957
1 #ifndef LIB_H
2 #define LIB_H
3 /*
4 * Basic helper routine descriptions for 'sparse'.
6 * Copyright (C) 2003 Transmeta Corp, all rights reserved.
7 */
9 extern unsigned int hexval(unsigned int c);
11 struct position {
12 unsigned int type:6,
13 stream:10,
14 pos:14,
15 newline:1,
16 whitespace:1;
17 unsigned int line;
20 struct ident;
21 struct token;
22 struct symbol;
23 struct symbol_list;
24 struct statement;
25 struct statement_list;
26 struct expression;
27 struct expression_list;
29 struct token *skip_to(struct token *, int);
30 struct token *expect(struct token *, int, const char *);
31 extern void warn(struct position, const char *, ...);
32 extern void error(struct position, const char *, ...);
34 #define __DECLARE_ALLOCATOR(type, x) \
35 extern type *__alloc_##x(int); \
36 extern void show_##x##_alloc(void); \
37 extern void clear_##x##_alloc(void);
38 #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
40 DECLARE_ALLOCATOR(ident);
41 DECLARE_ALLOCATOR(token);
42 DECLARE_ALLOCATOR(symbol);
43 DECLARE_ALLOCATOR(expression);
44 DECLARE_ALLOCATOR(statement);
45 DECLARE_ALLOCATOR(string);
46 __DECLARE_ALLOCATOR(void, bytes);
48 #define LIST_NODE_NR (29)
50 struct ptr_list {
51 int nr;
52 struct ptr_list *prev;
53 struct ptr_list *next;
54 void *list[LIST_NODE_NR];
57 #define ITERATE_FIRST 1
58 #define ITERATE_LAST 2
59 void iterate(struct ptr_list *,void (*callback)(void *, void *, int), void*);
60 extern void add_ptr_list(struct ptr_list **, void *);
61 extern int ptr_list_size(struct ptr_list *);
63 #define symbol_list_size(list) ptr_list_size((struct ptr_list *)(list))
64 #define statement_list_size(list) ptr_list_size((struct ptr_list *)(list))
65 #define expression_list_size(list) ptr_list_size((struct ptr_list *)(list))
67 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
69 add_ptr_list((struct ptr_list **)list, sym);
72 static inline void add_statement(struct statement_list **list, struct statement *stmt)
74 add_ptr_list((struct ptr_list **)list, stmt);
77 static inline void add_expression(struct expression_list **list, struct expression *expr)
79 add_ptr_list((struct ptr_list **)list, expr);
82 static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
84 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
87 static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
89 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
92 static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
94 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
97 #define PREPARE_PTR_LIST(head, ptr) \
98 do { \
99 struct ptr_list *__head##ptr = (struct ptr_list *) (head); \
100 struct ptr_list *__list##ptr = __head##ptr; \
101 int __nr##ptr = 0; \
102 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]
104 #define NEXT_PTR_LIST(head, ptr) \
105 if (ptr) { \
106 if (++__nr##ptr < __list##ptr->nr) { \
107 ptr = (__typeof__(ptr)) __list##ptr->list[__nr##ptr]; \
108 } else { \
109 __list##ptr = __list##ptr->next; \
110 ptr = NULL; \
111 if (__list##ptr != __head##ptr) { \
112 __nr##ptr = 0; \
113 ptr = (__typeof__(ptr)) __list##ptr->list[0]; \
118 #define FINISH_PTR_LIST \
119 } while (0)
121 #define FOR_EACH_PTR(head, ptr) do { \
122 struct ptr_list *__head = (struct ptr_list *) (head); \
123 struct ptr_list *__list = __head; \
124 int __flag = ITERATE_FIRST; \
125 if (__head) { \
126 do { int __i; \
127 for (__i = 0; __i < __list->nr; __i++) { \
128 if (__i == __list->nr-1 && __list->next == __head) \
129 __flag |= ITERATE_LAST; \
130 do { \
131 ptr = (__typeof__(ptr)) (__list->list[__i]); \
132 do {
134 #define END_FOR_EACH_PTR } while (0); \
135 } while (0); \
136 __flag = 0; \
138 } while ((__list = __list->next) != __head); \
140 } while (0)
142 #define FOR_EACH_PTR_REVERSE(head, ptr) do { \
143 struct ptr_list *__head = (struct ptr_list *) (head); \
144 struct ptr_list *__list = __head; \
145 int __flag = ITERATE_FIRST; \
146 if (__head) { \
147 do { int __i; \
148 __list = __list->prev; \
149 __i = __list->nr; \
150 while (--__i >= 0) { \
151 if (__i == 0 && __list == __head) \
152 __flag |= ITERATE_LAST; \
153 do { \
154 ptr = (__typeof__(ptr)) (__list->list[__i]); \
155 do {
157 #define END_FOR_EACH_PTR_REVERSE } while (0); \
158 } while (0); \
159 __flag = 0; \
161 } while (__list != __head); \
163 } while (0)
165 #define THIS_ADDRESS(x) \
166 ((__typeof__(&(x))) (__list->list + __i))
168 #endif