*new* smatch_clear_buffer.c: handle memset() type functions
[smatch.git] / symbol.h
blob63dc587e625cde51c68257c450dd83d1c35fac74
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;
89 struct ctype {
90 unsigned long modifiers;
91 unsigned long alignment;
92 struct attribute *attribute;
93 struct symbol *base_type;
96 struct decl_state {
97 struct ctype ctype;
98 struct ident **ident;
99 struct symbol_op *mode;
100 unsigned char prefer_abstract, is_inline, storage_class, is_tls;
103 struct symbol_op {
104 enum keyword type;
105 int (*evaluate)(struct expression *);
106 int (*expand)(struct expression *, int);
107 int (*args)(struct expression *);
109 /* keywords */
110 struct token *(*declarator)(struct token *token, struct decl_state *ctx);
111 struct token *(*statement)(struct token *token, struct statement *stmt);
112 struct token *(*toplevel)(struct token *token, struct symbol_list **list);
113 struct token *(*attribute)(struct token *token, struct symbol *attr, struct decl_state *ctx);
114 struct symbol *(*to_mode)(struct symbol *);
116 int test, set, class;
119 extern int expand_safe_p(struct expression *expr, int cost);
120 extern int expand_constant_p(struct expression *expr, int cost);
122 #define SYM_ATTR_WEAK 0
123 #define SYM_ATTR_NORMAL 1
124 #define SYM_ATTR_STRONG 2
126 struct symbol {
127 enum type type:8;
128 enum namespace namespace:9;
129 unsigned char used:1, attr:2, enum_member:1, bound:1;
130 struct position pos; /* Where this symbol was declared */
131 struct position endpos; /* Where this symbol ends*/
132 struct ident *ident; /* What identifier this symbol is associated with */
133 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
134 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
135 struct scope *scope;
136 union {
137 struct symbol *same_symbol;
138 struct symbol *next_subobject;
141 struct symbol_op *op;
143 union {
144 struct /* NS_MACRO */ {
145 struct token *expansion;
146 struct token *arglist;
147 struct scope *used_in;
149 struct /* NS_PREPROCESSOR */ {
150 int (*handler)(struct stream *, struct token **, struct token *);
151 int normal;
153 struct /* NS_SYMBOL */ {
154 unsigned long offset;
155 int bit_size;
156 unsigned int bit_offset:8,
157 arg_count:10,
158 variadic:1,
159 initialized:1,
160 examined:1,
161 expanding:1,
162 evaluated:1,
163 string:1,
164 designated_init:1;
165 struct expression *array_size;
166 struct ctype ctype;
167 struct symbol_list *arguments;
168 struct statement *stmt;
169 struct symbol_list *symbol_list;
170 struct statement *inline_stmt;
171 struct symbol_list *inline_symbol_list;
172 struct expression *initializer;
173 struct entrypoint *ep;
174 long long value; /* Initial value */
175 struct symbol *definition;
178 union /* backend */ {
179 struct basic_block *bb_target; /* label */
180 void *aux; /* Auxiliary info, e.g. backend information */
181 struct { /* sparse ctags */
182 char kind;
183 unsigned char visited:1;
186 pseudo_t pseudo;
189 /* Modifiers */
190 #define MOD_AUTO 0x0001
191 #define MOD_REGISTER 0x0002
192 #define MOD_STATIC 0x0004
193 #define MOD_EXTERN 0x0008
195 #define MOD_CONST 0x0010
196 #define MOD_VOLATILE 0x0020
197 #define MOD_SIGNED 0x0040
198 #define MOD_UNSIGNED 0x0080
200 #define MOD_CHAR 0x0100
201 #define MOD_SHORT 0x0200
202 #define MOD_LONG 0x0400
203 #define MOD_LONGLONG 0x0800
204 #define MOD_LONGLONGLONG 0x1000
205 #define MOD_PURE 0x2000
207 #define MOD_TYPEDEF 0x10000
209 #define MOD_TLS 0x20000
210 #define MOD_INLINE 0x40000
211 #define MOD_ADDRESSABLE 0x80000
213 #define MOD_NOCAST 0x100000
214 #define MOD_NODEREF 0x200000
215 #define MOD_ACCESSED 0x400000
216 #define MOD_TOPLEVEL 0x800000 // scoping..
218 #define MOD_ASSIGNED 0x2000000
219 #define MOD_TYPE 0x4000000
220 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
222 #define MOD_USERTYPE 0x10000000
223 #define MOD_NORETURN 0x20000000
224 #define MOD_EXPLICITLY_SIGNED 0x40000000
225 #define MOD_BITWISE 0x80000000
228 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
229 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
230 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
231 #define MOD_LONG_ALL (MOD_LONG | MOD_LONGLONG | MOD_LONGLONGLONG)
232 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL | MOD_SIGNEDNESS)
233 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL)
234 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
235 MOD_ASSIGNED | MOD_USERTYPE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
236 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE | MOD_NORETURN)
238 /* default empty attribute */
239 extern struct attribute null_attr;
241 /* Current parsing/evaluation function */
242 extern struct symbol *current_fn;
244 /* Abstract types */
245 extern struct symbol int_type,
246 fp_type;
248 /* C types */
249 extern struct symbol bool_ctype, void_ctype, type_ctype,
250 char_ctype, schar_ctype, uchar_ctype,
251 short_ctype, sshort_ctype, ushort_ctype,
252 int_ctype, sint_ctype, uint_ctype,
253 long_ctype, slong_ctype, ulong_ctype,
254 llong_ctype, sllong_ctype, ullong_ctype,
255 lllong_ctype, slllong_ctype, ulllong_ctype,
256 float_ctype, double_ctype, ldouble_ctype,
257 string_ctype, ptr_ctype, lazy_ptr_ctype,
258 incomplete_ctype, label_ctype, bad_ctype,
259 null_ctype;
261 /* Special internal symbols */
262 extern struct symbol zero_int;
264 #define __IDENT(n,str,res) \
265 extern struct ident n
266 #include "ident-list.h"
268 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
270 extern struct symbol_list *translation_unit_used_list;
272 extern void access_symbol(struct symbol *);
274 extern const char * type_difference(struct ctype *c1, struct ctype *c2,
275 unsigned long mod1, unsigned long mod2);
277 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
278 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
279 extern void init_symbols(void);
280 extern void init_ctype(void);
281 extern struct symbol *alloc_symbol(struct position, int type);
282 extern void show_type(struct symbol *);
283 extern const char *modifier_string(unsigned long mod);
284 extern void show_symbol(struct symbol *);
285 extern int show_symbol_expr_init(struct symbol *sym);
286 extern void show_type_list(struct symbol *);
287 extern void show_symbol_list(struct symbol_list *, const char *);
288 extern void add_symbol(struct symbol_list **, struct symbol *);
289 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
291 extern struct symbol *examine_symbol_type(struct symbol *);
292 extern struct symbol *examine_pointer_target(struct symbol *);
293 extern void examine_simple_symbol_type(struct symbol *);
294 extern const char *show_typename(struct symbol *sym);
295 extern const char *builtin_typename(struct symbol *sym);
296 extern const char *builtin_ctypename(struct ctype *ctype);
297 extern const char* get_type_name(enum type type);
299 extern void debug_symbol(struct symbol *);
300 extern void merge_type(struct symbol *sym, struct symbol *base_type);
301 extern void check_declaration(struct symbol *sym);
303 static inline struct symbol *get_base_type(const struct symbol *sym)
305 return examine_symbol_type(sym->ctype.base_type);
308 static inline int is_int_type(const struct symbol *type)
310 if (type->type == SYM_NODE)
311 type = type->ctype.base_type;
312 if (type->type == SYM_ENUM)
313 type = type->ctype.base_type;
314 return type->type == SYM_BITFIELD ||
315 type->ctype.base_type == &int_type;
318 static inline int is_enum_type(const struct symbol *type)
320 if (type->type == SYM_NODE)
321 type = type->ctype.base_type;
322 return (type->type == SYM_ENUM);
325 static inline int is_type_type(struct symbol *type)
327 return (type->ctype.modifiers & MOD_TYPE) != 0;
330 static inline int is_ptr_type(struct symbol *type)
332 if (type->type == SYM_NODE)
333 type = type->ctype.base_type;
334 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
337 static inline int is_float_type(struct symbol *type)
339 if (type->type == SYM_NODE)
340 type = type->ctype.base_type;
341 return type->ctype.base_type == &fp_type;
344 static inline int is_byte_type(struct symbol *type)
346 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
349 static inline int is_void_type(struct symbol *type)
351 if (type->type == SYM_NODE)
352 type = type->ctype.base_type;
353 return type == &void_ctype;
356 static inline int is_bool_type(struct symbol *type)
358 if (type->type == SYM_NODE)
359 type = type->ctype.base_type;
360 return type == &bool_ctype;
363 static inline int is_function(struct symbol *type)
365 return type && type->type == SYM_FN;
368 static inline int is_extern_inline(struct symbol *sym)
370 return (sym->ctype.modifiers & MOD_EXTERN) &&
371 (sym->ctype.modifiers & MOD_INLINE) &&
372 is_function(sym->ctype.base_type);
375 static inline int get_sym_type(struct symbol *type)
377 if (type->type == SYM_NODE)
378 type = type->ctype.base_type;
379 if (type->type == SYM_ENUM)
380 type = type->ctype.base_type;
381 return type->type;
384 static inline struct symbol *lookup_keyword(struct ident *ident, enum namespace ns)
386 if (!ident->keyword)
387 return NULL;
388 return lookup_symbol(ident, ns);
391 static inline struct attribute *duplicate_attribute(struct attribute *attr)
393 struct attribute *newattr = __alloc_attribute(0);
394 *newattr = *attr;
395 return newattr;
398 static inline void attr_set_as(struct ctype *ctype, unsigned int as)
400 if (ctype->attribute->as != as) {
401 ctype->attribute = duplicate_attribute(ctype->attribute);
402 ctype->attribute->as = as;
406 static inline void attr_add_context(struct ctype *ctype, struct context *context)
408 ctype->attribute = duplicate_attribute(ctype->attribute);
409 add_ptr_list(&ctype->attribute->contexts, context);
412 static inline void merge_attr(struct ctype *dst, struct ctype *src)
414 struct attribute *attr;
416 if (src->attribute == &null_attr)
417 return;
418 if (dst->attribute == &null_attr) {
419 dst->attribute = src->attribute;
420 return;
423 dst->attribute = attr = duplicate_attribute(dst->attribute);
424 attr->as |= src->attribute->as;
425 concat_ptr_list((struct ptr_list *)src->attribute->contexts,
426 (struct ptr_list **)&attr->contexts);
429 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
430 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
431 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
432 extern int is_ptr_type(struct symbol *);
434 void create_fouled(struct symbol *type);
435 struct symbol *befoul(struct symbol *type);
437 #endif /* SYMBOL_H */