Clean up for/while/do parsing by separating them out
[smatch.git] / lib.h
blob4239e0ad54c847f239ea1e09dce9507cff76919a
1 #ifndef LIB_H
2 #define LIB_H
3 /*
4 * Basic helper routine descriptions for 'sparse'.
6 * Copyright (C) 2003 Transmeta Corp.
8 * Licensed under the Open Software License version 1.1
9 */
11 extern unsigned int hexval(unsigned int c);
13 struct position {
14 unsigned int type:6,
15 stream:10,
16 pos:14,
17 newline:1,
18 whitespace:1;
19 unsigned int line;
22 struct ident;
23 struct token;
24 struct symbol;
25 struct symbol_list;
26 struct statement;
27 struct statement_list;
28 struct expression;
29 struct expression_list;
31 struct token *skip_to(struct token *, int);
32 struct token *expect(struct token *, int, const char *);
33 extern void warn(struct position, const char *, ...);
34 extern void error(struct position, const char *, ...);
36 #define __DECLARE_ALLOCATOR(type, x) \
37 extern type *__alloc_##x(int); \
38 extern void show_##x##_alloc(void); \
39 extern void clear_##x##_alloc(void);
40 #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
42 DECLARE_ALLOCATOR(ident);
43 DECLARE_ALLOCATOR(token);
44 DECLARE_ALLOCATOR(symbol);
45 DECLARE_ALLOCATOR(expression);
46 DECLARE_ALLOCATOR(statement);
47 DECLARE_ALLOCATOR(string);
48 __DECLARE_ALLOCATOR(void, bytes);
50 #define LIST_NODE_NR (29)
52 struct ptr_list {
53 int nr;
54 struct ptr_list *prev;
55 struct ptr_list *next;
56 void *list[LIST_NODE_NR];
59 #define ITERATE_FIRST 1
60 #define ITERATE_LAST 2
61 void iterate(struct ptr_list *,void (*callback)(void *, void *, int), void*);
62 extern void add_ptr_list(struct ptr_list **, void *);
63 extern int ptr_list_size(struct ptr_list *);
65 #define symbol_list_size(list) ptr_list_size((struct ptr_list *)(list))
66 #define statement_list_size(list) ptr_list_size((struct ptr_list *)(list))
67 #define expression_list_size(list) ptr_list_size((struct ptr_list *)(list))
69 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
71 add_ptr_list((struct ptr_list **)list, sym);
74 static inline void add_statement(struct statement_list **list, struct statement *stmt)
76 add_ptr_list((struct ptr_list **)list, stmt);
79 static inline void add_expression(struct expression_list **list, struct expression *expr)
81 add_ptr_list((struct ptr_list **)list, expr);
84 static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
86 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
89 static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
91 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
94 static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
96 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
99 #define PREPARE_PTR_LIST(head, ptr) \
100 do { \
101 struct ptr_list *__head##ptr = (struct ptr_list *) (head); \
102 struct ptr_list *__list##ptr = __head##ptr; \
103 int __nr##ptr = 0; \
104 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
105 else ptr = NULL
107 #define NEXT_PTR_LIST(ptr) \
108 if (ptr) { \
109 if (++__nr##ptr < __list##ptr->nr) { \
110 ptr = (__typeof__(ptr)) __list##ptr->list[__nr##ptr]; \
111 } else { \
112 __list##ptr = __list##ptr->next; \
113 ptr = NULL; \
114 if (__list##ptr != __head##ptr) { \
115 __nr##ptr = 0; \
116 ptr = (__typeof__(ptr)) __list##ptr->list[0]; \
121 #define RESET_PTR_LIST(ptr) do { \
122 __nr##ptr = 0; \
123 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
124 } while (0)
127 #define FINISH_PTR_LIST(ptr) \
128 (void)(__nr##ptr); /* Sanity-check nesting */ \
129 } while (0)
131 #define FOR_EACH_PTR(head, ptr) do { \
132 struct ptr_list *__head = (struct ptr_list *) (head); \
133 struct ptr_list *__list = __head; \
134 int __flag = ITERATE_FIRST; \
135 if (__head) { \
136 do { int __i; \
137 for (__i = 0; __i < __list->nr; __i++) { \
138 if (__i == __list->nr-1 && __list->next == __head) \
139 __flag |= ITERATE_LAST; \
140 do { \
141 ptr = (__typeof__(ptr)) (__list->list[__i]); \
142 do {
144 #define END_FOR_EACH_PTR } while (0); \
145 } while (0); \
146 __flag = 0; \
148 } while ((__list = __list->next) != __head); \
150 } while (0)
152 #define FOR_EACH_PTR_REVERSE(head, ptr) do { \
153 struct ptr_list *__head = (struct ptr_list *) (head); \
154 struct ptr_list *__list = __head; \
155 int __flag = ITERATE_FIRST; \
156 if (__head) { \
157 do { int __i; \
158 __list = __list->prev; \
159 __i = __list->nr; \
160 while (--__i >= 0) { \
161 if (__i == 0 && __list == __head) \
162 __flag |= ITERATE_LAST; \
163 do { \
164 ptr = (__typeof__(ptr)) (__list->list[__i]); \
165 do {
167 #define END_FOR_EACH_PTR_REVERSE } while (0); \
168 } while (0); \
169 __flag = 0; \
171 } while (__list != __head); \
173 } while (0)
175 #define THIS_ADDRESS(x) \
176 ((__typeof__(&(x))) (__list->list + __i))
178 #endif