Clean up type expression syntax.
[smatch.git] / lib.h
blob0ad0771596c70279ef9289e29bc1dc1342beed66
1 #ifndef LIB_H
2 #define LIB_H
3 /*
4 * Basic helper routine descriptions for 'sparse'.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
12 extern unsigned int hexval(unsigned int c);
14 struct position {
15 unsigned int type:6,
16 stream:10,
17 pos:14,
18 newline:1,
19 whitespace:1;
20 unsigned int line;
23 struct ident;
24 struct token;
25 struct symbol;
26 struct symbol_list;
27 struct statement;
28 struct statement_list;
29 struct expression;
30 struct expression_list;
31 struct basic_block;
32 struct entrypoint;
33 struct instruction;
35 struct token *skip_to(struct token *, int);
36 struct token *expect(struct token *, int, const char *);
37 extern void warn(struct position, const char *, ...);
38 extern void error(struct position, const char *, ...);
40 #define __DECLARE_ALLOCATOR(type, x) \
41 extern type *__alloc_##x(int); \
42 extern void show_##x##_alloc(void); \
43 extern void clear_##x##_alloc(void);
44 #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
46 DECLARE_ALLOCATOR(ident);
47 DECLARE_ALLOCATOR(token);
48 DECLARE_ALLOCATOR(symbol);
49 DECLARE_ALLOCATOR(expression);
50 DECLARE_ALLOCATOR(statement);
51 DECLARE_ALLOCATOR(string);
52 DECLARE_ALLOCATOR(scope);
53 __DECLARE_ALLOCATOR(void, bytes);
54 DECLARE_ALLOCATOR(basic_block);
55 DECLARE_ALLOCATOR(entrypoint);
56 DECLARE_ALLOCATOR(instruction);
59 #define LIST_NODE_NR (29)
61 struct ptr_list {
62 int nr;
63 struct ptr_list *prev;
64 struct ptr_list *next;
65 void *list[LIST_NODE_NR];
68 #define ITERATE_FIRST 1
69 #define ITERATE_LAST 2
71 #define ptr_list_empty(x) ((x) == NULL)
73 void iterate(struct ptr_list *,void (*callback)(void *, void *, int), void*);
74 extern void add_ptr_list(struct ptr_list **, void *);
75 extern void concat_ptr_list(struct ptr_list *a, struct ptr_list **b);
76 extern void free_ptr_list(struct ptr_list **);
77 extern int ptr_list_size(struct ptr_list *);
78 extern char **handle_switch(char *arg, char **next);
79 extern void add_pre_buffer(const char *fmt, ...);
81 extern unsigned int pre_buffer_size;
82 extern unsigned char pre_buffer[8192];
83 extern int include_fd;
84 extern char *include;
85 extern int preprocess_only;
87 extern void create_builtin_stream(void);
89 #define symbol_list_size(list) ptr_list_size((struct ptr_list *)(list))
90 #define statement_list_size(list) ptr_list_size((struct ptr_list *)(list))
91 #define expression_list_size(list) ptr_list_size((struct ptr_list *)(list))
93 static inline void concat_symbol_list(struct symbol_list *from, struct symbol_list **to)
95 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
98 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
100 add_ptr_list((struct ptr_list **)list, sym);
103 static inline void add_statement(struct statement_list **list, struct statement *stmt)
105 add_ptr_list((struct ptr_list **)list, stmt);
108 static inline void add_expression(struct expression_list **list, struct expression *expr)
110 add_ptr_list((struct ptr_list **)list, expr);
113 static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
115 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
118 static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
120 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
123 static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
125 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
128 #define PREPARE_PTR_LIST(head, ptr) \
129 do { \
130 struct ptr_list *__head##ptr = (struct ptr_list *) (head); \
131 struct ptr_list *__list##ptr = __head##ptr; \
132 int __nr##ptr = 0; \
133 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
134 else ptr = NULL
136 #define NEXT_PTR_LIST(ptr) \
137 if (ptr) { \
138 if (++__nr##ptr < __list##ptr->nr) { \
139 ptr = (__typeof__(ptr)) __list##ptr->list[__nr##ptr]; \
140 } else { \
141 __list##ptr = __list##ptr->next; \
142 ptr = NULL; \
143 if (__list##ptr != __head##ptr) { \
144 __nr##ptr = 0; \
145 ptr = (__typeof__(ptr)) __list##ptr->list[0]; \
150 #define RESET_PTR_LIST(ptr) do { \
151 __nr##ptr = 0; \
152 __list##ptr = __head##ptr; \
153 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
154 } while (0)
157 #define FINISH_PTR_LIST(ptr) \
158 (void)(__nr##ptr); /* Sanity-check nesting */ \
159 } while (0)
161 #define FOR_EACH_PTR(head, ptr) do { \
162 struct ptr_list *__head = (struct ptr_list *) (head); \
163 struct ptr_list *__list = __head; \
164 int __flag = ITERATE_FIRST; \
165 if (__head) { \
166 do { int __i; \
167 for (__i = 0; __i < __list->nr; __i++) { \
168 if (__i == __list->nr-1 && __list->next == __head) \
169 __flag |= ITERATE_LAST; \
170 do { \
171 ptr = (__typeof__(ptr)) (__list->list[__i]); \
172 do {
174 #define END_FOR_EACH_PTR } while (0); \
175 } while (0); \
176 __flag = 0; \
178 } while ((__list = __list->next) != __head); \
180 } while (0)
182 #define FOR_EACH_PTR_REVERSE(head, ptr) do { \
183 struct ptr_list *__head = (struct ptr_list *) (head); \
184 struct ptr_list *__list = __head; \
185 int __flag = ITERATE_FIRST; \
186 if (__head) { \
187 do { int __i; \
188 __list = __list->prev; \
189 __i = __list->nr; \
190 while (--__i >= 0) { \
191 if (__i == 0 && __list == __head) \
192 __flag |= ITERATE_LAST; \
193 do { \
194 ptr = (__typeof__(ptr)) (__list->list[__i]); \
195 do {
197 #define END_FOR_EACH_PTR_REVERSE } while (0); \
198 } while (0); \
199 __flag = 0; \
201 } while (__list != __head); \
203 } while (0)
205 #define THIS_ADDRESS(x) \
206 ((__typeof__(&(x))) (__list->list + __i))
208 #endif