[PATCH] recovery from typeof on expression with bad type
[smatch.git] / lib.h
blobfb5db1851ed790da7c2683118494722ae5256c35
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
8 * 2004 Christopher Li
10 * Licensed under the Open Software License version 1.1
13 extern unsigned int hexval(unsigned int c);
15 struct position {
16 unsigned int type:6,
17 stream:10,
18 pos:14,
19 newline:1,
20 whitespace:1;
21 unsigned int line:31,
22 noexpand:1;
25 struct ident;
26 struct token;
27 struct symbol;
28 struct symbol_list;
29 struct statement;
30 struct statement_list;
31 struct expression;
32 struct expression_list;
33 struct basic_block;
34 struct basic_block_list;
35 struct entrypoint;
36 struct instruction;
37 struct instruction_list;
38 struct multijmp;
39 struct multijmp_list;
40 struct phi;
41 struct phi_list;
43 struct token *skip_to(struct token *, int);
44 struct token *expect(struct token *, int, const char *);
45 #ifdef __GNUC__
46 #define FORMAT_ATTR __attribute__ ((__format__ (__printf__, 2, 3)))
47 #else
48 #define FORMAT_ATTR
49 #endif
50 extern void info(struct position, const char *, ...) FORMAT_ATTR;
51 extern void warning(struct position, const char *, ...) FORMAT_ATTR;
52 extern void error(struct position, const char *, ...) FORMAT_ATTR;
53 extern void error_die(struct position, const char *, ...) FORMAT_ATTR;
54 #undef FORMAT_ATTR
56 #define __DECLARE_ALLOCATOR(type, x) \
57 extern type *__alloc_##x(int); \
58 extern void show_##x##_alloc(void); \
59 extern void clear_##x##_alloc(void);
60 #define DECLARE_ALLOCATOR(x) __DECLARE_ALLOCATOR(struct x, x)
62 DECLARE_ALLOCATOR(ident);
63 DECLARE_ALLOCATOR(token);
64 DECLARE_ALLOCATOR(symbol);
65 DECLARE_ALLOCATOR(expression);
66 DECLARE_ALLOCATOR(statement);
67 DECLARE_ALLOCATOR(string);
68 DECLARE_ALLOCATOR(scope);
69 __DECLARE_ALLOCATOR(void, bytes);
70 DECLARE_ALLOCATOR(basic_block);
71 DECLARE_ALLOCATOR(entrypoint);
72 DECLARE_ALLOCATOR(instruction);
73 DECLARE_ALLOCATOR(multijmp);
74 DECLARE_ALLOCATOR(phi);
75 DECLARE_ALLOCATOR(pseudo);
78 #define LIST_NODE_NR (29)
80 struct ptr_list {
81 int nr;
82 struct ptr_list *prev;
83 struct ptr_list *next;
84 void *list[LIST_NODE_NR];
87 struct list_iterator {
88 struct ptr_list **head;
89 struct ptr_list *active;
90 int index;
91 unsigned int flags;
94 enum iterator_br_state {
95 BR_INIT,
96 BR_TRUE,
97 BR_FALSE,
98 BR_END,
101 struct terminator_iterator {
102 struct instruction *terminator;
103 union {
104 struct list_iterator multijmp;
105 int branch;
109 #define ITERATOR_BACKWARDS 1
110 #define ITERATOR_CURRENT 2
112 #define ITERATE_FIRST 1
113 #define ITERATE_LAST 2
115 #define ptr_list_empty(x) ((x) == NULL)
117 void iterate(struct ptr_list *,void (*callback)(void *, void *, int), void*);
118 void init_iterator(struct ptr_list **head, struct list_iterator *iterator, int flags);
119 void * next_iterator(struct list_iterator *iterator);
120 void delete_iterator(struct list_iterator *iterator);
121 void init_terminator_iterator(struct instruction* terminator, struct terminator_iterator *iterator);
122 struct basic_block* next_terminator_bb(struct terminator_iterator *iterator);
123 void replace_terminator_bb(struct terminator_iterator *iterator, struct basic_block* bb);
124 void * delete_ptr_list_last(struct ptr_list **head);
125 int replace_ptr_list(struct ptr_list *head, void *old_ptr, void *new_ptr);
127 extern void add_ptr_list(struct ptr_list **, void *);
128 extern void concat_ptr_list(struct ptr_list *a, struct ptr_list **b);
129 extern void free_ptr_list(struct ptr_list **);
130 extern int ptr_list_size(struct ptr_list *);
131 extern char **handle_switch(char *arg, char **next);
132 extern void add_pre_buffer(const char *fmt, ...);
133 void * next_iterator(struct list_iterator *iterator);
135 extern unsigned int pre_buffer_size;
136 extern unsigned char pre_buffer[8192];
137 extern int include_fd;
138 extern char *include;
139 extern int preprocess_only;
140 extern int Wdefault_bitfield_sign;
141 extern int Wbitwise;
143 extern void create_builtin_stream(void);
145 static inline int symbol_list_size(struct symbol_list* list)
147 return ptr_list_size((struct ptr_list *)(list));
150 static inline int statement_list_size(struct statement_list* list)
152 return ptr_list_size((struct ptr_list *)(list));
155 static inline int expression_list_size(struct expression_list* list)
157 return ptr_list_size((struct ptr_list *)(list));
160 static inline int instruction_list_size(struct instruction_list* list)
162 return ptr_list_size((struct ptr_list *)(list));
165 static inline int phi_list_size(struct phi_list* list)
167 return ptr_list_size((struct ptr_list *)(list));
170 static inline int bb_list_size(struct basic_block_list* list)
172 return ptr_list_size((struct ptr_list *)(list));
175 static inline struct basic_block* next_basic_block(struct list_iterator *iterator)
177 return next_iterator(iterator);
180 static inline struct multijmp* next_multijmp(struct list_iterator *iterator)
182 return next_iterator(iterator);
185 static inline void free_instruction_list(struct instruction_list **head)
187 free_ptr_list((struct ptr_list **)head);
190 static inline void init_multijmp_iterator(struct multijmp_list **head, struct list_iterator *iterator, int flags)
192 init_iterator((struct ptr_list **)head, iterator, flags);
195 static inline void init_bb_iterator(struct basic_block_list **head, struct list_iterator *iterator, int flags)
197 init_iterator((struct ptr_list **)head, iterator, flags);
200 static inline struct instruction * delete_last_instruction(struct instruction_list **head)
202 return delete_ptr_list_last((struct ptr_list **)head);
205 static inline struct basic_block * delete_last_basic_block(struct basic_block_list **head)
207 return delete_ptr_list_last((struct ptr_list **)head);
210 static inline void *first_ptr_list(struct ptr_list *list)
212 if (!list)
213 return NULL;
214 return list->list[0];
217 static inline void *last_ptr_list(struct ptr_list *list)
220 if (!list)
221 return NULL;
222 list = list->prev;
223 return list->list[list->nr-1];
226 static inline void * current_iterator(struct list_iterator *iterator)
228 struct ptr_list *list = iterator->active;
229 return list ? list->list[iterator->index] : NULL;
232 static inline struct basic_block *first_basic_block(struct basic_block_list *head)
234 return first_ptr_list((struct ptr_list *)head);
236 static inline struct instruction *last_instruction(struct instruction_list *head)
238 return last_ptr_list((struct ptr_list *)head);
241 static inline struct instruction *first_instruction(struct instruction_list *head)
243 return first_ptr_list((struct ptr_list *)head);
246 static inline struct phi *first_phi(struct phi_list *head)
248 return first_ptr_list((struct ptr_list *)head);
251 static inline int replace_basic_block_list(struct basic_block_list *head, struct basic_block *from, struct basic_block *to)
253 return replace_ptr_list((struct ptr_list *)head, (void*)from, (void*)to);
256 static inline void concat_symbol_list(struct symbol_list *from, struct symbol_list **to)
258 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
261 static inline void concat_basic_block_list(struct basic_block_list *from, struct basic_block_list **to)
263 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
266 static inline void concat_instruction_list(struct instruction_list *from, struct instruction_list **to)
268 concat_ptr_list((struct ptr_list *)from, (struct ptr_list **)to);
271 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
273 add_ptr_list((struct ptr_list **)list, sym);
276 static inline void add_statement(struct statement_list **list, struct statement *stmt)
278 add_ptr_list((struct ptr_list **)list, stmt);
281 static inline void add_expression(struct expression_list **list, struct expression *expr)
283 add_ptr_list((struct ptr_list **)list, expr);
286 static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
288 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
291 static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
293 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
296 static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
298 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
301 #define DO_PREPARE(head, ptr, __head, __list, __nr) \
302 do { \
303 struct ptr_list *__head = (struct ptr_list *) (head); \
304 struct ptr_list *__list = __head; \
305 int __nr = 0; \
306 if (__head) ptr = (__typeof__(ptr)) __head->list[0]; \
307 else ptr = NULL
309 #define DO_NEXT(ptr, __head, __list, __nr) \
310 if (ptr) { \
311 if (++__nr < __list->nr) { \
312 ptr = (__typeof__(ptr)) __list->list[__nr]; \
313 } else { \
314 __list = __list->next; \
315 ptr = NULL; \
316 if (__list != __head) { \
317 __nr = 0; \
318 ptr = (__typeof__(ptr)) __list->list[0]; \
323 #define DO_RESET(ptr, __head, __list, __nr) \
324 do { \
325 __nr = 0; \
326 __list = __head; \
327 if (__head) ptr = (__typeof__(ptr)) __head->list[0]; \
328 } while (0)
330 #define DO_FINISH(ptr, __head, __list, __nr) \
331 (void)(__nr); /* Sanity-check nesting */ \
332 } while (0)
334 #define PREPARE_PTR_LIST(head, ptr) \
335 DO_PREPARE(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
337 #define NEXT_PTR_LIST(ptr) \
338 DO_NEXT(ptr, __head##ptr, __list##ptr, __nr##ptr)
340 #define RESET_PTR_LIST(ptr) \
341 DO_RESET(ptr, __head##ptr, __list##ptr, __nr##ptr)
343 #define FINISH_PTR_LIST(ptr) \
344 DO_FINISH(ptr, __head##ptr, __list##ptr, __nr##ptr)
346 #define DO_FOR_EACH(head, ptr, __head, __list, __nr) do { \
347 struct ptr_list *__head = (struct ptr_list *) (head); \
348 struct ptr_list *__list = __head; \
349 if (__head) { \
350 do { int __nr; \
351 for (__nr = 0; __nr < __list->nr; __nr++) { \
352 do { \
353 ptr = (__typeof__(ptr)) (__list->list[__nr]); \
354 do {
356 #define DO_END_FOR_EACH(ptr, __head, __list, __nr) \
357 } while (0); \
358 } while (0); \
360 } while ((__list = __list->next) != __head); \
362 } while (0)
364 #define DO_FOR_EACH_REVERSE(head, ptr, __head, __list, __nr) do { \
365 struct ptr_list *__head = (struct ptr_list *) (head); \
366 struct ptr_list *__list = __head; \
367 if (__head) { \
368 do { int __nr; \
369 __list = __list->prev; \
370 __nr = __list->nr; \
371 while (--__nr >= 0) { \
372 do { \
373 ptr = (__typeof__(ptr)) (__list->list[__nr]); \
374 do {
377 #define DO_END_FOR_EACH_REVERSE(ptr, __head, __list, __nr) \
378 } while (0); \
379 } while (0); \
381 } while (__list != __head); \
383 } while (0)
385 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr) \
386 ((__typeof__(&(ptr))) (__list->list + __nr))
388 #define FOR_EACH_PTR(head, ptr) \
389 DO_FOR_EACH(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
391 #define END_FOR_EACH_PTR(ptr) \
392 DO_END_FOR_EACH(ptr, __head##ptr, __list##ptr, __nr##ptr)
394 #define FOR_EACH_PTR_REVERSE(head, ptr) \
395 DO_FOR_EACH_REVERSE(head, ptr, __head##ptr, __list##ptr, __nr##ptr)
397 #define END_FOR_EACH_PTR_REVERSE(ptr) \
398 DO_END_FOR_EACH_REVERSE(ptr, __head##ptr, __list##ptr, __nr##ptr)
400 #define THIS_ADDRESS(ptr) \
401 DO_THIS_ADDRESS(ptr, __head##ptr, __list##ptr, __nr##ptr)
403 #define DO_DELETE_CURRENT(ptr, __head, __list, __nr) do { \
404 void **__this = __list->list + __nr; \
405 void **__last = __list->list + __list->nr - 1; \
406 while (__this < __last) { \
407 __this[0] = __this[1]; \
408 __this++; \
410 *__this = (void *)0xf0f0f0f0; \
411 __list->nr--; __nr--; \
412 } while (0)
414 #define DELETE_CURRENT_PTR(ptr) \
415 DO_DELETE_CURRENT(ptr, __head##ptr, __list##ptr, __nr##ptr)
417 #define REPLACE_CURRENT_PTR(ptr, new_ptr) \
418 do { *THIS_ADDRESS(ptr) = (new_ptr); } while (0)
420 #endif