sval: cast bitwise negates
[smatch.git] / symbol.h
blobeb259e352a4fe6b2fa5e9ceef609a66a07bf6c0e
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"
13 #include "target.h"
14 #include "allocate.h"
17 * An identifier with semantic meaning is a "symbol".
19 * There's a 1:n relationship: each symbol is always
20 * associated with one identifier, while each identifier
21 * can have one or more semantic meanings due to C scope
22 * rules.
24 * The progression is symbol -> token -> identifier. The
25 * token contains the information on where the symbol was
26 * declared.
28 enum namespace {
29 NS_NONE = 0,
30 NS_MACRO = 1,
31 NS_TYPEDEF = 2,
32 NS_STRUCT = 4, // Also used for unions and enums.
33 NS_LABEL = 8,
34 NS_SYMBOL = 16,
35 NS_ITERATOR = 32,
36 NS_PREPROCESSOR = 64,
37 NS_UNDEF = 128,
38 NS_KEYWORD = 256,
41 enum type {
42 SYM_UNINITIALIZED,
43 SYM_PREPROCESSOR,
44 SYM_BASETYPE,
45 SYM_NODE,
46 SYM_PTR,
47 SYM_FN,
48 SYM_ARRAY,
49 SYM_STRUCT,
50 SYM_UNION,
51 SYM_ENUM,
52 SYM_TYPEDEF,
53 SYM_TYPEOF,
54 SYM_MEMBER,
55 SYM_BITFIELD,
56 SYM_LABEL,
57 SYM_RESTRICT,
58 SYM_FOULED,
59 SYM_KEYWORD,
60 SYM_BAD,
63 enum keyword {
64 KW_SPECIFIER = 1 << 0,
65 KW_MODIFIER = 1 << 1,
66 KW_QUALIFIER = 1 << 2,
67 KW_ATTRIBUTE = 1 << 3,
68 KW_STATEMENT = 1 << 4,
69 KW_ASM = 1 << 5,
70 KW_MODE = 1 << 6,
71 KW_SHORT = 1 << 7,
72 KW_LONG = 1 << 8,
73 KW_EXACT = 1 << 9,
76 struct context {
77 struct expression *context;
78 unsigned int in, out;
81 extern struct context *alloc_context(void);
83 struct attribute {
84 struct context_list *contexts;
85 unsigned int as;
86 unsigned int is_packed:1;
90 struct ctype {
91 unsigned long modifiers;
92 unsigned long alignment;
93 struct attribute *attribute;
94 struct symbol *base_type;
97 struct decl_state {
98 struct ctype ctype;
99 struct ident **ident;
100 struct symbol_op *mode;
101 unsigned char prefer_abstract, is_inline, storage_class, is_tls;
104 struct symbol_op {
105 enum keyword type;
106 int (*evaluate)(struct expression *);
107 int (*expand)(struct expression *, int);
108 int (*args)(struct expression *);
110 /* keywords */
111 struct token *(*declarator)(struct token *token, struct decl_state *ctx);
112 struct token *(*statement)(struct token *token, struct statement *stmt);
113 struct token *(*toplevel)(struct token *token, struct symbol_list **list);
114 struct token *(*attribute)(struct token *token, struct symbol *attr, struct decl_state *ctx);
115 struct symbol *(*to_mode)(struct symbol *);
117 int test, set, class;
120 extern int expand_safe_p(struct expression *expr, int cost);
121 extern int expand_constant_p(struct expression *expr, int cost);
123 #define SYM_ATTR_WEAK 0
124 #define SYM_ATTR_NORMAL 1
125 #define SYM_ATTR_STRONG 2
127 struct symbol {
128 enum type type:8;
129 enum namespace namespace:9;
130 unsigned char used:1, attr:2, enum_member:1, bound:1;
131 struct position pos; /* Where this symbol was declared */
132 struct position endpos; /* Where this symbol ends*/
133 struct ident *ident; /* What identifier this symbol is associated with */
134 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
135 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
136 struct scope *scope;
137 union {
138 struct symbol *same_symbol;
139 struct symbol *next_subobject;
142 struct symbol_op *op;
144 union {
145 struct /* NS_MACRO */ {
146 struct token *expansion;
147 struct token *arglist;
148 struct scope *used_in;
150 struct /* NS_PREPROCESSOR */ {
151 int (*handler)(struct stream *, struct token **, struct token *);
152 int normal;
154 struct /* NS_SYMBOL */ {
155 unsigned long offset;
156 int bit_size;
157 unsigned int bit_offset:8,
158 arg_count:10,
159 variadic:1,
160 initialized:1,
161 examined:1,
162 expanding:1,
163 evaluated:1,
164 string:1,
165 designated_init:1;
166 struct expression *array_size;
167 struct ctype ctype;
168 struct symbol_list *arguments;
169 struct statement *stmt;
170 struct symbol_list *symbol_list;
171 struct statement *inline_stmt;
172 struct symbol_list *inline_symbol_list;
173 struct expression *initializer;
174 struct entrypoint *ep;
175 long long value; /* Initial value */
176 struct symbol *definition;
179 union /* backend */ {
180 struct basic_block *bb_target; /* label */
181 void *aux; /* Auxiliary info, e.g. backend information */
182 struct { /* sparse ctags */
183 char kind;
184 unsigned char visited:1;
187 pseudo_t pseudo;
190 /* Modifiers */
191 #define MOD_AUTO 0x0001
192 #define MOD_REGISTER 0x0002
193 #define MOD_STATIC 0x0004
194 #define MOD_EXTERN 0x0008
196 #define MOD_CONST 0x0010
197 #define MOD_VOLATILE 0x0020
198 #define MOD_SIGNED 0x0040
199 #define MOD_UNSIGNED 0x0080
201 #define MOD_CHAR 0x0100
202 #define MOD_SHORT 0x0200
203 #define MOD_LONG 0x0400
204 #define MOD_LONGLONG 0x0800
205 #define MOD_LONGLONGLONG 0x1000
206 #define MOD_PURE 0x2000
208 #define MOD_TYPEDEF 0x10000
210 #define MOD_TLS 0x20000
211 #define MOD_INLINE 0x40000
212 #define MOD_ADDRESSABLE 0x80000
214 #define MOD_NOCAST 0x100000
215 #define MOD_NODEREF 0x200000
216 #define MOD_ACCESSED 0x400000
217 #define MOD_TOPLEVEL 0x800000 // scoping..
219 #define MOD_ASSIGNED 0x2000000
220 #define MOD_TYPE 0x4000000
221 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
223 #define MOD_USERTYPE 0x10000000
224 #define MOD_NORETURN 0x20000000
225 #define MOD_EXPLICITLY_SIGNED 0x40000000
226 #define MOD_BITWISE 0x80000000
229 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
230 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
231 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
232 #define MOD_LONG_ALL (MOD_LONG | MOD_LONGLONG | MOD_LONGLONGLONG)
233 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL | MOD_SIGNEDNESS)
234 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL)
235 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
236 MOD_ASSIGNED | MOD_USERTYPE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
237 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE | MOD_NORETURN)
239 /* default empty attribute */
240 extern struct attribute null_attr;
242 /* Current parsing/evaluation function */
243 extern struct symbol *current_fn;
245 /* Abstract types */
246 extern struct symbol int_type,
247 fp_type;
249 /* C types */
250 extern struct symbol bool_ctype, void_ctype, type_ctype,
251 char_ctype, schar_ctype, uchar_ctype,
252 short_ctype, sshort_ctype, ushort_ctype,
253 int_ctype, sint_ctype, uint_ctype,
254 long_ctype, slong_ctype, ulong_ctype,
255 llong_ctype, sllong_ctype, ullong_ctype,
256 lllong_ctype, slllong_ctype, ulllong_ctype,
257 float_ctype, double_ctype, ldouble_ctype,
258 string_ctype, ptr_ctype, lazy_ptr_ctype,
259 incomplete_ctype, label_ctype, bad_ctype,
260 null_ctype;
262 /* Special internal symbols */
263 extern struct symbol zero_int;
265 #define __IDENT(n,str,res) \
266 extern struct ident n
267 #include "ident-list.h"
269 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
271 extern struct symbol_list *translation_unit_used_list;
273 extern void access_symbol(struct symbol *);
275 extern const char * type_difference(struct ctype *c1, struct ctype *c2,
276 unsigned long mod1, unsigned long mod2);
278 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
279 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
280 extern void init_symbols(void);
281 extern void init_ctype(void);
282 extern struct symbol *alloc_symbol(struct position, int type);
283 extern void show_type(struct symbol *);
284 extern const char *modifier_string(unsigned long mod);
285 extern void show_symbol(struct symbol *);
286 extern int show_symbol_expr_init(struct symbol *sym);
287 extern void show_type_list(struct symbol *);
288 extern void show_symbol_list(struct symbol_list *, const char *);
289 extern void add_symbol(struct symbol_list **, struct symbol *);
290 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
292 extern struct symbol *examine_symbol_type(struct symbol *);
293 extern struct symbol *examine_pointer_target(struct symbol *);
294 extern void examine_simple_symbol_type(struct symbol *);
295 extern const char *show_typename(struct symbol *sym);
296 extern const char *builtin_typename(struct symbol *sym);
297 extern const char *builtin_ctypename(struct ctype *ctype);
298 extern const char* get_type_name(enum type type);
300 extern void debug_symbol(struct symbol *);
301 extern void merge_type(struct symbol *sym, struct symbol *base_type);
302 extern void check_declaration(struct symbol *sym);
304 static inline struct symbol *get_base_type(const struct symbol *sym)
306 return examine_symbol_type(sym->ctype.base_type);
309 static inline int is_int_type(const struct symbol *type)
311 if (type->type == SYM_NODE)
312 type = type->ctype.base_type;
313 if (type->type == SYM_ENUM)
314 type = type->ctype.base_type;
315 return type->type == SYM_BITFIELD ||
316 type->ctype.base_type == &int_type;
319 static inline int is_enum_type(const struct symbol *type)
321 if (type->type == SYM_NODE)
322 type = type->ctype.base_type;
323 return (type->type == SYM_ENUM);
326 static inline int is_type_type(struct symbol *type)
328 return (type->ctype.modifiers & MOD_TYPE) != 0;
331 static inline int is_ptr_type(struct symbol *type)
333 if (type->type == SYM_NODE)
334 type = type->ctype.base_type;
335 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
338 static inline int is_float_type(struct symbol *type)
340 if (type->type == SYM_NODE)
341 type = type->ctype.base_type;
342 return type->ctype.base_type == &fp_type;
345 static inline int is_byte_type(struct symbol *type)
347 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
350 static inline int is_void_type(struct symbol *type)
352 if (type->type == SYM_NODE)
353 type = type->ctype.base_type;
354 return type == &void_ctype;
357 static inline int is_bool_type(struct symbol *type)
359 if (type->type == SYM_NODE)
360 type = type->ctype.base_type;
361 return type == &bool_ctype;
364 static inline int is_function(struct symbol *type)
366 return type && type->type == SYM_FN;
369 static inline int is_extern_inline(struct symbol *sym)
371 return (sym->ctype.modifiers & MOD_EXTERN) &&
372 (sym->ctype.modifiers & MOD_INLINE) &&
373 is_function(sym->ctype.base_type);
376 static inline int get_sym_type(struct symbol *type)
378 if (type->type == SYM_NODE)
379 type = type->ctype.base_type;
380 if (type->type == SYM_ENUM)
381 type = type->ctype.base_type;
382 return type->type;
385 static inline struct symbol *lookup_keyword(struct ident *ident, enum namespace ns)
387 if (!ident->keyword)
388 return NULL;
389 return lookup_symbol(ident, ns);
392 static inline struct attribute *duplicate_attribute(struct attribute *attr)
394 struct attribute *newattr = __alloc_attribute(0);
395 *newattr = *attr;
396 return newattr;
399 static inline void attr_set_as(struct ctype *ctype, unsigned int as)
401 if (ctype->attribute->as != as) {
402 ctype->attribute = duplicate_attribute(ctype->attribute);
403 ctype->attribute->as = as;
407 static inline void attr_add_context(struct ctype *ctype, struct context *context)
409 ctype->attribute = duplicate_attribute(ctype->attribute);
410 add_ptr_list(&ctype->attribute->contexts, context);
413 static inline void merge_attr(struct ctype *dst, struct ctype *src)
415 struct attribute *attr;
417 if (src->attribute == &null_attr)
418 return;
419 if (dst->attribute == &null_attr) {
420 dst->attribute = src->attribute;
421 return;
424 dst->attribute = attr = duplicate_attribute(dst->attribute);
425 attr->as |= src->attribute->as;
426 concat_ptr_list((struct ptr_list *)src->attribute->contexts,
427 (struct ptr_list **)&attr->contexts);
430 static inline void set_attr_is_packed(struct ctype *ctype)
432 if (ctype->attribute == &null_attr)
433 ctype->attribute = __alloc_attribute(0);
434 ctype->attribute->is_packed = 1;
437 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
438 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
439 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
440 extern int is_ptr_type(struct symbol *);
442 void create_fouled(struct symbol *type);
443 struct symbol *befoul(struct symbol *type);
445 #endif /* SYMBOL_H */