validation/sm_implied8.c: update test
[smatch.git] / symbol.h
bloba0f681598aab11a7413cc980e2eb40f03dbc75a5
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 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
28 #include "token.h"
29 #include "target.h"
30 #include "allocate.h"
33 * An identifier with semantic meaning is a "symbol".
35 * There's a 1:n relationship: each symbol is always
36 * associated with one identifier, while each identifier
37 * can have one or more semantic meanings due to C scope
38 * rules.
40 * The progression is symbol -> token -> identifier. The
41 * token contains the information on where the symbol was
42 * declared.
44 enum namespace {
45 NS_NONE = 0,
46 NS_MACRO = 1,
47 NS_TYPEDEF = 2,
48 NS_STRUCT = 4, // Also used for unions and enums.
49 NS_LABEL = 8,
50 NS_SYMBOL = 16,
51 NS_ITERATOR = 32,
52 NS_PREPROCESSOR = 64,
53 NS_UNDEF = 128,
54 NS_KEYWORD = 256,
57 enum type {
58 SYM_UNINITIALIZED,
59 SYM_PREPROCESSOR,
60 SYM_BASETYPE,
61 SYM_NODE,
62 SYM_PTR,
63 SYM_FN,
64 SYM_ARRAY,
65 SYM_STRUCT,
66 SYM_UNION,
67 SYM_ENUM,
68 SYM_TYPEDEF,
69 SYM_TYPEOF,
70 SYM_MEMBER,
71 SYM_BITFIELD,
72 SYM_LABEL,
73 SYM_RESTRICT,
74 SYM_FOULED,
75 SYM_KEYWORD,
76 SYM_BAD,
79 enum keyword {
80 KW_SPECIFIER = 1 << 0,
81 KW_MODIFIER = 1 << 1,
82 KW_QUALIFIER = 1 << 2,
83 KW_ATTRIBUTE = 1 << 3,
84 KW_STATEMENT = 1 << 4,
85 KW_ASM = 1 << 5,
86 KW_MODE = 1 << 6,
87 KW_SHORT = 1 << 7,
88 KW_LONG = 1 << 8,
89 KW_EXACT = 1 << 9,
92 struct context {
93 struct expression *context;
94 unsigned int in, out;
97 extern struct context *alloc_context(void);
99 struct attribute {
100 struct context_list *contexts;
101 unsigned int as;
102 unsigned int is_packed:1;
106 struct ctype {
107 unsigned long modifiers;
108 unsigned long alignment;
109 struct attribute *attribute;
110 struct symbol *base_type;
113 struct decl_state {
114 struct ctype ctype;
115 struct ident **ident;
116 struct symbol_op *mode;
117 unsigned char prefer_abstract, is_inline, storage_class, is_tls;
120 struct symbol_op {
121 enum keyword type;
122 int (*evaluate)(struct expression *);
123 int (*expand)(struct expression *, int);
124 int (*args)(struct expression *);
126 /* keywords */
127 struct token *(*declarator)(struct token *token, struct decl_state *ctx);
128 struct token *(*statement)(struct token *token, struct statement *stmt);
129 struct token *(*toplevel)(struct token *token, struct symbol_list **list);
130 struct token *(*attribute)(struct token *token, struct symbol *attr, struct decl_state *ctx);
131 struct symbol *(*to_mode)(struct symbol *);
133 int test, set, class;
136 extern int expand_safe_p(struct expression *expr, int cost);
137 extern int expand_constant_p(struct expression *expr, int cost);
139 #define SYM_ATTR_WEAK 0
140 #define SYM_ATTR_NORMAL 1
141 #define SYM_ATTR_STRONG 2
143 struct symbol {
144 enum type type:8;
145 enum namespace namespace:9;
146 unsigned char used:1, attr:2, enum_member:1, bound:1;
147 struct position pos; /* Where this symbol was declared */
148 struct position endpos; /* Where this symbol ends*/
149 struct ident *ident; /* What identifier this symbol is associated with */
150 struct symbol *next_id; /* Next semantic symbol that shares this identifier */
151 struct symbol *replace; /* What is this symbol shadowed by in copy-expression */
152 struct scope *scope;
153 union {
154 struct symbol *same_symbol;
155 struct symbol *next_subobject;
158 struct symbol_op *op;
160 union {
161 struct /* NS_MACRO */ {
162 struct token *expansion;
163 struct token *arglist;
164 struct scope *used_in;
166 struct /* NS_PREPROCESSOR */ {
167 int (*handler)(struct stream *, struct token **, struct token *);
168 int normal;
170 struct /* NS_SYMBOL */ {
171 unsigned long offset;
172 int bit_size;
173 unsigned int bit_offset:8,
174 arg_count:10,
175 variadic:1,
176 initialized:1,
177 examined:1,
178 expanding:1,
179 evaluated:1,
180 string:1,
181 designated_init:1,
182 forced_arg:1,
183 transparent_union:1;
184 struct expression *array_size;
185 struct ctype ctype;
186 struct symbol_list *arguments;
187 struct statement *stmt;
188 struct symbol_list *symbol_list;
189 struct statement *inline_stmt;
190 struct symbol_list *inline_symbol_list;
191 struct expression *initializer;
192 struct entrypoint *ep;
193 long long value; /* Initial value */
194 struct symbol *definition;
197 union /* backend */ {
198 struct basic_block *bb_target; /* label */
199 void *aux; /* Auxiliary info, e.g. backend information */
200 struct { /* sparse ctags */
201 char kind;
202 unsigned char visited:1;
205 pseudo_t pseudo;
208 /* Modifiers */
209 #define MOD_AUTO 0x0001
210 #define MOD_REGISTER 0x0002
211 #define MOD_STATIC 0x0004
212 #define MOD_EXTERN 0x0008
214 #define MOD_CONST 0x0010
215 #define MOD_VOLATILE 0x0020
216 #define MOD_SIGNED 0x0040
217 #define MOD_UNSIGNED 0x0080
219 #define MOD_CHAR 0x0100
220 #define MOD_SHORT 0x0200
221 #define MOD_LONG 0x0400
222 #define MOD_LONGLONG 0x0800
223 #define MOD_LONGLONGLONG 0x1000
224 #define MOD_PURE 0x2000
226 #define MOD_TYPEDEF 0x10000
228 #define MOD_TLS 0x20000
229 #define MOD_INLINE 0x40000
230 #define MOD_ADDRESSABLE 0x80000
232 #define MOD_NOCAST 0x100000
233 #define MOD_NODEREF 0x200000
234 #define MOD_ACCESSED 0x400000
235 #define MOD_TOPLEVEL 0x800000 // scoping..
237 #define MOD_ASSIGNED 0x2000000
238 #define MOD_TYPE 0x4000000
239 #define MOD_SAFE 0x8000000 // non-null/non-trapping pointer
241 #define MOD_USERTYPE 0x10000000
242 #define MOD_NORETURN 0x20000000
243 #define MOD_EXPLICITLY_SIGNED 0x40000000
244 #define MOD_BITWISE 0x80000000
247 #define MOD_NONLOCAL (MOD_EXTERN | MOD_TOPLEVEL)
248 #define MOD_STORAGE (MOD_AUTO | MOD_REGISTER | MOD_STATIC | MOD_EXTERN | MOD_INLINE | MOD_TOPLEVEL)
249 #define MOD_SIGNEDNESS (MOD_SIGNED | MOD_UNSIGNED | MOD_EXPLICITLY_SIGNED)
250 #define MOD_LONG_ALL (MOD_LONG | MOD_LONGLONG | MOD_LONGLONGLONG)
251 #define MOD_SPECIFIER (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL | MOD_SIGNEDNESS)
252 #define MOD_SIZE (MOD_CHAR | MOD_SHORT | MOD_LONG_ALL)
253 #define MOD_IGNORE (MOD_TOPLEVEL | MOD_STORAGE | MOD_ADDRESSABLE | \
254 MOD_ASSIGNED | MOD_USERTYPE | MOD_ACCESSED | MOD_EXPLICITLY_SIGNED)
255 #define MOD_PTRINHERIT (MOD_VOLATILE | MOD_CONST | MOD_NODEREF | MOD_STORAGE | MOD_NORETURN)
257 /* default empty attribute */
258 extern struct attribute null_attr;
260 /* Current parsing/evaluation function */
261 extern struct symbol *current_fn;
263 /* Abstract types */
264 extern struct symbol int_type,
265 fp_type;
267 /* C types */
268 extern struct symbol bool_ctype, void_ctype, type_ctype,
269 char_ctype, schar_ctype, uchar_ctype,
270 short_ctype, sshort_ctype, ushort_ctype,
271 int_ctype, sint_ctype, uint_ctype,
272 long_ctype, slong_ctype, ulong_ctype,
273 llong_ctype, sllong_ctype, ullong_ctype,
274 lllong_ctype, slllong_ctype, ulllong_ctype,
275 float_ctype, double_ctype, ldouble_ctype,
276 string_ctype, ptr_ctype, lazy_ptr_ctype,
277 incomplete_ctype, label_ctype, bad_ctype,
278 null_ctype;
280 /* Special internal symbols */
281 extern struct symbol zero_int;
283 #define __IDENT(n,str,res) \
284 extern struct ident n
285 #include "ident-list.h"
287 #define symbol_is_typename(sym) ((sym)->type == SYM_TYPE)
289 extern struct symbol_list *translation_unit_used_list;
291 extern void access_symbol(struct symbol *);
293 extern const char * type_difference(struct ctype *c1, struct ctype *c2,
294 unsigned long mod1, unsigned long mod2);
296 extern struct symbol *lookup_symbol(struct ident *, enum namespace);
297 extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
298 extern void init_symbols(void);
299 extern void init_ctype(void);
300 extern struct symbol *alloc_symbol(struct position, int type);
301 extern void show_type(struct symbol *);
302 extern const char *modifier_string(unsigned long mod);
303 extern void show_symbol(struct symbol *);
304 extern int show_symbol_expr_init(struct symbol *sym);
305 extern void show_type_list(struct symbol *);
306 extern void show_symbol_list(struct symbol_list *, const char *);
307 extern void add_symbol(struct symbol_list **, struct symbol *);
308 extern void bind_symbol(struct symbol *, struct ident *, enum namespace);
310 extern struct symbol *examine_symbol_type(struct symbol *);
311 extern struct symbol *examine_pointer_target(struct symbol *);
312 extern void examine_simple_symbol_type(struct symbol *);
313 extern const char *show_typename(struct symbol *sym);
314 extern const char *builtin_typename(struct symbol *sym);
315 extern const char *builtin_ctypename(struct ctype *ctype);
316 extern const char* get_type_name(enum type type);
318 extern void debug_symbol(struct symbol *);
319 extern void merge_type(struct symbol *sym, struct symbol *base_type);
320 extern void check_declaration(struct symbol *sym);
322 static inline struct symbol *get_base_type(const struct symbol *sym)
324 return examine_symbol_type(sym->ctype.base_type);
327 static inline int is_int_type(const struct symbol *type)
329 if (type->type == SYM_NODE)
330 type = type->ctype.base_type;
331 if (type->type == SYM_ENUM)
332 type = type->ctype.base_type;
333 return type->type == SYM_BITFIELD ||
334 type->ctype.base_type == &int_type;
337 static inline int is_enum_type(const struct symbol *type)
339 if (type->type == SYM_NODE)
340 type = type->ctype.base_type;
341 return (type->type == SYM_ENUM);
344 static inline int is_type_type(struct symbol *type)
346 return (type->ctype.modifiers & MOD_TYPE) != 0;
349 static inline int is_ptr_type(struct symbol *type)
351 if (type->type == SYM_NODE)
352 type = type->ctype.base_type;
353 return type->type == SYM_PTR || type->type == SYM_ARRAY || type->type == SYM_FN;
356 static inline int is_float_type(struct symbol *type)
358 if (type->type == SYM_NODE)
359 type = type->ctype.base_type;
360 return type->ctype.base_type == &fp_type;
363 static inline int is_byte_type(struct symbol *type)
365 return type->bit_size == bits_in_char && type->type != SYM_BITFIELD;
368 static inline int is_void_type(struct symbol *type)
370 if (type->type == SYM_NODE)
371 type = type->ctype.base_type;
372 return type == &void_ctype;
375 static inline int is_bool_type(struct symbol *type)
377 if (type->type == SYM_NODE)
378 type = type->ctype.base_type;
379 return type == &bool_ctype;
382 static inline int is_function(struct symbol *type)
384 return type && type->type == SYM_FN;
387 static inline int is_extern_inline(struct symbol *sym)
389 return (sym->ctype.modifiers & MOD_EXTERN) &&
390 (sym->ctype.modifiers & MOD_INLINE) &&
391 is_function(sym->ctype.base_type);
394 static inline int get_sym_type(struct symbol *type)
396 if (type->type == SYM_NODE)
397 type = type->ctype.base_type;
398 if (type->type == SYM_ENUM)
399 type = type->ctype.base_type;
400 return type->type;
403 static inline struct symbol *lookup_keyword(struct ident *ident, enum namespace ns)
405 if (!ident->keyword)
406 return NULL;
407 return lookup_symbol(ident, ns);
410 static inline struct attribute *duplicate_attribute(struct attribute *attr)
412 struct attribute *newattr = __alloc_attribute(0);
413 *newattr = *attr;
414 return newattr;
417 static inline void attr_set_as(struct ctype *ctype, unsigned int as)
419 if (ctype->attribute->as != as) {
420 ctype->attribute = duplicate_attribute(ctype->attribute);
421 ctype->attribute->as = as;
425 static inline void attr_add_context(struct ctype *ctype, struct context *context)
427 ctype->attribute = duplicate_attribute(ctype->attribute);
428 add_ptr_list(&ctype->attribute->contexts, context);
431 static inline void merge_attr(struct ctype *dst, struct ctype *src)
433 struct attribute *attr;
435 if (src->attribute == &null_attr)
436 return;
437 if (dst->attribute == &null_attr) {
438 dst->attribute = src->attribute;
439 return;
442 dst->attribute = attr = duplicate_attribute(dst->attribute);
443 attr->as |= src->attribute->as;
444 concat_ptr_list((struct ptr_list *)src->attribute->contexts,
445 (struct ptr_list **)&attr->contexts);
448 static inline void set_attr_is_packed(struct ctype *ctype)
450 if (ctype->attribute == &null_attr)
451 ctype->attribute = __alloc_attribute(0);
452 ctype->attribute->is_packed = 1;
455 #define is_restricted_type(type) (get_sym_type(type) == SYM_RESTRICT)
456 #define is_fouled_type(type) (get_sym_type(type) == SYM_FOULED)
457 #define is_bitfield_type(type) (get_sym_type(type) == SYM_BITFIELD)
458 extern int is_ptr_type(struct symbol *);
460 void create_fouled(struct symbol *type);
461 struct symbol *befoul(struct symbol *type);
463 #endif /* SYMBOL_H */