Update the information in README about using the library.
[smatch.git] / symbol.h
blobe6081e687cf5a138e0d124490239b52d9d8b685c
1 #ifndef SYMBOL_H
2 #define SYMBOL_H
3 /*
4 * Basic symbol and namespace definitions.
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
12 #include "token.h"
15 * An identifier with semantic meaning is a "symbol".
17 * There's a 1:n relationship: each symbol is always
18 * associated with one identifier, while each identifier
19 * can have one or more semantic meanings due to C scope
20 * rules.
22 * The progression is symbol -> token -> identifier. The
23 * token contains the information on where the symbol was
24 * declared.
26 enum namespace {
27 NS_NONE = 0,
28 NS_MACRO = 1,
29 NS_TYPEDEF = 2,
30 NS_STRUCT = 4, // Also used for unions and enums.
31 NS_LABEL = 8,
32 NS_SYMBOL = 16,
33 NS_ITERATOR = 32,
34 NS_PREPROCESSOR = 64,
35 NS_UNDEF = 128,
38 enum type {
39 SYM_UNINITIALIZED,
40 SYM_PREPROCESSOR,
41 SYM_BASETYPE,
42 SYM_NODE,
43 SYM_PTR,
44 SYM_FN,
45 SYM_ARRAY,
46 SYM_STRUCT,
47 SYM_UNION,
48 SYM_ENUM,
49 SYM_TYPEDEF,
50 SYM_TYPEOF,
51 SYM_MEMBER,
52 SYM_BITFIELD,
53 SYM_LABEL,
54 SYM_RESTRICT,
55 SYM_FOULED,
56 SYM_BAD,
59 struct context {
60 struct expression *context;
61 unsigned int in, out;
64 extern struct context *alloc_context(void);
66 DECLARE_PTR_LIST(context_list, struct context);
68 struct ctype {
69 unsigned long modifiers;
70 unsigned long alignment;
71 struct context_list *contexts;
72 unsigned int as;
73 struct symbol *base_type;
76 struct symbol_op {
77 int (*evaluate)(struct expression *);
78 int (*expand)(struct expression *, int);
79 int (*args)(struct expression *);
80 };
82 extern int expand_safe_p(struct expression *expr, int cost);
83 extern int expand_constant_p(struct expression *expr, int cost);
85 #define SYM_ATTR_WEAK 0
86 #define SYM_ATTR_NORMAL 1
87 #define SYM_ATTR_STRONG 2
89 struct symbol {
90 enum namespace namespace:8;
91 enum type type:8;
92 unsigned char used:1, attr:2;
93 struct position pos; /* Where this symbol was declared */
94 struct ident *ident; /* What identifier this symbol is associated with */
95 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
96 struct symbol **id_list; /* Backpointer to symbol list head */
97 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
98 struct scope *scope;
99 struct symbol *same_symbol;
100 struct symbol_op *op;
102 union {
103 struct /* NS_MACRO */ {
104 struct token *expansion;
105 struct token *arglist;
106 struct scope *used_in;
108 struct /* NS_PREPROCESSOR */ {
109 int (*handler)(struct stream *, struct token **, struct token *);
110 int normal;
112 struct /* NS_SYMBOL */ {
113 unsigned long offset;
114 int bit_size;
115 unsigned int bit_offset:8,
116 arg_count:10,
117 variadic:1,
118 initialized:1,
119 examined:1,
120 expanding:1,
121 evaluated:1,
122 string:1;
123 struct expression *array_size;
124 struct ctype ctype;
125 struct symbol_list *arguments;
126 struct statement *stmt;
127 struct symbol_list *symbol_list;
128 struct statement *inline_stmt;
129 struct symbol_list *inline_symbol_list;
130 struct expression *initializer;
131 long long value; /* Initial value */
134 union /* backend */ {
135 struct basic_block *bb_target; /* label */
136 void *aux; /* Auxiliary info, eg. backend information */
137 struct { /* sparse ctags */
138 char kind;
139 unsigned char visited:1;
142 pseudo_t pseudo;
145 /* Modifiers */
146 #define MOD_AUTO 0x0001
147 #define MOD_REGISTER 0x0002
148 #define MOD_STATIC 0x0004
149 #define MOD_EXTERN 0x0008
151 #define MOD_CONST 0x0010
152 #define MOD_VOLATILE 0x0020
153 #define MOD_SIGNED 0x0040
154 #define MOD_UNSIGNED 0x0080
156 #define MOD_CHAR 0x0100
157 #define MOD_SHORT 0x0200
158 #define MOD_LONG 0x0400
159 #define MOD_LONGLONG 0x0800
161 #define MOD_TYPEDEF 0x1000
162 #define MOD_STRUCTOF 0x2000
163 #define MOD_UNIONOF 0x4000
164 #define MOD_ENUMOF 0x8000
166 #define MOD_TYPEOF 0x10000
167 #define MOD_ATTRIBUTE 0x20000
168 #define MOD_INLINE 0x40000
169 #define MOD_ADDRESSABLE 0x80000
171 #define MOD_NOCAST 0x100000
172 #define MOD_NODEREF 0x200000
173 #define MOD_ACCESSED 0x400000
174 #define MOD_TOPLEVEL 0x800000 // scoping..
176 #define MOD_LABEL 0x1000000
177 #define MOD_ASSIGNED 0x2000000
178 #define MOD_TYPE 0x4000000
179 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
181 #define MOD_USERTYPE 0x10000000
182 #define MOD_FORCE 0x20000000
183 #define MOD_EXPLICITLY_SIGNED 0x40000000
184 #define MOD_BITWISE 0x80000000
186 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
187 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
188 #define MOD_SPECIALBITS (MOD_STRUCTOF | MOD_UNIONOF | MOD_ENUMOF | MOD_ATTRIBUTE | MOD_TYPEOF)
189 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
190 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG | MOD_SIGNEDNESS)
191 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG | MOD_LONGLONG)
192 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
193 MOD_ASSIGNED | MOD_USERTYPE | MOD_FORCE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
194 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE)
197 /* Current parsing/evaluation function */
198 extern struct symbol *current_fn;
200 /* Abstract types */
201 extern struct symbol int_type,
202 fp_type;
204 /* C types */
205 extern struct symbol bool_ctype, void_ctype, type_ctype,
206 char_ctype, schar_ctype, uchar_ctype,
207 short_ctype, sshort_ctype, ushort_ctype,
208 int_ctype, sint_ctype, uint_ctype,
209 long_ctype, slong_ctype, ulong_ctype,
210 llong_ctype, sllong_ctype, ullong_ctype,
211 float_ctype, double_ctype, ldouble_ctype,
212 string_ctype, ptr_ctype, lazy_ptr_ctype,
213 incomplete_ctype, label_ctype, bad_ctype;
215 /* Special internal symbols */
216 extern struct symbol zero_int;
218 #define __IDENT(n,str,res) \
219 extern struct ident n
220 #include "ident-list.h"
222 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
224 extern struct symbol_list *translation_unit_used_list;
226 extern void access_symbol(struct symbol *);
228 extern const char * type_difference(struct symbol *target, struct symbol *source,
229 unsigned long target_mod_ignore, unsigned long source_mod_ignore);
231 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
232 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
233 extern void init_symbols(void);
234 extern void init_ctype(void);
235 extern struct symbol *alloc_symbol(struct position, int type);
236 extern void show_type(struct symbol *);
237 extern const char *modifier_string(unsigned long mod);
238 extern void show_symbol(struct symbol *);
239 extern int show_symbol_expr_init(struct symbol *sym);
240 extern void show_type_list(struct symbol *);
241 extern void show_symbol_list(struct symbol_list *, const char *);
242 extern void add_symbol(struct symbol_list **, struct symbol *);
243 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
245 extern struct symbol *examine_symbol_type(struct symbol *);
246 extern void examine_simple_symbol_type(struct symbol *);
247 extern const char *show_typename(struct symbol *sym);
248 extern const char *builtin_typename(struct symbol *sym);
249 extern const char *builtin_ctypename(struct ctype *ctype);
251 extern void debug_symbol(struct symbol *);
252 extern void merge_type(struct symbol *sym, struct symbol *base_type);
253 extern void check_declaration(struct symbol *sym);
255 static inline struct symbol *get_base_type(const struct symbol *sym)
257 return examine_symbol_type(sym->ctype.base_type);
260 static inline int is_int_type(const struct symbol *type)
262 if (type->type == SYM_NODE)
263 type = type->ctype.base_type;
264 if (type->type == SYM_ENUM)
265 type = type->ctype.base_type;
266 return type->type == SYM_BITFIELD ||
267 type->ctype.base_type == &int_type;
270 static inline int is_enum_type(const struct symbol *type)
272 if (type->type == SYM_NODE)
273 type = type->ctype.base_type;
274 return (type->type == SYM_ENUM);
277 static inline int get_sym_type(struct symbol *type)
279 if (type->type == SYM_NODE)
280 type = type->ctype.base_type;
281 if (type->type == SYM_ENUM)
282 type = type->ctype.base_type;
283 return type->type;
286 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
287 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
288 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
289 extern int is_ptr_type(struct symbol *);
291 void create_fouled(struct symbol *type);
292 struct symbol *befoul(struct symbol *type);
294 #endif /* SYMBOL_H */