Add a type checking validation test-case that shows some of
[smatch.git] / lib.h
blobae1b4a92d91e18156d79355c4e3c5685a12c20a6
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(scope);
49 __DECLARE_ALLOCATOR(void, bytes);
51 #define LIST_NODE_NR (29)
53 struct ptr_list {
54 int nr;
55 struct ptr_list *prev;
56 struct ptr_list *next;
57 void *list[LIST_NODE_NR];
60 #define ITERATE_FIRST 1
61 #define ITERATE_LAST 2
62 void iterate(struct ptr_list *,void (*callback)(void *, void *, int), void*);
63 extern void add_ptr_list(struct ptr_list **, void *);
64 extern int ptr_list_size(struct ptr_list *);
66 #define symbol_list_size(list) ptr_list_size((struct ptr_list *)(list))
67 #define statement_list_size(list) ptr_list_size((struct ptr_list *)(list))
68 #define expression_list_size(list) ptr_list_size((struct ptr_list *)(list))
70 static inline void add_symbol(struct symbol_list **list, struct symbol *sym)
72 add_ptr_list((struct ptr_list **)list, sym);
75 static inline void add_statement(struct statement_list **list, struct statement *stmt)
77 add_ptr_list((struct ptr_list **)list, stmt);
80 static inline void add_expression(struct expression_list **list, struct expression *expr)
82 add_ptr_list((struct ptr_list **)list, expr);
85 static inline void symbol_iterate(struct symbol_list *list, void (*callback)(struct symbol *, void *, int), void *data)
87 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
90 static inline void statement_iterate(struct statement_list *list, void (*callback)(struct statement *, void *, int), void *data)
92 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
95 static inline void expression_iterate(struct expression_list *list, void (*callback)(struct expression *, void *, int), void *data)
97 iterate((struct ptr_list *)list, (void (*)(void *, void *, int))callback, data);
100 #define PREPARE_PTR_LIST(head, ptr) \
101 do { \
102 struct ptr_list *__head##ptr = (struct ptr_list *) (head); \
103 struct ptr_list *__list##ptr = __head##ptr; \
104 int __nr##ptr = 0; \
105 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
106 else ptr = NULL
108 #define NEXT_PTR_LIST(ptr) \
109 if (ptr) { \
110 if (++__nr##ptr < __list##ptr->nr) { \
111 ptr = (__typeof__(ptr)) __list##ptr->list[__nr##ptr]; \
112 } else { \
113 __list##ptr = __list##ptr->next; \
114 ptr = NULL; \
115 if (__list##ptr != __head##ptr) { \
116 __nr##ptr = 0; \
117 ptr = (__typeof__(ptr)) __list##ptr->list[0]; \
122 #define RESET_PTR_LIST(ptr) do { \
123 __nr##ptr = 0; \
124 __list##ptr = __head##ptr; \
125 if (__head##ptr) ptr = (__typeof__(ptr)) __head##ptr->list[0]; \
126 } while (0)
129 #define FINISH_PTR_LIST(ptr) \
130 (void)(__nr##ptr); /* Sanity-check nesting */ \
131 } while (0)
133 #define FOR_EACH_PTR(head, ptr) do { \
134 struct ptr_list *__head = (struct ptr_list *) (head); \
135 struct ptr_list *__list = __head; \
136 int __flag = ITERATE_FIRST; \
137 if (__head) { \
138 do { int __i; \
139 for (__i = 0; __i < __list->nr; __i++) { \
140 if (__i == __list->nr-1 && __list->next == __head) \
141 __flag |= ITERATE_LAST; \
142 do { \
143 ptr = (__typeof__(ptr)) (__list->list[__i]); \
144 do {
146 #define END_FOR_EACH_PTR } while (0); \
147 } while (0); \
148 __flag = 0; \
150 } while ((__list = __list->next) != __head); \
152 } while (0)
154 #define FOR_EACH_PTR_REVERSE(head, ptr) do { \
155 struct ptr_list *__head = (struct ptr_list *) (head); \
156 struct ptr_list *__list = __head; \
157 int __flag = ITERATE_FIRST; \
158 if (__head) { \
159 do { int __i; \
160 __list = __list->prev; \
161 __i = __list->nr; \
162 while (--__i >= 0) { \
163 if (__i == 0 && __list == __head) \
164 __flag |= ITERATE_LAST; \
165 do { \
166 ptr = (__typeof__(ptr)) (__list->list[__i]); \
167 do {
169 #define END_FOR_EACH_PTR_REVERSE } while (0); \
170 } while (0); \
171 __flag = 0; \
173 } while (__list != __head); \
175 } while (0)
177 #define THIS_ADDRESS(x) \
178 ((__typeof__(&(x))) (__list->list + __i))
180 #endif